Skip to content

Commit d1bfbc3

Browse files
committed
2 07 JAXB
1 parent 182830f commit d1bfbc3

8 files changed

Lines changed: 287 additions & 0 deletions

File tree

pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
3030
<target>${java.version}</target>
3131
</configuration>
3232
</plugin>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-surefire-plugin</artifactId>
36+
<version>2.19.1</version>
37+
<configuration>
38+
<argLine>-Dfile.encoding=UTF-8</argLine>
39+
</configuration>
40+
</plugin>
3341
<plugin>
3442
<groupId>org.apache.maven.plugins</groupId>
3543
<artifactId>maven-shade-plugin</artifactId>
@@ -80,6 +88,17 @@
8088
<version>RELEASE</version>
8189
<scope>provided</scope>
8290
</dependency>
91+
<dependency>
92+
<groupId>com.google.guava</groupId>
93+
<artifactId>guava</artifactId>
94+
<version>21.0</version>
95+
</dependency>
96+
<dependency>
97+
<groupId>junit</groupId>
98+
<artifactId>junit</artifactId>
99+
<version>4.12</version>
100+
<scope>test</scope>
101+
</dependency>
83102
</dependencies>
84103

85104
<profiles>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ru.javaops.masterjava.xml.util;
2+
3+
import javax.xml.bind.JAXBContext;
4+
import javax.xml.bind.JAXBException;
5+
import javax.xml.bind.Marshaller;
6+
import javax.xml.bind.PropertyException;
7+
import javax.xml.validation.Schema;
8+
import java.io.StringWriter;
9+
import java.io.Writer;
10+
11+
/**
12+
* @author GKislin
13+
* Date: 18.11.2008
14+
*/
15+
public class JaxbMarshaller {
16+
private Marshaller marshaller;
17+
18+
public JaxbMarshaller(JAXBContext ctx) throws JAXBException {
19+
marshaller = ctx.createMarshaller();
20+
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
21+
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
22+
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
23+
}
24+
25+
public void setProperty(String prop, Object value) throws PropertyException {
26+
marshaller.setProperty(prop, value);
27+
}
28+
29+
public synchronized void setSchema(Schema schema) {
30+
marshaller.setSchema(schema);
31+
}
32+
33+
public String marshal(Object instance) throws JAXBException {
34+
StringWriter sw = new StringWriter();
35+
marshal(instance, sw);
36+
return sw.toString();
37+
}
38+
39+
public synchronized void marshal(Object instance, Writer writer) throws JAXBException {
40+
marshaller.marshal(instance, writer);
41+
}
42+
43+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package ru.javaops.masterjava.xml.util;
2+
3+
import org.xml.sax.SAXException;
4+
5+
import javax.xml.bind.JAXBContext;
6+
import javax.xml.bind.JAXBException;
7+
import javax.xml.bind.PropertyException;
8+
import javax.xml.transform.stream.StreamSource;
9+
import javax.xml.validation.Schema;
10+
import java.io.*;
11+
12+
13+
/**
14+
* Marshalling/Unmarshalling JAXB helper
15+
* XML Facade
16+
* @author Grigory Kislin
17+
*/
18+
public class JaxbParser {
19+
20+
protected JaxbMarshaller jaxbMarshaller;
21+
protected JaxbUnmarshaller jaxbUnmarshaller;
22+
protected Schema schema;
23+
24+
public JaxbParser(Class... classesToBeBound) {
25+
try {
26+
init(JAXBContext.newInstance(classesToBeBound));
27+
} catch (JAXBException e) {
28+
throw new IllegalArgumentException(e);
29+
}
30+
}
31+
32+
// http://stackoverflow.com/questions/30643802/what-is-jaxbcontext-newinstancestring-contextpath
33+
public JaxbParser(String context) {
34+
try {
35+
init(JAXBContext.newInstance(context));
36+
} catch (JAXBException e) {
37+
throw new IllegalArgumentException(e);
38+
}
39+
}
40+
41+
private void init(JAXBContext ctx) throws JAXBException {
42+
jaxbMarshaller = new JaxbMarshaller(ctx);
43+
jaxbUnmarshaller = new JaxbUnmarshaller(ctx);
44+
}
45+
46+
// Unmarshaller
47+
public <T> T unmarshal(InputStream is) throws JAXBException {
48+
return (T) jaxbUnmarshaller.unmarshal(is);
49+
}
50+
51+
public <T> T unmarshal(Reader reader) throws JAXBException {
52+
return (T) jaxbUnmarshaller.unmarshal(reader);
53+
}
54+
55+
public <T> T unmarshal(String str) throws JAXBException {
56+
return (T) jaxbUnmarshaller.unmarshal(str);
57+
}
58+
59+
// Marshaller
60+
public void setMarshallerProperty(String prop, Object value) {
61+
try {
62+
jaxbMarshaller.setProperty(prop, value);
63+
} catch (PropertyException e) {
64+
throw new IllegalArgumentException(e);
65+
}
66+
}
67+
68+
public synchronized String marshal(Object instance) throws JAXBException {
69+
return jaxbMarshaller.marshal(instance);
70+
}
71+
72+
public void marshal(Object instance, Writer writer) throws JAXBException {
73+
jaxbMarshaller.marshal(instance, writer);
74+
}
75+
76+
public void setSchema(Schema schema) {
77+
this.schema = schema;
78+
jaxbUnmarshaller.setSchema(schema);
79+
jaxbMarshaller.setSchema(schema);
80+
}
81+
82+
public void validate(String str) throws IOException, SAXException {
83+
validate(new StringReader(str));
84+
}
85+
86+
public void validate(Reader reader) throws IOException, SAXException {
87+
schema.newValidator().validate(new StreamSource(reader));
88+
}
89+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ru.javaops.masterjava.xml.util;
2+
3+
import javax.xml.bind.JAXBContext;
4+
import javax.xml.bind.JAXBException;
5+
import javax.xml.bind.Unmarshaller;
6+
import javax.xml.validation.Schema;
7+
import java.io.InputStream;
8+
import java.io.Reader;
9+
import java.io.StringReader;
10+
11+
/**
12+
* @author GKislin
13+
* Date: 18.11.2008
14+
*/
15+
public class JaxbUnmarshaller {
16+
private Unmarshaller unmarshaller;
17+
18+
public JaxbUnmarshaller(JAXBContext ctx) throws JAXBException {
19+
unmarshaller = ctx.createUnmarshaller();
20+
}
21+
22+
public synchronized void setSchema(Schema schema) {
23+
unmarshaller.setSchema(schema);
24+
}
25+
26+
public synchronized Object unmarshal(InputStream is) throws JAXBException {
27+
return unmarshaller.unmarshal(is);
28+
}
29+
30+
public synchronized Object unmarshal(Reader reader) throws JAXBException {
31+
return unmarshaller.unmarshal(reader);
32+
}
33+
34+
public Object unmarshal(String str) throws JAXBException {
35+
return unmarshal(new StringReader(str));
36+
}
37+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ru.javaops.masterjava.xml.util;
2+
3+
import com.google.common.io.Resources;
4+
import org.xml.sax.SAXException;
5+
6+
import javax.xml.XMLConstants;
7+
import javax.xml.transform.stream.StreamSource;
8+
import javax.xml.validation.Schema;
9+
import javax.xml.validation.SchemaFactory;
10+
import java.io.File;
11+
import java.io.StringReader;
12+
import java.net.URL;
13+
14+
15+
/**
16+
* @author Grigory Kislin
17+
*/
18+
public class Schemas {
19+
20+
// SchemaFactory is not thread-safe
21+
private static final SchemaFactory SCHEMA_FACTORY = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
22+
23+
public static synchronized Schema ofString(String xsd) {
24+
try {
25+
return SCHEMA_FACTORY.newSchema(new StreamSource(new StringReader(xsd)));
26+
} catch (SAXException e) {
27+
throw new IllegalArgumentException(e);
28+
}
29+
}
30+
31+
public static synchronized Schema ofClasspath(String resource) {
32+
// http://digitalsanctum.com/2012/11/30/how-to-read-file-contents-in-java-the-easy-way-with-guava/
33+
return ofURL(Resources.getResource(resource));
34+
}
35+
36+
public static synchronized Schema ofURL(URL url) {
37+
try {
38+
return SCHEMA_FACTORY.newSchema(url);
39+
} catch (SAXException e) {
40+
throw new IllegalArgumentException(e);
41+
}
42+
}
43+
44+
public static synchronized Schema ofFile(File file) {
45+
try {
46+
return SCHEMA_FACTORY.newSchema(file);
47+
} catch (SAXException e) {
48+
throw new IllegalArgumentException(e);
49+
}
50+
}
51+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package ru.javaops.masterjava.xml.util;
2+
3+
import com.google.common.io.Resources;
4+
import org.junit.Test;
5+
import ru.javaops.masterjava.xml.schema.CityType;
6+
import ru.javaops.masterjava.xml.schema.ObjectFactory;
7+
import ru.javaops.masterjava.xml.schema.Payload;
8+
9+
import javax.xml.bind.JAXBElement;
10+
import javax.xml.namespace.QName;
11+
12+
/**
13+
* gkislin
14+
* 23.09.2016
15+
*/
16+
public class JaxbParserTest {
17+
private static final JaxbParser JAXB_PARSER = new JaxbParser(ObjectFactory.class);
18+
19+
static {
20+
JAXB_PARSER.setSchema(Schemas.ofClasspath("payload.xsd"));
21+
}
22+
23+
@Test
24+
public void testPayload() throws Exception {
25+
// JaxbParserTest.class.getResourceAsStream("/city.xml")
26+
Payload payload = JAXB_PARSER.unmarshal(
27+
Resources.getResource("payload.xml").openStream());
28+
String strPayload = JAXB_PARSER.marshal(payload);
29+
JAXB_PARSER.validate(strPayload);
30+
System.out.println(strPayload);
31+
}
32+
33+
@Test
34+
public void testCity() throws Exception {
35+
JAXBElement<CityType> cityElement = JAXB_PARSER.unmarshal(
36+
Resources.getResource("city.xml").openStream());
37+
CityType city = cityElement.getValue();
38+
JAXBElement<CityType> cityElement2 =
39+
new JAXBElement<>(new QName("http://javaops.ru", "City"), CityType.class, city);
40+
String strCity = JAXB_PARSER.marshal(cityElement2);
41+
JAXB_PARSER.validate(strCity);
42+
System.out.println(strCity);
43+
}
44+
}

src/test/resources/city.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<City id="spb" xmlns="http://javaops.ru"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://javaops.ru payload.xsd">Санкт-Петербург
4+
</City>

0 commit comments

Comments
 (0)