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
.util
.xml
;
18 import com
.intellij
.openapi
.util
.TextRange
;
19 import com
.intellij
.openapi
.util
.text
.StringUtil
;
20 import com
.intellij
.pom
.PomRenameableTarget
;
21 import com
.intellij
.pom
.PsiDeclaredTarget
;
22 import com
.intellij
.psi
.DelegatePsiTarget
;
23 import com
.intellij
.psi
.PsiElement
;
24 import com
.intellij
.psi
.xml
.XmlAttributeValue
;
25 import com
.intellij
.psi
.xml
.XmlTag
;
26 import com
.intellij
.psi
.xml
.XmlTagValue
;
27 import com
.intellij
.xml
.util
.XmlTagUtil
;
28 import com
.intellij
.util
.ObjectUtils
;
29 import org
.jetbrains
.annotations
.NotNull
;
30 import org
.jetbrains
.annotations
.Nullable
;
35 public class DomTarget
extends DelegatePsiTarget
implements PsiDeclaredTarget
, PomRenameableTarget
{
36 private final DomElement myDomElement
;
37 private final TextRange myRange
;
39 private DomTarget(DomElement domElement
, PsiElement navigationElement
, TextRange range
) {
40 super(navigationElement
);
41 myDomElement
= domElement
;
46 public static DomTarget
getTarget(@NotNull DomElement element
) {
47 final GenericDomValue value
= element
.getGenericInfo().getNameDomElement(element
);
52 if (value
instanceof GenericAttributeValue
) {
53 final GenericAttributeValue genericAttributeValue
= (GenericAttributeValue
)value
;
54 final XmlAttributeValue attributeValue
= genericAttributeValue
.getXmlAttributeValue();
55 if (attributeValue
== null) {
59 final int length
= attributeValue
.getTextLength();
61 return new DomTarget(element
, attributeValue
, new TextRange(1, length
- 1));
65 final XmlTag tag
= value
.getXmlTag();
70 XmlTagValue tagValue
= tag
.getValue();
71 if (StringUtil
.isEmpty(tagValue
.getTrimmedText())) {
75 return new DomTarget(element
, tag
, XmlTagUtil
.getTrimmedValueRange(tag
));
78 public TextRange
getNameIdentifierRange() {
82 public boolean isWritable() {
83 return getNavigationElement().isWritable();
86 public Object
setName(@NotNull String newName
) {
87 ObjectUtils
.assertNotNull(myDomElement
.getGenericInfo().getNameDomElement(myDomElement
)).setStringValue(newName
);
92 public String
getName() {
93 return myDomElement
.getGenericInfo().getElementName(myDomElement
);
97 public boolean equals(Object o
) {
98 if (this == o
) return true;
99 if (o
== null || getClass() != o
.getClass()) return false;
100 if (!super.equals(o
)) return false;
102 DomTarget domTarget
= (DomTarget
)o
;
104 if (myDomElement
!= null ?
!myDomElement
.equals(domTarget
.myDomElement
) : domTarget
.myDomElement
!= null) return false;
110 public int hashCode() {
111 int result
= super.hashCode();
112 result
= 31 * result
+ (myDomElement
!= null ? myDomElement
.hashCode() : 0);
113 result
= 31 * result
+ (myRange
!= null ? myRange
.hashCode() : 0);
117 public DomElement
getDomElement() {