Search in sources :

Example 16 with XMLChunkMeta

use of io.georocket.storage.XMLChunkMeta in project georocket by georocket.

the class XMLSplitter method makeResult.

/**
 * Create a new chunk starting from the marked position and ending on the
 * given position. Reset the mark afterwards and advance the window to the
 * end position. Return a {@link io.georocket.input.Splitter.Result} object
 * with the new chunk and its metadata.
 * @param pos the end position
 * @return the {@link io.georocket.input.Splitter.Result} object
 */
protected Result<XMLChunkMeta> makeResult(int pos) {
    StringBuilder sb = new StringBuilder();
    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
    // append the full stack of start elements (backwards)
    List<XMLStartElement> chunkParents = new ArrayList<>();
    startElements.descendingIterator().forEachRemaining(e -> {
        chunkParents.add(e);
        sb.append(e + "\n");
    });
    // get chunk start in bytes
    int chunkStart = sb.toString().getBytes(StandardCharsets.UTF_8).length;
    // append current element
    byte[] bytes = window.getBytes(mark, pos);
    sb.append(new String(bytes, StandardCharsets.UTF_8));
    window.advanceTo(pos);
    mark = -1;
    // get chunk end in bytes
    int chunkEnd = chunkStart + bytes.length;
    // append the full stack of end elements
    startElements.iterator().forEachRemaining(e -> sb.append("\n</" + e.getName() + ">"));
    XMLChunkMeta meta = new XMLChunkMeta(chunkParents, chunkStart, chunkEnd);
    return new Result<XMLChunkMeta>(sb.toString(), meta);
}
Also used : XMLStartElement(io.georocket.util.XMLStartElement) ArrayList(java.util.ArrayList) XMLChunkMeta(io.georocket.storage.XMLChunkMeta)

Aggregations

XMLChunkMeta (io.georocket.storage.XMLChunkMeta)16 XMLStartElement (io.georocket.util.XMLStartElement)15 Test (org.junit.Test)14 Buffer (io.vertx.core.buffer.Buffer)8 Result (io.georocket.input.Splitter.Result)7 BufferWriteStream (io.georocket.util.io.BufferWriteStream)5 DelegateChunkReadStream (io.georocket.util.io.DelegateChunkReadStream)5 Async (io.vertx.ext.unit.Async)5 TestContext (io.vertx.ext.unit.TestContext)5 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)5 Arrays (java.util.Arrays)5 RunWith (org.junit.runner.RunWith)5 ChunkReadStream (io.georocket.storage.ChunkReadStream)3 RunTestOnContext (io.vertx.ext.unit.junit.RunTestOnContext)3 Pair (org.apache.commons.lang3.tuple.Pair)3 Rule (org.junit.Rule)3 Observable (rx.Observable)3 ChunkMeta (io.georocket.storage.ChunkMeta)2 GeoJsonChunkMeta (io.georocket.storage.GeoJsonChunkMeta)2 ArrayList (java.util.ArrayList)2