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   * <code>ProxyXmlStartTag</code> implements the XPP <code>XmlSmartTag</code>
24   * interface while creating a dom4j <code>Element</code> underneath.
25   * 
26   * @author James Strachan
27   * @author Maarten Coene
28   * @author Wolfgang Baer
29   */
30  public class ProxyXmlStartTag implements XmlStartTag {
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      public ProxyXmlStartTag() {
38      }
39  
40      public ProxyXmlStartTag(Element element) {
41          this.element = element;
42      }
43  
44      
45      
46      public void resetStartTag() {
47          this.element = null;
48      }
49  
50      public int getAttributeCount() {
51          return (element != null) ? element.attributeCount() : 0;
52      }
53  
54      public String getAttributeNamespaceUri(int index) {
55          if (element != null) {
56              Attribute attribute = element.attribute(index);
57  
58              if (attribute != null) {
59                  return attribute.getNamespaceURI();
60              }
61          }
62  
63          return null;
64      }
65  
66      public String getAttributeLocalName(int index) {
67          if (element != null) {
68              Attribute attribute = element.attribute(index);
69  
70              if (attribute != null) {
71                  return attribute.getName();
72              }
73          }
74  
75          return null;
76      }
77  
78      public String getAttributePrefix(int index) {
79          if (element != null) {
80              Attribute attribute = element.attribute(index);
81  
82              if (attribute != null) {
83                  String prefix = attribute.getNamespacePrefix();
84  
85                  if ((prefix != null) && (prefix.length() > 0)) {
86                      return prefix;
87                  }
88              }
89          }
90  
91          return null;
92      }
93  
94      public String getAttributeRawName(int index) {
95          if (element != null) {
96              Attribute attribute = element.attribute(index);
97  
98              if (attribute != null) {
99                  return attribute.getQualifiedName();
100             }
101         }
102 
103         return null;
104     }
105 
106     public String getAttributeValue(int index) {
107         if (element != null) {
108             Attribute attribute = element.attribute(index);
109 
110             if (attribute != null) {
111                 return attribute.getValue();
112             }
113         }
114 
115         return null;
116     }
117 
118     public String getAttributeValueFromRawName(String rawName) {
119         if (element != null) {
120             for (Iterator iter = element.attributeIterator(); iter.hasNext();) {
121                 Attribute attribute = (Attribute) iter.next();
122 
123                 if (rawName.equals(attribute.getQualifiedName())) {
124                     return attribute.getValue();
125                 }
126             }
127         }
128 
129         return null;
130     }
131 
132     public String getAttributeValueFromName(String namespaceURI,
133             String localName) {
134         if (element != null) {
135             for (Iterator iter = element.attributeIterator(); iter.hasNext();) {
136                 Attribute attribute = (Attribute) iter.next();
137 
138                 if (namespaceURI.equals(attribute.getNamespaceURI())
139                         && localName.equals(attribute.getName())) {
140                     return attribute.getValue();
141                 }
142             }
143         }
144 
145         return null;
146     }
147 
148     public boolean isAttributeNamespaceDeclaration(int index) {
149         if (element != null) {
150             Attribute attribute = element.attribute(index);
151 
152             if (attribute != null) {
153                 return "xmlns".equals(attribute.getNamespacePrefix());
154             }
155         }
156 
157         return false;
158     }
159 
160     /***
161      * parameters modeled after SAX2 attribute approach
162      * 
163      * @param namespaceURI DOCUMENT ME!
164      * @param localName DOCUMENT ME!
165      * @param rawName DOCUMENT ME!
166      * @param value DOCUMENT ME!
167      * 
168      * @throws XmlPullParserException DOCUMENT ME!
169      */
170     public void addAttribute(String namespaceURI, String localName,
171             String rawName, String value) throws XmlPullParserException {
172         QName qname = QName.get(rawName, namespaceURI);
173         element.addAttribute(qname, value);
174     }
175 
176     public void addAttribute(String namespaceURI, String localName,
177             String rawName, String value, boolean isNamespaceDeclaration)
178             throws XmlPullParserException {
179         if (isNamespaceDeclaration) {
180             String prefix = "";
181             int idx = rawName.indexOf(':');
182 
183             if (idx > 0) {
184                 prefix = rawName.substring(0, idx);
185             }
186 
187             element.addNamespace(prefix, namespaceURI);
188         } else {
189             QName qname = QName.get(rawName, namespaceURI);
190             element.addAttribute(qname, value);
191         }
192     }
193 
194     public void ensureAttributesCapacity(int minCapacity)
195             throws XmlPullParserException {
196         if (element instanceof AbstractElement) {
197             AbstractElement elementImpl = (AbstractElement) element;
198             elementImpl.ensureAttributesCapacity(minCapacity);
199         }
200     }
201 
202     /***
203      * Remove all atributes.
204      * 
205      * @deprecated Use {@link #removeAttributes()} instead.
206      */
207     public void removeAtttributes() throws XmlPullParserException {
208         removeAttributes();
209     }
210 
211     public void removeAttributes() throws XmlPullParserException {
212         if (element != null) {
213             element.setAttributes(new ArrayList());
214 
215             
216             
217             
218         }
219     }
220 
221     public String getLocalName() {
222         return element.getName();
223     }
224 
225     public String getNamespaceUri() {
226         return element.getNamespaceURI();
227     }
228 
229     public String getPrefix() {
230         return element.getNamespacePrefix();
231     }
232 
233     public String getRawName() {
234         return element.getQualifiedName();
235     }
236 
237     public void modifyTag(String namespaceURI, String lName, String rawName) {
238         this.element = factory.createElement(rawName, namespaceURI);
239     }
240 
241     public void resetTag() {
242         this.element = null;
243     }
244 
245     public boolean removeAttributeByName(String namespaceURI, String localName)
246             throws XmlPullParserException {
247         if (element != null) {
248             QName qname = QName.get(localName, namespaceURI);
249             Attribute attribute = element.attribute(qname);
250             return element.remove(attribute);
251         }
252         return false;
253     }
254 
255     public boolean removeAttributeByRawName(String rawName)
256             throws XmlPullParserException {
257         if (element != null) {
258             Attribute attribute = null;
259             Iterator it = element.attributeIterator();
260             while (it.hasNext()) {
261                 Attribute current = (Attribute) it.next();
262                 if (current.getQualifiedName().equals(rawName)) {
263                     attribute = current;
264                     break;
265                 }
266             }
267             return element.remove(attribute);
268         }
269         return false;
270     }
271 
272     
273     
274     public DocumentFactory getDocumentFactory() {
275         return factory;
276     }
277 
278     public void setDocumentFactory(DocumentFactory documentFactory) {
279         this.factory = documentFactory;
280     }
281 
282     public Element getElement() {
283         return element;
284     }
285 }
286 
287 
288 
289 
290 
291 
292 
293 
294 
295 
296 
297 
298 
299 
300 
301 
302 
303 
304 
305 
306 
307 
308 
309 
310 
311 
312 
313 
314 
315 
316 
317 
318 
319 
320 
321 
322