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 <T extends DomElement> DomAnchor<T> createAnchor(T domElement) {
50 return DomAnchorImpl.createAnchor(domElement);
54 public XmlFile getContainingFile(@NotNull DomElement domElement) {
55 if (domElement instanceof DomFileElement) {
56 return ((DomFileElement)domElement).getFile();
58 DomInvocationHandler handler = DomManagerImpl.getDomInvocationHandler(domElement);
59 assert handler != null : domElement;
60 while (handler != null && !(handler instanceof DomRootInvocationHandler) && handler.getXmlTag() == null) {
61 handler = handler.getParentHandler();
63 if (handler instanceof DomRootInvocationHandler) {
64 return ((DomRootInvocationHandler)handler).getParent().getFile();
66 assert handler != null;
67 XmlTag tag = handler.getXmlTag();
70 final PsiElement parentTag = PhysicalDomParentStrategy.getParentTagCandidate(tag);
71 if (!(parentTag instanceof XmlTag)) {
72 return (XmlFile)tag.getContainingFile();
75 tag = (XmlTag)parentTag;
80 public EvaluatedXmlName getEvaluatedXmlName(@NotNull final DomElement element) {
81 return DomManagerImpl.getDomInvocationHandler(element).getXmlName();
84 public Collection<VirtualFile> getDomFileCandidates(Class<? extends DomElement> description, Project project) {
85 return FileBasedIndex.getInstance().getContainingFiles(DomFileIndex.NAME, description.getName(), GlobalSearchScope.allScope(project));
88 public <T extends DomElement> List<DomFileElement<T>> getFileElements(final Class<T> clazz, final Project project, @Nullable final GlobalSearchScope scope) {
89 final Collection<VirtualFile> list = scope == null ? getDomFileCandidates(clazz, project) : getDomFileCandidates(clazz, project, scope);
90 final ArrayList<DomFileElement<T>> result = new ArrayList<DomFileElement<T>>(list.size());
91 for (VirtualFile file : list) {
92 final PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
93 if (psiFile instanceof XmlFile) {
94 final DomFileElement<T> element = DomManager.getDomManager(project).getFileElement((XmlFile)psiFile, clazz);
95 if (element != null) {
105 public StructureViewBuilder createSimpleStructureViewBuilder(final XmlFile file, final Function<DomElement, StructureViewMode> modeProvider) {
106 return new DomStructureViewBuilder(file, modeProvider);