View Javadoc

1   /*
2    * Copyright 2001-2004 (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    * $Id: ProxyXmlStartTag.java,v 1.6 2004/06/25 08:03:43 maartenc Exp $
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      // 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              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             // ##### FIXME
177             // adding this method would be nice...
178             // element.clearAttributes();
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     // Properties
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  * Redistribution and use of this software and associated documentation
228  * ("Software"), with or without modification, are permitted provided
229  * that the following conditions are met:
230  *
231  * 1. Redistributions of source code must retain copyright
232  *    statements and notices.  Redistributions must also contain a
233  *    copy of this document.
234  *
235  * 2. Redistributions in binary form must reproduce the
236  *    above copyright notice, this list of conditions and the
237  *    following disclaimer in the documentation and/or other
238  *    materials provided with the distribution.
239  *
240  * 3. The name "DOM4J" must not be used to endorse or promote
241  *    products derived from this Software without prior written
242  *    permission of MetaStuff, Ltd.  For written permission,
243  *    please contact dom4j-info@metastuff.com.
244  *
245  * 4. Products derived from this Software may not be called "DOM4J"
246  *    nor may "DOM4J" appear in their names without prior written
247  *    permission of MetaStuff, Ltd. DOM4J is a registered
248  *    trademark of MetaStuff, Ltd.
249  *
250  * 5. Due credit should be given to the DOM4J Project - 
251  *    http://www.dom4j.org
252  *
253  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
254  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
255  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
256  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
257  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
258  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
259  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
260  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
262  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
263  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
264  * OF THE POSSIBILITY OF SUCH DAMAGE.
265  *
266  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
267  *
268  * $Id: ProxyXmlStartTag.java,v 1.6 2004/06/25 08:03:43 maartenc Exp $
269  */