update copyright
[fedora-idea.git] / xml / impl / src / com / intellij / codeInsight / editorActions / XmlCopyPastePreProcessor.java
blobfd0b9c6e778c4a55d078c4887069166b3e6106a1
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.editorActions;
18 import com.intellij.lang.ASTNode;
19 import com.intellij.openapi.editor.Document;
20 import com.intellij.openapi.editor.Editor;
21 import com.intellij.openapi.editor.RawText;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.psi.PsiDocumentManager;
24 import com.intellij.psi.PsiElement;
25 import com.intellij.psi.PsiFile;
26 import com.intellij.psi.impl.source.tree.TreeUtil;
27 import com.intellij.psi.impl.source.xml.behavior.EncodeEachSymbolPolicy;
28 import com.intellij.psi.util.PsiTreeUtil;
29 import com.intellij.psi.util.PsiUtilBase;
30 import com.intellij.psi.xml.XmlAttributeValue;
31 import com.intellij.psi.xml.XmlElement;
32 import com.intellij.psi.xml.XmlElementType;
33 import com.intellij.psi.xml.XmlText;
34 import org.jetbrains.annotations.Nullable;
36 /**
37 * @author Dmitry Avdeev
39 public class XmlCopyPastePreProcessor implements CopyPastePreProcessor {
41 private static final EncodeEachSymbolPolicy ENCODE_EACH_SYMBOL_POLICY = new EncodeEachSymbolPolicy();
43 @Nullable
44 public String preprocessOnCopy(PsiFile file, int[] startOffsets, int[] endOffsets, String text) {
45 return null;
48 public String preprocessOnPaste(Project project, PsiFile file, Editor editor, String text, RawText rawText) {
49 final Document document = editor.getDocument();
50 PsiDocumentManager.getInstance(project).commitDocument(document);
51 int caretOffset = editor.getCaretModel().getOffset();
52 PsiElement element = PsiUtilBase.getElementAtOffset(file, caretOffset);
54 ASTNode node = element.getNode();
55 if (node != null) {
56 boolean hasMarkup = text.indexOf('>') >= 0 || text.indexOf('<') >= 0;
57 if (element.getTextOffset() == caretOffset &&
58 node.getElementType() == XmlElementType.XML_END_TAG_START &&
59 node.getTreePrev().getElementType() == XmlElementType.XML_TAG_END) {
61 return hasMarkup ? text : encode(text, element);
62 } else {
63 XmlElement parent = PsiTreeUtil.getParentOfType(element, XmlText.class, XmlAttributeValue.class);
64 if (parent != null) {
65 if (parent instanceof XmlText && hasMarkup) {
66 return text;
69 if (TreeUtil.findParent(node, XmlElementType.XML_CDATA) == null &&
70 TreeUtil.findParent(node, XmlElementType.XML_COMMENT) == null) {
71 return encode(text, element);
76 return text;
79 private static String encode(String text, PsiElement element) {
80 ASTNode astNode = ENCODE_EACH_SYMBOL_POLICY.encodeXmlTextContents(text, element);
81 return astNode.getTreeParent().getText();