Skip to content

Commit aa426bb

Browse files
\2_07_JAXB
1 parent 86d6bd4 commit aa426bb

File tree

9 files changed

+279
-2
lines changed

9 files changed

+279
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
out
33
target
44
*.iml
5-
log
5+
log
6+
lib

pom.xml

Lines changed: 24 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.20.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,22 @@
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>
102+
<dependency>
103+
<groupId>javax.xml.bind</groupId>
104+
<artifactId>jaxb-api</artifactId>
105+
<version>2.4.0-b180830.0359</version>
106+
</dependency>
83107
</dependencies>
84108

85109
<profiles>

src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void main(String[] args) throws RunnerException {
5252
public int[][] singleThreadMultiplyOpt() throws Exception {
5353
return MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB);
5454
}
55-
55+
//3c7aec4a7d241ec2254c862d621b81c80d8b07239fd907f76b309a4f27180fe3 *jetbrains-toolbox-1.28.1.15219.exe
5656
// @Benchmark
5757
public int[][] singleThreadMultiplyOpt2() throws Exception {
5858
return MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
public class JaxbMarshaller {
12+
private Marshaller marshaller;
13+
14+
public JaxbMarshaller(JAXBContext ctx) throws JAXBException {
15+
marshaller = ctx.createMarshaller();
16+
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
17+
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
18+
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
19+
}
20+
21+
public void setProperty(String prop, Object value) throws PropertyException {
22+
marshaller.setProperty(prop, value);
23+
}
24+
25+
public synchronized void setSchema(Schema schema) {
26+
marshaller.setSchema(schema);
27+
}
28+
29+
public String marshal(Object instance) throws JAXBException {
30+
StringWriter sw = new StringWriter();
31+
marshal(instance, sw);
32+
return sw.toString();
33+
}
34+
35+
public synchronized void marshal(Object instance, Writer writer) throws JAXBException {
36+
marshaller.marshal(instance, writer);
37+
}
38+
39+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
*/
17+
public class JaxbParser {
18+
19+
protected JaxbMarshaller jaxbMarshaller;
20+
protected JaxbUnmarshaller jaxbUnmarshaller;
21+
protected Schema schema;
22+
23+
public JaxbParser(Class... classesToBeBound) {
24+
try {
25+
init(JAXBContext.newInstance(classesToBeBound));
26+
} catch (JAXBException e) {
27+
throw new IllegalArgumentException(e);
28+
}
29+
}
30+
31+
// http://stackoverflow.com/questions/30643802/what-is-jaxbcontext-newinstancestring-contextpath
32+
public JaxbParser(String context) {
33+
try {
34+
init(JAXBContext.newInstance(context));
35+
} catch (JAXBException e) {
36+
throw new IllegalArgumentException(e);
37+
}
38+
}
39+
40+
private void init(JAXBContext ctx) throws JAXBException {
41+
jaxbMarshaller = new JaxbMarshaller(ctx);
42+
jaxbUnmarshaller = new JaxbUnmarshaller(ctx);
43+
}
44+
45+
// Unmarshaller
46+
public <T> T unmarshal(InputStream is) throws JAXBException {
47+
return (T) jaxbUnmarshaller.unmarshal(is);
48+
}
49+
50+
public <T> T unmarshal(Reader reader) throws JAXBException {
51+
return (T) jaxbUnmarshaller.unmarshal(reader);
52+
}
53+
54+
public <T> T unmarshal(String str) throws JAXBException {
55+
return (T) jaxbUnmarshaller.unmarshal(str);
56+
}
57+
58+
// Marshaller
59+
public void setMarshallerProperty(String prop, Object value) {
60+
try {
61+
jaxbMarshaller.setProperty(prop, value);
62+
} catch (PropertyException e) {
63+
throw new IllegalArgumentException(e);
64+
}
65+
}
66+
67+
public String marshal(Object instance) throws JAXBException {
68+
return jaxbMarshaller.marshal(instance);
69+
}
70+
71+
public void marshal(Object instance, Writer writer) throws JAXBException {
72+
jaxbMarshaller.marshal(instance, writer);
73+
}
74+
75+
public void setSchema(Schema schema) {
76+
this.schema = schema;
77+
jaxbUnmarshaller.setSchema(schema);
78+
jaxbMarshaller.setSchema(schema);
79+
}
80+
81+
public void validate(String str) throws IOException, SAXException {
82+
validate(new StringReader(str));
83+
}
84+
85+
public void validate(Reader reader) throws IOException, SAXException {
86+
schema.newValidator().validate(new StreamSource(reader));
87+
}
88+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
public class JaxbUnmarshaller {
12+
private Unmarshaller unmarshaller;
13+
14+
public JaxbUnmarshaller(JAXBContext ctx) throws JAXBException {
15+
unmarshaller = ctx.createUnmarshaller();
16+
}
17+
18+
public synchronized void setSchema(Schema schema) {
19+
unmarshaller.setSchema(schema);
20+
}
21+
22+
public synchronized Object unmarshal(InputStream is) throws JAXBException {
23+
return unmarshaller.unmarshal(is);
24+
}
25+
26+
public synchronized Object unmarshal(Reader reader) throws JAXBException {
27+
return unmarshaller.unmarshal(reader);
28+
}
29+
30+
public Object unmarshal(String str) throws JAXBException {
31+
return unmarshal(new StringReader(str));
32+
}
33+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
public class Schemas {
16+
17+
// SchemaFactory is not thread-safe
18+
private static final SchemaFactory SCHEMA_FACTORY = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
19+
20+
public static synchronized Schema ofString(String xsd) {
21+
try {
22+
return SCHEMA_FACTORY.newSchema(new StreamSource(new StringReader(xsd)));
23+
} catch (SAXException e) {
24+
throw new IllegalArgumentException(e);
25+
}
26+
}
27+
28+
public static synchronized Schema ofClasspath(String resource) {
29+
// http://digitalsanctum.com/2012/11/30/how-to-read-file-contents-in-java-the-easy-way-with-guava/
30+
return ofURL(Resources.getResource(resource));
31+
}
32+
33+
public static synchronized Schema ofURL(URL url) {
34+
try {
35+
return SCHEMA_FACTORY.newSchema(url);
36+
} catch (SAXException e) {
37+
throw new IllegalArgumentException(e);
38+
}
39+
}
40+
41+
public static synchronized Schema ofFile(File file) {
42+
try {
43+
return SCHEMA_FACTORY.newSchema(file);
44+
} catch (SAXException e) {
45+
throw new IllegalArgumentException(e);
46+
}
47+
}
48+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
public class JaxbParserTest {
13+
private static final JaxbParser JAXB_PARSER = new JaxbParser(ObjectFactory.class);
14+
15+
static {
16+
JAXB_PARSER.setSchema(Schemas.ofClasspath("payload.xsd"));
17+
}
18+
19+
@Test
20+
public void testPayload() throws Exception {
21+
// JaxbParserTest.class.getResourceAsStream("/city.xml")
22+
Payload payload = JAXB_PARSER.unmarshal(
23+
Resources.getResource("payload.xml").openStream());
24+
String strPayload = JAXB_PARSER.marshal(payload);
25+
JAXB_PARSER.validate(strPayload);
26+
System.out.println(strPayload);
27+
}
28+
29+
@Test
30+
public void testCity() throws Exception {
31+
JAXBElement<CityType> cityElement = JAXB_PARSER.unmarshal(
32+
Resources.getResource("city.xml").openStream());
33+
CityType city = cityElement.getValue();
34+
JAXBElement<CityType> cityElement2 =
35+
new JAXBElement<>(new QName("http://javaops.ru", "City"), CityType.class, city);
36+
String strCity = JAXB_PARSER.marshal(cityElement2);
37+
JAXB_PARSER.validate(strCity);
38+
System.out.println(strCity);
39+
}
40+
}

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)