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