Skip to content

Commit 907cc2e

Browse files
committed
4_4_HW3_jaxb_stax
1 parent 94ec2e1 commit 907cc2e

File tree

6 files changed

+75
-76
lines changed

6 files changed

+75
-76
lines changed

web/upload/src/main/java/ru/javaops/masterjava/upload/UserProcessor.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22

33
import ru.javaops.masterjava.model.User;
44
import ru.javaops.masterjava.model.UserFlag;
5+
import ru.javaops.masterjava.xml.schema.ObjectFactory;
6+
import ru.javaops.masterjava.xml.util.JaxbParser;
7+
import ru.javaops.masterjava.xml.util.JaxbUnmarshaller;
58
import ru.javaops.masterjava.xml.util.StaxStreamProcessor;
69

10+
import javax.xml.bind.JAXBException;
711
import javax.xml.stream.XMLStreamException;
812
import javax.xml.stream.events.XMLEvent;
913
import java.io.InputStream;
1014
import java.util.ArrayList;
1115
import java.util.List;
1216

1317
public class UserProcessor {
18+
private static final JaxbParser jaxbParser = new JaxbParser(ObjectFactory.class);
1419

15-
public List<User> process(final InputStream is) throws XMLStreamException {
20+
public List<User> process(final InputStream is) throws XMLStreamException, JAXBException {
1621
final StaxStreamProcessor processor = new StaxStreamProcessor(is);
1722
List<User> users = new ArrayList<>();
1823

24+
JaxbUnmarshaller unmarshaller = jaxbParser.createUnmarshaller();
1925
while (processor.doUntil(XMLEvent.START_ELEMENT, "User")) {
20-
final String email = processor.getAttribute("email");
21-
final UserFlag flag = UserFlag.valueOf(processor.getAttribute("flag"));
22-
final String fullName = processor.getReader().getElementText();
23-
final User user = new User(fullName, email, flag);
26+
ru.javaops.masterjava.xml.schema.User xmlUser = unmarshaller.unmarshal(processor.getReader(), ru.javaops.masterjava.xml.schema.User.class);
27+
final User user = new User(xmlUser.getValue(), xmlUser.getEmail(), UserFlag.valueOf(xmlUser.getFlag().value()));
2428
users.add(user);
2529
}
2630
return users;

web/upload/src/main/java/ru/javaops/masterjava/xml/util/JaxbMarshaller.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ public JaxbMarshaller(JAXBContext ctx) throws JAXBException {
1818
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
1919
}
2020

21-
public void setProperty(String prop, Object value) throws PropertyException {
22-
marshaller.setProperty(prop, value);
21+
public void setProperty(String prop, Object value) {
22+
try {
23+
marshaller.setProperty(prop, value);
24+
} catch (PropertyException e) {
25+
throw new IllegalArgumentException(e);
26+
}
2327
}
2428

25-
public synchronized void setSchema(Schema schema) {
29+
public void setSchema(Schema schema) {
2630
marshaller.setSchema(schema);
2731
}
2832

@@ -32,8 +36,7 @@ public String marshal(Object instance) throws JAXBException {
3236
return sw.toString();
3337
}
3438

35-
public synchronized void marshal(Object instance, Writer writer) throws JAXBException {
39+
public void marshal(Object instance, Writer writer) throws JAXBException {
3640
marshaller.marshal(instance, writer);
3741
}
38-
39-
}
42+
}

web/upload/src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,26 @@
44

55
import javax.xml.bind.JAXBContext;
66
import javax.xml.bind.JAXBException;
7-
import javax.xml.bind.PropertyException;
8-
import javax.xml.stream.XMLStreamReader;
97
import javax.xml.transform.stream.StreamSource;
108
import javax.xml.validation.Schema;
11-
import java.io.*;
9+
import java.io.IOException;
10+
import java.io.Reader;
11+
import java.io.StringReader;
1212

1313

1414
/**
15-
* Marshalling/Unmarshalling JAXB helper
16-
* XML Facade
15+
* Marshalling/Unmarshalling JAXB facade
1716
*/
1817
public class JaxbParser {
1918

20-
protected JaxbMarshaller jaxbMarshaller;
21-
protected JaxbUnmarshaller jaxbUnmarshaller;
19+
private JAXBContext ctx;
2220
protected Schema schema;
2321

2422
public JaxbParser(Class... classesToBeBound) {
2523
try {
2624
init(JAXBContext.newInstance(classesToBeBound));
2725
} catch (JAXBException e) {
28-
throw new IllegalArgumentException(e);
26+
throw new IllegalStateException(e);
2927
}
3028
}
3129

@@ -34,53 +32,42 @@ public JaxbParser(String context) {
3432
try {
3533
init(JAXBContext.newInstance(context));
3634
} catch (JAXBException e) {
37-
throw new IllegalArgumentException(e);
35+
throw new IllegalStateException(e);
3836
}
3937
}
4038

41-
private void init(JAXBContext ctx) throws JAXBException {
42-
jaxbMarshaller = new JaxbMarshaller(ctx);
43-
jaxbUnmarshaller = new JaxbUnmarshaller(ctx);
39+
private void init(JAXBContext ctx) {
40+
this.ctx = ctx;
4441
}
4542

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-
public <T> T unmarshal(XMLStreamReader reader, Class<T> elementClass) throws JAXBException {
60-
return jaxbUnmarshaller.unmarshal(reader, elementClass);
61-
}
62-
63-
// Marshaller
64-
public void setMarshallerProperty(String prop, Object value) {
43+
// https://stackoverflow.com/a/7400735/548473
44+
public JaxbMarshaller createMarshaller() {
6545
try {
66-
jaxbMarshaller.setProperty(prop, value);
67-
} catch (PropertyException e) {
68-
throw new IllegalArgumentException(e);
46+
JaxbMarshaller marshaller = new JaxbMarshaller(ctx);
47+
if (schema != null) {
48+
marshaller.setSchema(schema);
49+
}
50+
return marshaller;
51+
} catch (JAXBException e) {
52+
throw new IllegalStateException(e);
6953
}
7054
}
7155

72-
public String marshal(Object instance) throws JAXBException {
73-
return jaxbMarshaller.marshal(instance);
74-
}
75-
76-
public void marshal(Object instance, Writer writer) throws JAXBException {
77-
jaxbMarshaller.marshal(instance, writer);
56+
// https://stackoverflow.com/a/7400735/548473
57+
public JaxbUnmarshaller createUnmarshaller() {
58+
try {
59+
JaxbUnmarshaller unmarshaller = new JaxbUnmarshaller(ctx);
60+
if (schema != null) {
61+
unmarshaller.setSchema(schema);
62+
}
63+
return unmarshaller;
64+
} catch (JAXBException e) {
65+
throw new IllegalStateException(e);
66+
}
7867
}
7968

8069
public void setSchema(Schema schema) {
8170
this.schema = schema;
82-
jaxbUnmarshaller.setSchema(schema);
83-
jaxbMarshaller.setSchema(schema);
8471
}
8572

8673
public void validate(String str) throws IOException, SAXException {

web/upload/src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ public JaxbUnmarshaller(JAXBContext ctx) throws JAXBException {
1616
unmarshaller = ctx.createUnmarshaller();
1717
}
1818

19-
public synchronized void setSchema(Schema schema) {
19+
public void setSchema(Schema schema) {
2020
unmarshaller.setSchema(schema);
2121
}
2222

23-
public synchronized Object unmarshal(InputStream is) throws JAXBException {
24-
return unmarshaller.unmarshal(is);
23+
public <T> T unmarshal(InputStream is) throws JAXBException {
24+
return (T) unmarshaller.unmarshal(is);
2525
}
2626

27-
public synchronized Object unmarshal(Reader reader) throws JAXBException {
28-
return unmarshaller.unmarshal(reader);
27+
public <T> T unmarshal(Reader reader) throws JAXBException {
28+
return (T) unmarshaller.unmarshal(reader);
2929
}
3030

31-
public Object unmarshal(String str) throws JAXBException {
32-
return unmarshal(new StringReader(str));
31+
public <T> T unmarshal(String str) throws JAXBException {
32+
return (T) unmarshal(new StringReader(str));
3333
}
3434

35-
public synchronized <T> T unmarshal(XMLStreamReader reader, Class<T> elementClass) throws JAXBException {
35+
public <T> T unmarshal(XMLStreamReader reader, Class<T> elementClass) throws JAXBException {
3636
return unmarshaller.unmarshal(reader, elementClass).getValue();
3737
}
3838
}

web/upload/src/test/java/ru/javaops/masterjava/MainXml.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
import ru.javaops.masterjava.xml.schema.Payload;
99
import ru.javaops.masterjava.xml.schema.Project;
1010
import ru.javaops.masterjava.xml.schema.User;
11-
import ru.javaops.masterjava.xml.util.JaxbParser;
12-
import ru.javaops.masterjava.xml.util.Schemas;
13-
import ru.javaops.masterjava.xml.util.StaxStreamProcessor;
14-
import ru.javaops.masterjava.xml.util.XsltProcessor;
11+
import ru.javaops.masterjava.xml.util.*;
1512

1613
import javax.xml.stream.events.XMLEvent;
1714
import java.io.InputStream;
@@ -59,10 +56,11 @@ public static void main(String[] args) throws Exception {
5956

6057
private static Set<User> parseByJaxb(String projectName, URL payloadUrl) throws Exception {
6158
JaxbParser parser = new JaxbParser(ObjectFactory.class);
59+
JaxbUnmarshaller unmarshaller = parser.createUnmarshaller();
6260
parser.setSchema(Schemas.ofClasspath("payload.xsd"));
6361
Payload payload;
6462
try (InputStream is = payloadUrl.openStream()) {
65-
payload = parser.unmarshal(is);
63+
payload = unmarshaller.unmarshal(is);
6664
}
6765

6866
Project project = StreamEx.of(payload.getProjects().getProject())
@@ -99,11 +97,12 @@ private static Set<User> processByStax(String projectName, URL payloadUrl) throw
9997
// Users loop
10098
Set<User> users = new TreeSet<>(USER_COMPARATOR);
10199

102-
JaxbParser parser = new JaxbParser(User.class);
100+
JaxbParser parser = new JaxbParser(ObjectFactory.class);
101+
JaxbUnmarshaller unmarshaller = parser.createUnmarshaller();
103102
while (processor.doUntil(XMLEvent.START_ELEMENT, "User")) {
104103
String groupRefs = processor.getAttribute("groupRefs");
105104
if (!Collections.disjoint(groupNames, Splitter.on(' ').splitToList(nullToEmpty(groupRefs)))) {
106-
User user = parser.unmarshal(processor.getReader(), User.class);
105+
User user = unmarshaller.unmarshal(processor.getReader(), User.class);
107106
users.add(user);
108107
}
109108
}

web/upload/src/test/java/ru/javaops/masterjava/xml/util/JaxbParserTest.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,37 @@
1010
import javax.xml.namespace.QName;
1111

1212
public class JaxbParserTest {
13-
private static final JaxbParser JAXB_PARSER = new JaxbParser(ObjectFactory.class);
13+
// https://google.github.io/styleguide/javaguide.html#s5.2.4-constant-names
14+
private static final JaxbParser jaxbParser;
15+
private static final JaxbMarshaller marshaller;
16+
private static final JaxbUnmarshaller unmarshaller;
1417

1518
static {
16-
JAXB_PARSER.setSchema(Schemas.ofClasspath("payload.xsd"));
19+
jaxbParser = new JaxbParser(ObjectFactory.class);
20+
jaxbParser.setSchema(Schemas.ofClasspath("payload.xsd"));
21+
marshaller = jaxbParser.createMarshaller();
22+
unmarshaller = jaxbParser.createUnmarshaller();
1723
}
1824

1925
@Test
2026
public void testPayload() throws Exception {
2127
// JaxbParserTest.class.getResourceAsStream("/city.xml")
22-
Payload payload = JAXB_PARSER.unmarshal(
28+
Payload payload = unmarshaller.unmarshal(
2329
Resources.getResource("payload.xml").openStream());
24-
String strPayload = JAXB_PARSER.marshal(payload);
25-
JAXB_PARSER.validate(strPayload);
30+
String strPayload = marshaller.marshal(payload);
31+
jaxbParser.validate(strPayload);
2632
System.out.println(strPayload);
2733
}
2834

2935
@Test
3036
public void testCity() throws Exception {
31-
JAXBElement<CityType> cityElement = JAXB_PARSER.unmarshal(
37+
JAXBElement<CityType> cityElement = unmarshaller.unmarshal(
3238
Resources.getResource("city.xml").openStream());
3339
CityType city = cityElement.getValue();
3440
JAXBElement<CityType> cityElement2 =
3541
new JAXBElement<>(new QName("http://javaops.ru", "City"), CityType.class, city);
36-
String strCity = JAXB_PARSER.marshal(cityElement2);
37-
JAXB_PARSER.validate(strCity);
42+
String strCity = marshaller.marshal(cityElement2);
43+
jaxbParser.validate(strCity);
3844
System.out.println(strCity);
3945
}
4046
}

0 commit comments

Comments
 (0)