001/*
002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
003 * 
004 * This software is open source. 
005 * See the bottom of this file for the licence.
006 * 
007 * $Id: ProxyXmlStartTag.java,v 1.2 2001/12/19 09:51:39 jstrachan Exp $
008 */
009
010package org.dom4j.xpp;
011
012import java.util.ArrayList;
013import java.util.Iterator;
014
015import org.dom4j.Attribute;
016import org.dom4j.DocumentFactory;
017import org.dom4j.Element;
018import org.dom4j.QName;
019import org.dom4j.tree.AbstractElement;
020
021import org.gjt.xpp.XmlPullParserException;
022import org.gjt.xpp.XmlStartTag;
023
024/** <p><code>ProxyXmlStartTag</code> implements the XPP XmlSmartTag
025  * interface while creating a dom4j Element underneath.</p>
026  *
027  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
028  * @version $Revision: 1.2 $
029  */
030public class ProxyXmlStartTag implements XmlStartTag {
031
032    /** The element being constructed */
033    private Element element;
034    
035    /** The factory used to create new elements */
036    private DocumentFactory factory = DocumentFactory.getInstance();
037
038    
039    public ProxyXmlStartTag() { 
040    }
041    
042    public ProxyXmlStartTag(Element element) { 
043        this.element = element;
044    }
045
046    // XmlStartTag interface 
047    //-------------------------------------------------------------------------                        
048    public void resetStartTag() {
049        this.element = null;
050    }
051    
052    public int getAttributeCount() {
053        return (element != null) ? element.attributeCount() : 0;
054    }
055    
056    public String getAttributeNamespaceUri(int index) {
057        if (element != null ) {
058            Attribute attribute = element.attribute(index);
059            if ( attribute != null ) {
060                return attribute.getNamespaceURI();
061            }
062        }
063        return null;
064    }
065    
066    public String getAttributeLocalName(int index) {
067        if (element != null ) {
068            Attribute attribute = element.attribute(index);
069            if ( attribute != null ) {
070                return attribute.getName();
071            }
072        }
073        return null;
074    }
075    
076    public String getAttributePrefix(int index) {
077        if (element != null ) {
078            Attribute attribute = element.attribute(index);
079            if ( attribute != null ) {
080                String prefix = attribute.getNamespacePrefix();
081                if ( prefix != null && prefix.length() > 0 ) {
082                    return prefix;
083                }
084            }
085        }
086        return null;
087    }
088    
089    public String getAttributeRawName(int index) {
090        if (element != null ) {
091            Attribute attribute = element.attribute(index);
092            if ( attribute != null ) {
093                return attribute.getQualifiedName();
094            }
095        }
096        return null;
097    }
098    
099    public String getAttributeValue(int index) {
100        if (element != null ) {
101            Attribute attribute = element.attribute(index);
102            if ( attribute != null ) {
103                return attribute.getValue();
104            }
105        }
106        return null;
107    }
108    
109    public String getAttributeValueFromRawName(String rawName) {
110        if (element != null ) {
111            for ( Iterator iter = element.attributeIterator(); iter.hasNext(); ) {
112                Attribute attribute = (Attribute) iter.next();
113                if ( rawName.equals( attribute.getQualifiedName() ) ) {
114                    return attribute.getValue();
115                }
116            }
117        }
118        return null;
119    }
120    
121    public String getAttributeValueFromName(String namespaceURI, String localName) {
122        if (element != null ) {
123            for ( Iterator iter = element.attributeIterator(); iter.hasNext(); ) {
124                Attribute attribute = (Attribute) iter.next();
125                if ( namespaceURI.equals( attribute.getNamespaceURI() ) && localName.equals( attribute.getName() ) ) {
126                    return attribute.getValue();
127                }
128            }
129        }
130        return null;
131    }
132    
133    public boolean isAttributeNamespaceDeclaration(int index) {
134        if (element != null ) {
135            Attribute attribute = element.attribute(index);
136            if ( attribute != null ) {
137                return "xmlns".equals( attribute.getNamespacePrefix() );
138            }
139        }
140        return false;
141    }
142    
143    
144    /** parameters modeled after SAX2 attribute approach */
145    public void addAttribute(String namespaceURI, String localName, String rawName, String value) throws XmlPullParserException {
146        QName qname = QName.get( rawName, namespaceURI );
147        element.addAttribute( qname, value );
148    }
149    
150    
151    public void addAttribute(String namespaceURI, String localName, String rawName, String value, boolean isNamespaceDeclaration) throws XmlPullParserException {
152        if ( isNamespaceDeclaration ) {
153            String prefix = "";
154            int idx = rawName.indexOf( ':' );
155            if ( idx > 0 ) {
156                prefix = rawName.substring( 0, idx );
157            }
158            element.addNamespace( prefix, namespaceURI );
159        }
160        else {
161            QName qname = QName.get( rawName, namespaceURI );
162            element.addAttribute( qname, value );
163        }
164    }
165    
166    public void ensureAttributesCapacity(int minCapacity) throws XmlPullParserException {
167        if ( element instanceof AbstractElement ) {
168            AbstractElement elementImpl = (AbstractElement) element;
169            elementImpl.ensureAttributesCapacity(minCapacity);
170        }
171    }
172    
173    /** remove all atribute */
174    public void removeAtttributes() throws XmlPullParserException {
175        if ( element != null ) {
176            element.setAttributes( new ArrayList() );
177            // ##### FIXME
178            // adding this method would be nice...
179            // element.clearAttributes();
180        }
181    }
182    
183
184    public String getLocalName() {
185        return element.getName();
186    }
187    
188    public String getNamespaceUri() {
189        return element.getNamespaceURI();
190    }
191    
192    public String getPrefix() {
193        return element.getNamespacePrefix();
194    }
195    
196    
197    public String getRawName() {
198        return element.getQualifiedName();
199    }
200    
201    public void modifyTag(String namespaceURI, String localName, String rawName) {
202        this.element = factory.createElement( rawName, namespaceURI );
203    }
204    
205    public void resetTag() {
206        this.element = null;
207    }
208    
209    // Properties
210    //-------------------------------------------------------------------------                        
211    public DocumentFactory getDocumentFactory() {
212        return factory;
213    }
214    
215    public void setDocumentFactory(DocumentFactory factory) {
216        this.factory = factory;
217    }
218    
219    public Element getElement() {
220        return element;
221    }
222}
223
224
225
226
227/*
228 * Redistribution and use of this software and associated documentation
229 * ("Software"), with or without modification, are permitted provided
230 * that the following conditions are met:
231 *
232 * 1. Redistributions of source code must retain copyright
233 *    statements and notices.  Redistributions must also contain a
234 *    copy of this document.
235 *
236 * 2. Redistributions in binary form must reproduce the
237 *    above copyright notice, this list of conditions and the
238 *    following disclaimer in the documentation and/or other
239 *    materials provided with the distribution.
240 *
241 * 3. The name "DOM4J" must not be used to endorse or promote
242 *    products derived from this Software without prior written
243 *    permission of MetaStuff, Ltd.  For written permission,
244 *    please contact dom4j-info@metastuff.com.
245 *
246 * 4. Products derived from this Software may not be called "DOM4J"
247 *    nor may "DOM4J" appear in their names without prior written
248 *    permission of MetaStuff, Ltd. DOM4J is a registered
249 *    trademark of MetaStuff, Ltd.
250 *
251 * 5. Due credit should be given to the DOM4J Project
252 *    (http://dom4j.org/).
253 *
254 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
255 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
256 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
257 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
258 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
259 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
260 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
261 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
263 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
264 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
265 * OF THE POSSIBILITY OF SUCH DAMAGE.
266 *
267 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
268 *
269 * $Id: ProxyXmlStartTag.java,v 1.2 2001/12/19 09:51:39 jstrachan Exp $
270 */