/
XmlElementSplitter.java
199 lines (178 loc) · 6.36 KB
/
XmlElementSplitter.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
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Copyright 2013, 2014 Pascal Christoph (hbz)
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 org.metafacture.xml;
import org.metafacture.commons.XmlUtil;
import org.metafacture.framework.FluxCommand;
import org.metafacture.framework.StreamReceiver;
import org.metafacture.framework.XmlReceiver;
import org.metafacture.framework.annotations.Description;
import org.metafacture.framework.annotations.In;
import org.metafacture.framework.annotations.Out;
import org.metafacture.framework.helpers.DefaultXmlPipe;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import java.util.HashSet;
import java.util.Set;
/**
* An XML Element splitter.
*
* @author Pascal Christoph (dr0i)
*/
@Description("Splits elements (e.g. defining single records) residing in one XML document into multiple single XML documents.")
@In(XmlReceiver.class)
@Out(StreamReceiver.class)
@FluxCommand("split-xml-elements")
public final class XmlElementSplitter extends DefaultXmlPipe<StreamReceiver> {
private String element;
private StringBuilder builder = new StringBuilder();
private Set<String> namespaces = new HashSet<>();
private boolean inElement;
private int recordCnt;
private String root;
private String rootStart = "";
private String rootEnd = "";
private String xmlDeclaration = "<?xml version = \"1.0\" encoding = \"UTF-8\"?>";
private int elementDepth;
/**
* Default constructor
*/
public XmlElementSplitter() {
}
/**
* Enriched constructor setting the top level element and the Element name
*
* @param topLevelElement the name of the top level XML tag
* @param elementName the name of the tag defining a new Element to be split
*/
public XmlElementSplitter(final String topLevelElement, final String elementName) {
setTopLevelElement(topLevelElement);
setElementName(elementName);
}
/**
* Sets the name of the Element. All these elements in the XML stream will
* be XML documents on their own.
*
* @param name Identifies the elements
*/
public void setElementName(final String name) {
this.element = name;
}
/**
* Sets the top-level XML document element.
*
* @param newRoot the top level element. Leave at default to omit the
* top-level element.
*/
public void setTopLevelElement(final String newRoot) {
root = newRoot;
rootStart = "<" + newRoot;
rootEnd = "</" + newRoot + ">";
}
/**
* Sets the XML declaration. The default is
* {@code <?xml version = "1.0" encoding = "UTF-8"?>}. If an empty value is
* given, the XML declaration is skipped.
*
* @param xmlDeclaration the XML declaration
*/
public void setXmlDeclaration(final String xmlDeclaration) {
this.xmlDeclaration = xmlDeclaration;
}
@Override
public void startPrefixMapping(final String prefix, final String uri) throws SAXException {
super.startPrefixMapping(prefix, uri);
if (root != null & !prefix.isEmpty() && uri != null) {
namespaces.add(" xmlns:" + prefix + "=\"" + uri + "\"");
}
}
@Override
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
if (!inElement) {
if (element.equals(localName)) {
builder = new StringBuilder();
getReceiver().startRecord(String.valueOf(recordCnt));
++recordCnt;
inElement = true;
appendValuesToElement(qName, attributes);
++elementDepth;
}
}
else {
if (element.equals(localName)) {
++elementDepth;
}
appendValuesToElement(qName, attributes);
}
}
private void appendValuesToElement(final String qName, final Attributes attributes) {
this.builder.append("<" + qName);
if (attributes.getLength() > 0) {
for (int i = 0; i < attributes.getLength(); ++i) {
builder.append(" " + attributes.getQName(i) + "=\"" + XmlUtil.escape(attributes.getValue(i)) + "\"");
}
}
builder.append(">");
}
@Override
public void endElement(final String uri, final String localName, final String qName) throws SAXException {
if (inElement) {
builder.append("</" + qName + ">");
if (element.equals(localName)) {
if (elementDepth <= 1) {
final StringBuilder sb = new StringBuilder(xmlDeclaration + rootStart);
if (this.root != null && namespaces.size() > 0) {
for (final String ns : namespaces) {
sb.append(ns);
}
sb.append(">");
}
builder.insert(0, sb.toString()).append(rootEnd);
getReceiver().literal("Element", builder.toString());
getReceiver().endRecord();
reset();
return;
}
--elementDepth;
}
}
}
@Override
public void characters(final char[] chars, final int start, final int length) throws SAXException {
try {
builder.append(XmlUtil.escape(new String(chars, start, length)));
}
catch (final Exception e) { // checkstyle-disable-line IllegalCatch
reset();
}
}
private void reset() {
inElement = false;
builder = new StringBuilder();
elementDepth = 0;
}
/**
* Get the XML declaration.
*
* @return the XML decalration
*/
public String getXmlDeclaration() {
return xmlDeclaration;
}
@Override
protected void onResetStream() {
reset();
}
}