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