update copyright
[fedora-idea.git] / xml / dom-impl / src / com / intellij / xml / impl / dom / DomElementXmlDescriptor.java
blobd16e6d3e4ad3f8fe46c8547c8d699fe5744d1693
1 /*
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.xml.impl.dom;
18 import com.intellij.openapi.util.Key;
19 import com.intellij.openapi.util.text.StringUtil;
20 import com.intellij.psi.PsiElement;
21 import com.intellij.psi.PsiFile;
22 import com.intellij.psi.impl.FakePsiElement;
23 import com.intellij.psi.xml.XmlAttribute;
24 import com.intellij.psi.xml.XmlDocument;
25 import com.intellij.psi.xml.XmlFile;
26 import com.intellij.psi.xml.XmlTag;
27 import com.intellij.util.xml.*;
28 import com.intellij.util.xml.reflect.*;
29 import com.intellij.xml.XmlAttributeDescriptor;
30 import com.intellij.xml.XmlElementDescriptor;
31 import com.intellij.xml.XmlNSDescriptor;
32 import com.intellij.xml.impl.schema.AnyXmlElementDescriptor;
33 import org.jetbrains.annotations.NonNls;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
37 import java.lang.annotation.Annotation;
38 import java.lang.reflect.Type;
39 import java.util.ArrayList;
40 import java.util.List;
42 /**
43 * @author mike
45 public class DomElementXmlDescriptor implements XmlElementDescriptor {
46 private final DomChildrenDescription myChildrenDescription;
47 private final DomManager myManager;
48 @NotNull private final DomElement mySomeElement;
50 public DomElementXmlDescriptor(@NotNull final DomElement domElement) {
51 myChildrenDescription = new MyRootDomChildrenDescription(domElement);
53 myManager = domElement.getManager();
54 mySomeElement = domElement;
57 public DomElementXmlDescriptor(@NotNull final DomChildrenDescription childrenDescription, @NotNull DomElement someElement) {
58 myChildrenDescription = childrenDescription;
59 myManager = someElement.getManager();
60 mySomeElement = someElement;
63 public String getQualifiedName() {
64 return myChildrenDescription.getXmlElementName();
67 public String getDefaultName() {
68 return myChildrenDescription.getXmlElementName();
71 public XmlElementDescriptor[] getElementsDescriptors(final XmlTag context) {
72 DomElement domElement = myManager.getDomElement(context);
73 if (domElement == null) return EMPTY_ARRAY;
75 List<XmlElementDescriptor> xmlElementDescriptors = new ArrayList<XmlElementDescriptor>();
77 for (DomCollectionChildDescription childrenDescription : domElement.getGenericInfo().getCollectionChildrenDescriptions()) {
78 xmlElementDescriptors.add(new DomElementXmlDescriptor(childrenDescription, domElement));
81 for (DomFixedChildDescription childrenDescription : domElement.getGenericInfo().getFixedChildrenDescriptions()) {
82 xmlElementDescriptors.add(new DomElementXmlDescriptor(childrenDescription, domElement));
85 CustomDomChildrenDescription customDescription = domElement.getGenericInfo().getCustomNameChildrenDescription();
86 if (customDescription != null) {
87 xmlElementDescriptors.add(new AnyXmlElementDescriptor(this, getNSDescriptor()));
90 return xmlElementDescriptors.toArray(new XmlElementDescriptor[xmlElementDescriptors.size()]);
93 @Nullable
94 public XmlElementDescriptor getElementDescriptor(@NotNull final XmlTag childTag, @Nullable XmlTag contextTag) {
95 DomElement domElement = myManager.getDomElement(childTag);
96 if (domElement == null) {
97 domElement = myManager.getDomElement(contextTag);
98 if (domElement != null) {
99 AbstractDomChildrenDescription description = myManager.findChildrenDescription(childTag, domElement);
100 if (description instanceof DomChildrenDescription) {
101 return new DomElementXmlDescriptor((DomChildrenDescription)description, domElement);
104 return null;
107 final DomElement parent = domElement.getParent();
108 if (parent == null) return new DomElementXmlDescriptor(domElement);
110 AbstractDomChildrenDescription description = domElement.getChildDescription();
111 if (description instanceof CustomDomChildrenDescription) return new AnyXmlElementDescriptor(this, getNSDescriptor());
112 if (!(description instanceof DomChildrenDescription)) return null;
114 return new DomElementXmlDescriptor((DomChildrenDescription)description, parent);
117 public XmlAttributeDescriptor[] getAttributesDescriptors(final @Nullable XmlTag context) {
118 if (context == null) return XmlAttributeDescriptor.EMPTY;
120 DomElement domElement = myManager.getDomElement(context);
121 if (domElement == null) return XmlAttributeDescriptor.EMPTY;
123 final List<? extends DomAttributeChildDescription> descriptions = domElement.getGenericInfo().getAttributeChildrenDescriptions();
124 List<XmlAttributeDescriptor> descriptors = new ArrayList<XmlAttributeDescriptor>();
126 for (DomAttributeChildDescription description : descriptions) {
127 descriptors.add(new DomAttributeXmlDescriptor(description));
130 return descriptors.toArray(new XmlAttributeDescriptor[descriptors.size()]);
133 @Nullable
134 public XmlAttributeDescriptor getAttributeDescriptor(final String attributeName, final @Nullable XmlTag context) {
135 DomElement domElement = myManager.getDomElement(context);
136 if (domElement == null) return null;
138 for (DomAttributeChildDescription description : domElement.getGenericInfo().getAttributeChildrenDescriptions()) {
139 if (attributeName.equals(DomAttributeXmlDescriptor.getQualifiedAttributeName(context, description.getXmlName()))) {
140 return new DomAttributeXmlDescriptor(description);
143 return null;
146 @Nullable
147 public XmlAttributeDescriptor getAttributeDescriptor(final XmlAttribute attribute) {
148 return getAttributeDescriptor(attribute.getName(), attribute.getParent());
151 public XmlNSDescriptor getNSDescriptor() {
152 return new XmlNSDescriptor() {
153 @Nullable
154 public XmlElementDescriptor getElementDescriptor(@NotNull final XmlTag tag) {
155 throw new UnsupportedOperationException("Method getElementDescriptor not implemented in " + getClass());
158 @NotNull
159 public XmlElementDescriptor[] getRootElementsDescriptors(@Nullable final XmlDocument document) {
160 throw new UnsupportedOperationException("Method getRootElementsDescriptors not implemented in " + getClass());
163 @Nullable
164 public XmlFile getDescriptorFile() {
165 return null;
168 public boolean isHierarhyEnabled() {
169 throw new UnsupportedOperationException("Method isHierarhyEnabled not implemented in " + getClass());
172 @Nullable
173 public PsiElement getDeclaration() {
174 throw new UnsupportedOperationException("Method getDeclaration not implemented in " + getClass());
177 @NonNls
178 public String getName(final PsiElement context) {
179 throw new UnsupportedOperationException("Method getName not implemented in " + getClass());
182 @NonNls
183 public String getName() {
184 throw new UnsupportedOperationException("Method getName not implemented in " + getClass());
187 public void init(final PsiElement element) {
188 throw new UnsupportedOperationException("Method init not implemented in " + getClass());
191 public Object[] getDependences() {
192 throw new UnsupportedOperationException("Method getDependences not implemented in " + getClass());
197 public int getContentType() {
198 throw new UnsupportedOperationException("Method getContentType not implemented in " + getClass());
201 @Nullable
202 public PsiElement getDeclaration() {
203 final DomElement declaration = myChildrenDescription.getUserData(DomExtension.KEY_DECLARATION);
205 if (declaration != null) return declaration.getXmlElement();
207 return new FakePsiElement() {
208 public PsiElement getParent() {
209 return DomUtil.getFile(mySomeElement);
214 @NonNls
215 public String getName(final PsiElement context) {
216 final String name = getDefaultName();
217 if (context instanceof XmlTag) {
218 XmlTag tag = (XmlTag)context;
219 final PsiFile file = tag.getContainingFile();
220 DomElement element = myManager.getDomElement(tag);
221 if (element == null && tag.getParentTag() != null) {
222 element = myManager.getDomElement(tag.getParentTag());
224 if (element != null && file instanceof XmlFile && !(myChildrenDescription instanceof MyRootDomChildrenDescription)) {
225 final String namespace = DomService.getInstance().getEvaluatedXmlName(element).evaluateChildName(myChildrenDescription.getXmlName()).getNamespace(tag, (XmlFile)file);
226 if (!tag.getNamespaceByPrefix("").equals(namespace)) {
227 final String s = tag.getPrefixByNamespace(namespace);
228 if (StringUtil.isNotEmpty(s)) {
229 return s + ":" + name;
235 return name;
238 @NonNls
239 public String getName() {
240 return getDefaultName();
243 public void init(final PsiElement element) {
244 throw new UnsupportedOperationException("Method init not implemented in " + getClass());
247 public Object[] getDependences() {
248 throw new UnsupportedOperationException("Method getDependences not implemented in " + getClass());
251 private static class MyRootDomChildrenDescription implements DomChildrenDescription {
252 private final DomElement myDomElement;
254 public MyRootDomChildrenDescription(final DomElement domElement) {
255 myDomElement = domElement;
258 @NotNull
259 public XmlName getXmlName() {
260 throw new UnsupportedOperationException("Method getXmlName not implemented in " + getClass());
263 @NotNull
264 public String getXmlElementName() {
265 return myDomElement.getXmlElementName();
268 @NotNull
269 public String getCommonPresentableName(@NotNull final DomNameStrategy strategy) {
270 throw new UnsupportedOperationException("Method getCommonPresentableName not implemented in " + getClass());
273 @NotNull
274 public String getCommonPresentableName(@NotNull final DomElement parent) {
275 throw new UnsupportedOperationException("Method getCommonPresentableName not implemented in " + getClass());
278 @NotNull
279 public List<? extends DomElement> getValues(@NotNull final DomElement parent) {
280 throw new UnsupportedOperationException("Method getValues not implemented in " + getClass());
283 @NotNull
284 public List<? extends DomElement> getStableValues(@NotNull final DomElement parent) {
285 throw new UnsupportedOperationException("Method getStableValues not implemented in " + getClass());
288 @NotNull
289 public Type getType() {
290 throw new UnsupportedOperationException("Method getType not implemented in " + getClass());
293 @NotNull
294 public DomNameStrategy getDomNameStrategy(@NotNull final DomElement parent) {
295 throw new UnsupportedOperationException("Method getDomNameStrategy not implemented in " + getClass());
298 public <T> T getUserData(final Key<T> key) {
299 return null;
302 @Nullable
303 public <T extends Annotation> T getAnnotation(final Class<T> annotationClass) {
304 throw new UnsupportedOperationException("Method getAnnotation not implemented in " + getClass());