View Javadoc

1   /*
2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3    *
4    * This software is open source.
5    * See the bottom of this file for the licence.
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      // XmlStartTag interface
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             // ##### FIXME
219             // adding this method would be nice...
220             // element.clearAttributes();
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     // Properties
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  * Redistribution and use of this software and associated documentation
265  * ("Software"), with or without modification, are permitted provided that the
266  * following conditions are met:
267  * 
268  * 1. Redistributions of source code must retain copyright statements and
269  * notices. Redistributions must also contain a copy of this document.
270  * 
271  * 2. Redistributions in binary form must reproduce the above copyright notice,
272  * this list of conditions and the following disclaimer in the documentation
273  * and/or other materials provided with the distribution.
274  * 
275  * 3. The name "DOM4J" must not be used to endorse or promote products derived
276  * from this Software without prior written permission of MetaStuff, Ltd. For
277  * written permission, please contact dom4j-info@metastuff.com.
278  * 
279  * 4. Products derived from this Software may not be called "DOM4J" nor may
280  * "DOM4J" appear in their names without prior written permission of MetaStuff,
281  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
282  * 
283  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
284  * 
285  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
286  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
287  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
288  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
289  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
290  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
291  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
292  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
293  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
294  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
295  * POSSIBILITY OF SUCH DAMAGE.
296  * 
297  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
298  */