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
.refactoring
.rename
;
18 import com
.intellij
.openapi
.diagnostic
.Logger
;
19 import com
.intellij
.psi
.PsiElement
;
20 import com
.intellij
.psi
.PsiFileFactory
;
21 import com
.intellij
.psi
.PsiManager
;
22 import com
.intellij
.psi
.PsiReference
;
23 import com
.intellij
.psi
.search
.LocalSearchScope
;
24 import com
.intellij
.psi
.search
.searches
.ReferencesSearch
;
25 import com
.intellij
.psi
.xml
.XmlAttribute
;
26 import com
.intellij
.psi
.xml
.XmlAttributeValue
;
27 import com
.intellij
.psi
.xml
.XmlFile
;
28 import com
.intellij
.refactoring
.listeners
.RefactoringElementListener
;
29 import com
.intellij
.usageView
.UsageInfo
;
30 import com
.intellij
.util
.IncorrectOperationException
;
31 import com
.intellij
.util
.containers
.Queue
;
32 import org
.jetbrains
.annotations
.NotNull
;
34 public class RenameXmlAttributeProcessor
extends RenamePsiElementProcessor
{
35 private static final Logger LOG
= Logger
.getInstance("#com.intellij.refactoring.rename.RenameXmlAttributeProcessor");
37 public boolean canProcessElement(final PsiElement element
) {
38 return element
instanceof XmlAttribute
|| element
instanceof XmlAttributeValue
;
41 public void renameElement(final PsiElement element
,
43 final UsageInfo
[] usages
,
44 final RefactoringElementListener listener
) throws IncorrectOperationException
{
45 if (element
instanceof XmlAttribute
) {
46 doRenameXmlAttribute((XmlAttribute
)element
, newName
, listener
);
48 else if (element
instanceof XmlAttributeValue
) {
49 doRenameXmlAttributeValue((XmlAttributeValue
)element
, newName
, usages
, listener
);
53 private static void doRenameXmlAttribute(XmlAttribute attribute
,
55 RefactoringElementListener listener
) {
57 final PsiElement element
= attribute
.setName(newName
);
58 listener
.elementRenamed(element
);
60 catch (IncorrectOperationException e
) {
65 private static void doRenameXmlAttributeValue(@NotNull XmlAttributeValue value
,
68 RefactoringElementListener listener
)
69 throws IncorrectOperationException
{
70 LOG
.assertTrue(value
.isValid());
72 renameAll(value
, infos
, newName
, value
.getValue());
74 PsiManager psiManager
= value
.getManager();
75 LOG
.assertTrue(psiManager
!= null);
76 XmlFile file
= (XmlFile
)PsiFileFactory
.getInstance(psiManager
.getProject()).createFileFromText("dummy.xml", "<a attr=\"" + newName
+ "\"/>");
77 final PsiElement element
= value
.replace(file
.getDocument().getRootTag().getAttributes()[0].getValueElement());
78 listener
.elementRenamed(element
);
81 private static void renameAll(PsiElement originalElement
, UsageInfo
[] infos
, String newName
,
82 String originalName
) throws IncorrectOperationException
{
83 if (newName
.equals(originalName
)) return;
84 Queue
<PsiReference
> queue
= new Queue
<PsiReference
>(infos
.length
);
85 for (UsageInfo info
: infos
) {
86 if (info
.getElement() == null) continue;
87 PsiReference ref
= info
.getReference();
88 if (ref
== null) continue;
92 while(!queue
.isEmpty()) {
93 final PsiReference reference
= queue
.pullFirst();
94 final PsiElement oldElement
= reference
.getElement();
95 if (!oldElement
.isValid() || oldElement
== originalElement
) continue;
96 final PsiElement newElement
= reference
.handleElementRename(newName
);
97 if (!oldElement
.isValid()) {
98 for (PsiReference psiReference
: ReferencesSearch
.search(originalElement
, new LocalSearchScope(newElement
), false)) {
99 queue
.addLast(psiReference
);