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