src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/stax/events/StartElementEvent.java

changeset 286
f50545b5e2f1
child 384
8f2986ff0235
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/stax/events/StartElementEvent.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,258 @@
     1.4 +/*
     1.5 + * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + *
    1.28 + * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
    1.29 + */
    1.30 +
    1.31 +package com.sun.xml.internal.fastinfoset.stax.events ;
    1.32 +
    1.33 +import java.util.ArrayList;
    1.34 +import java.util.Collection;
    1.35 +import java.util.HashMap;
    1.36 +import java.util.Iterator;
    1.37 +import java.util.List;
    1.38 +import java.util.Map;
    1.39 +
    1.40 +import javax.xml.namespace.NamespaceContext;
    1.41 +import javax.xml.namespace.QName;
    1.42 +import javax.xml.stream.XMLStreamConstants;
    1.43 +import javax.xml.stream.events.Attribute;
    1.44 +import javax.xml.stream.events.Namespace;
    1.45 +import javax.xml.stream.events.StartElement;
    1.46 +
    1.47 +
    1.48 +public class StartElementEvent extends EventBase implements StartElement {
    1.49 +
    1.50 +    private Map _attributes;
    1.51 +    private List _namespaces;
    1.52 +    private NamespaceContext _context = null;
    1.53 +    private QName _qname;
    1.54 +
    1.55 +    public void reset() {
    1.56 +        if (_attributes != null) _attributes.clear();
    1.57 +        if (_namespaces != null) _namespaces.clear();
    1.58 +        if (_context != null) _context = null;
    1.59 +    }
    1.60 +
    1.61 +    public StartElementEvent() {
    1.62 +        init();
    1.63 +    }
    1.64 +
    1.65 +    public StartElementEvent(String prefix, String uri, String localpart) {
    1.66 +        init();
    1.67 +        if (uri == null) uri = "";
    1.68 +        if (prefix == null) prefix ="";
    1.69 +        _qname = new QName(uri, localpart, prefix);
    1.70 +        setEventType(START_ELEMENT);
    1.71 +    }
    1.72 +
    1.73 +    public StartElementEvent(QName qname) {
    1.74 +        init();
    1.75 +        _qname = qname;
    1.76 +    }
    1.77 +
    1.78 +    public StartElementEvent(StartElement startelement) {
    1.79 +        this(startelement.getName());
    1.80 +        addAttributes(startelement.getAttributes());
    1.81 +        addNamespaces(startelement.getNamespaces());
    1.82 +    }
    1.83 +
    1.84 +    protected void init() {
    1.85 +        setEventType(XMLStreamConstants.START_ELEMENT);
    1.86 +        _attributes = new HashMap();
    1.87 +        _namespaces = new ArrayList();
    1.88 +    }
    1.89 +
    1.90 +    // ---------------------methods defined by StartElement-----------------//
    1.91 +    /**
    1.92 +    * Get the name of this event
    1.93 +    * @return the qualified name of this event
    1.94 +    */
    1.95 +    public QName getName() {
    1.96 +        return _qname;
    1.97 +    }
    1.98 +    /**
    1.99 +    * Returns an Iterator of non-namespace declared attributes
   1.100 +    * returns an empty iterator if there are no attributes.  The
   1.101 +    * iterator must contain only implementations of the javax.xml.stream.Attribute
   1.102 +    * interface.   Attributes are fundamentally unordered and may not be reported
   1.103 +    * in any order.
   1.104 +    *
   1.105 +    * @return a readonly Iterator over Attribute interfaces, or an
   1.106 +    * empty iterator
   1.107 +    */
   1.108 +    public Iterator getAttributes() {
   1.109 +        if(_attributes != null){
   1.110 +            Collection coll = _attributes.values();
   1.111 +            return new ReadIterator(coll.iterator());
   1.112 +        }
   1.113 +        return EmptyIterator.getInstance();
   1.114 +    }
   1.115 +
   1.116 +  /**
   1.117 +   * Returns an Iterator of namespaces declared on this element.
   1.118 +   * This Iterator does not contain previously declared namespaces
   1.119 +   * unless they appear on the current START_ELEMENT.
   1.120 +   * Therefore this list may contain redeclared namespaces and duplicate namespace
   1.121 +   * declarations. Use the getNamespaceContext() method to get the
   1.122 +   * current context of namespace declarations.
   1.123 +   *
   1.124 +   * <p>The iterator must contain only implementations of the
   1.125 +   * javax.xml.stream.Namespace interface.
   1.126 +   *
   1.127 +   * <p>A Namespace is an Attribute.  One
   1.128 +   * can iterate over a list of namespaces as a list of attributes.
   1.129 +   * However this method returns only the list of namespaces
   1.130 +   * declared on this START_ELEMENT and does not
   1.131 +   * include the attributes declared on this START_ELEMENT.
   1.132 +   *
   1.133 +   * @return a readonly Iterator over Namespace interfaces, or an
   1.134 +   * empty iterator if there are no namespaces.
   1.135 +   *
   1.136 +   */
   1.137 +    public Iterator getNamespaces() {
   1.138 +        if(_namespaces != null){
   1.139 +            return new ReadIterator(_namespaces.iterator());
   1.140 +        }
   1.141 +        return EmptyIterator.getInstance();
   1.142 +    }
   1.143 +
   1.144 +  /**
   1.145 +   * Returns the attribute referred to by this name
   1.146 +   * @param qname the qname of the desired name
   1.147 +   * @return the attribute corresponding to the name value or null
   1.148 +   */
   1.149 +    public Attribute getAttributeByName(QName qname) {
   1.150 +        if(qname == null)
   1.151 +            return null;
   1.152 +        return (Attribute)_attributes.get(qname);
   1.153 +    }
   1.154 +
   1.155 +    /** Gets a read-only namespace context. If no context is
   1.156 +     * available this method will return an empty namespace context.
   1.157 +     * The NamespaceContext contains information about all namespaces
   1.158 +     * in scope for this StartElement.
   1.159 +     *
   1.160 +     * @return the current namespace context
   1.161 +     */
   1.162 +    public NamespaceContext getNamespaceContext() {
   1.163 +        return _context;
   1.164 +    }
   1.165 +// ---------------------end of methods defined by StartElement-----------------//
   1.166 +
   1.167 +    public void setName(QName qname) {
   1.168 +        this._qname = qname;
   1.169 +    }
   1.170 +
   1.171 +
   1.172 +    public String getNamespace(){
   1.173 +        return _qname.getNamespaceURI();
   1.174 +    }
   1.175 +
   1.176 +    /**
   1.177 +    * Gets the value that the prefix is bound to in the
   1.178 +    * context of this element.  Returns null if
   1.179 +    * the prefix is not bound in this context
   1.180 +    * @param prefix the prefix to lookup
   1.181 +    * @return the uri bound to the prefix or null
   1.182 +    */
   1.183 +    public String getNamespaceURI(String prefix) {
   1.184 +        //first check if the URI was supplied when creating this startElement event
   1.185 +        if( getNamespace() != null ) return getNamespace();
   1.186 +        //else check the namespace context
   1.187 +        if(_context != null)
   1.188 +            return _context.getNamespaceURI(prefix);
   1.189 +        return null;
   1.190 +    }
   1.191 +
   1.192 +    public String toString() {
   1.193 +        String s = "<" + nameAsString();
   1.194 +
   1.195 +        if(_attributes != null){
   1.196 +            Iterator it = this.getAttributes();
   1.197 +            Attribute attr = null;
   1.198 +            while(it.hasNext()){
   1.199 +                attr = (Attribute)it.next();
   1.200 +                s = s + " " + attr.toString();
   1.201 +            }
   1.202 +        }
   1.203 +
   1.204 +        if(_namespaces != null){
   1.205 +            Iterator it = _namespaces.iterator();
   1.206 +            Namespace attr = null;
   1.207 +            while(it.hasNext()){
   1.208 +                attr = (Namespace)it.next();
   1.209 +                s = s + " " + attr.toString();
   1.210 +            }
   1.211 +        }
   1.212 +        s = s + ">";
   1.213 +        return s;
   1.214 +    }
   1.215 +
   1.216 +    /** Return this event as String
   1.217 +     * @return String Event returned as string.
   1.218 +     */
   1.219 +    public String nameAsString() {
   1.220 +        if("".equals(_qname.getNamespaceURI()))
   1.221 +            return _qname.getLocalPart();
   1.222 +        if(_qname.getPrefix() != null)
   1.223 +            return "['" + _qname.getNamespaceURI() + "']:" + _qname.getPrefix() + ":" + _qname.getLocalPart();
   1.224 +        else
   1.225 +            return "['" + _qname.getNamespaceURI() + "']:" + _qname.getLocalPart();
   1.226 +    }
   1.227 +
   1.228 +
   1.229 +    public void setNamespaceContext(NamespaceContext context) {
   1.230 +        _context = context;
   1.231 +    }
   1.232 +
   1.233 +    public void addAttribute(Attribute attr){
   1.234 +        _attributes.put(attr.getName(),attr);
   1.235 +    }
   1.236 +
   1.237 +    public void addAttributes(Iterator attrs){
   1.238 +        if(attrs != null) {
   1.239 +            while(attrs.hasNext()){
   1.240 +                Attribute attr = (Attribute)attrs.next();
   1.241 +                _attributes.put(attr.getName(),attr);
   1.242 +            }
   1.243 +        }
   1.244 +    }
   1.245 +
   1.246 +    public void addNamespace(Namespace namespace){
   1.247 +        if(namespace != null) {
   1.248 +            _namespaces.add(namespace);
   1.249 +        }
   1.250 +    }
   1.251 +
   1.252 +    public void addNamespaces(Iterator namespaces){
   1.253 +        if(namespaces != null) {
   1.254 +            while(namespaces.hasNext()){
   1.255 +                Namespace namespace = (Namespace)namespaces.next();
   1.256 +                _namespaces.add(namespace);
   1.257 +            }
   1.258 +        }
   1.259 +    }
   1.260 +
   1.261 +}

mercurial