forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLWriter.java
More file actions
179 lines (161 loc) · 5.34 KB
/
XMLWriter.java
File metadata and controls
179 lines (161 loc) · 5.34 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
/*
* Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.amazonaws.util;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.Stack;
import com.amazonaws.AmazonClientException;
/**
* Utility for creating easily creating XML documents, one element at a time.
*/
public class XMLWriter {
/** Standard XML prolog to add to the beginning of each XML document. */
private static final String PROLOG = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
/** The writer to which the XML document created by this writer will be written. */
private final Writer writer;
/** Optional XML namespace attribute value to include in the root element. */
private final String xmlns;
private Stack<String> elementStack = new Stack<String>();
private boolean rootElement = true;
/**
* Creates a new XMLWriter, ready to write an XML document to the specified
* writer. The XML document will not specify an xmlns attribute.
*
* @param w
* The writer this XMLWriter will write to.
*/
public XMLWriter(Writer w) {
this(w, null);
}
/**
* Creates a new XMLWriter, ready to write an XML document to the specified
* writer. The root element in the XML document will specify an xmlns
* attribute with the specified namespace parameter.
*
* @param w
* The writer this XMLWriter will write to.
* @param xmlns
* The XML namespace to include in the xmlns attribute of the
* root element.
*/
public XMLWriter(Writer w, String xmlns) {
this.writer = w;
this.xmlns = xmlns;
append(PROLOG);
}
/**
* Starts a new element with the specified name at the current position in
* the in-progress XML document.
*
* @param element
* The name of the new element.
*
* @return This XMLWriter so that additional method calls can be chained
* together.
*/
public XMLWriter startElement(String element) {
append("<" + element);
if (rootElement && xmlns != null) {
append(" xmlns=\"" + xmlns + "\"");
rootElement = false;
}
append(">");
elementStack.push(element);
return this;
}
/**
* Closes the last opened element at the current position in the in-progress
* XML document.
*
* @return This XMLWriter so that additional method calls can be chained
* together.
*/
public XMLWriter endElement() {
String lastElement = elementStack.pop();
append("</" + lastElement + ">");
return this;
}
/**
* Adds the specified value as text to the current position of the in
* progress XML document.
*
* @param s
* The text to add to the XML document.
*
* @return This XMLWriter so that additional method calls can be chained
* together.
*/
public XMLWriter value(String s) {
append(escapeXMLEntities(s));
return this;
}
/**
* Adds the specified date as text to the current position of the
* in-progress XML document.
*
* @param date
* The date to add to the XML document.
*
* @return This XMLWriter so that additional method calls can be chained
* together.
*/
public XMLWriter value(Date date) {
append(escapeXMLEntities(StringUtils.fromDate(date)));
return this;
}
/**
* Adds the string representation of the specified object to the current
* position of the in progress XML document.
*
* @param obj
* The object to translate to a string and add to the XML
* document.
*
* @return This XMLWriter so that additional method calls can be chained
* together.
*/
public XMLWriter value(Object obj) {
append(escapeXMLEntities(obj.toString()));
return this;
}
private void append(String s) {
try {
writer.append(s);
} catch (IOException e) {
throw new AmazonClientException("Unable to write XML document", e);
}
}
private String escapeXMLEntities(String s) {
/**
* Unescape any escaped characters.
*/
if (s.contains("&")) {
s = s.replace(""", "\"");
s = s.replace("'", "'");
s = s.replace("<", "<");
s = s.replace(">", ">");
// Ampersands should always be the last to unescape
s = s.replace("&", "&");
}
// Ampersands should always be the first to escape
s = s.replace("&", "&");
s = s.replace("\"", """);
s = s.replace("'", "'");
s = s.replace("<", "<");
s = s.replace(">", ">");
return s;
}
}