Skip to content

Commit 41ce64e

Browse files
committed
Tidied up SimpleXmlWriter to prepare for refactoring
1 parent 205a527 commit 41ce64e

File tree

1 file changed

+76
-58
lines changed

1 file changed

+76
-58
lines changed

src/main/java/org/culturegraph/mf/stream/sink/SimpleXmlWriter.java

Lines changed: 76 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -33,67 +33,71 @@
3333
import org.culturegraph.mf.util.ResourceUtil;
3434

3535
/**
36-
*
36+
*
3737
* writes a stream to XML
38-
*
38+
*
3939
* @author Markus Michael Geipel
40-
*
40+
*
4141
*/
4242
@Description("writes a stream to xml")
4343
@In(StreamReceiver.class)
4444
@Out(String.class)
4545
public final class SimpleXmlWriter extends DefaultStreamPipe<ObjectReceiver<String>> {
46+
4647
public static final String ATTRIBUTE_MARKER = "~";
47-
// public static final String TEXT_CONTENT_MARKER = "_text";
4848
public static final String NAMESPACES = "namespaces";
49-
public static final String NEW_LINE = "\n";
5049

51-
private Element element;
50+
public static final String DEFAULT_ROOT_TAG = "records";
51+
public static final String DEFAULT_RECORD_TAG = "record";
52+
53+
private static final String NEW_LINE = "\n";
54+
private static final String INDENT = "\t";
55+
56+
private static final String BEGIN_ATTRIBUTE = "=\"";
57+
private static final String END_ATTRIBUTE = "\"";
58+
private static final String BEGIN_OPEN_ELEMENT = "<";
59+
private static final String END_OPEN_ELEMENT = ">";
60+
private static final String END_EMPTY_ELEMENT = " />";
61+
private static final String BEGIN_CLOSE_ELEMENT = "</";
62+
private static final String END_CLOSE_ELEMENT = ">";
63+
64+
private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
65+
private static final String XMLNS_MARKER = " xmlns:";
66+
67+
private String rootTag = DEFAULT_ROOT_TAG;
68+
private String recordTag = DEFAULT_RECORD_TAG;
5269
private Map<String, String> namespaces = new HashMap<String, String>();
53-
private String recordTag = "record";
54-
private String rootTag = "records";
55-
private boolean start = true;
56-
private boolean separateRoots;
5770
private boolean writeXmlHeader = true;
71+
private boolean separateRoots;
72+
73+
private Element element;
74+
private boolean start = true;
5875

5976
public void setRootTag(final String rootTag) {
6077
this.rootTag = rootTag;
6178
}
6279

63-
public void setWriteXmlHeader(final boolean writeXmlHeader) {
64-
this.writeXmlHeader = writeXmlHeader;
65-
}
66-
67-
public void setSeparateRoots(final boolean separateRoots) {
68-
this.separateRoots = separateRoots;
80+
public void setRecordTag(final String tag) {
81+
recordTag = tag;
6982
}
7083

7184
public void setNamespaceFile(final String file) {
7285
final Properties properties = ResourceUtil.loadProperties(file);
73-
for (Entry<Object, Object> entry : properties.entrySet()) {
86+
for (final Entry<Object, Object> entry : properties.entrySet()) {
7487
namespaces.put(entry.getKey().toString(), entry.getValue().toString());
7588
}
7689
}
7790

78-
private void writeHeader() {
79-
final StringBuilder builder = new StringBuilder();
91+
public void setWriteXmlHeader(final boolean writeXmlHeader) {
92+
this.writeXmlHeader = writeXmlHeader;
93+
}
8094

81-
if (writeXmlHeader) {
82-
builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
83-
}
95+
public void setSeparateRoots(final boolean separateRoots) {
96+
this.separateRoots = separateRoots;
97+
}
8498

85-
builder.append("<");
86-
builder.append(rootTag);
87-
for (Entry<String, String> entry : namespaces.entrySet()) {
88-
builder.append(" xmlns:");
89-
builder.append(entry.getKey());
90-
builder.append("=\"");
91-
escape(builder, entry.getValue());
92-
builder.append("\"");
93-
}
94-
builder.append(">");
95-
getReceiver().process(builder.toString());
96-
start = false;
99+
public void configure(final MultiMap multimap) {
100+
this.namespaces = multimap.getMap(NAMESPACES);
97101
}
98102

99103
@Override
@@ -108,7 +112,7 @@ public void startRecord(final String identifier) {
108112
public void endRecord() {
109113
if (recordTag.isEmpty()) {
110114
final StringBuilder builder = new StringBuilder();
111-
for (Element child : element.getChildren()) {
115+
for (final Element child : element.getChildren()) {
112116
child.writeToStringBuilder(builder, 1);
113117
}
114118
getReceiver().process(builder.toString());
@@ -142,12 +146,10 @@ public void literal(final String name, final String value) {
142146
}
143147
}
144148

145-
public void configure(final MultiMap multimap) {
146-
this.namespaces = multimap.getMap(NAMESPACES);
147-
}
148-
149-
public void setRecordTag(final String tag) {
150-
recordTag = tag;
149+
@Override
150+
protected void onResetStream() {
151+
writeFooter();
152+
start = true;
151153
}
152154

153155
@Override
@@ -157,8 +159,30 @@ protected void onCloseStream() {
157159
}
158160
}
159161

162+
private void writeHeader() {
163+
164+
final StringBuilder builder = new StringBuilder();
165+
166+
if (writeXmlHeader) {
167+
builder.append(XML_HEADER);
168+
}
169+
170+
builder.append(BEGIN_OPEN_ELEMENT);
171+
builder.append(rootTag);
172+
for (final Entry<String, String> entry : namespaces.entrySet()) {
173+
builder.append(XMLNS_MARKER);
174+
builder.append(entry.getKey());
175+
builder.append(BEGIN_ATTRIBUTE);
176+
escape(builder, entry.getValue());
177+
builder.append(END_ATTRIBUTE);
178+
}
179+
builder.append(END_OPEN_ELEMENT);
180+
getReceiver().process(builder.toString());
181+
start = false;
182+
}
183+
160184
private void writeFooter() {
161-
getReceiver().process("</" + rootTag + ">");
185+
getReceiver().process(BEGIN_CLOSE_ELEMENT + rootTag + END_CLOSE_ELEMENT);
162186
}
163187

164188
/**
@@ -190,9 +214,9 @@ public List<Element> getChildren() {
190214
public void addAttribute(final String name, final String value) {
191215
attributes.append(" ");
192216
attributes.append(name);
193-
attributes.append("=\"");
217+
attributes.append(BEGIN_ATTRIBUTE);
194218
escape(attributes, value);
195-
attributes.append("\"");
219+
attributes.append(END_ATTRIBUTE);
196220
}
197221

198222
public void setText(final String text) {
@@ -222,18 +246,18 @@ public String toString() {
222246
public void writeToStringBuilder(final StringBuilder builder, final int indent) {
223247
builder.append(NEW_LINE);
224248
indent(builder, indent);
225-
builder.append("<");
249+
builder.append(BEGIN_OPEN_ELEMENT);
226250
builder.append(name);
227251
builder.append(attributes);
228252
if (text.isEmpty() && children.isEmpty()) {
229-
builder.append(" /");
253+
builder.append(END_EMPTY_ELEMENT);
254+
} else {
255+
builder.append(END_OPEN_ELEMENT);
230256
}
231257

232-
builder.append(">");
233-
234258
escape(builder, text);
235259

236-
for (Element element : children) {
260+
for (final Element element : children) {
237261
element.writeToStringBuilder(builder, indent + 1);
238262
}
239263
if (text.isEmpty() && !children.isEmpty()) {
@@ -242,25 +266,19 @@ public void writeToStringBuilder(final StringBuilder builder, final int indent)
242266
}
243267

244268
if (!text.isEmpty() || !children.isEmpty()) {
245-
builder.append("</");
269+
builder.append(BEGIN_CLOSE_ELEMENT);
246270
builder.append(name);
247-
builder.append(">");
271+
builder.append(END_CLOSE_ELEMENT);
248272
}
249273
}
250274

251275
private static void indent(final StringBuilder builder, final int indent) {
252276
for (int i = 0; i < indent; ++i) {
253-
builder.append("\t");
277+
builder.append(INDENT);
254278
}
255279
}
256280
}
257281

258-
@Override
259-
protected void onResetStream() {
260-
writeFooter();
261-
start = true;
262-
}
263-
264282
protected static void escape(final StringBuilder builder, final String str) {
265283

266284
final int len = str.length();

0 commit comments

Comments
 (0)