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
.codeInsight
.folding
.impl
;
18 import com
.intellij
.openapi
.diagnostic
.Logger
;
19 import com
.intellij
.openapi
.fileTypes
.StdFileTypes
;
20 import com
.intellij
.psi
.PsiElement
;
21 import com
.intellij
.psi
.PsiFile
;
22 import com
.intellij
.psi
.xml
.XmlDocument
;
23 import com
.intellij
.psi
.xml
.XmlFile
;
24 import com
.intellij
.psi
.xml
.XmlTag
;
25 import com
.intellij
.xml
.util
.HtmlUtil
;
27 import java
.util
.StringTokenizer
;
32 public class XmlElementSignatureProvider
extends ElementSignatureProvider
{
33 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInsight.folding.impl.XmlElementSignatureProvider");
35 public String
getSignature(PsiElement element
) {
36 if (element
instanceof XmlTag
) {
37 XmlTag tag
= (XmlTag
)element
;
38 PsiElement parent
= tag
.getParent();
40 StringBuilder buffer
= new StringBuilder();
41 buffer
.append("tag#");
42 String name
= tag
.getName();
43 buffer
.append(name
.length() == 0 ?
"<unnamed>" : name
);
46 buffer
.append(getChildIndex(tag
, parent
, name
, XmlTag
.class));
48 if (parent
instanceof XmlTag
) {
49 String parentSignature
= getSignature(parent
);
51 buffer
.append(parentSignature
);
54 return buffer
.toString();
60 protected PsiElement
restoreBySignatureTokens(PsiFile file
, PsiElement parent
, String type
, StringTokenizer tokenizer
) {
61 if (type
.equals("tag")) {
62 String name
= tokenizer
.nextToken();
64 if (parent
instanceof XmlFile
) {
65 parent
= ((XmlFile
)parent
).getDocument();
69 int index
= Integer
.parseInt(tokenizer
.nextToken());
70 PsiElement result
= restoreElementInternal(parent
, name
, index
, XmlTag
.class);
73 file
.getFileType() == StdFileTypes
.JSP
) {
74 //TODO: FoldingBuilder API, psi roots, etc?
75 if (parent
instanceof XmlDocument
) {
76 // html tag, not found in jsp tree
77 result
= restoreElementInternal(HtmlUtil
.getRealXmlDocument((XmlDocument
)parent
), name
, index
, XmlTag
.class);
79 else if (name
.equals("<unnamed>") && parent
!= null) {
80 // scriplet/declaration missed because null name
81 result
= restoreElementInternal(parent
, "", index
, XmlTag
.class);
87 catch (NumberFormatException e
) {