Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/com/sun/xml/internal/stream/events/StartElementEvent.java
86414 views
1
/*
2
* Copyright (c) 2005, Oracle and/or its affiliates. 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* 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
protected void writeAsEncodedUnicodeEx(java.io.Writer writer)
194
throws java.io.IOException
195
{
196
writer.write(toString());
197
}
198
199
void addAttribute(Attribute attr){
200
if(attr.isNamespace()){
201
fNamespaces.add(attr);
202
}else{
203
fAttributes.put(attr.getName(),attr);
204
}
205
}
206
207
void addAttributes(Iterator attrs){
208
if(attrs == null)
209
return;
210
while(attrs.hasNext()){
211
Attribute attr = (Attribute)attrs.next();
212
fAttributes.put(attr.getName(),attr);
213
}
214
}
215
216
void addNamespaceAttribute(Namespace attr){
217
if(attr == null)
218
return;
219
fNamespaces.add(attr);
220
}
221
222
void addNamespaceAttributes(Iterator attrs){
223
if(attrs == null)
224
return;
225
while(attrs.hasNext()){
226
Namespace attr = (Namespace)attrs.next();
227
fNamespaces.add(attr);
228
}
229
}
230
231
}
232
233