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
.impl
;
18 import com
.intellij
.lang
.annotation
.Annotation
;
19 import com
.intellij
.lang
.annotation
.AnnotationHolder
;
20 import com
.intellij
.lang
.annotation
.Annotator
;
21 import com
.intellij
.psi
.PsiElement
;
22 import com
.intellij
.psi
.xml
.XmlAttribute
;
23 import com
.intellij
.psi
.xml
.XmlTag
;
24 import com
.intellij
.util
.xml
.*;
25 import com
.intellij
.util
.xml
.highlighting
.*;
26 import org
.jetbrains
.annotations
.Nullable
;
28 import java
.util
.List
;
33 public class DefaultDomAnnotator
implements Annotator
{
36 private static DomElement
getDomElement(PsiElement psiElement
, DomManager myDomManager
) {
37 if (psiElement
instanceof XmlTag
) {
38 return myDomManager
.getDomElement((XmlTag
)psiElement
);
40 if (psiElement
instanceof XmlAttribute
) {
41 return myDomManager
.getDomElement((XmlAttribute
)psiElement
);
46 public <T
extends DomElement
> void runInspection(@Nullable final DomElementsInspection
<T
> inspection
, final DomFileElement
<T
> fileElement
, List
<Annotation
> toFill
) {
47 if (inspection
== null) return;
48 DomElementAnnotationsManagerImpl annotationsManager
= getAnnotationsManager(fileElement
);
49 if (DomElementAnnotationsManagerImpl
.isHolderUpToDate(fileElement
) && annotationsManager
.getProblemHolder(fileElement
).isInspectionCompleted(inspection
)) return;
51 final DomElementAnnotationHolderImpl annotationHolder
= new DomElementAnnotationHolderImpl();
52 inspection
.checkFileElement(fileElement
, annotationHolder
);
53 annotationsManager
.appendProblems(fileElement
, annotationHolder
, inspection
.getClass());
54 for (final DomElementProblemDescriptor descriptor
: annotationHolder
) {
55 toFill
.addAll(descriptor
.getAnnotations());
57 toFill
.addAll(annotationHolder
.getAnnotations());
60 protected DomElementAnnotationsManagerImpl
getAnnotationsManager(final DomElement element
) {
61 return (DomElementAnnotationsManagerImpl
)DomElementAnnotationsManager
.getInstance(element
.getManager().getProject());
65 public void annotate(final PsiElement psiElement
, AnnotationHolder holder
) {
66 final List
<Annotation
> list
= (List
<Annotation
>)holder
;
68 final DomManagerImpl domManager
= DomManagerImpl
.getDomManager(psiElement
.getProject());
69 final DomFileDescription description
= domManager
.getDomFileDescription(psiElement
);
70 if (description
!= null) {
71 final DomElement domElement
= getDomElement(psiElement
, domManager
);
72 if (domElement
!= null) {
73 runInspection(domElement
, list
);
78 public final void runInspection(final DomElement domElement
, final List
<Annotation
> list
) {
79 final DomFileElement root
= DomUtil
.getFileElement(domElement
);
80 runInspection(getAnnotationsManager(domElement
).getMockInspection(root
), root
, list
);