Search in sources :

Example 1 with XMLStreamEvent

use of io.georocket.util.XMLStreamEvent in project georocket by georocket.

the class FirstLevelSplitterTest method split.

/**
 * Use the {@link FirstLevelSplitter} and split an XML string
 * @param xml the XML string
 * @return the chunks created by the splitter
 * @throws Exception if the XML string could not be parsed
 */
private List<Result<XMLChunkMeta>> split(String xml) throws Exception {
    Window window = new Window();
    window.append(Buffer.buffer(xml));
    AsyncXMLInputFactory xmlInputFactory = new InputFactoryImpl();
    AsyncXMLStreamReader<AsyncByteArrayFeeder> reader = xmlInputFactory.createAsyncForByteArray();
    byte[] xmlBytes = xml.getBytes(StandardCharsets.UTF_8);
    reader.getInputFeeder().feedInput(xmlBytes, 0, xmlBytes.length);
    FirstLevelSplitter splitter = new FirstLevelSplitter(window);
    List<Result<XMLChunkMeta>> chunks = new ArrayList<>();
    while (reader.hasNext()) {
        int event = reader.next();
        if (event == AsyncXMLStreamReader.EVENT_INCOMPLETE) {
            reader.close();
            continue;
        }
        int pos = reader.getLocation().getCharacterOffset();
        Result<XMLChunkMeta> chunk = splitter.onEvent(new XMLStreamEvent(event, pos, reader));
        if (chunk != null) {
            chunks.add(chunk);
        }
    }
    return chunks;
}
Also used : Window(io.georocket.util.Window) XMLStreamEvent(io.georocket.util.XMLStreamEvent) ArrayList(java.util.ArrayList) AsyncByteArrayFeeder(com.fasterxml.aalto.AsyncByteArrayFeeder) InputFactoryImpl(com.fasterxml.aalto.stax.InputFactoryImpl) Result(io.georocket.input.Splitter.Result) XMLChunkMeta(io.georocket.storage.XMLChunkMeta) AsyncXMLInputFactory(com.fasterxml.aalto.AsyncXMLInputFactory)

Aggregations

AsyncByteArrayFeeder (com.fasterxml.aalto.AsyncByteArrayFeeder)1 AsyncXMLInputFactory (com.fasterxml.aalto.AsyncXMLInputFactory)1 InputFactoryImpl (com.fasterxml.aalto.stax.InputFactoryImpl)1 Result (io.georocket.input.Splitter.Result)1 XMLChunkMeta (io.georocket.storage.XMLChunkMeta)1 Window (io.georocket.util.Window)1 XMLStreamEvent (io.georocket.util.XMLStreamEvent)1 ArrayList (java.util.ArrayList)1