Skip to content

Commit 5918da0

Browse files
committed
3_4_StAX_refactoring
1 parent 52e6136 commit 5918da0

4 files changed

Lines changed: 41 additions & 24 deletions

File tree

‎src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import javax.xml.bind.JAXBContext;
66
import javax.xml.bind.JAXBException;
77
import javax.xml.bind.PropertyException;
8+
import javax.xml.stream.XMLStreamReader;
89
import javax.xml.transform.stream.StreamSource;
910
import javax.xml.validation.Schema;
1011
import java.io.*;
@@ -55,6 +56,10 @@ public <T> T unmarshal(String str) throws JAXBException {
5556
return (T) jaxbUnmarshaller.unmarshal(str);
5657
}
5758

59+
public <T> T unmarshal(XMLStreamReader reader, Class<T> elementClass) throws JAXBException {
60+
return jaxbUnmarshaller.unmarshal(reader, elementClass);
61+
}
62+
5863
// Marshaller
5964
public void setMarshallerProperty(String prop, Object value) {
6065
try {

‎src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.xml.bind.JAXBContext;
44
import javax.xml.bind.JAXBException;
55
import javax.xml.bind.Unmarshaller;
6+
import javax.xml.stream.XMLStreamReader;
67
import javax.xml.validation.Schema;
78
import java.io.InputStream;
89
import java.io.Reader;
@@ -30,4 +31,8 @@ public synchronized Object unmarshal(Reader reader) throws JAXBException {
3031
public Object unmarshal(String str) throws JAXBException {
3132
return unmarshal(new StringReader(str));
3233
}
33-
}
34+
35+
public synchronized <T> T unmarshal(XMLStreamReader reader, Class<T> elementClass) throws JAXBException {
36+
return unmarshaller.unmarshal(reader, elementClass).getValue();
37+
}
38+
}

‎src/main/java/ru/javaops/masterjava/xml/util/StaxStreamProcessor.java‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,43 @@ public StaxStreamProcessor(InputStream is) throws XMLStreamException {
1515
reader = FACTORY.createXMLStreamReader(is);
1616
}
1717

18-
public XMLStreamReader getReader() {
19-
return reader;
18+
public boolean startElement(String element, String parent) throws XMLStreamException {
19+
while (reader.hasNext()) {
20+
int event = reader.next();
21+
if (parent != null && isElementEnd(event, parent)) {
22+
return false;
23+
}
24+
if (isElementStart(event, element)) {
25+
return true;
26+
}
27+
}
28+
return false;
2029
}
2130

22-
public boolean doUntil(int stopEvent, String value) throws XMLStreamException {
23-
return doUntilAny(stopEvent, value) != null;
31+
private boolean isElementStart(int event, String el) {
32+
return event == XMLEvent.START_ELEMENT && el.equals(reader.getLocalName());
33+
}
34+
35+
private boolean isElementEnd(int event, String el) {
36+
return event == XMLEvent.END_ELEMENT && el.equals(reader.getLocalName());
37+
}
38+
39+
public XMLStreamReader getReader() {
40+
return reader;
2441
}
2542

2643
public String getAttribute(String name) throws XMLStreamException {
2744
return reader.getAttributeValue(null, name);
2845
}
2946

30-
public String doUntilAny(int stopEvent, String... values) throws XMLStreamException {
47+
public boolean doUntil(int stopEvent, String value) throws XMLStreamException {
3148
while (reader.hasNext()) {
3249
int event = reader.next();
33-
if (event == stopEvent) {
34-
String xmlValue = getValue(event);
35-
for (String value : values) {
36-
if (value.equals(xmlValue)) {
37-
return xmlValue;
38-
}
39-
}
50+
if (event == stopEvent && value.equals(getValue(event))) {
51+
return true;
4052
}
4153
}
42-
return null;
54+
return false;
4355
}
4456

4557
public String getValue(int event) throws XMLStreamException {

‎src/test/java/ru/javaops/masterjava/MainXml.java‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,12 @@ private static Set<User> processByStax(String projectName, URL payloadUrl) throw
8080

8181
// Projects loop
8282
projects:
83-
while (processor.doUntil(XMLEvent.START_ELEMENT, "Project")) {
83+
while (processor.startElement("Project", "Projects")) {
8484
if (projectName.equals(processor.getAttribute("name"))) {
85-
// Groups loop
86-
String element;
87-
while ((element = processor.doUntilAny(XMLEvent.START_ELEMENT, "Project", "Group", "Users")) != null) {
88-
if (!element.equals("Group")) {
89-
break projects;
90-
}
85+
while (processor.startElement("Group", "Project")) {
9186
groupNames.add(processor.getAttribute("name"));
9287
}
88+
break;
9389
}
9490
}
9591
if (groupNames.isEmpty()) {
@@ -99,12 +95,11 @@ private static Set<User> processByStax(String projectName, URL payloadUrl) throw
9995
// Users loop
10096
Set<User> users = new TreeSet<>(USER_COMPARATOR);
10197

98+
JaxbParser parser = new JaxbParser(User.class);
10299
while (processor.doUntil(XMLEvent.START_ELEMENT, "User")) {
103100
String groupRefs = processor.getAttribute("groupRefs");
104101
if (!Collections.disjoint(groupNames, Splitter.on(' ').splitToList(nullToEmpty(groupRefs)))) {
105-
User user = new User();
106-
user.setEmail(processor.getAttribute("email"));
107-
user.setValue(processor.getText());
102+
User user = parser.unmarshal(processor.getReader(), User.class);
108103
users.add(user);
109104
}
110105
}

0 commit comments

Comments
 (0)