Package com.threealike.life.thirdparty.org.json

Examples of com.threealike.life.thirdparty.org.json.JSONObject


  }
 
  public static final JSONObject getServiceConfig(String serviceLabel) {
    JSONArray services = getConfig().getJSONArray("services");
    for(int i=0; i < services.length(); i++) {
      JSONObject service = services.getJSONObject(i);
      if(service.getString("label").equals(serviceLabel)) {
        return service;
      }
    }
    return null;
  }
View Full Code Here


    InputStream resource = ClassLoader.getSystemResourceAsStream(configFileLabel+".possible");
    URL res = ClassLoader.getSystemResource(configFileLabel+".possible");
    System.out.println(" file="+(res == null ? "null" : res.getFile()));
   
    if(resource == null) return null;
    JSONObject possible = new JSONObject(resource, true);
   
    if(possible == null && appLabel == null)
      throw new RuntimeException("Could not load *.possible file for "+appLabel);
    if(possible == null) return null;
   
    System.out.print("(niche) loading "+configFileLabel);
    InputStream configResource = ClassLoader.getSystemResourceAsStream(configFileLabel);
    res = ClassLoader.getSystemResource(configFileLabel);
    System.out.println(" file="+(res == null ? "null" : res.getFile()));

//    if(configResource == null) return null;
//    JSONArray allActual = new JSONArray(configResource);
//    if(allActual == null)
//      allActual = new JSONArray();
    JSONArray allActual = null;
    if(configResource == null)
      allActual = new JSONArray("[{'apply':['"+MODE+"']}]");
    else
      allActual = new JSONArray(configResource, true);
   
    tmp = new JSONObject();
   
    for(int i=0; i < allActual.length(); i++) {
      JSONObject next = allActual.getJSONObject(i);
      if(!next.has("apply")) {
        throw new RuntimeException("required key, \"apply\" not found in config block");
      }
      JSONArray apply = next.getJSONArray("apply");
      inner:
      for(int j=0; j < apply.length(); j++) {
        if(apply.getString(j).equals(MODE)) {
//          if(CONF.get(appLabel) == null)
//            CONF.put(appLabel, next);
//          else {
            for(Iterator it = next.keys(); it.hasNext();) {
              String next1 = (String)it.next();
              tmp.put(next1, next.get(next1));
            }
//          }
          break inner;
        }
      }
    }
    /*
     * apply all default config keys (which are global across all modes)
     */
    for(Iterator it = possible.keys(); it.hasNext();) {
      String next = (String)it.next();
      JSONObject desc = possible.getJSONObject(next);
      if(desc.has("default")) {
        assign(null, possible, 0, explodeKey(next.split("\\.")));
      }
    }

    checked = new ArrayList<String>();
   
    validate(possible, tmp, 0);
   
    for(Iterator it = possible.keys(); it.hasNext();) {
      String next = (String)it.next();
      String parent = getParent(next);
      if(parent != null && !checked.contains(parent)) {
        continue;
      }
      JSONObject desc = possible.getJSONObject(next);
      boolean required = true;
      if(desc.has("required"))
        required = desc.getBoolean("required");
      if(required && !checked.contains(next))
        throw new RuntimeException("Key, \""+configFileLabel+"."+next+"\", is required");
    }
   
    return tmp;
View Full Code Here

    String sub = "";
    for(int i=0; i <= index; i++) {
      sub += key[i];
      if(i < index && !key[i+1].equals("[]")) sub += ".";
    }
    JSONObject desc = possible.getJSONObject(sub);
    Types type = Types.String;
    if(desc.has("type"))
      type = Types.valueOf(desc.getString("type"));
    Object apply = null;
    if(type == Types.Array) {
      if(parent == null) {
        if(tmp.has(key[index]))
          apply = tmp.getJSONArray(key[index]);
      } else if(parent instanceof JSONObject) {
        if(((JSONObject )parent).has(key[index])) {
          apply = ((JSONObject)parent).getJSONArray(key[index]);
        }
      } else if(parent instanceof JSONArray) {
        for(int i=0; i < ((JSONArray)parent).length(); i++) {
          if(((JSONArray)parent).get(i) instanceof JSONArray) {
            apply = ((JSONArray)parent).get(i);
            break;
          }
        }
      }
      if(apply == null)
        apply = new JSONArray();
    } else if(type == Types.Object) {
      if(parent == null) {
        if(tmp.has(key[index]))
          apply = tmp.getJSONObject(key[index]);
      } else if(parent instanceof JSONObject) {
        apply = ((JSONObject)parent).opt(key[index]);
      } else if(parent instanceof JSONArray) {
        for(int i=0; i < ((JSONArray)parent).length(); i++) {
          if(((JSONArray)parent).get(i) instanceof JSONObject && i == arrayIndex) {
            apply = ((JSONArray)parent).get(i);
            break;
          }
        }
      }
      if(apply == null)
        apply = new JSONObject();
    } else if(type == Types.String)
      apply = desc.get("default");
    else if(type == Types.Int) {
      apply = desc.get("default");
    } else if(type == Types.Boolean)
      apply = desc.get("default");
   
    if(apply == null)
      throw new RuntimeException("Could not determine type for key, \""+sub+"\"");
   
    if(parent == null) {
View Full Code Here

  private static final void validate(JSONObject possible, Object node, int level)
  {
    Types nextNodeType = null;
    Object nextNode = null;
    if(node instanceof JSONObject) {
      JSONObject _node = (JSONObject)node;
      for(Iterator it = _node.keys(); it.hasNext();) {
        String next = (String)it.next();
        if(next.equals("apply") && level == 0)
          continue;
        if(next.matches(".*[^a-zA-Z_0-9\\-$].*"))
          throw new RuntimeException("Bad characters found in key, \""+next+"\"");
        STACK[level] = next;
        nextNodeType = checkType(possible, (nextNode=_node.get(next)), level);
        if(nextNodeType == Types.Object || nextNodeType == Types.Array)
          validate(possible, nextNode, level+1);
      }
    }
    if(node instanceof JSONArray) {
      STACK[level] = "[]";
      JSONArray _node = (JSONArray)node;
      for(int i=0; i < _node.length(); i++) {
        nextNodeType = checkType(possible, (nextNode=_node.get(i)), level);
        if(nextNodeType == Types.Object || nextNodeType == Types.Array)
          validate(possible, nextNode, level+1);
      }
    }
  }
View Full Code Here

    if(!possible.has(sig))
      throw new RuntimeException("No config descriptor found for key, \""+sig+"\"");
   
    checked.add(sig);
   
    JSONObject descr = possible.getJSONObject(sig);
   
    Types type = Types.String;
    if(descr.has("type"))
      type = Types.valueOf(descr.getString("type"));
   
    boolean failed = false;
   
    if(type == Types.String) {
      if(!(node instanceof String))
View Full Code Here

TOP

Related Classes of com.threealike.life.thirdparty.org.json.JSONObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.