-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_07_JAXB.patch
More file actions
342 lines (341 loc) · 11.6 KB
/
Copy path2_07_JAXB.patch
File metadata and controls
342 lines (341 loc) · 11.6 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
Index: src/main/java/ru/javaops/masterjava/xml/util/JaxbMarshaller.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javaops/masterjava/xml/util/JaxbMarshaller.java (revision )
+++ src/main/java/ru/javaops/masterjava/xml/util/JaxbMarshaller.java (revision )
@@ -0,0 +1,39 @@
+package ru.javaops.masterjava.xml.util;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.PropertyException;
+import javax.xml.validation.Schema;
+import java.io.StringWriter;
+import java.io.Writer;
+
+public class JaxbMarshaller {
+ private Marshaller marshaller;
+
+ public JaxbMarshaller(JAXBContext ctx) throws JAXBException {
+ marshaller = ctx.createMarshaller();
+ marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+ marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
+ marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
+ }
+
+ public void setProperty(String prop, Object value) throws PropertyException {
+ marshaller.setProperty(prop, value);
+ }
+
+ public synchronized void setSchema(Schema schema) {
+ marshaller.setSchema(schema);
+ }
+
+ public String marshal(Object instance) throws JAXBException {
+ StringWriter sw = new StringWriter();
+ marshal(instance, sw);
+ return sw.toString();
+ }
+
+ public synchronized void marshal(Object instance, Writer writer) throws JAXBException {
+ marshaller.marshal(instance, writer);
+ }
+
+}
Index: src/main/java/ru/javaops/masterjava/xml/util/Schemas.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javaops/masterjava/xml/util/Schemas.java (revision )
+++ src/main/java/ru/javaops/masterjava/xml/util/Schemas.java (revision )
@@ -0,0 +1,48 @@
+package ru.javaops.masterjava.xml.util;
+
+import com.google.common.io.Resources;
+import org.xml.sax.SAXException;
+
+import javax.xml.XMLConstants;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+import java.io.File;
+import java.io.StringReader;
+import java.net.URL;
+
+
+public class Schemas {
+
+ // SchemaFactory is not thread-safe
+ private static final SchemaFactory SCHEMA_FACTORY = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+
+ public static synchronized Schema ofString(String xsd) {
+ try {
+ return SCHEMA_FACTORY.newSchema(new StreamSource(new StringReader(xsd)));
+ } catch (SAXException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ public static synchronized Schema ofClasspath(String resource) {
+ // http://digitalsanctum.com/2012/11/30/how-to-read-file-contents-in-java-the-easy-way-with-guava/
+ return ofURL(Resources.getResource(resource));
+ }
+
+ public static synchronized Schema ofURL(URL url) {
+ try {
+ return SCHEMA_FACTORY.newSchema(url);
+ } catch (SAXException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ public static synchronized Schema ofFile(File file) {
+ try {
+ return SCHEMA_FACTORY.newSchema(file);
+ } catch (SAXException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+}
Index: src/test/java/ru/javaops/masterjava/xml/util/JaxbParserTest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/test/java/ru/javaops/masterjava/xml/util/JaxbParserTest.java (revision )
+++ src/test/java/ru/javaops/masterjava/xml/util/JaxbParserTest.java (revision )
@@ -0,0 +1,40 @@
+package ru.javaops.masterjava.xml.util;
+
+import com.google.common.io.Resources;
+import org.junit.Test;
+import ru.javaops.masterjava.xml.schema.CityType;
+import ru.javaops.masterjava.xml.schema.ObjectFactory;
+import ru.javaops.masterjava.xml.schema.Payload;
+
+import javax.xml.bind.JAXBElement;
+import javax.xml.namespace.QName;
+
+public class JaxbParserTest {
+ private static final JaxbParser JAXB_PARSER = new JaxbParser(ObjectFactory.class);
+
+ static {
+ JAXB_PARSER.setSchema(Schemas.ofClasspath("payload.xsd"));
+ }
+
+ @Test
+ public void testPayload() throws Exception {
+// JaxbParserTest.class.getResourceAsStream("/city.xml")
+ Payload payload = JAXB_PARSER.unmarshal(
+ Resources.getResource("payload.xml").openStream());
+ String strPayload = JAXB_PARSER.marshal(payload);
+ JAXB_PARSER.validate(strPayload);
+ System.out.println(strPayload);
+ }
+
+ @Test
+ public void testCity() throws Exception {
+ JAXBElement<CityType> cityElement = JAXB_PARSER.unmarshal(
+ Resources.getResource("city.xml").openStream());
+ CityType city = cityElement.getValue();
+ JAXBElement<CityType> cityElement2 =
+ new JAXBElement<>(new QName("http://javaops.ru", "City"), CityType.class, city);
+ String strCity = JAXB_PARSER.marshal(cityElement2);
+ JAXB_PARSER.validate(strCity);
+ System.out.println(strCity);
+ }
+}
\ No newline at end of file
Index: src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java (revision )
+++ src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java (revision )
@@ -0,0 +1,88 @@
+package ru.javaops.masterjava.xml.util;
+
+import org.xml.sax.SAXException;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.PropertyException;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.Schema;
+import java.io.*;
+
+
+/**
+ * Marshalling/Unmarshalling JAXB helper
+ * XML Facade
+ */
+public class JaxbParser {
+
+ protected JaxbMarshaller jaxbMarshaller;
+ protected JaxbUnmarshaller jaxbUnmarshaller;
+ protected Schema schema;
+
+ public JaxbParser(Class... classesToBeBound) {
+ try {
+ init(JAXBContext.newInstance(classesToBeBound));
+ } catch (JAXBException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ // http://stackoverflow.com/questions/30643802/what-is-jaxbcontext-newinstancestring-contextpath
+ public JaxbParser(String context) {
+ try {
+ init(JAXBContext.newInstance(context));
+ } catch (JAXBException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ private void init(JAXBContext ctx) throws JAXBException {
+ jaxbMarshaller = new JaxbMarshaller(ctx);
+ jaxbUnmarshaller = new JaxbUnmarshaller(ctx);
+ }
+
+ // Unmarshaller
+ public <T> T unmarshal(InputStream is) throws JAXBException {
+ return (T) jaxbUnmarshaller.unmarshal(is);
+ }
+
+ public <T> T unmarshal(Reader reader) throws JAXBException {
+ return (T) jaxbUnmarshaller.unmarshal(reader);
+ }
+
+ public <T> T unmarshal(String str) throws JAXBException {
+ return (T) jaxbUnmarshaller.unmarshal(str);
+ }
+
+ // Marshaller
+ public void setMarshallerProperty(String prop, Object value) {
+ try {
+ jaxbMarshaller.setProperty(prop, value);
+ } catch (PropertyException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ public String marshal(Object instance) throws JAXBException {
+ return jaxbMarshaller.marshal(instance);
+ }
+
+ public void marshal(Object instance, Writer writer) throws JAXBException {
+ jaxbMarshaller.marshal(instance, writer);
+ }
+
+ public void setSchema(Schema schema) {
+ this.schema = schema;
+ jaxbUnmarshaller.setSchema(schema);
+ jaxbMarshaller.setSchema(schema);
+ }
+
+ public void validate(String str) throws IOException, SAXException {
+ validate(new StringReader(str));
+ }
+
+ public void validate(Reader reader) throws IOException, SAXException {
+ schema.newValidator().validate(new StreamSource(reader));
+ }
+}
Index: src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java (revision )
+++ src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java (revision )
@@ -0,0 +1,33 @@
+package ru.javaops.masterjava.xml.util;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.validation.Schema;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
+
+public class JaxbUnmarshaller {
+ private Unmarshaller unmarshaller;
+
+ public JaxbUnmarshaller(JAXBContext ctx) throws JAXBException {
+ unmarshaller = ctx.createUnmarshaller();
+ }
+
+ public synchronized void setSchema(Schema schema) {
+ unmarshaller.setSchema(schema);
+ }
+
+ public synchronized Object unmarshal(InputStream is) throws JAXBException {
+ return unmarshaller.unmarshal(is);
+ }
+
+ public synchronized Object unmarshal(Reader reader) throws JAXBException {
+ return unmarshaller.unmarshal(reader);
+ }
+
+ public Object unmarshal(String str) throws JAXBException {
+ return unmarshal(new StringReader(str));
+ }
+}
Index: src/test/resources/city.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/test/resources/city.xml (revision )
+++ src/test/resources/city.xml (revision )
@@ -0,0 +1,4 @@
+<City id="spb" xmlns="http://javaops.ru"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://javaops.ru payload.xsd">Санкт-Петербург
+</City>
\ No newline at end of file
Index: pom.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- pom.xml (date 1508793189000)
+++ pom.xml (revision )
@@ -32,6 +32,14 @@
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>2.20.1</version>
+ <configuration>
+ <argLine>-Dfile.encoding=UTF-8</argLine>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
@@ -79,6 +87,17 @@
<artifactId>jmh-generator-annprocess</artifactId>
<version>RELEASE</version>
<scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ <version>21.0</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.12</version>
+ <scope>test</scope>
</dependency>
</dependencies>