2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.util
.xml
;
18 import com
.intellij
.openapi
.fileTypes
.StdFileTypes
;
19 import com
.intellij
.openapi
.util
.text
.StringUtil
;
20 import com
.intellij
.openapi
.vfs
.VirtualFile
;
21 import com
.intellij
.util
.containers
.ContainerUtil
;
22 import com
.intellij
.util
.indexing
.*;
23 import com
.intellij
.util
.io
.EnumeratorStringDescriptor
;
24 import com
.intellij
.util
.io
.KeyDescriptor
;
25 import com
.intellij
.util
.xml
.impl
.DomApplicationComponent
;
26 import gnu
.trove
.THashMap
;
27 import gnu
.trove
.THashSet
;
28 import org
.jetbrains
.annotations
.NotNull
;
30 import java
.io
.ByteArrayInputStream
;
31 import java
.util
.Arrays
;
32 import java
.util
.Collections
;
39 public class DomFileIndex
extends ScalarIndexExtension
<String
>{
40 public static final ID
<String
,Void
> NAME
= ID
.create("DomFileIndex");
41 private static final FileBasedIndex
.InputFilter INPUT_FILTER
= new FileBasedIndex
.InputFilter() {
42 public boolean acceptInput(final VirtualFile file
) {
43 return file
.getFileType() == StdFileTypes
.XML
;
46 private final DataIndexer
<String
,Void
, FileContent
> myDataIndexer
;
48 public DomFileIndex() {
49 myDataIndexer
= new DataIndexer
<String
, Void
, FileContent
>() {
51 public Map
<String
, Void
> map(final FileContent inputData
) {
52 final Set
<String
> namespaces
= new THashSet
<String
>();
53 final XmlFileHeader header
= NanoXmlUtil
.parseHeader(new ByteArrayInputStream(inputData
.getContent()));
54 ContainerUtil
.addIfNotNull(header
.getPublicId(), namespaces
);
55 ContainerUtil
.addIfNotNull(header
.getSystemId(), namespaces
);
56 ContainerUtil
.addIfNotNull(header
.getRootTagNamespace(), namespaces
);
57 final String tagName
= header
.getRootTagLocalName();
58 if (StringUtil
.isNotEmpty(tagName
)) {
59 final THashMap
<String
, Void
> result
= new THashMap
<String
, Void
>();
60 final DomApplicationComponent component
= DomApplicationComponent
.getInstance();
61 for (final DomFileDescription description
: component
.getFileDescriptions(tagName
)) {
62 final String
[] strings
= description
.getAllPossibleRootTagNamespaces();
63 if (strings
.length
== 0 || ContainerUtil
.intersects(Arrays
.asList(strings
), namespaces
)) {
64 result
.put(description
.getRootElementClass().getName(), null);
67 for (final DomFileDescription description
: component
.getAcceptingOtherRootTagNameDescriptions()) {
68 final String
[] strings
= description
.getAllPossibleRootTagNamespaces();
69 if (strings
.length
== 0 || ContainerUtil
.intersects(Arrays
.asList(strings
), namespaces
)) {
70 result
.put(description
.getRootElementClass().getName(), null);
75 return Collections
.emptyMap();
80 public ID
<String
, Void
> getName() {
84 public DataIndexer
<String
, Void
, FileContent
> getIndexer() {
88 public KeyDescriptor
<String
> getKeyDescriptor() {
89 return new EnumeratorStringDescriptor();
92 public FileBasedIndex
.InputFilter
getInputFilter() {
96 public boolean dependsOnFileContent() {
100 public int getVersion() {
104 private static class RootTagReachedException
extends RuntimeException
{