1
2
3
4
5
6
7
8 package org.dom4j.xpp;
9
10 import java.util.ArrayList;
11 import java.util.Iterator;
12
13 import org.dom4j.Attribute;
14 import org.dom4j.DocumentFactory;
15 import org.dom4j.Element;
16 import org.dom4j.QName;
17 import org.dom4j.tree.AbstractElement;
18
19 import org.gjt.xpp.XmlPullParserException;
20 import org.gjt.xpp.XmlStartTag;
21
22 /***
23 * <p>
24 * <code>ProxyXmlStartTag</code> implements the XPP XmlSmartTag interface
25 * while creating a dom4j Element underneath.
26 * </p>
27 *
28 * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
29 * @version $Revision: 1.8 $
30 */
31 public class ProxyXmlStartTag implements XmlStartTag {
32 /*** The element being constructed */
33 private Element element;
34
35 /*** The factory used to create new elements */
36 private DocumentFactory factory = DocumentFactory.getInstance();
37
38 public ProxyXmlStartTag() {
39 }
40
41 public ProxyXmlStartTag(Element element) {
42 this.element = element;
43 }
44
45
46
47 public void resetStartTag() {
48 this.element = null;
49 }
50
51 public int getAttributeCount() {
52 return (element != null) ? element.attributeCount() : 0;
53 }
54
55 public String getAttributeNamespaceUri(int index) {
56 if (element != null) {
57 Attribute attribute = element.attribute(index);
58
59 if (attribute != null) {
60 return attribute.getNamespaceURI();
61 }
62 }
63
64 return null;
65 }
66
67 public String getAttributeLocalName(int index) {
68 if (element != null) {
69 Attribute attribute = element.attribute(index);
70
71 if (attribute != null) {
72 return attribute.getName();
73 }
74 }
75
76 return null;
77 }
78
79 public String getAttributePrefix(int index) {
80 if (element != null) {
81 Attribute attribute = element.attribute(index);
82
83 if (attribute != null) {
84 String prefix = attribute.getNamespacePrefix();
85
86 if ((prefix != null) && (prefix.length() > 0)) {
87 return prefix;
88 }
89 }
90 }
91
92 return null;
93 }
94
95 public String getAttributeRawName(int index) {
96 if (element != null) {
97 Attribute attribute = element.attribute(index);
98
99 if (attribute != null) {
100 return attribute.getQualifiedName();
101 }
102 }
103
104 return null;
105 }
106
107 public String getAttributeValue(int index) {
108 if (element != null) {
109 Attribute attribute = element.attribute(index);
110
111 if (attribute != null) {
112 return attribute.getValue();
113 }
114 }
115
116 return null;
117 }
118
119 public String getAttributeValueFromRawName(String rawName) {
120 if (element != null) {
121 for (Iterator iter = element.attributeIterator(); iter.hasNext();) {
122 Attribute attribute = (Attribute) iter.next();
123
124 if (rawName.equals(attribute.getQualifiedName())) {
125 return attribute.getValue();
126 }
127 }
128 }
129
130 return null;
131 }
132
133 public String getAttributeValueFromName(String namespaceURI,
134 String localName) {
135 if (element != null) {
136 for (Iterator iter = element.attributeIterator(); iter.hasNext();) {
137 Attribute attribute = (Attribute) iter.next();
138
139 if (namespaceURI.equals(attribute.getNamespaceURI())
140 && localName.equals(attribute.getName())) {
141 return attribute.getValue();
142 }
143 }
144 }
145
146 return null;
147 }
148
149 public boolean isAttributeNamespaceDeclaration(int index) {
150 if (element != null) {
151 Attribute attribute = element.attribute(index);
152
153 if (attribute != null) {
154 return "xmlns".equals(attribute.getNamespacePrefix());
155 }
156 }
157
158 return false;
159 }
160
161 /***
162 * parameters modeled after SAX2 attribute approach
163 *
164 * @param namespaceURI
165 * DOCUMENT ME!
166 * @param localName
167 * DOCUMENT ME!
168 * @param rawName
169 * DOCUMENT ME!
170 * @param value
171 * DOCUMENT ME!
172 *
173 * @throws XmlPullParserException
174 * DOCUMENT ME!
175 */
176 public void addAttribute(String namespaceURI, String localName,
177 String rawName, String value) throws XmlPullParserException {
178 QName qname = QName.get(rawName, namespaceURI);
179 element.addAttribute(qname, value);
180 }
181
182 public void addAttribute(String namespaceURI, String localName,
183 String rawName, String value, boolean isNamespaceDeclaration)
184 throws XmlPullParserException {
185 if (isNamespaceDeclaration) {
186 String prefix = "";
187 int idx = rawName.indexOf(':');
188
189 if (idx > 0) {
190 prefix = rawName.substring(0, idx);
191 }
192
193 element.addNamespace(prefix, namespaceURI);
194 } else {
195 QName qname = QName.get(rawName, namespaceURI);
196 element.addAttribute(qname, value);
197 }
198 }
199
200 public void ensureAttributesCapacity(int minCapacity)
201 throws XmlPullParserException {
202 if (element instanceof AbstractElement) {
203 AbstractElement elementImpl = (AbstractElement) element;
204 elementImpl.ensureAttributesCapacity(minCapacity);
205 }
206 }
207
208 /***
209 * remove all atribute
210 *
211 * @throws XmlPullParserException
212 * DOCUMENT ME!
213 */
214 public void removeAtttributes() throws XmlPullParserException {
215 if (element != null) {
216 element.setAttributes(new ArrayList());
217
218
219
220
221 }
222 }
223
224 public String getLocalName() {
225 return element.getName();
226 }
227
228 public String getNamespaceUri() {
229 return element.getNamespaceURI();
230 }
231
232 public String getPrefix() {
233 return element.getNamespacePrefix();
234 }
235
236 public String getRawName() {
237 return element.getQualifiedName();
238 }
239
240 public void modifyTag(String namespaceURI, String lName, String rawName) {
241 this.element = factory.createElement(rawName, namespaceURI);
242 }
243
244 public void resetTag() {
245 this.element = null;
246 }
247
248
249
250 public DocumentFactory getDocumentFactory() {
251 return factory;
252 }
253
254 public void setDocumentFactory(DocumentFactory documentFactory) {
255 this.factory = documentFactory;
256 }
257
258 public Element getElement() {
259 return element;
260 }
261 }
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298