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