jaxp/src/share/classes/com/sun/xml/internal/stream/events/StartElementEvent.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 6 7f561c08de6b
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
 * have any questions.
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
package com.sun.xml.internal.stream.events ;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.util.List;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import java.util.Map;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import java.util.HashMap;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import java.util.Iterator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import java.util.ArrayList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import java.util.Collection;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import javax.xml.namespace.QName;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import javax.xml.stream.events.StartElement;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import javax.xml.stream.events.Attribute;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import javax.xml.namespace.NamespaceContext;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import java.io.Writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import com.sun.xml.internal.stream.util.ReadOnlyIterator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import javax.xml.stream.XMLStreamConstants;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
import javax.xml.stream.events.Namespace;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
/** Implementation of StartElementEvent.
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * @author Neeraj Bajaj Sun Microsystems,Inc.
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * @author K.Venugopal Sun Microsystems,Inc.
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
public class StartElementEvent extends DummyEvent
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
implements StartElement {
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
    private Map fAttributes;
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
    private List fNamespaces;
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
    private NamespaceContext fNamespaceContext = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
    private QName fQName;
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
    public StartElementEvent(String prefix, String uri, String localpart) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
        this(new QName(uri, localpart, prefix));
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
    public StartElementEvent(QName qname) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
        fQName = qname;
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
        init();
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    public StartElementEvent(StartElement startelement) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
        this(startelement.getName());
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
        addAttributes(startelement.getAttributes());
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
        addNamespaceAttributes(startelement.getNamespaces());
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    protected void init() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
        setEventType(XMLStreamConstants.START_ELEMENT);
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
        fAttributes = new HashMap();
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
        fNamespaces = new ArrayList();
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    public QName getName() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
        return fQName;
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    public void setName(QName qname) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
        this.fQName = qname;
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    public Iterator getAttributes() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
        if(fAttributes != null){
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
            Collection coll = fAttributes.values();
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
            return new ReadOnlyIterator(coll.iterator());
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
        return new ReadOnlyIterator();
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    public Iterator getNamespaces() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
        if(fNamespaces != null){
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
            return new ReadOnlyIterator(fNamespaces.iterator());
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
        return new ReadOnlyIterator();
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    public Attribute getAttributeByName(QName qname) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
        if(qname == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
        return (Attribute)fAttributes.get(qname);
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
    public String getNamespace(){
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
        return fQName.getNamespaceURI();
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
    public String getNamespaceURI(String prefix) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
        //check that URI was supplied when creating this startElement event and prefix matches
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
        if( getNamespace() != null && fQName.getPrefix().equals(prefix)) return getNamespace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
        //else check the namespace context
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
        if(fNamespaceContext != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
            return fNamespaceContext.getNamespaceURI(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
     * <p>Return a <code>String</code> representation of this
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
     * <code>StartElement</code> formatted as XML.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
     * @return <code>String</code> representation of this
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
     *   <code>StartElement</code> formatted as XML.
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
    public String toString() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
        StringBuffer startElement = new StringBuffer();
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
        // open element
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
        startElement.append("<");
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        startElement.append(nameAsString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
        // add any attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
        if (fAttributes != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
            Iterator it = this.getAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
            Attribute attr = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
            while (it.hasNext()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
                attr = (Attribute) it.next();
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
                startElement.append(" ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
                startElement.append(attr.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
        // add any namespaces
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
        if (fNamespaces != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
            Iterator it = fNamespaces.iterator();
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
            Namespace attr = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
            while (it.hasNext()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
                attr = (Namespace) it.next();
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
                startElement.append(" ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
                startElement.append(attr.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
        // close start tag
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
        startElement.append(">");
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
        // return StartElement as a String
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
        return startElement.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
    /** Return this event as String
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
     * @return String Event returned as string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
    public String nameAsString() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
        if("".equals(fQName.getNamespaceURI()))
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
            return fQName.getLocalPart();
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
        if(fQName.getPrefix() != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
            return "['" + fQName.getNamespaceURI() + "']:" + fQName.getPrefix() + ":" + fQName.getLocalPart();
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
            return "['" + fQName.getNamespaceURI() + "']:" + fQName.getLocalPart();
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
    /** Gets a read-only namespace context. If no context is
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
     * available this method will return an empty namespace context.
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
     * The NamespaceContext contains information about all namespaces
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
     * in scope for this StartElement.
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
     * @return the current namespace context
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
    public NamespaceContext getNamespaceContext() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
        return fNamespaceContext;
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
    public void setNamespaceContext(NamespaceContext nc) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
        fNamespaceContext = nc;
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
    /** This method will write the XMLEvent as per the XML 1.0 specification as Unicode characters.
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
     * No indentation or whitespace should be outputted.
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
     * Any user defined event type SHALL have this method
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
     * called when being written to on an output stream.
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
     * Built in Event types MUST implement this method,
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
     * but implementations MAY choose not call these methods
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
     * for optimizations reasons when writing out built in
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
     * Events to an output stream.
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
     * The output generated MUST be equivalent in terms of the
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
     * infoset expressed.
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
     * @param writer The writer that will output the data
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
     * @throws XMLStreamException if there is a fatal error writing the event
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
    public void writeAsEncodedUnicode(Writer writer) throws javax.xml.stream.XMLStreamException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
    void addAttribute(Attribute attr){
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
        if(attr.isNamespace()){
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
            fNamespaces.add(attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
        }else{
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
            fAttributes.put(attr.getName(),attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
    void addAttributes(Iterator attrs){
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
        if(attrs == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
        while(attrs.hasNext()){
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
            Attribute attr = (Attribute)attrs.next();
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
            fAttributes.put(attr.getName(),attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
    void addNamespaceAttribute(Namespace attr){
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
        if(attr == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
        fNamespaces.add(attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
    void addNamespaceAttributes(Iterator attrs){
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
        if(attrs == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
        while(attrs.hasNext()){
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
            Namespace attr = (Namespace)attrs.next();
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
            fNamespaces.add(attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
}