KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > xpp > ProxyXmlStartTag


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 JavaDoc;
11 import java.util.Iterator JavaDoc;
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     // XmlStartTag interface
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 JavaDoc 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 JavaDoc 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 JavaDoc getAttributePrefix(int index) {
79         if (element != null) {
80             Attribute attribute = element.attribute(index);
81
82             if (attribute != null) {
83                 String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getAttributeValueFromRawName(String JavaDoc rawName) {
119         if (element != null) {
120             for (Iterator JavaDoc 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 JavaDoc getAttributeValueFromName(String JavaDoc namespaceURI,
133             String JavaDoc localName) {
134         if (element != null) {
135             for (Iterator JavaDoc 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 JavaDoc namespaceURI, String JavaDoc localName,
171             String JavaDoc rawName, String JavaDoc value) throws XmlPullParserException {
172         QName qname = QName.get(rawName, namespaceURI);
173         element.addAttribute(qname, value);
174     }
175
176     public void addAttribute(String JavaDoc namespaceURI, String JavaDoc localName,
177             String JavaDoc rawName, String JavaDoc value, boolean isNamespaceDeclaration)
178             throws XmlPullParserException {
179         if (isNamespaceDeclaration) {
180             String JavaDoc 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 JavaDoc());
214
215             // ##### FIXME
216
// adding this method would be nice...
217
// element.clearAttributes();
218
}
219     }
220
221     public String JavaDoc getLocalName() {
222         return element.getName();
223     }
224
225     public String JavaDoc getNamespaceUri() {
226         return element.getNamespaceURI();
227     }
228
229     public String JavaDoc getPrefix() {
230         return element.getNamespacePrefix();
231     }
232
233     public String JavaDoc getRawName() {
234         return element.getQualifiedName();
235     }
236
237     public void modifyTag(String JavaDoc namespaceURI, String JavaDoc lName, String JavaDoc 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 JavaDoc namespaceURI, String JavaDoc 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 JavaDoc rawName)
256             throws XmlPullParserException {
257         if (element != null) {
258             Attribute attribute = null;
259             Iterator JavaDoc 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     // Properties
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  * Redistribution and use of this software and associated documentation
289  * ("Software"), with or without modification, are permitted provided that the
290  * following conditions are met:
291  *
292  * 1. Redistributions of source code must retain copyright statements and
293  * notices. Redistributions must also contain a copy of this document.
294  *
295  * 2. Redistributions in binary form must reproduce the above copyright notice,
296  * this list of conditions and the following disclaimer in the documentation
297  * and/or other materials provided with the distribution.
298  *
299  * 3. The name "DOM4J" must not be used to endorse or promote products derived
300  * from this Software without prior written permission of MetaStuff, Ltd. For
301  * written permission, please contact dom4j-info@metastuff.com.
302  *
303  * 4. Products derived from this Software may not be called "DOM4J" nor may
304  * "DOM4J" appear in their names without prior written permission of MetaStuff,
305  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
306  *
307  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
308  *
309  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
310  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
311  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
312  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
313  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
314  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
315  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
316  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
317  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
318  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
319  * POSSIBILITY OF SUCH DAMAGE.
320  *
321  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
322  */

323
Popular Tags