Search in sources :

Example 11 with JsopTokenizer

use of org.apache.jackrabbit.oak.commons.json.JsopTokenizer in project jackrabbit-oak by apache.

the class NodeDocument method fromString.

public static NodeDocument fromString(DocumentStore store, String s) {
    JsopTokenizer json = new JsopTokenizer(s);
    NodeDocument doc = new NodeDocument(store);
    while (true) {
        if (json.matches(JsopReader.END)) {
            break;
        }
        String k = json.readString();
        json.read(':');
        if (json.matches(JsopReader.END)) {
            break;
        }
        doc.put(k, fromJson(json));
        json.matches(',');
    }
    doc.seal();
    return doc;
}
Also used : JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer)

Example 12 with JsopTokenizer

use of org.apache.jackrabbit.oak.commons.json.JsopTokenizer in project jackrabbit-oak by apache.

the class RDBDocumentSerializer method fromRow.

/**
     * Reconstructs a {@link Document} based on the persisted {@link RDBRow}.
     */
@Nonnull
public <T extends Document> T fromRow(@Nonnull Collection<T> collection, @Nonnull RDBRow row) throws DocumentStoreException {
    final String charData = row.getData();
    checkNotNull(charData, "RDBRow.getData() is null for collection " + collection + ", id: " + row.getId());
    T doc = collection.newDocument(store);
    doc.put(ID, row.getId());
    if (row.getModified() != RDBRow.LONG_UNSET) {
        doc.put(MODIFIED, row.getModified());
    }
    if (row.getModcount() != RDBRow.LONG_UNSET) {
        doc.put(MODCOUNT, row.getModcount());
    }
    if (RDBDocumentStore.USECMODCOUNT && row.getCollisionsModcount() != RDBRow.LONG_UNSET) {
        doc.put(CMODCOUNT, row.getCollisionsModcount());
    }
    if (row.hasBinaryProperties() != null) {
        doc.put(HASBINARY, row.hasBinaryProperties().longValue());
    }
    if (row.deletedOnce() != null) {
        doc.put(DELETEDONCE, row.deletedOnce().booleanValue());
    }
    byte[] bdata = row.getBdata();
    boolean blobInUse = false;
    JsopTokenizer json;
    // update operations
    try {
        if (bdata != null && bdata.length != 0) {
            String s = fromBlobData(bdata);
            json = new JsopTokenizer(s);
            json.read('{');
            readDocumentFromJson(json, doc);
            json.read(JsopReader.END);
            blobInUse = true;
        }
    } catch (Exception ex) {
        throw new DocumentStoreException(ex);
    }
    json = new JsopTokenizer(charData);
    // start processing the VARCHAR data
    try {
        int next = json.read();
        if (next == '{') {
            if (blobInUse) {
                throw new DocumentStoreException("expected literal \"blob\" but found: " + row.getData());
            }
            readDocumentFromJson(json, doc);
        } else if (next == JsopReader.STRING) {
            if (!blobInUse) {
                throw new DocumentStoreException("did not expect \"blob\" here: " + row.getData());
            }
            if (!"blob".equals(json.getToken())) {
                throw new DocumentStoreException("expected string literal \"blob\"");
            }
        } else {
            throw new DocumentStoreException("unexpected token " + next + " in " + row.getData());
        }
        next = json.read();
        if (next == ',') {
            do {
                Object ob = JSON.parse(json);
                if (!(ob instanceof List)) {
                    throw new DocumentStoreException("expected array but got: " + ob);
                }
                List<List<Object>> update = (List<List<Object>>) ob;
                for (List<Object> op : update) {
                    applyUpdate(doc, update, op);
                }
            } while (json.matches(','));
        }
        json.read(JsopReader.END);
        return doc;
    } catch (Exception ex) {
        String message = String.format("Error processing persisted data for document '%s'", row.getId());
        if (charData.length() > 0) {
            int last = charData.charAt(charData.length() - 1);
            if (last != '}' && last != '"' && last != ']') {
                message += " (DATA column might be truncated)";
            }
        }
        LOG.error(message, ex);
        throw new DocumentStoreException(message, ex);
    }
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) List(java.util.List) RDBJSONSupport.appendJsonString(org.apache.jackrabbit.oak.plugins.document.rdb.RDBJSONSupport.appendJsonString) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) IOException(java.io.IOException) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer) Nonnull(javax.annotation.Nonnull)

Example 13 with JsopTokenizer

use of org.apache.jackrabbit.oak.commons.json.JsopTokenizer in project jackrabbit-oak by apache.

the class ChangeSet method fromString.

