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
.psi
.impl
.source
.html
;
18 import com
.intellij
.openapi
.util
.Key
;
19 import com
.intellij
.psi
.PsiElement
;
20 import com
.intellij
.psi
.PsiFile
;
21 import com
.intellij
.psi
.PsiReference
;
22 import com
.intellij
.psi
.ResolveState
;
23 import com
.intellij
.psi
.scope
.PsiScopeProcessor
;
24 import com
.intellij
.psi
.search
.PsiElementProcessor
;
25 import com
.intellij
.psi
.xml
.*;
26 import com
.intellij
.xml
.XmlElementDescriptor
;
27 import com
.intellij
.xml
.util
.HtmlUtil
;
28 import com
.intellij
.xml
.util
.XmlUtil
;
29 import org
.jetbrains
.annotations
.NonNls
;
31 import java
.util
.ArrayList
;
32 import java
.util
.List
;
35 * Created by IntelliJ IDEA.
36 * User: Maxim.Mossienko
39 * To change this template use File | Settings | File Templates.
41 public class ScriptSupportUtil
{
42 private static final Key
<XmlTag
[]> CachedScriptTagsKey
= Key
.create("script tags");
43 private static final ThreadLocal
<String
> ProcessingDeclarationsFlag
= new ThreadLocal
<String
>();
44 private static final @NonNls String SCRIPT_TAG
= "script";
46 public static void clearCaches(XmlFile element
) {
47 element
.putUserData(CachedScriptTagsKey
,null);
50 public static boolean processDeclarations(XmlFile element
, PsiScopeProcessor processor
, ResolveState state
, PsiElement lastParent
, PsiElement place
) {
51 XmlTag
[] myCachedScriptTags
= element
.getUserData(CachedScriptTagsKey
);
53 if (myCachedScriptTags
== null) {
54 final List
<XmlTag
> scriptTags
= new ArrayList
<XmlTag
>();
55 final XmlDocument document
= HtmlUtil
.getRealXmlDocument(element
.getDocument());
57 if (document
!= null) {
58 XmlUtil
.processXmlElements(document
,
59 new PsiElementProcessor() {
60 public boolean execute(final PsiElement element
) {
61 if (element
instanceof XmlTag
) {
62 final XmlTag tag
= (XmlTag
)element
;
64 if (SCRIPT_TAG
.equalsIgnoreCase(tag
.getName())) {
65 final XmlElementDescriptor descriptor
= tag
.getDescriptor();
66 if (descriptor
!= null && SCRIPT_TAG
.equals(descriptor
.getName())) {
76 myCachedScriptTags
= scriptTags
.toArray(new XmlTag
[scriptTags
.size()]);
77 element
.putUserData(CachedScriptTagsKey
, myCachedScriptTags
);
80 if (ProcessingDeclarationsFlag
.get() != null) return true;
83 ProcessingDeclarationsFlag
.set("");
85 for (XmlTag tag
: myCachedScriptTags
) {
86 final XmlTagChild
[] children
= tag
.getValue().getChildren();
87 for (XmlTagChild child
: children
) {
88 if (!child
.processDeclarations(processor
, state
, null, place
)) return false;
91 if(tag
.getAttributeValue("src") != null) {
92 final XmlAttribute attribute
= tag
.getAttribute("src", null);
94 if (attribute
!= null) {
95 final XmlAttributeValue valueElement
= attribute
.getValueElement();
97 if (valueElement
!= null) {
98 final PsiReference
[] references
= valueElement
.getReferences();
100 if (references
.length
> 0) {
101 final PsiElement psiElement
= references
[references
.length
- 1].resolve();
103 if (psiElement
instanceof PsiFile
&& psiElement
.isValid()) {
104 if(!psiElement
.processDeclarations(processor
, state
, null, place
))
113 ProcessingDeclarationsFlag
.set(null);