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.
17 package com.intellij.util.xml.impl;
19 import com.intellij.ide.structureView.StructureViewBuilder;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.vfs.VirtualFile;
22 import com.intellij.psi.PsiElement;
23 import com.intellij.psi.PsiFile;
24 import com.intellij.psi.PsiManager;
25 import com.intellij.psi.search.GlobalSearchScope;
26 import com.intellij.psi.xml.XmlFile;
27 import com.intellij.psi.xml.XmlTag;
28 import com.intellij.util.Function;
29 import com.intellij.util.indexing.FileBasedIndex;
30 import com.intellij.util.xml.*;
31 import com.intellij.util.xml.structure.DomStructureViewBuilder;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.List;
40 * @author Gregory.Shrago
42 public class DomServiceImpl extends DomService {
44 public ModelMerger createModelMerger() {
45 return new ModelMergerImpl();
49 public XmlFile getContainingFile(@NotNull DomElement domElement) {
50 if (domElement instanceof DomFileElement) {
51 return ((DomFileElement)domElement).getFile();
53 DomInvocationHandler handler = DomManagerImpl.getDomInvocationHandler(domElement);
54 assert handler != null : domElement;
55 while (handler != null && !(handler instanceof DomRootInvocationHandler) && handler.getXmlTag() == null) {
56 handler = handler.getParentHandler();
58 if (handler instanceof DomRootInvocationHandler) {
59 return ((DomRootInvocationHandler)handler).getParent().getFile();
61 assert handler != null;
62 XmlTag tag = handler.getXmlTag();
65 final PsiElement parentTag = PhysicalDomParentStrategy.getParentTagCandidate(tag);
66 if (!(parentTag instanceof XmlTag)) {
67 return (XmlFile)tag.getContainingFile();
70 tag = (XmlTag)parentTag;
75 public EvaluatedXmlName getEvaluatedXmlName(@NotNull final DomElement element) {
76 return DomManagerImpl.getDomInvocationHandler(element).getXmlName();
79 public Collection<VirtualFile> getDomFileCandidates(Class<? extends DomElement> description, Project project) {
80 return FileBasedIndex.getInstance().getContainingFiles(DomFileIndex.NAME, description.getName(), GlobalSearchScope.allScope(project));
83 public <T extends DomElement> List<DomFileElement<T>> getFileElements(final Class<T> clazz, final Project project, @Nullable final GlobalSearchScope scope) {
84 final Collection<VirtualFile> list = scope == null ? getDomFileCandidates(clazz, project) : getDomFileCandidates(clazz, project, scope);
85 final ArrayList<DomFileElement<T>> result = new ArrayList<DomFileElement<T>>(list.size());
86 for (VirtualFile file : list) {
87 final PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
88 if (psiFile instanceof XmlFile) {
89 final DomFileElement<T> element = DomManager.getDomManager(project).getFileElement((XmlFile)psiFile, clazz);
90 if (element != null) {
100 public StructureViewBuilder createSimpleStructureViewBuilder(final XmlFile file, final Function<DomElement, StructureViewMode> modeProvider) {
101 return new DomStructureViewBuilder(file, modeProvider);