-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathOutputDocument.java
More file actions
371 lines (336 loc) · 11.9 KB
/
OutputDocument.java
File metadata and controls
371 lines (336 loc) · 11.9 KB
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
/*
* OutputDocument.java July 2006
*
* Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.simpleframework.xml.stream;
/**
* The <code>OutputDocument</code> object is used to represent the
* root of an XML document. This does not actually represent anything
* that will be written to the generated document. It is used as a
* way to create the root document element. Once the root element has
* been created it can be committed by using this object.
*
* @author Niall Gallagher
*/
class OutputDocument implements OutputNode {
/**
* Represents a dummy output node map for the attributes.
*/
private OutputNodeMap table;
/**
* Represents the writer that is used to create the element.
*/
private NodeWriter writer;
/**
* This is the output stack used by the node writer object.
*/
private OutputStack stack;
/**
* This represents the namespace reference used by this.
*/
private String reference;
/**
* This is the comment that is to be written for the node.
*/
private String comment;
/**
* Represents the value that has been set on this document.
*/
private String value;
/**
* This is the name of this output document node instance.
*/
private String name;
/**
* This is the output mode of this output document object.
*/
private Mode mode;
/**
* Constructor for the <code>OutputDocument</code> object. This
* is used to create an empty output node object that can be
* used to create a root element for the generated document.
*
* @param writer this is the node writer to write the node to
* @param stack this is the stack that contains the open nodes
*/
public OutputDocument(NodeWriter writer, OutputStack stack) {
this.table = new OutputNodeMap(this);
this.mode = Mode.INHERIT;
this.writer = writer;
this.stack = stack;
}
/**
* The default for the <code>OutputDocument</code> is null as it
* does not require a namespace. A null prefix is always used by
* the document as it represents a virtual node that does not
* exist and will not form any part of the resulting XML.
*
* @return this returns a null prefix for the output document
*/
public String getPrefix() {
return null;
}
/**
* The default for the <code>OutputDocument</code> is null as it
* does not require a namespace. A null prefix is always used by
* the document as it represents a virtual node that does not
* exist and will not form any part of the resulting XML.
*
* @param inherit if there is no explicit prefix then inherit
*
* @return this returns a null prefix for the output document
*/
public String getPrefix(boolean inherit) {
return null;
}
/**
* This is used to acquire the reference that has been set on
* this output node. Typically this should be null as this node
* does not represent anything that actually exists. However
* if a namespace reference is set it can be acquired.
*
* @return this returns the namespace reference for this node
*/
public String getReference() {
return reference;
}
/**
* This is used to set the namespace reference for the document.
* Setting a reference for the document node has no real effect
* as the document node is virtual and is not written to the
* resulting XML document that is generated.
*
* @param reference this is the namespace reference added
*/
public void setReference(String reference) {
this.reference = reference;
}
/**
* This returns the <code>NamespaceMap</code> for the document.
* The namespace map for the document must be null as this will
* signify the end of the resolution process for a prefix if
* given a namespace reference.
*
* @return this will return a null namespace map object
*/
public NamespaceMap getNamespaces() {
return null;
}
/**
* This is used to acquire the <code>Node</code> that is the
* parent of this node. This will return the node that is
* the direct parent of this node and allows for siblings to
* make use of nodes with their parents if required.
*
* @return this will always return null for this output
*/
public OutputNode getParent() {
return null;
}
/**
* To signify that this is the document element this method will
* return null. Any object with a handle on an output node that
* has been created can check the name to determine its type.
*
* @return this returns null for the name of the node
*/
public String getName() {
return null;
}
/**
* This returns the value that has been set for this document.
* The value returned is essentially a dummy value as this node
* is never written to the resulting XML document.
*
* @return the value that has been set with this document
*/
public String getValue() throws Exception {
return value;
}
/**
* This is used to get the text comment for the element. This can
* be null if no comment has been set. If no comment is set on
* the node then no comment will be written to the resulting XML.
*
* @return this is the comment associated with this element
*/
public String getComment() {
return comment;
}
/**
* This method is used to determine if this node is the root
* node for the XML document. The root node is the first node
* in the document and has no sibling nodes. This will return
* true although the document node is not strictly the root.
*
* @return returns true although this is not really a root
*/
public boolean isRoot() {
return true;
}
/**
* The <code>Mode</code> is used to indicate the output mode
* of this node. Three modes are possible, each determines
* how a value, if specified, is written to the resulting XML
* document. This is determined by the <code>setData</code>
* method which will set the output to be CDATA or escaped,
* if neither is specified the mode is inherited.
*
* @return this returns the mode of this output node object
*/
public Mode getMode() {
return mode;
}
/**
* This is used to set the output mode of this node to either
* be CDATA, escaped, or inherited. If the mode is set to data
* then any value specified will be written in a CDATA block,
* if this is set to escaped values are escaped. If however
* this method is set to inherited then the mode is inherited
* from the parent node.
*
* @param mode this is the output mode to set the node to
*/
public void setMode(Mode mode) {
this.mode = mode;
}
/**
* This method is used for convenience to add an attribute node
* to the attribute <code>NodeMap</code>. The attribute added
* can be removed from the element by using the node map.
*
* @param name this is the name of the attribute to be added
* @param value this is the value of the node to be added
*
* @return this returns the node that has just been added
*/
public OutputNode setAttribute(String name, String value) {
return table.put(name, value);
}
/**
* This returns a <code>NodeMap</code> which can be used to add
* nodes to this node. The node map returned by this is a dummy
* map, as this output node is never written to the XML document.
*
* @return returns the node map used to manipulate attributes
*/
public NodeMap<OutputNode> getAttributes() {
return table;
}
/**
* This is used to change the name of an output node. This will
* only affect the name of the node if the node has not yet been
* committed. If the node is committed then this will not be
* reflected in the resulting XML generated.
*
* @param name this is the name to change the node to
*/
public void setName(String name) {
this.name = name;
}
/**
* This is used to set a text value to the element. This effect
* of adding this to the document node will not change what
* is actually written to the generated XML document.
*
* @param value this is the text value to add to this element
*/
public void setValue(String value) {
this.value = value;
}
/**
* This is used to set a text comment to the element. This will
* be written just before the actual element is written. Only a
* single comment can be set for each output node written.
*
* @param comment this is the comment to set on the node
*/
public void setComment(String comment) {
this.comment = comment;
}
/**
* This is used to set the output mode of this node to either
* be CDATA or escaped. If this is set to true the any value
* specified will be written in a CDATA block, if this is set
* to false the values is escaped. If however this method is
* never invoked then the mode is inherited from the parent.
*
* @param data if true the value is written as a CDATA block
*/
public void setData(boolean data) {
if(data) {
mode = Mode.DATA;
} else {
mode = Mode.ESCAPE;
}
}
/**
* This is used to create a child element within the element that
* this object represents. When a new child is created with this
* method then the previous child is committed to the document.
* The created <code>OutputNode</code> object can be used to add
* attributes to the child element as well as other elements.
*
* @param name this is the name of the child element to create
*/
public OutputNode getChild(String name) throws Exception {
return writer.writeElement(this, name);
}
/**
* This is used to remove any uncommitted changes. Removal of an
* output node can only be done if it has no siblings and has
* not yet been committed. If the node is committed then this
* will throw an exception to indicate that it cannot be removed.
*
* @throws Exception thrown if the node cannot be removed
*/
public void remove() throws Exception {
if(stack.isEmpty()) {
throw new NodeException("No root node");
}
stack.bottom().remove();
}
/**
* This will commit this element and any uncommitted elements
* elements that are decendents of this node. For instance if
* any child or grand child remains open under this element
* then those elements will be closed before this is closed.
*
* @throws Exception this is thrown if there is an I/O error
* or if a root element has not yet been created
*/
public void commit() throws Exception {
if(stack.isEmpty()) {
throw new NodeException("No root node");
}
stack.bottom().commit();
}
/**
* This is used to determine whether this node has been committed.
* This will return true if no root element has been created or
* if the root element for the document has already been commited.
*
* @return true if the node is committed or has not been created
*/
public boolean isCommitted() {
return stack.isEmpty();
}
public IndentationMode getIndentationMode() {
return null;
}
public void setIndentationMode(IndentationMode mode) {
}
}