286N/A/*
286N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/Apackage com.sun.xml.internal.stream.events ;
286N/A
286N/Aimport java.util.List;
286N/Aimport java.util.Map;
286N/Aimport java.util.HashMap;
286N/Aimport java.util.Iterator;
286N/Aimport java.util.ArrayList;
286N/Aimport java.util.Collection;
286N/A
286N/Aimport javax.xml.namespace.QName;
286N/Aimport javax.xml.stream.events.StartElement;
286N/Aimport javax.xml.stream.events.Attribute;
286N/Aimport javax.xml.namespace.NamespaceContext;
286N/Aimport java.io.Writer;
286N/Aimport com.sun.xml.internal.stream.util.ReadOnlyIterator;
286N/Aimport javax.xml.stream.XMLStreamConstants;
286N/Aimport javax.xml.stream.events.Namespace;
286N/A
286N/A/** Implementation of StartElementEvent.
286N/A *
286N/A * @author Neeraj Bajaj Sun Microsystems,Inc.
286N/A * @author K.Venugopal Sun Microsystems,Inc.
286N/A */
286N/A
286N/Apublic class StartElementEvent extends DummyEvent
286N/Aimplements StartElement {
286N/A
286N/A private Map fAttributes;
286N/A private List fNamespaces;
286N/A private NamespaceContext fNamespaceContext = null;
286N/A private QName fQName;
286N/A
286N/A public StartElementEvent(String prefix, String uri, String localpart) {
286N/A this(new QName(uri, localpart, prefix));
286N/A }
286N/A
286N/A public StartElementEvent(QName qname) {
286N/A fQName = qname;
286N/A init();
286N/A }
286N/A
286N/A public StartElementEvent(StartElement startelement) {
286N/A this(startelement.getName());
286N/A addAttributes(startelement.getAttributes());
286N/A addNamespaceAttributes(startelement.getNamespaces());
286N/A }
286N/A
286N/A protected void init() {
286N/A setEventType(XMLStreamConstants.START_ELEMENT);
286N/A fAttributes = new HashMap();
286N/A fNamespaces = new ArrayList();
286N/A }
286N/A
286N/A public QName getName() {
286N/A return fQName;
286N/A }
286N/A
286N/A public void setName(QName qname) {
286N/A this.fQName = qname;
286N/A }
286N/A
286N/A public Iterator getAttributes() {
286N/A if(fAttributes != null){
286N/A Collection coll = fAttributes.values();
286N/A return new ReadOnlyIterator(coll.iterator());
286N/A }
286N/A return new ReadOnlyIterator();
286N/A }
286N/A
286N/A public Iterator getNamespaces() {
286N/A if(fNamespaces != null){
286N/A return new ReadOnlyIterator(fNamespaces.iterator());
286N/A }
286N/A return new ReadOnlyIterator();
286N/A }
286N/A
286N/A public Attribute getAttributeByName(QName qname) {
286N/A if(qname == null)
286N/A return null;
286N/A return (Attribute)fAttributes.get(qname);
286N/A }
286N/A
286N/A public String getNamespace(){
286N/A return fQName.getNamespaceURI();
286N/A }
286N/A
286N/A public String getNamespaceURI(String prefix) {
286N/A //check that URI was supplied when creating this startElement event and prefix matches
286N/A if( getNamespace() != null && fQName.getPrefix().equals(prefix)) return getNamespace();
286N/A //else check the namespace context
286N/A if(fNamespaceContext != null)
286N/A return fNamespaceContext.getNamespaceURI(prefix);
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * <p>Return a <code>String</code> representation of this
286N/A * <code>StartElement</code> formatted as XML.</p>
286N/A *
286N/A * @return <code>String</code> representation of this
286N/A * <code>StartElement</code> formatted as XML.
286N/A */
286N/A public String toString() {
286N/A
286N/A StringBuffer startElement = new StringBuffer();
286N/A
286N/A // open element
286N/A startElement.append("<");
286N/A startElement.append(nameAsString());
286N/A
286N/A // add any attributes
286N/A if (fAttributes != null) {
286N/A Iterator it = this.getAttributes();
286N/A Attribute attr = null;
286N/A while (it.hasNext()) {
286N/A attr = (Attribute) it.next();
286N/A startElement.append(" ");
286N/A startElement.append(attr.toString());
286N/A }
286N/A }
286N/A
286N/A // add any namespaces
286N/A if (fNamespaces != null) {
286N/A Iterator it = fNamespaces.iterator();
286N/A Namespace attr = null;
286N/A while (it.hasNext()) {
286N/A attr = (Namespace) it.next();
286N/A startElement.append(" ");
286N/A startElement.append(attr.toString());
286N/A }
286N/A }
286N/A
286N/A // close start tag
286N/A startElement.append(">");
286N/A
286N/A // return StartElement as a String
286N/A return startElement.toString();
286N/A }
286N/A
286N/A /** Return this event as String
286N/A * @return String Event returned as string.
286N/A */
286N/A public String nameAsString() {
286N/A if("".equals(fQName.getNamespaceURI()))
286N/A return fQName.getLocalPart();
286N/A if(fQName.getPrefix() != null)
286N/A return "['" + fQName.getNamespaceURI() + "']:" + fQName.getPrefix() + ":" + fQName.getLocalPart();
286N/A else
286N/A return "['" + fQName.getNamespaceURI() + "']:" + fQName.getLocalPart();
286N/A }
286N/A
286N/A
286N/A /** Gets a read-only namespace context. If no context is
286N/A * available this method will return an empty namespace context.
286N/A * The NamespaceContext contains information about all namespaces
286N/A * in scope for this StartElement.
286N/A *
286N/A * @return the current namespace context
286N/A */
286N/A public NamespaceContext getNamespaceContext() {
286N/A return fNamespaceContext;
286N/A }
286N/A
286N/A public void setNamespaceContext(NamespaceContext nc) {
286N/A fNamespaceContext = nc;
286N/A }
286N/A
286N/A protected void writeAsEncodedUnicodeEx(java.io.Writer writer)
286N/A throws java.io.IOException
286N/A {
286N/A writer.write(toString());
286N/A }
286N/A
286N/A void addAttribute(Attribute attr){
286N/A if(attr.isNamespace()){
286N/A fNamespaces.add(attr);
286N/A }else{
286N/A fAttributes.put(attr.getName(),attr);
286N/A }
286N/A }
286N/A
286N/A void addAttributes(Iterator attrs){
286N/A if(attrs == null)
286N/A return;
286N/A while(attrs.hasNext()){
286N/A Attribute attr = (Attribute)attrs.next();
286N/A fAttributes.put(attr.getName(),attr);
286N/A }
286N/A }
286N/A
286N/A void addNamespaceAttribute(Namespace attr){
286N/A if(attr == null)
286N/A return;
286N/A fNamespaces.add(attr);
286N/A }
286N/A
286N/A void addNamespaceAttributes(Iterator attrs){
286N/A if(attrs == null)
286N/A return;
286N/A while(attrs.hasNext()){
286N/A Namespace attr = (Namespace)attrs.next();
286N/A fNamespaces.add(attr);
286N/A }
286N/A }
286N/A
286N/A}