Example usage for org.json JSONObject sortedKeys

List of usage examples for org.json JSONObject sortedKeys

Introduction

In this page you can find the example usage for org.json JSONObject sortedKeys.

Prototype

public Iterator sortedKeys() 

Source Link

Document

Get an enumeration of the keys of the JSONObject.

Usage

From source file:com.ponysdk.generator.PropertiesDictionnaryGenerator.java

@SuppressWarnings("unchecked")
private void generateDictionnary() throws FileNotFoundException, JSONException {
    log.info("Generating " + fileName);

    final String pathToFile = srcGeneratedDirectory + "/" + GeneratorHelper.getDirectoryFromPackage(packageName)
            + "/";

    final TokenGenerator tokenGenerator = new TokenGenerator();

    final FileReader fileReader = new FileReader("src-core/main/resources/spec/propertiesDictionnary.json");
    final JSONTokener tokener = new JSONTokener(fileReader);

    final CodeWriter writer = new CodeWriter();
    writer.addLine("package " + packageName + ";");
    writer.addNewLine();/*from ww w  . j  a v  a 2s.  co m*/
    writer.addLine("public interface " + fileName + " {");
    writer.indentBlock();

    final JSONObject dico = new JSONObject(tokener);
    final Iterator<String> domainKeys = dico.sortedKeys();
    while (domainKeys.hasNext()) {
        // Domain
        final String domainKey = domainKeys.next();
        writer.addLine("public interface " + domainKey.toUpperCase() + " {");
        writer.indentBlock();

        final JSONObject domain = dico.getJSONObject(domainKey);
        final Iterator<String> keys = domain.sortedKeys();
        while (keys.hasNext()) {
            final String key = keys.next();
            final String keyUpper = GeneratorHelper.toUpperUnderscore(key);
            final JSONArray values = domain.getJSONArray(key);
            if (verbose)
                writer.addLine("public static final String " + keyUpper + " = \"" + key + "\";");
            else
                writer.addLine("public static final String " + keyUpper + " = \"" + tokenGenerator.nextToken()
                        + "\";");

            if (values.length() > 0) {
                writer.addLine("public interface " + keyUpper + "_" + " {");
                writer.indentBlock();
                for (int i = 0; i < values.length(); i++) {
                    if (verbose)
                        writer.addLine("public static final String "
                                + GeneratorHelper.toUpperUnderscore(values.getString(i)) + " = \""
                                + values.getString(i) + "\";");
                    else
                        writer.addLine("public static final String "
                                + GeneratorHelper.toUpperUnderscore(values.getString(i)) + " = \""
                                + tokenGenerator.nextToken() + "\";");
                }
                writer.unindentBlock();
                writer.addLine("}");
            }
        }

        writer.unindentBlock();
        writer.addLine("}");
        writer.addNewLine();
    }

    writer.unindentBlock();
    writer.addLine("}");

    final File file = new File(pathToFile);
    if (!file.exists())
        file.mkdirs();

    writer.saveToFile(pathToFile + fileName + ".java");
}