/
XmlAppendable.java
168 lines (143 loc) · 5.08 KB
/
XmlAppendable.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
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.xml;
import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Map;
import java.util.Stack;
import org.eclipse.jetty.util.StringUtil;
public class XmlAppendable
{
private static final String SPACES = " ";
private final Appendable _out;
private final int _indent;
private final Stack<String> _tags = new Stack<>();
private String _space = "";
public XmlAppendable(OutputStream out, String encoding) throws IOException
{
this(new OutputStreamWriter(out, encoding), encoding);
}
public XmlAppendable(Appendable out) throws IOException
{
this(out, 2);
}
public XmlAppendable(Appendable out, String encoding) throws IOException
{
this(out, 2, encoding);
}
public XmlAppendable(Appendable out, int indent) throws IOException
{
this(out, indent, "utf-8");
}
public XmlAppendable(Appendable out, int indent, String encoding) throws IOException
{
_out = out;
_indent = indent;
_out.append("<?xml version=\"1.0\" encoding=\"").append(encoding).append("\"?>\n");
}
public XmlAppendable openTag(String tag, Map<String, String> attributes) throws IOException
{
_out.append(_space).append('<').append(tag);
attributes(attributes);
_out.append(">\n");
_space = _space + SPACES.substring(0, _indent);
_tags.push(tag);
return this;
}
public XmlAppendable openTag(String tag) throws IOException
{
_out.append(_space).append('<').append(tag).append(">\n");
_space = _space + SPACES.substring(0, _indent);
_tags.push(tag);
return this;
}
public XmlAppendable content(String s) throws IOException
{
if (s != null)
_out.append(StringUtil.sanitizeXmlString(s));
return this;
}
public XmlAppendable cdata(String s) throws IOException
{
_out.append("<![CDATA[").append(s).append("]]>");
return this;
}
public XmlAppendable tag(String tag) throws IOException
{
_out.append(_space).append('<').append(tag).append("/>\n");
return this;
}
public XmlAppendable tag(String tag, Map<String, String> attributes) throws IOException
{
_out.append(_space).append('<').append(tag);
attributes(attributes);
_out.append("/>\n");
return this;
}
public XmlAppendable tag(String tag, String content) throws IOException
{
_out.append(_space).append('<').append(tag).append('>');
content(content);
_out.append("</").append(tag).append(">\n");
return this;
}
// @checkstyle-disable-check : AbbreviationAsWordInNameCheck
public XmlAppendable tagCDATA(String tag, String data) throws IOException
{
_out.append(_space).append('<').append(tag).append('>');
cdata(data);
_out.append("</").append(tag).append(">\n");
return this;
}
// @checkstyle-enable-check : AbbreviationAsWordInNameCheck
public XmlAppendable tag(String tag, Map<String, String> attributes, String content) throws IOException
{
_out.append(_space).append('<').append(tag);
attributes(attributes);
_out.append('>');
content(content);
_out.append("</").append(tag).append(">\n");
return this;
}
public XmlAppendable closeTag() throws IOException
{
if (_tags.isEmpty())
throw new IllegalStateException("Tags closed");
String tag = _tags.pop();
_space = _space.substring(0, _space.length() - _indent);
_out.append(_space).append("</").append(tag).append(">\n");
if (_tags.isEmpty() && _out instanceof Closeable)
((Closeable)_out).close();
return this;
}
private void attributes(Map<String, String> attributes) throws IOException
{
for (Map.Entry<String, String> entry : attributes.entrySet())
{
_out.append(' ').append(entry.getKey()).append("=\"");
content(entry.getValue());
_out.append('"');
}
}
public void literal(String xml) throws IOException
{
_out.append(xml);
}
}