Search in sources :

Example 6 with JsopTokenizer

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

the class FacetResult method parseJson.

private void parseJson(String dimension, String jsonFacetString) {
    JsopTokenizer jsopTokenizer = new JsopTokenizer(jsonFacetString);
    List<Facet> facets = new LinkedList<Facet>();
    int c;
    String label = null;
    int count;
    while ((c = jsopTokenizer.read()) != JsopReader.END) {
        if (JsopReader.STRING == c) {
            label = jsopTokenizer.getEscapedToken();
        } else if (JsopReader.NUMBER == c) {
            count = Integer.parseInt(jsopTokenizer.getEscapedToken());
            if (label != null) {
                facets.add(new Facet(label, count));
            }
            label = null;
        }
    }
    perDimFacets.put(dimension, facets);
}
Also used : LinkedList(java.util.LinkedList) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer)

Example 7 with JsopTokenizer

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

the class QueryTest method parseJson.

private static JsonObject parseJson(String json) {
    JsopTokenizer t = new JsopTokenizer(json);
    t.read('{');
    return JsonObject.create(t);
}
Also used : JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer)

Example 8 with JsopTokenizer

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

the class JsonIndexCommand method execute.

void execute(String command) throws RepositoryException {
    JsopTokenizer t = new JsopTokenizer(command);
    t.read('{');
    JsonObject json = JsonObject.create(t);
    Map<String, String> properties = json.getProperties();
    if (properties.containsKey("if")) {
        Object value = getValueOrVariable(properties.get("if"));
        Object equals = getValueOrVariable(properties.get("="));
        if (value == null) {
            if (equals != null) {
                return;
            }
        } else if (!value.equals(equals)) {
            return;
        }
    }
    for (Entry<String, String> e : properties.entrySet()) {
        String k = e.getKey();
        Object value = getValueOrVariable(e.getValue());
        if ("addNode".equals(k)) {
            String nodePath = value.toString();
            String parent = PathUtils.getParentPath(nodePath);
            if (session.nodeExists(parent)) {
                Node p = session.getNode(parent);
                String nodeName = PathUtils.getName(nodePath);
                if (!p.hasNode(nodeName)) {
                    JsonObject node = json.getChildren().get("node");
                    addNode(p, nodeName, node);
                }
            }
        } else if ("removeNode".equals(k)) {
            String path = value.toString();
            if (session.nodeExists(path)) {
                session.getNode(path).remove();
            }
        } else if ("setProperty".equals(k)) {
            String itemPath = value.toString();
            String nodePath = PathUtils.getParentPath(itemPath);
            if (session.nodeExists(nodePath)) {
                String propertyName = PathUtils.getName(itemPath);
                Object propertyValue = getValueOrVariable(properties.get("value"));
                setProperty(session.getNode(nodePath), propertyName, propertyValue);
            }
        } else if ("session".equals(k)) {
            if ("save".equals(value)) {
                session.save();
            }
        } else if ("xpath".equals(k) || "sql".equals(k)) {
            String language = "xpath".equals(k) ? k : Query.JCR_SQL2;
            String columnName = "xpath".equals(k) ? "jcr:path" : null;
            boolean quiet = properties.containsKey("quiet");
            int depth = properties.containsKey("depth") ? Integer.parseInt(properties.get("depth")) : 0;
            runQuery(value.toString(), language, columnName, quiet, depth);
        } else if ("print".equals(k)) {
            output.println(value);
        } else if ("for".equals(k)) {
            String name = JsopTokenizer.decodeQuoted(properties.get(k));
            Object old = data.get(name);
            String[] commands = (String[]) getValueOrVariable(properties.get("do"));
            for (String x : (String[]) value) {
                data.put(name, x);
                for (String c : commands) {
                    execute(c);
                }
            }
            data.put(name, old);
        } else if ("loop".equals(k)) {
            while (true) {
                for (String c : (String[]) value) {
                    execute(c);
                    if (data.remove("$break") != null) {
                        return;
                    }
                }
            }
        } else if (k.startsWith("$")) {
            setVariable(properties, k, value);
        }
    }
}
Also used : Node(javax.jcr.Node) JsonObject(org.apache.jackrabbit.oak.commons.json.JsonObject) JsonObject(org.apache.jackrabbit.oak.commons.json.JsonObject) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer)

Example 9 with JsopTokenizer

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

the class DocumentNodeStoreHelper method loadValue.

private static void loadValue(String v, java.util.Collection<Blob> blobs, DocumentNodeStore nodeStore) {
    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(b);
            }
        }
    } else {
        p = DocumentPropertyState.readProperty("x", nodeStore, reader);
        if (p.getType() == Type.BINARY) {
            Blob b = p.getValue(Type.BINARY);
            blobs.add(b);
        }
    }
}
Also used : JsopReader(org.apache.jackrabbit.oak.commons.json.JsopReader) Blob(org.apache.jackrabbit.oak.api.Blob) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer) PropertyState(org.apache.jackrabbit.oak.api.PropertyState)

Example 10 with JsopTokenizer

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

the class LuceneJournalPropertyBuilder method addSerializedProperty.

//~---------------------------------< deserialize >
@Override
public void addSerializedProperty(@Nullable String json) {
    if (json == null || json.isEmpty()) {
        return;
    }
    //TODO Add support for overflow
    JsopReader reader = new JsopTokenizer(json);
    reader.read('{');
    if (!reader.matches('}')) {
        do {
            String path = reader.readString();
            reader.read(':');
            reader.read('[');
            for (boolean first = true; !reader.matches(']'); first = false) {
                if (!first) {
                    reader.read(',');
                }
                if (sizeWithinLimits()) {
                    indexedNodes.put(path, reader.readString());
                }
            }
        } while (reader.matches(','));
        reader.read('}');
    }
    reader.read(JsopReader.END);
}
Also used : JsopReader(org.apache.jackrabbit.oak.commons.json.JsopReader) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer)

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