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
159 lines (144 loc) · 4.51 KB
/
XmlWriter.java
File metadata and controls
159 lines (144 loc) · 4.51 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
/*
* Copyright 2010-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.services.s3.internal;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
/**
* Basic XML Writer helper library. It does not do full XML Well-Formedness
* checks, but provides basic element and attribute value escaping, and tracks
* open tags to simplify use.
* <p>
* Sampe usage:
* <pre>
* <code>
* XmlWriter w = new XmlWriter();
* w.start(DOC_TAG, "xmlns", DOC_NAMESPACE);
* if (foo != null)
* w.start(FOO_TAG).value(foo).end();
* w.end();
* </code>
* </pre>
*/
public class XmlWriter {
List<String> tags = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
public XmlWriter start(String name) {
sb.append("<").append(name).append(">");
tags.add(name);
return this;
}
public XmlWriter start(String name, String attr, String value) {
sb.append("<").append(name);
writeAttr(attr, value);
sb.append(">");
tags.add(name);
return this;
}
public XmlWriter start(String name, String[] attrs, String[] values) {
sb.append("<").append(name);
for (int i = 0; i < Math.min(attrs.length, values.length); i++) {
writeAttr(attrs[i], values[i]);
}
sb.append(">");
tags.add(name);
return this;
}
public XmlWriter end() {
assert(tags.size() > 0);
String name = tags.remove(tags.size() - 1);
sb.append("</").append(name).append(">");
return this;
}
public byte[] getBytes() {
assert(tags.size() == 0);
try {
return this.toString().getBytes(Constants.DEFAULT_ENCODING);
} catch (UnsupportedEncodingException e) {
return this.toString().toString().getBytes();
}
}
public String toString() {
return sb.toString();
}
public XmlWriter value(String value) {
appendEscapedString(value, sb);
return this;
}
private void writeAttr(String name, String value) {
sb.append(' ').append(name).append("=\"");
appendEscapedString(value, sb);
sb.append("\"");
}
/**
* Appends the specified string (with any non-XML-compatible characters
* replaced with the corresponding escape code) to the specified
* StringBuilder.
*
* @param s
* The string to escape and append to the specified
* StringBuilder.
* @param builder
* The StringBuilder to which the escaped string should be
* appened.
*/
private void appendEscapedString(String s, StringBuilder builder) {
if (s == null)
s = "";
int pos;
int start = 0;
int len = s.length();
for (pos = 0; pos < len; pos++) {
char ch = s.charAt(pos);
String escape;
switch (ch) {
case '\t':
escape = "	";
break;
case '\n':
escape = " ";
break;
case '\r':
escape = " ";
break;
case '&':
escape = "&";
break;
case '"':
escape = """;
break;
case '<':
escape = "<";
break;
case '>':
escape = ">";
break;
default:
escape = null;
break;
}
// If we found an escape character, write all the characters up to that
// character, then write the escaped char and get back to scanning
if (escape != null) {
if (start < pos)
builder.append(s, start, pos);
sb.append(escape);
start = pos + 1;
}
}
// Write anything that's left
if (start < pos) sb.append(s, start, pos);
}
}