-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
SimpleOutputElement.java
376 lines (333 loc) · 11.2 KB
/
SimpleOutputElement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* Woodstox XML processor
*
* Copyright (c) 2005 Tatu Saloranta, tatu.saloranta@iki.fi
*
* Licensed under the License specified in file LICENSE, included with
* the source code.
* You may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ctc.wstx.sw;
import java.util.*;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import com.ctc.wstx.util.BijectiveNsMap;
/**
* Class that encapsulates information about a specific element in virtual
* output stack for namespace-aware writers.
* It provides support for URI-to-prefix mappings as well as namespace
* mapping generation.
*<p>
* One noteworthy feature of the class is that it is designed to allow
* "short-term recycling", ie. instances can be reused within context
* of a simple document output. While reuse/recycling of such lightweight
* object is often useless or even counter productive, here it may
* be worth using, due to simplicity of the scheme (basically using
* a very simple free-elements linked list).
*/
public final class SimpleOutputElement
extends OutputElementBase
{
/*
///////////////////////////////////////////////////////////////////////
// Information about element itself:
///////////////////////////////////////////////////////////////////////
*/
/**
* Reference to the parent element, element enclosing this element.
* Null for root element.
* Non-final only to allow temporary pooling
* (on per-writer basis, to keep these short-lived).
*/
protected SimpleOutputElement mParent;
/**
* Prefix that is used for the element. Can not be final, since sometimes
* it needs to be dynamically generated and bound after creating the
* element instance.
*/
protected String mPrefix;
/**
* Local name of the element.
* Non-final only to allow reuse.
*/
protected String mLocalName;
/**
* Namespace of the element, whatever {@link #mPrefix} maps to.
* Non-final only to allow reuse.
*/
protected String mURI;
/*
///////////////////////////////////////////////////////////////////////
// Attribute information
///////////////////////////////////////////////////////////////////////
*/
/**
* Map used to check for duplicate attribute declarations, if
* feature is enabled.
*/
protected HashSet<AttrName> mAttrSet = null;
/*
///////////////////////////////////////////////////////////////////////
// Life-cycle
///////////////////////////////////////////////////////////////////////
*/
/**
* Constructor for the virtual root element
*/
private SimpleOutputElement()
{
super();
mParent = null;
mPrefix = null;
mLocalName = "";
mURI = null;
}
private SimpleOutputElement(SimpleOutputElement parent,
String prefix, String localName, String uri,
BijectiveNsMap ns)
{
super(parent, ns);
mParent = parent;
mPrefix = prefix;
mLocalName = localName;
mURI = uri;
}
/**
* Method called to reuse a pooled instance.
*
* @returns Chained pooled instance that should now be head of the
* reuse chain
*/
private void relink(SimpleOutputElement parent,
String prefix, String localName, String uri)
{
super.relink(parent);
mParent = parent;
mPrefix = prefix;
mLocalName = localName;
mURI = uri;
mNsMapping = parent.mNsMapping;
mNsMapShared = (mNsMapping != null);
mDefaultNsURI = parent.mDefaultNsURI;
mRootNsContext = parent.mRootNsContext;
}
public static SimpleOutputElement createRoot()
{
return new SimpleOutputElement();
}
/**
* Simplest factory method, which gets called when a 1-argument
* element output method is called. It is, then, assumed to
* use the default namespce.
*/
protected SimpleOutputElement createChild(String localName)
{
/* At this point we can also discard attribute Map; it is assumed
* that when a child element has been opened, no more attributes
* can be output.
*/
mAttrSet = null;
return new SimpleOutputElement(this, null, localName,
mDefaultNsURI, mNsMapping);
}
/**
* @return New head of the recycle pool
*/
protected SimpleOutputElement reuseAsChild(SimpleOutputElement parent,
String localName)
{
mAttrSet = null;
SimpleOutputElement poolHead = mParent;
relink(parent, null, localName, mDefaultNsURI);
return poolHead;
}
protected SimpleOutputElement reuseAsChild(SimpleOutputElement parent,
String prefix, String localName,
String uri)
{
mAttrSet = null;
SimpleOutputElement poolHead = mParent;
relink(parent, prefix, localName, uri);
return poolHead;
}
/**
* Full factory method, used for 'normal' namespace qualified output
* methods.
*/
protected SimpleOutputElement createChild(String prefix, String localName,
String uri)
{
/* At this point we can also discard attribute Map; it is assumed
* that when a child element has been opened, no more attributes
* can be output.
*/
mAttrSet = null;
return new SimpleOutputElement(this, prefix, localName, uri, mNsMapping);
}
/**
* Method called to temporarily link this instance to a pool, to
* allow reusing of instances with the same reader.
*/
protected void addToPool(SimpleOutputElement poolHead)
{
mParent = poolHead;
}
/*
///////////////////////////////////////////////////////////////////////
// Public API, accessors
///////////////////////////////////////////////////////////////////////
*/
public SimpleOutputElement getParent() {
return mParent;
}
@Override
public boolean isRoot() {
// (Virtual) Root element has no parent...
return (mParent == null);
}
/**
* @return String presentation of the fully-qualified name, in
* "prefix:localName" format (no URI). Useful for error and
* debugging messages.
*/
@Override
public String getNameDesc() {
if (mPrefix != null && mPrefix.length() > 0) {
return mPrefix + ":" +mLocalName;
}
if (mLocalName != null && mLocalName.length() > 0) {
return mLocalName;
}
return "#error"; // unexpected case
}
public String getPrefix() {
return mPrefix;
}
public String getLocalName() {
return mLocalName;
}
public String getNamespaceURI() {
return mURI;
}
public QName getName() {
if (mPrefix == null) {
return new QName(mURI, mLocalName);
}
return new QName(mURI, mLocalName, mPrefix);
}
/*
///////////////////////////////////////////////////////////////////////
// Public API, ns binding, checking
///////////////////////////////////////////////////////////////////////
*/
public void checkAttrWrite(String nsURI, String localName)
throws XMLStreamException
{
AttrName an = new AttrName(nsURI, localName);
if (mAttrSet == null) {
/* 13-Dec-2005, TSa: Should use a more efficient Set/Map value
* for this in future -- specifically one that could use
* ns/local-name pairs without intermediate objects
*/
mAttrSet = new HashSet<AttrName>();
}
if (!mAttrSet.add(an)) {
throw new XMLStreamException("Duplicate attribute write for attribute '"+an+"'");
}
}
/*
///////////////////////////////////////////////////////////////////////
// Public API, mutators
///////////////////////////////////////////////////////////////////////
*/
public void setPrefix(String prefix) {
mPrefix = prefix;
}
@Override
public void setDefaultNsUri(String uri) {
mDefaultNsURI = uri;
}
/**
* Note: this method can and will only be called before outputting
* the root element.
*/
@Override
protected final void setRootNsContext(NamespaceContext ctxt)
{
mRootNsContext = ctxt;
// Let's also figure out the default ns binding, if any:
String defURI = ctxt.getNamespaceURI("");
if (defURI != null && defURI.length() > 0) {
mDefaultNsURI = defURI;
}
}
/*
///////////////////////////////////////////////////////////////////////
// Helper classes:
///////////////////////////////////////////////////////////////////////
*/
/**
* Simple key class used to represent two-piece (attribute) names;
* first part being optional (URI), and second non-optional (local name).
*/
final static class AttrName
implements Comparable<AttrName>
{
final String mNsURI;
final String mLocalName;
/**
* Let's cache the hash code, since although hash calculation is
* fast, hash code is needed a lot as this is always used as a
* HashSet/TreeMap key.
*/
final int mHashCode;
public AttrName(String nsURI, String localName) {
mNsURI = (nsURI == null) ? "" : nsURI;
mLocalName = localName;
mHashCode = mNsURI.hashCode() * 31 ^ mLocalName.hashCode();
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof AttrName)) {
return false;
}
AttrName other = (AttrName) o;
String otherLN = other.mLocalName;
// Local names are shorter, more varying:
if (otherLN != mLocalName && !otherLN.equals(mLocalName)) {
return false;
}
String otherURI = other.mNsURI;
return (otherURI == mNsURI || otherURI.equals(mNsURI));
}
@Override
public String toString() {
if (mNsURI.length() > 0) {
return "{"+mNsURI + "} " +mLocalName;
}
return mLocalName;
}
@Override
public int hashCode() {
return mHashCode;
}
@Override
public int compareTo(AttrName other) {
// Let's first order by namespace:
int result = mNsURI.compareTo(other.mNsURI);
if (result == 0) {
result = mLocalName.compareTo(other.mLocalName);
}
return result;
}
}
}