update copyright
[fedora-idea.git] / xml / impl / src / com / intellij / codeInsight / folding / impl / XmlElementSignatureProvider.java
blob87f12b27ed4212ca9ea3b96a4df9e2cda88d7046
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.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;
29 /**
30 * @author yole
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);
45 buffer.append("#");
46 buffer.append(getChildIndex(tag, parent, name, XmlTag.class));
48 if (parent instanceof XmlTag) {
49 String parentSignature = getSignature(parent);
50 buffer.append(";");
51 buffer.append(parentSignature);
54 return buffer.toString();
56 return null;
59 @Override
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();
68 try {
69 int index = Integer.parseInt(tokenizer.nextToken());
70 PsiElement result = restoreElementInternal(parent, name, index, XmlTag.class);
72 if (result == null &&
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);
85 return result;
87 catch (NumberFormatException e) {
88 LOG.error(e);
89 return null;
92 return null;