Example usage for org.json JSONTokener nextTo

List of usage examples for org.json JSONTokener nextTo

Introduction

In this page you can find the example usage for org.json JSONTokener nextTo.

Prototype

public String nextTo(String delimiters) 

Source Link

Document

Get the text up but not including one of the specified delimeter characters or the end of line, whichever comes first.

Usage

From source file:org.araqne.confdb.file.Importer.java

private void parseCollections(JSONTokener t, Manifest manifest, List<ConfigChange> configChanges)
        throws JSONException, ParseException, IOException {
    Object key = t.nextValue();// ww  w .  j a  v  a  2 s.  c om
    if (!key.equals("collections"))
        throw new ParseException("collections should be placed after metadata: token is " + key, -1);

    // "collections":{"COLNAME":["list",[...]]}
    t.nextClean(); // :
    t.nextClean(); // {

    if (t.nextClean() == '}')
        return;
    t.back();

    int i = 0;
    List<String> importColNames = new ArrayList<String>();
    while (true) {
        if (i++ != 0)
            t.nextClean();

        String colName = (String) t.nextValue();
        importColNames.add(colName);
        CollectionEntry collectionEntry = checkCollectionEntry(manifest, colName);
        manifest.add(collectionEntry);

        t.nextTo('[');
        t.nextClean();

        // type token (should be 'list')
        t.nextValue();
        t.nextTo("[");
        t.nextClean();

        // check empty config list
        char c = t.nextClean();
        if (c == ']') {
            t.nextClean(); // last ']'
            char marker = t.nextClean(); // ',' or '}'
            if (marker == '}')
                break;
            else
                t.back();

            continue;
        }

        t.back();

        int collectionId = collectionEntry.getId();
        RevLogWriter writer = null;
        try {
            File logFile = new File(db.getDbDirectory(), "col" + collectionId + ".log");
            File datFile = new File(db.getDbDirectory(), "col" + collectionId + ".dat");

            writer = new RevLogWriter(logFile, datFile);

            while (true) {
                @SuppressWarnings("unchecked")
                Object doc = removeType((List<Object>) parse((JSONArray) t.nextValue()));
                ConfigEntry configEntry = writeConfigEntry(writer, doc, collectionId);
                configChanges.add(new ConfigChange(CommitOp.CreateDoc, colName, collectionEntry.getId(),
                        configEntry.getDocId()));
                manifest.add(configEntry);

                // check next list item
                char delimiter = t.nextClean();
                if (delimiter == ']')
                    break;
            }
        } finally {
            if (writer != null)
                writer.close();
        }

        // end of list
        t.nextClean();

        char delimiter = t.nextClean();
        if (delimiter == '}')
            break;
    }

    for (String colName : db.getCollectionNames()) {
        if (importColNames.contains(colName))
            continue;
        configChanges.add(new ConfigChange(CommitOp.DropCol, colName, 0, 0));
        manifest.remove(new CollectionEntry(db.getCollectionId(colName), colName));
    }
}

From source file:org.official.json.CDL.java

/**
 * Get the next value. The value can be wrapped in quotes. The value can
 * be empty./*from  w ww. j  a  v  a2  s. c  o m*/
 * @param x A JSONTokener of the source text.
 * @return The value string, or null if empty.
 * @throws JSONException if the quoted string is badly formed.
 */
private static String getValue(JSONTokener x) throws JSONException {
    char c;
    char q;
    StringBuffer sb;
    do {
        c = x.next();
    } while (c == ' ' || c == '\t');
    switch (c) {
    case 0:
        return null;
    case '"':
    case '\'':
        q = c;
        sb = new StringBuffer();
        for (;;) {
            c = x.next();
            if (c == q) {
                break;
            }
            if (c == 0 || c == '\n' || c == '\r') {
                throw x.syntaxError("Missing close quote '" + q + "'.");
            }
            sb.append(c);
        }
        return sb.toString();
    case ',':
        x.back();
        return "";
    default:
        x.back();
        return x.nextTo(',');
    }
}

From source file:org.official.json.CookieList.java

/**
 * Convert a cookie list into a JSONObject. A cookie list is a sequence
 * of name/value pairs. The names are separated from the values by '='.
 * The pairs are separated by ';'. The names and the values
 * will be unescaped, possibly converting '+' and '%' sequences.
 *
 * To add a cookie to a cooklist,/*from  ww w  .ja  va2s . com*/
 * cookielistJSONObject.put(cookieJSONObject.getString("name"),
 *     cookieJSONObject.getString("value"));
 * @param string  A cookie list string
 * @return A JSONObject
 * @throws JSONException
 */
public static JSONObject toJSONObject(String string) throws JSONException {
    JSONObject jo = new JSONObject();
    JSONTokener x = new JSONTokener(string);
    while (x.more()) {
        String name = Cookie.unescape(x.nextTo('='));
        x.next('=');
        jo.put(name, Cookie.unescape(x.nextTo(';')));
        x.next();
    }
    return jo;
}

From source file:org.official.json.Cookie.java

/**
 * Convert a cookie specification string into a JSONObject. The string
 * will contain a name value pair separated by '='. The name and the value
 * will be unescaped, possibly converting '+' and '%' sequences. The
 * cookie properties may follow, separated by ';', also represented as
 * name=value (except the secure property, which does not have a value).
 * The name will be stored under the key "name", and the value will be
 * stored under the key "value". This method does not do checking or
 * validation of the parameters. It only converts the cookie string into
 * a JSONObject./*from   www.  j a v  a2 s  .co m*/
 * @param string The cookie specification string.
 * @return A JSONObject containing "name", "value", and possibly other
 *  members.
 * @throws JSONException
 */
public static JSONObject toJSONObject(String string) throws JSONException {
    String name;
    JSONObject jo = new JSONObject();
    Object value;
    JSONTokener x = new JSONTokener(string);
    jo.put("name", x.nextTo('='));
    x.next('=');
    jo.put("value", x.nextTo(';'));
    x.next();
    while (x.more()) {
        name = unescape(x.nextTo("=;"));
        if (x.next() != '=') {
            if (name.equals("secure")) {
                value = Boolean.TRUE;
            } else {
                throw x.syntaxError("Missing '=' in cookie parameter.");
            }
        } else {
            value = unescape(x.nextTo(';'));
            x.next();
        }
        jo.put(name, value);
    }
    return jo;
}