-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add first possible implementation of parsing of XML file
- Loading branch information
1 parent
0866e98
commit 9d7008a
Showing
4 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/io/github/aplotnikov/batch/processing/reactor/Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.github.aplotnikov.batch.processing.reactor; | ||
|
||
import java.util.Objects; | ||
|
||
public class Client { | ||
|
||
private final long id; | ||
|
||
public Client(long id) { | ||
this.id = id; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (this == other) { | ||
return true; | ||
} | ||
|
||
if (other == null || getClass() != other.getClass()) { | ||
return false; | ||
} | ||
|
||
Client client = (Client) other; | ||
return id == client.id; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Client{id=" + id + '}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/io/github/aplotnikov/batch/processing/reactor/XmlFileReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.github.aplotnikov.batch.processing.reactor; | ||
|
||
import io.vavr.control.Try; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.SynchronousSink; | ||
|
||
import javax.xml.stream.XMLInputFactory; | ||
import javax.xml.stream.XMLStreamReader; | ||
import java.io.BufferedInputStream; | ||
import java.util.concurrent.Callable; | ||
import java.util.function.Consumer; | ||
|
||
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; | ||
|
||
class XmlFileReader { | ||
|
||
Flux<Client> read(String path) { | ||
return Flux.generate( | ||
reader(path), | ||
this::findClients, | ||
close() | ||
); | ||
} | ||
|
||
private Callable<XMLStreamReader> reader(String path) { | ||
return () -> { | ||
BufferedInputStream inputStream = new BufferedInputStream(getClass().getResourceAsStream(path)); | ||
return XMLInputFactory.newInstance().createXMLStreamReader(inputStream); | ||
}; | ||
} | ||
|
||
private XMLStreamReader findClients(XMLStreamReader reader, SynchronousSink<Client> sink) { | ||
Try.run(() -> { | ||
// Need to think about refactoring it to Stream | ||
while (reader.hasNext()) { | ||
if (reader.next() == START_ELEMENT && reader.getLocalName().equals("client")) { | ||
sink.next(parseClient(reader)); | ||
} | ||
} | ||
}) | ||
.onFailure(sink::error) | ||
.onSuccess(aVoid -> sink.complete()); | ||
return reader; | ||
} | ||
|
||
private Client parseClient(XMLStreamReader reader) { | ||
return new Client( | ||
Long.parseLong(reader.getAttributeValue("", "id")) | ||
); | ||
} | ||
|
||
private Consumer<XMLStreamReader> close() { | ||
return reader -> Try.run(reader::close).get(); | ||
} | ||
} |