1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.xml;
17
18 import java.io.StringReader;
19 import java.util.Iterator;
20
21 import javax.xml.stream.XMLEventReader;
22 import javax.xml.stream.XMLInputFactory;
23 import javax.xml.stream.XMLStreamException;
24 import javax.xml.stream.XMLStreamWriter;
25 import javax.xml.stream.events.Attribute;
26 import javax.xml.stream.events.Characters;
27 import javax.xml.stream.events.StartElement;
28 import javax.xml.stream.events.XMLEvent;
29
30
31
32
33
34
35
36 public class CopyReader
37 {
38
39
40
41
42
43
44
45
46
47
48 private static final String ARTIFICIAL_GI = "root";
49
50
51
52
53
54
55 private static final String ARTIFICIAL_START_TAG = '<' + ARTIFICIAL_GI + '>';
56
57
58
59
60
61
62 private static final String ARTIFICIAL_END_TAG = "</" + ARTIFICIAL_GI + '>';
63
64
65
66
67
68
69 private final XMLStreamWriter xmlWriter;
70
71
72
73
74
75
76
77
78
79
80 public CopyReader(final XMLStreamWriter xmlWriter)
81 {
82 this.xmlWriter = xmlWriter;
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public void copy(final String xmlFragment) throws XMLStreamException
102 {
103 final XMLInputFactory factory = XMLInputFactory.newInstance();
104 final boolean isFragment = isFragment(xmlFragment);
105 final XMLEventReader eventReader;
106 if (isFragment)
107 {
108 eventReader =
109 factory.createXMLEventReader(new StringReader(ARTIFICIAL_START_TAG
110 + xmlFragment
111 + ARTIFICIAL_END_TAG));
112 boolean run = true;
113 while (run && eventReader.hasNext())
114 {
115 final XMLEvent event = eventReader.nextEvent();
116 if (event.getEventType() == XMLEvent.START_ELEMENT)
117 {
118 run = false;
119 }
120 }
121 }
122 else
123 {
124 eventReader = factory.createXMLEventReader(new StringReader(xmlFragment));
125 }
126 parse(eventReader);
127 }
128
129
130
131
132
133
134
135
136 private static boolean isFragment(final String xmlFragment)
137 {
138 return xmlFragment.charAt(0) != '<';
139 }
140
141
142
143
144
145
146
147 private void parse(final XMLEventReader eventReader)
148 throws XMLStreamException
149 {
150 int level = 0;
151 while (eventReader.hasNext())
152 {
153 final XMLEvent event = eventReader.nextEvent();
154 switch (event.getEventType())
155 {
156 case XMLEvent.START_ELEMENT:
157 level++;
158 handleStartElement(event);
159 break;
160 case XMLEvent.END_ELEMENT:
161 level--;
162 if (level >= 0)
163 {
164 xmlWriter.writeEndElement();
165 }
166 break;
167 case XMLEvent.CHARACTERS:
168 final Characters chars = event.asCharacters();
169 xmlWriter.writeCharacters(chars.getData());
170 default:
171
172 break;
173 }
174 }
175 }
176
177
178
179
180
181
182
183 @SuppressWarnings("unchecked")
184 private void handleStartElement(final XMLEvent event)
185 throws XMLStreamException
186 {
187 final StartElement element = event.asStartElement();
188 final String gi = element.getName().toString();
189 xmlWriter.writeStartElement(gi);
190 for (final Iterator<Attribute> i = element.getAttributes(); i.hasNext();)
191 {
192 final Attribute attribute = i.next();
193 xmlWriter.writeAttribute(attribute.getName().toString(),
194 attribute.getValue());
195 }
196 }
197
198
199
200 }