public static ChangeSet fromString(String json) {
    JsopReader reader = new JsopTokenizer(json);
    int maxPathDepth = 0;
    Set<String> parentPaths = null;
    Set<String> parentNodeNames = null;
    Set<String> parentNodeTypes = null;
    Set<String> propertyNames = null;
    Set<String> allNodeTypes = null;
    reader.read('{');
    if (!reader.matches('}')) {
        do {
            String name = reader.readString();
            reader.read(':');
            if ("maxPathDepth".equals(name)) {
                maxPathDepth = Integer.parseInt(reader.read(JsopReader.NUMBER));
            } else {
                Set<String> data = readArrayAsSet(reader);
                if ("parentPaths".equals(name)) {
                    parentPaths = data;
                } else if ("parentNodeNames".equals(name)) {
                    parentNodeNames = data;
                } else if ("parentNodeTypes".equals(name)) {
                    parentNodeTypes = data;
                } else if ("propertyNames".equals(name)) {
                    propertyNames = data;
                } else if ("allNodeTypes".equals(name)) {
                    allNodeTypes = data;
                }
            }
        } while (reader.matches(','));
        reader.read('}');
    }
    reader.read(JsopReader.END);
    return new ChangeSet(maxPathDepth, parentPaths, parentNodeNames, parentNodeTypes, propertyNames, allNodeTypes);
}
Also used : JsopReader(org.apache.jackrabbit.oak.commons.json.JsopReader) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer)

Example 14 with JsopTokenizer

use of org.apache.jackrabbit.oak.commons.json.JsopTokenizer in project jackrabbit-oak by apache.

the class AbstractBlobStoreTest method extractFiles.

public static void extractFiles(BlobStore store, String listingId, String target) throws IOException {
    String listing = new String(BlobStoreInputStream.readFully(store, listingId), "UTF-8");
    JsopTokenizer t = new JsopTokenizer(listing);
    File targetDir = new File(target);
    targetDir.mkdirs();
    t.read('{');
    if (!t.matches('}')) {
        do {
            String file = t.readString();
            t.read(':');
            String id = t.readString();
            byte[] data = BlobStoreInputStream.readFully(store, id);
            File outFile = new File(targetDir, file);
            outFile.getParentFile().mkdirs();
            FileOutputStream out = new FileOutputStream(outFile);
            try {
                out.write(data);
            } finally {
                out.close();
            }
        } while (t.matches(','));
    }
    t.read('}');
}
Also used : FileOutputStream(java.io.FileOutputStream) File(java.io.File) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer)

Example 15 with JsopTokenizer

use of org.apache.jackrabbit.oak.commons.json.JsopTokenizer in project jackrabbit-oak by apache.

the class BlobCollector method loadValue.

private void loadValue(String v, Collection<ReferencedBlob> blobs, String nodeId) {
    JsopReader reader = new JsopTokenizer(v);
    PropertyState p;
    if (reader.matches('[')) {
        p = DocumentPropertyState.readArrayProperty("x", nodeStore, reader);
        if (p.getType() == Type.BINARIES) {
            for (int i = 0; i < p.count(); i++) {
                Blob b = p.getValue(Type.BINARY, i);
                blobs.add(new ReferencedBlob(b, nodeId));
            }
        }
    } else {
        p = DocumentPropertyState.readProperty("x", nodeStore, reader);
        if (p.getType() == Type.BINARY) {
            Blob b = p.getValue(Type.BINARY);
            blobs.add(new ReferencedBlob(b, nodeId));
        }
    }
}
Also used : ReferencedBlob(org.apache.jackrabbit.oak.plugins.blob.ReferencedBlob) JsopReader(org.apache.jackrabbit.oak.commons.json.JsopReader) ReferencedBlob(org.apache.jackrabbit.oak.plugins.blob.ReferencedBlob) Blob(org.apache.jackrabbit.oak.api.Blob) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer) PropertyState(org.apache.jackrabbit.oak.api.PropertyState)

Aggregations

JsopTokenizer (org.apache.jackrabbit.oak.commons.json.JsopTokenizer)19 JsopReader (org.apache.jackrabbit.oak.commons.json.JsopReader)5 PropertyState (org.apache.jackrabbit.oak.api.PropertyState)3 JsonObject (org.apache.jackrabbit.oak.commons.json.JsonObject)3 Nonnull (javax.annotation.Nonnull)2 Blob (org.apache.jackrabbit.oak.api.Blob)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Node (javax.jcr.Node)1 Tree (org.apache.jackrabbit.oak.api.Tree)1 JsopBuilder (org.apache.jackrabbit.oak.commons.json.JsopBuilder)1 ReferencedBlob (org.apache.jackrabbit.oak.plugins.blob.ReferencedBlob)1 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)1 RDBJSONSupport.appendJsonString (org.apache.jackrabbit.oak.plugins.document.rdb.RDBJSONSupport.appendJsonString)1 Utils.unshareString (org.apache.jackrabbit.oak.plugins.document.util.Utils.unshareString)1