001: /*
002: * $Id: StartElementEvent.java,v 1.2 2006/04/01 06:01:35 jeffsuttor Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * [Name of File] [ver.__] [Date]
025: *
026: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027: */
028:
029: package com.sun.xml.stream.events;
030:
031: import java.util.List;
032: import java.util.Map;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.ArrayList;
036: import java.util.Collection;
037:
038: import javax.xml.namespace.QName;
039: import javax.xml.stream.events.StartElement;
040: import javax.xml.stream.events.Attribute;
041: import javax.xml.namespace.NamespaceContext;
042: import java.io.Writer;
043: import com.sun.xml.stream.util.ReadOnlyIterator;
044: import javax.xml.stream.XMLStreamConstants;
045: import javax.xml.stream.events.Namespace;
046:
047: /** Implementation of StartElementEvent.
048: *
049: * @author Neeraj Bajaj Sun Microsystems,Inc.
050: * @author K.Venugopal Sun Microsystems,Inc.
051: */
052:
053: public class StartElementEvent extends DummyEvent implements
054: StartElement {
055:
056: private Map fAttributes;
057: private List fNamespaces;
058: private NamespaceContext fNamespaceContext = null;
059: private QName fQName;
060:
061: public StartElementEvent(String prefix, String uri, String localpart) {
062: this (new QName(uri, localpart, prefix));
063: }
064:
065: public StartElementEvent(QName qname) {
066: fQName = qname;
067: init();
068: }
069:
070: public StartElementEvent(StartElement startelement) {
071: this (startelement.getName());
072: addAttributes(startelement.getAttributes());
073: addNamespaceAttributes(startelement.getNamespaces());
074: }
075:
076: protected void init() {
077: setEventType(XMLStreamConstants.START_ELEMENT);
078: fAttributes = new HashMap();
079: fNamespaces = new ArrayList();
080: }
081:
082: public QName getName() {
083: return fQName;
084: }
085:
086: public void setName(QName qname) {
087: this .fQName = qname;
088: }
089:
090: public Iterator getAttributes() {
091: if (fAttributes != null) {
092: Collection coll = fAttributes.values();
093: return new ReadOnlyIterator(coll.iterator());
094: }
095: return new ReadOnlyIterator();
096: }
097:
098: public Iterator getNamespaces() {
099: if (fNamespaces != null) {
100: return new ReadOnlyIterator(fNamespaces.iterator());
101: }
102: return new ReadOnlyIterator();
103: }
104:
105: public Attribute getAttributeByName(QName qname) {
106: if (qname == null)
107: return null;
108: return (Attribute) fAttributes.get(qname);
109: }
110:
111: public String getNamespace() {
112: return fQName.getNamespaceURI();
113: }
114:
115: public String getNamespaceURI(String prefix) {
116: //check that URI was supplied when creating this startElement event and prefix matches
117: if (getNamespace() != null && fQName.getPrefix().equals(prefix))
118: return getNamespace();
119: //else check the namespace context
120: if (fNamespaceContext != null)
121: return fNamespaceContext.getNamespaceURI(prefix);
122: return null;
123: }
124:
125: public String toString() {
126: String s = "<" + nameAsString();
127:
128: if (fAttributes != null) {
129: Iterator it = this .getAttributes();
130: Attribute attr = null;
131: while (it.hasNext()) {
132: attr = (Attribute) it.next();
133: s = s + " " + attr.toString();
134: }
135: }
136:
137: if (fNamespaces != null) {
138: Iterator it = fNamespaces.iterator();
139: Namespace attr = null;
140: while (it.hasNext()) {
141: attr = (Namespace) it.next();
142: s = s + " " + attr.toString();
143: }
144: }
145: s = s + ">";
146: return s;
147: }
148:
149: /** Return this event as String
150: * @return String Event returned as string.
151: */
152: public String nameAsString() {
153: if ("".equals(fQName.getNamespaceURI()))
154: return fQName.getLocalPart();
155: if (fQName.getPrefix() != null)
156: return "['" + fQName.getNamespaceURI() + "']:"
157: + fQName.getPrefix() + ":" + fQName.getLocalPart();
158: else
159: return "['" + fQName.getNamespaceURI() + "']:"
160: + fQName.getLocalPart();
161: }
162:
163: /** Gets a read-only namespace context. If no context is
164: * available this method will return an empty namespace context.
165: * The NamespaceContext contains information about all namespaces
166: * in scope for this StartElement.
167: *
168: * @return the current namespace context
169: */
170: public NamespaceContext getNamespaceContext() {
171: return fNamespaceContext;
172: }
173:
174: public void setNamespaceContext(NamespaceContext nc) {
175: fNamespaceContext = nc;
176: }
177:
178: /** This method will write the XMLEvent as per the XML 1.0 specification as Unicode characters.
179: * No indentation or whitespace should be outputted.
180: *
181: * Any user defined event type SHALL have this method
182: * called when being written to on an output stream.
183: * Built in Event types MUST implement this method,
184: * but implementations MAY choose not call these methods
185: * for optimizations reasons when writing out built in
186: * Events to an output stream.
187: * The output generated MUST be equivalent in terms of the
188: * infoset expressed.
189: *
190: * @param writer The writer that will output the data
191: * @throws XMLStreamException if there is a fatal error writing the event
192: */
193: public void writeAsEncodedUnicode(Writer writer)
194: throws javax.xml.stream.XMLStreamException {
195: }
196:
197: void addAttribute(Attribute attr) {
198: if (attr.isNamespace()) {
199: fNamespaces.add(attr);
200: } else {
201: fAttributes.put(attr.getName(), attr);
202: }
203: }
204:
205: void addAttributes(Iterator attrs) {
206: if (attrs == null)
207: return;
208: while (attrs.hasNext()) {
209: Attribute attr = (Attribute) attrs.next();
210: fAttributes.put(attr.getName(), attr);
211: }
212: }
213:
214: void addNamespaceAttribute(Namespace attr) {
215: if (attr == null)
216: return;
217: fNamespaces.add(attr);
218: }
219:
220: void addNamespaceAttributes(Iterator attrs) {
221: if (attrs == null)
222: return;
223: while (attrs.hasNext()) {
224: Namespace attr = (Namespace) attrs.next();
225: fNamespaces.add(attr);
226: }
227: }
228:
229: }
|