-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathXmlWriters.java
186 lines (151 loc) · 5.05 KB
/
XmlWriters.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
package me.hao0.common.xml;
import me.hao0.common.exception.XmlException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 简陋的XML构建器
* Author: haolin
* Email: haolin.h0@gmail.com
* Date: 6/11/15
* @since 1.0.6
*/
public class XmlWriters {
List<E> es = new ArrayList<>();
private XmlWriters(){}
public static XmlWriters create(){
return new XmlWriters();
}
public XmlWriters element(String name, String text){
E e = new TextE(name, text);
es.add(e);
return this;
}
public XmlWriters element(String name, Number number){
E e = new NumberE(name, number);
es.add(e);
return this;
}
public XmlWriters element(String parentName, String childName, String childText){
return element(parentName, new TextE(childName, childText));
}
public XmlWriters element(String parentName, String childName, Number childNumber){
return element(parentName, new NumberE(childName, childNumber));
}
/**
* 构建包含多个子元素的元素
* @param parentName 父元素标签名
* @param childPairs childName1, childValue1, childName2, childValu2, ...,长度必读为2的倍数
* @return this
*/
public XmlWriters element(String parentName, Object... childPairs){
if (childPairs.length % 2 != 0){
throw new XmlException("var args's length must % 2 = 0");
}
E parent = new TextE(parentName, null);
List<E> children = new ArrayList<>();
E child;
for (int i=0; i<childPairs.length ; i=i+2){
if (childPairs[i+1] instanceof Number){
child = new NumberE((String)childPairs[i], (Serializable)childPairs[i+1]);
} else {
child = new TextE((String)childPairs[i], (Serializable)childPairs[i+1]);
}
children.add(child);
}
parent.children = children;
es.add(parent);
return this;
}
public XmlWriters element(String parentName, E child){
E e = new TextE(parentName, null);
e.children = Arrays.asList(child);
es.add(e);
return this;
}
public XmlWriters element(String parentName, List<E> children){
E e = new TextE(parentName, null);
e.children = children;
es.add(e);
return this;
}
/**
* 构建包含多个子元素的元素
* @param parentName 父元素标签名
* @param childPairs childName1, childValue1, childName2, childValu2, ...,长度必读为2的倍数
* @return an element
*/
public E newElement(String parentName, Object... childPairs){
E parent = new TextE(parentName, null);
List<E> children = new ArrayList<>();
E child;
for (int i=0; i<childPairs.length ; i=i+2){
if (childPairs[i+1] instanceof Number){
child = new NumberE((String)childPairs[i], (Serializable)childPairs[i+1]);
} else {
child = new TextE((String)childPairs[i], (Serializable)childPairs[i+1]);
}
children.add(child);
}
parent.children = children;
return parent;
}
public String build(){
return buildElements();
}
private String buildElements() {
StringBuilder xml = new StringBuilder();
xml.append("<xml>");
if (es != null && es.size() > 0){
for (E e : es){
xml.append(e.render());
}
}
xml.append("</xml>");
return xml.toString();
}
public static abstract class E {
String name;
Object text;
List<E> children;
public E(String name, Object text) {
this.name = name;
this.text = text;
}
protected abstract String render();
}
public static final class TextE extends E {
public TextE(String name, Serializable content) {
super(name, content);
}
@Override
protected String render() {
StringBuilder content = new StringBuilder();
content.append("<").append(name).append(">");
if (text != null){
content.append("<![CDATA[").append(text).append("]]>");
}
if (children != null && children.size() > 0){
for (E child : children){
content.append(child.render());
}
}
content.append("</").append(name).append(">");
return content.toString();
}
}
public static final class NumberE extends E {
public NumberE(String name, Serializable content) {
super(name, content);
}
@Override
protected String render() {
StringBuilder content = new StringBuilder();
content.append("<").append(name).append(">")
.append(text)
.append("</").append(name).append(">");
return content.toString();
}
}
}