1
2
3
4
5
6
7
8
9
10 package org.dom4j.tree;
11
12 import java.io.IOException;
13 import java.io.Writer;
14
15 import org.dom4j.Attribute;
16 import org.dom4j.Element;
17 import org.dom4j.Namespace;
18 import org.dom4j.Node;
19 import org.dom4j.Visitor;
20
21 /*** <p><code>AbstractNamespace</code> is an abstract base class for
22 * tree implementors to use for implementation inheritence.</p>
23 *
24 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
25 * @version $Revision: 1.19 $
26 */
27 public abstract class AbstractAttribute extends AbstractNode implements Attribute {
28
29 public short getNodeType() {
30 return ATTRIBUTE_NODE;
31 }
32
33
34 public void setNamespace(Namespace namespace) {
35 throw new UnsupportedOperationException("This Attribute is read only and cannot be changed" );
36 }
37
38 public String getText() {
39 return getValue();
40 }
41
42 public void setText(String text) {
43 setValue(text);
44 }
45
46 public void setValue(String value) {
47 throw new UnsupportedOperationException("This Attribute is read only and cannot be changed" );
48 }
49
50 public Object getData() {
51 return getValue();
52 }
53
54 public void setData(Object data) {
55 setValue( data == null ? null : data.toString() );
56 }
57
58 public String toString() {
59 return super.toString() + " [Attribute: name " + getQualifiedName()
60 + " value \"" + getValue() + "\"]";
61 }
62
63 public String asXML() {
64 return getQualifiedName() + "=\"" + getValue() + "\"";
65 }
66
67 public void write(Writer writer) throws IOException {
68 writer.write( getQualifiedName() );
69 writer.write( "=\"" );
70 writer.write( getValue() );
71 writer.write( "\"" );
72 }
73
74 public void accept(Visitor visitor) {
75 visitor.visit(this);
76 }
77
78
79
80 public Namespace getNamespace() {
81 return getQName().getNamespace();
82 }
83
84 public String getName() {
85 return getQName().getName();
86 }
87
88 public String getNamespacePrefix() {
89 return getQName().getNamespacePrefix();
90 }
91
92 public String getNamespaceURI() {
93 return getQName().getNamespaceURI();
94 }
95
96 public String getQualifiedName() {
97 return getQName().getQualifiedName();
98 }
99
100 public String getPath(Element context) {
101 StringBuffer result = new StringBuffer();
102
103 Element parent = getParent();
104 if ((parent != null) && (parent != context)) {
105 result.append(parent.getPath(context));
106 result.append("/");
107 }
108 result.append("@");
109
110 String uri = getNamespaceURI();
111 String prefix = getNamespacePrefix();
112 if (uri == null || uri.length() == 0 || prefix == null || prefix.length() == 0) {
113 result.append(getName());
114 } else {
115 result.append(getQualifiedName());
116 }
117
118 return result.toString();
119 }
120
121 public String getUniquePath(Element context) {
122 StringBuffer result = new StringBuffer();
123
124 Element parent = getParent();
125 if ((parent != null) && (parent != context)) {
126 result.append(parent.getUniquePath(context));
127 result.append("/");
128 }
129 result.append("@");
130
131 String uri = getNamespaceURI();
132 String prefix = getNamespacePrefix();
133 if (uri == null || uri.length() == 0 || prefix == null || prefix.length() == 0) {
134 result.append(getName());
135 } else {
136 result.append(getQualifiedName());
137 }
138
139 return result.toString();
140 }
141
142 protected Node createXPathResult(Element parent) {
143 return new DefaultAttribute(parent, getQName(), getValue());
144 }
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195