Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/com/sun/xml/internal/stream/events/StartElementEvent.java
86414 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.xml.internal.stream.events ;2627import java.util.List;28import java.util.Map;29import java.util.HashMap;30import java.util.Iterator;31import java.util.ArrayList;32import java.util.Collection;3334import javax.xml.namespace.QName;35import javax.xml.stream.events.StartElement;36import javax.xml.stream.events.Attribute;37import javax.xml.namespace.NamespaceContext;38import java.io.Writer;39import com.sun.xml.internal.stream.util.ReadOnlyIterator;40import javax.xml.stream.XMLStreamConstants;41import javax.xml.stream.events.Namespace;4243/** Implementation of StartElementEvent.44*45* @author Neeraj Bajaj Sun Microsystems,Inc.46* @author K.Venugopal Sun Microsystems,Inc.47*/4849public class StartElementEvent extends DummyEvent50implements StartElement {5152private Map fAttributes;53private List fNamespaces;54private NamespaceContext fNamespaceContext = null;55private QName fQName;5657public StartElementEvent(String prefix, String uri, String localpart) {58this(new QName(uri, localpart, prefix));59}6061public StartElementEvent(QName qname) {62fQName = qname;63init();64}6566public StartElementEvent(StartElement startelement) {67this(startelement.getName());68addAttributes(startelement.getAttributes());69addNamespaceAttributes(startelement.getNamespaces());70}7172protected void init() {73setEventType(XMLStreamConstants.START_ELEMENT);74fAttributes = new HashMap();75fNamespaces = new ArrayList();76}7778public QName getName() {79return fQName;80}8182public void setName(QName qname) {83this.fQName = qname;84}8586public Iterator getAttributes() {87if(fAttributes != null){88Collection coll = fAttributes.values();89return new ReadOnlyIterator(coll.iterator());90}91return new ReadOnlyIterator();92}9394public Iterator getNamespaces() {95if(fNamespaces != null){96return new ReadOnlyIterator(fNamespaces.iterator());97}98return new ReadOnlyIterator();99}100101public Attribute getAttributeByName(QName qname) {102if(qname == null)103return null;104return (Attribute)fAttributes.get(qname);105}106107public String getNamespace(){108return fQName.getNamespaceURI();109}110111public String getNamespaceURI(String prefix) {112//check that URI was supplied when creating this startElement event and prefix matches113if( getNamespace() != null && fQName.getPrefix().equals(prefix)) return getNamespace();114//else check the namespace context115if(fNamespaceContext != null)116return fNamespaceContext.getNamespaceURI(prefix);117return null;118}119120/**121* <p>Return a <code>String</code> representation of this122* <code>StartElement</code> formatted as XML.</p>123*124* @return <code>String</code> representation of this125* <code>StartElement</code> formatted as XML.126*/127public String toString() {128129StringBuffer startElement = new StringBuffer();130131// open element132startElement.append("<");133startElement.append(nameAsString());134135// add any attributes136if (fAttributes != null) {137Iterator it = this.getAttributes();138Attribute attr = null;139while (it.hasNext()) {140attr = (Attribute) it.next();141startElement.append(" ");142startElement.append(attr.toString());143}144}145146// add any namespaces147if (fNamespaces != null) {148Iterator it = fNamespaces.iterator();149Namespace attr = null;150while (it.hasNext()) {151attr = (Namespace) it.next();152startElement.append(" ");153startElement.append(attr.toString());154}155}156157// close start tag158startElement.append(">");159160// return StartElement as a String161return startElement.toString();162}163164/** Return this event as String165* @return String Event returned as string.166*/167public String nameAsString() {168if("".equals(fQName.getNamespaceURI()))169return fQName.getLocalPart();170if(fQName.getPrefix() != null)171return "['" + fQName.getNamespaceURI() + "']:" + fQName.getPrefix() + ":" + fQName.getLocalPart();172else173return "['" + fQName.getNamespaceURI() + "']:" + fQName.getLocalPart();174}175176177/** Gets a read-only namespace context. If no context is178* available this method will return an empty namespace context.179* The NamespaceContext contains information about all namespaces180* in scope for this StartElement.181*182* @return the current namespace context183*/184public NamespaceContext getNamespaceContext() {185return fNamespaceContext;186}187188public void setNamespaceContext(NamespaceContext nc) {189fNamespaceContext = nc;190}191192protected void writeAsEncodedUnicodeEx(java.io.Writer writer)193throws java.io.IOException194{195writer.write(toString());196}197198void addAttribute(Attribute attr){199if(attr.isNamespace()){200fNamespaces.add(attr);201}else{202fAttributes.put(attr.getName(),attr);203}204}205206void addAttributes(Iterator attrs){207if(attrs == null)208return;209while(attrs.hasNext()){210Attribute attr = (Attribute)attrs.next();211fAttributes.put(attr.getName(),attr);212}213}214215void addNamespaceAttribute(Namespace attr){216if(attr == null)217return;218fNamespaces.add(attr);219}220221void addNamespaceAttributes(Iterator attrs){222if(attrs == null)223return;224while(attrs.hasNext()){225Namespace attr = (Namespace)attrs.next();226fNamespaces.add(attr);227}228}229230}231232233