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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 384
8f2986ff0235
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  *
    25  * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
    26  */
    28 package com.sun.xml.internal.fastinfoset.stax.events ;
    30 import java.util.ArrayList;
    31 import java.util.Collection;
    32 import java.util.HashMap;
    33 import java.util.Iterator;
    34 import java.util.List;
    35 import java.util.Map;
    37 import javax.xml.namespace.NamespaceContext;
    38 import javax.xml.namespace.QName;
    39 import javax.xml.stream.XMLStreamConstants;
    40 import javax.xml.stream.events.Attribute;
    41 import javax.xml.stream.events.Namespace;
    42 import javax.xml.stream.events.StartElement;
    45 public class StartElementEvent extends EventBase implements StartElement {
    47     private Map _attributes;
    48     private List _namespaces;
    49     private NamespaceContext _context = null;
    50     private QName _qname;
    52     public void reset() {
    53         if (_attributes != null) _attributes.clear();
    54         if (_namespaces != null) _namespaces.clear();
    55         if (_context != null) _context = null;
    56     }
    58     public StartElementEvent() {
    59         init();
    60     }
    62     public StartElementEvent(String prefix, String uri, String localpart) {
    63         init();
    64         if (uri == null) uri = "";
    65         if (prefix == null) prefix ="";
    66         _qname = new QName(uri, localpart, prefix);
    67         setEventType(START_ELEMENT);
    68     }
    70     public StartElementEvent(QName qname) {
    71         init();
    72         _qname = qname;
    73     }
    75     public StartElementEvent(StartElement startelement) {
    76         this(startelement.getName());
    77         addAttributes(startelement.getAttributes());
    78         addNamespaces(startelement.getNamespaces());
    79     }
    81     protected void init() {
    82         setEventType(XMLStreamConstants.START_ELEMENT);
    83         _attributes = new HashMap();
    84         _namespaces = new ArrayList();
    85     }
    87     // ---------------------methods defined by StartElement-----------------//
    88     /**
    89     * Get the name of this event
    90     * @return the qualified name of this event
    91     */
    92     public QName getName() {
    93         return _qname;
    94     }
    95     /**
    96     * Returns an Iterator of non-namespace declared attributes
    97     * returns an empty iterator if there are no attributes.  The
    98     * iterator must contain only implementations of the javax.xml.stream.Attribute
    99     * interface.   Attributes are fundamentally unordered and may not be reported
   100     * in any order.
   101     *
   102     * @return a readonly Iterator over Attribute interfaces, or an
   103     * empty iterator
   104     */
   105     public Iterator getAttributes() {
   106         if(_attributes != null){
   107             Collection coll = _attributes.values();
   108             return new ReadIterator(coll.iterator());
   109         }
   110         return EmptyIterator.getInstance();
   111     }
   113   /**
   114    * Returns an Iterator of namespaces declared on this element.
   115    * This Iterator does not contain previously declared namespaces
   116    * unless they appear on the current START_ELEMENT.
   117    * Therefore this list may contain redeclared namespaces and duplicate namespace
   118    * declarations. Use the getNamespaceContext() method to get the
   119    * current context of namespace declarations.
   120    *
   121    * <p>The iterator must contain only implementations of the
   122    * javax.xml.stream.Namespace interface.
   123    *
   124    * <p>A Namespace is an Attribute.  One
   125    * can iterate over a list of namespaces as a list of attributes.
   126    * However this method returns only the list of namespaces
   127    * declared on this START_ELEMENT and does not
   128    * include the attributes declared on this START_ELEMENT.
   129    *
   130    * @return a readonly Iterator over Namespace interfaces, or an
   131    * empty iterator if there are no namespaces.
   132    *
   133    */
   134     public Iterator getNamespaces() {
   135         if(_namespaces != null){
   136             return new ReadIterator(_namespaces.iterator());
   137         }
   138         return EmptyIterator.getInstance();
   139     }
   141   /**
   142    * Returns the attribute referred to by this name
   143    * @param qname the qname of the desired name
   144    * @return the attribute corresponding to the name value or null
   145    */
   146     public Attribute getAttributeByName(QName qname) {
   147         if(qname == null)
   148             return null;
   149         return (Attribute)_attributes.get(qname);
   150     }
   152     /** Gets a read-only namespace context. If no context is
   153      * available this method will return an empty namespace context.
   154      * The NamespaceContext contains information about all namespaces
   155      * in scope for this StartElement.
   156      *
   157      * @return the current namespace context
   158      */
   159     public NamespaceContext getNamespaceContext() {
   160         return _context;
   161     }
   162 // ---------------------end of methods defined by StartElement-----------------//
   164     public void setName(QName qname) {
   165         this._qname = qname;
   166     }
   169     public String getNamespace(){
   170         return _qname.getNamespaceURI();
   171     }
   173     /**
   174     * Gets the value that the prefix is bound to in the
   175     * context of this element.  Returns null if
   176     * the prefix is not bound in this context
   177     * @param prefix the prefix to lookup
   178     * @return the uri bound to the prefix or null
   179     */
   180     public String getNamespaceURI(String prefix) {
   181         //first check if the URI was supplied when creating this startElement event
   182         if( getNamespace() != null ) return getNamespace();
   183         //else check the namespace context
   184         if(_context != null)
   185             return _context.getNamespaceURI(prefix);
   186         return null;
   187     }
   189     public String toString() {
   190         final StringBuilder sb = new StringBuilder(64);
   192         sb.append('<').append(nameAsString());
   194         if(_attributes != null){
   195             Iterator it = this.getAttributes();
   196             Attribute attr = null;
   197             while(it.hasNext()){
   198                 attr = (Attribute)it.next();
   199                 sb.append(' ').append(attr.toString());
   200             }
   201         }
   203         if(_namespaces != null){
   204             Iterator it = _namespaces.iterator();
   205             Namespace attr = null;
   206             while(it.hasNext()){
   207                 attr = (Namespace)it.next();
   208                 sb.append(' ').append(attr.toString());
   209             }
   210         }
   211         sb.append('>');
   212         return sb.toString();
   213     }
   215     /** Return this event as String
   216      * @return String Event returned as string.
   217      */
   218     public String nameAsString() {
   219         if("".equals(_qname.getNamespaceURI()))
   220             return _qname.getLocalPart();
   221         if(_qname.getPrefix() != null)
   222             return "['" + _qname.getNamespaceURI() + "']:" + _qname.getPrefix() + ":" + _qname.getLocalPart();
   223         else
   224             return "['" + _qname.getNamespaceURI() + "']:" + _qname.getLocalPart();
   225     }
   228     public void setNamespaceContext(NamespaceContext context) {
   229         _context = context;
   230     }
   232     public void addAttribute(Attribute attr){
   233         _attributes.put(attr.getName(),attr);
   234     }
   236     public void addAttributes(Iterator attrs){
   237         if(attrs != null) {
   238             while(attrs.hasNext()){
   239                 Attribute attr = (Attribute)attrs.next();
   240                 _attributes.put(attr.getName(),attr);
   241             }
   242         }
   243     }
   245     public void addNamespace(Namespace namespace){
   246         if(namespace != null) {
   247             _namespaces.add(namespace);
   248         }
   249     }
   251     public void addNamespaces(Iterator namespaces){
   252         if(namespaces != null) {
   253             while(namespaces.hasNext()){
   254                 Namespace namespace = (Namespace)namespaces.next();
   255                 _namespaces.add(namespace);
   256             }
   257         }
   258     }
   260 }

mercurial