Ejemplo n.º 1
0
 /*package*/
 static PagableResponseList<UserList> createPagableUserListList(
     HttpResponse res, Configuration conf) throws TwitterException {
   try {
     if (conf.isJSONStoreEnabled()) {
       TwitterObjectFactory.clearThreadLocalMap();
     }
     JSONObject json = res.asJSONObject();
     JSONArray list = json.getJSONArray("lists");
     int size = list.length();
     PagableResponseList<UserList> users = new PagableResponseListImpl<UserList>(size, json, res);
     for (int i = 0; i < size; i++) {
       JSONObject userListJson = list.getJSONObject(i);
       UserList userList = new UserListJSONImpl(userListJson);
       users.add(userList);
       if (conf.isJSONStoreEnabled()) {
         TwitterObjectFactory.registerJSONObject(userList, userListJson);
       }
     }
     if (conf.isJSONStoreEnabled()) {
       TwitterObjectFactory.registerJSONObject(users, json);
     }
     return users;
   } catch (JSONException jsone) {
     throw new TwitterException(jsone);
   }
 }
Ejemplo n.º 2
0
 public static String getZipcode(JSONObject data) {
   try {
     JSONArray results = data.getJSONArray("results");
     JSONObject address = results.getJSONObject(0);
     JSONArray address_components = address.getJSONArray("address_components");
     for (int i = 0; i < address_components.size(); i++) {
       JSONObject postal_code = address_components.getJSONObject(i);
       JSONArray types = postal_code.getJSONArray("types");
       if (types.getString(0).equals("postal_code")) {
         return (String) postal_code.get("long_name");
       }
     }
     return "0";
   } catch (NullPointerException npe) {
     npe.printStackTrace();
   }
   return null;
 }
Ejemplo n.º 3
0
 /**
  * Private method helps to generate the viewport of the given JSONObject
  *
  * @param data the JSONObject
  * @return the viewport for parsing the bound, null if the parsing fails.
  */
 private static JSONObject getViewport(JSONObject data) {
   try {
     JSONArray results = data.getJSONArray("results");
     JSONObject address = results.getJSONObject(0);
     JSONObject geometry = (JSONObject) address.get("geometry");
     JSONObject viewport = (JSONObject) geometry.get("viewport");
     return viewport;
   } catch (NullPointerException npe) {
     npe.printStackTrace();
   }
   return null;
 }
Ejemplo n.º 4
0
 /**
  * Private method helps to generate the location of the given JSONObject
  *
  * @param data the JSONObject
  * @return the viewport for parsing the bound, null if the parsing fails.
  */
 private static JSONObject getLocationObj(JSONObject data) {
   try {
     JSONArray results = data.getJSONArray("results");
     JSONObject address = results.getJSONObject(0);
     JSONObject geometry = (JSONObject) address.get("geometry");
     JSONObject location = (JSONObject) geometry.get("location");
     return location;
   } catch (NullPointerException npe) {
     npe.printStackTrace();
   }
   return null;
 }
Ejemplo n.º 5
0
  /**
   * Get the formated address from the JSONObject. If the JSONObject is not the given format, the
   * method would probably throw a NullPointerException.
   *
   * @param data the JSONObject
   * @return the formatted address in the string format, null if the parsing fails.
   */
  public static String getFormattedAddress(JSONObject data) {
    try {
      JSONArray results = data.getJSONArray("results");

      JSONObject address = results.getJSONObject(0);
      String formatted_address = (String) address.get("formatted_address");
      return formatted_address;
    } catch (NullPointerException npe) {
      npe.printStackTrace();
    }
    return null;
  }
 /**
  * Deserializa el bean en formato JSON
  *
  * @param json objeto que contiene los datos del bean en formato JSON
  * @throws es.tid.serial.json.JSONException Cuando se produce un error en la deserialización del
  *     objeto JSON
  */
 public void fromJSON(JSONObject json) throws JSONException {
   JSONArray jArray;
   JSONArray jObjectArray;
   jObjectArray = json.getJSONArray("literalProperties");
   this.clearLiteralProperties();
   for (int i = 0; i < jObjectArray.length(); i++) {
     JSONObject jObj_i = jObjectArray.getJSONObject(i);
     org.qualipso.advdoc.ws.client.retrieval.beans.LiteralProperty tmp =
         new org.qualipso.advdoc.ws.client.retrieval.beans.LiteralProperty();
     tmp.fromJSON(jObj_i);
     this.addLiteralProperties(tmp);
   }
   jObjectArray = json.getJSONArray("objectProperties");
   this.clearObjectProperties();
   for (int i = 0; i < jObjectArray.length(); i++) {
     JSONObject jObj_i = jObjectArray.getJSONObject(i);
     org.qualipso.advdoc.ws.client.retrieval.beans.ObjectProperty tmp =
         new org.qualipso.advdoc.ws.client.retrieval.beans.ObjectProperty();
     tmp.fromJSON(jObj_i);
     this.addObjectProperties(tmp);
   }
 }
 /**
  * Deserializa el bean en formato JSON
  *
  * @param json objeto que contiene los datos del bean en formato JSON
  * @throws es.tid.serial.json.JSONException Cuando se produce un error en la deserialización del
  *     objeto JSON
  */
 public void fromJSON(JSONObject json) throws JSONException {
   JSONArray jArray;
   JSONArray jObjectArray;
   jObjectArray = json.getJSONArray("subproperties");
   this.clearSubproperties();
   for (int i = 0; i < jObjectArray.length(); i++) {
     JSONObject jObj_i = jObjectArray.getJSONObject(i);
     org.qualipso.advdoc.ws.client.metadata.beans.ResourceElement tmp =
         new org.qualipso.advdoc.ws.client.metadata.beans.ResourceElement();
     tmp.fromJSON(jObj_i);
     this.addSubproperties(tmp);
   }
 }
 private void init(JSONObject jsonobject) throws TwitterException {
   try {
     JSONArray jsonarray = jsonobject.getJSONArray("indices");
     setStart(jsonarray.getInt(0));
     setEnd(jsonarray.getInt(1));
     if (!jsonobject.isNull("text")) {
       text = jsonobject.getString("text");
     }
     return;
   }
   // Misplaced declaration of an exception variable
   catch (JSONObject jsonobject) {
     throw new TwitterException(jsonobject);
   }
 }
  private void init(JSONObject json) throws TwitterException {
    try {
      JSONArray indicesArray = json.getJSONArray("indices");
      setStart(indicesArray.getInt(0));
      setEnd(indicesArray.getInt(1));

      if (!json.isNull("name")) {
        this.name = json.getString("name");
      }
      if (!json.isNull("screen_name")) {
        this.screenName = json.getString("screen_name");
      }
      id = ParseUtil.getLong("id", json);
    } catch (JSONException jsone) {
      throw new TwitterException(jsone);
    }
  }
Ejemplo n.º 10
0
  //
  // ONE TIME ADD VIOLATIONS
  //
  //
  private static void oneTimeAddViolations() {

    String jsonString = "";

    try {
      jsonString = readFile("./json/violations.json", StandardCharsets.UTF_8);
    } catch (IOException e) {
      System.out.println(e);
    }

    try {
      JSONObject rootObject = new JSONObject(jsonString); // Parse the JSON to a JSONObject
      JSONArray rows = rootObject.getJSONArray("stuff"); // Get all JSONArray rows

      System.out.println("row lengths: " + rows.length());

      for (int j = 0; j < rows.length(); j++) { // Iterate each element in the elements array
        JSONObject element = rows.getJSONObject(j); // Get the element object

        int id = element.getInt("id");
        int citationNumber = element.getInt("citation_number");
        String violationNumber = element.getString("violation_number");
        String violationDescription = element.getString("violation_description");
        String warrantStatus = element.getString("warrant_status");
        String warrantNumber = " ";
        Boolean isWarrantNumberNull = element.isNull("warrant_number");
        if (!isWarrantNumberNull) warrantNumber = element.getString("warrant_number");
        String status = element.getString("status");
        String statusDate = element.getString("status_date");
        String fineAmount = " ";
        Boolean isFineAmountNull = element.isNull("fine_amount");
        if (!isFineAmountNull) fineAmount = element.getString("fine_amount");
        String courtCost = " ";
        Boolean isCourtCostNull = element.isNull("court_cost");
        if (!isCourtCostNull) courtCost = element.getString("court_cost");
        /*
        Map<String, AttributeValue> item = newViolationItem(citationNumber, violationNumber, violationDescription, warrantStatus, warrantNumber, status, statusDate, fineAmount, courtCost);
        PutItemRequest putItemRequest = new PutItemRequest("violations-table", item);
        PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
        */
      }
    } catch (JSONException e) {
      // JSON Parsing error
      e.printStackTrace();
    }
  }
Ejemplo n.º 11
0
  public void Json2java(String str) {
    try {

      JSONObject jar = JSONObject.fromObject(str);
      JSONArray array = jar.getJSONArray("books");
      json_length = array.size();
      System.out.println(json_length);

      bklist = new ArrayList<DouBan_Book>();
      for (int i = 0; i < json_length; i++) {
        System.out.print(i + "¡¢");
        System.out.println(array.get(i));
        jsonobj = array.getJSONObject(i);
        DouBan_Book bk = new DouBan_Book();
        bk.setId(jsonobj.getString("id"));
        bk.setTitle(jsonobj.getString("title"));
        bk.setIsbn10(jsonobj.getString("isbn10"));
        // bk.setIsbn13(jsonobj.getString("isbn13"));
        bk.setImagePath(jsonobj.getString("image"));
        bk.setUrl(jsonobj.getString("url"));
        bk.setAuthor(jsonobj.getString("author"));
        bk.setBinding(jsonobj.getString("binding"));
        bk.setPages(jsonobj.getString("pages"));
        bk.setPrice(jsonobj.getString("price"));
        bk.setPubdate(jsonobj.getString("pubdate"));
        bk.setPublisher(jsonobj.getString("publisher"));
        bk.setSummary(jsonobj.getString("summary"));
        // bklist=new  ArrayList<DouBan_Book>();
        bklist.add(bk);
      }

    } catch (Exception r) {
      r.printStackTrace();
    }
    System.out.println(bklist.size());
  }
Ejemplo n.º 12
0
 /**
  * Read recursively the JSON object in order to parse the content.
  *
  * @param jsonObj JSON object.
  * @param data Holds the parsed data.
  * @return Parsed data.
  * @throws ParserException If there is an error in the document format.
  */
 protected Hashtable readJSON(JSONObject jsonObj, Hashtable data) throws ParserException {
   Enumeration keys = jsonObj.keys();
   //
   while (keys.hasMoreElements()) {
     String key = keys.nextElement().toString();
     //
     if (isMember(jsonObj, key)) {
       data.put(key, readJSON(jsonObj.getJSONObject(key), new Hashtable()));
     } else if (isArray(jsonObj, key)) {
       JSONArray array = jsonObj.getJSONArray(key);
       Hashtable[] arrayObj = new Hashtable[array.length()];
       //
       for (int i = 0; i < arrayObj.length; i++) {
         arrayObj[i] = readJSON(array.getJSONObject(i), new Hashtable());
       }
       //
       data.put(key, arrayObj);
     } else {
       data.put(key, jsonObj.getString(key));
     }
   }
   //
   return data;
 }
Ejemplo n.º 13
0
  //
  // ONE TIME ADD WARRANTS
  //
  //
  private static void oneTimeAddWarrants() {

    String jsonString = "";

    try {
      jsonString = readFile("./json/warrants.json", StandardCharsets.UTF_8);
    } catch (IOException e) {
      System.out.println(e);
    }

    try {
      JSONObject rootObject = new JSONObject(jsonString); // Parse the JSON to a JSONObject
      JSONArray rows = rootObject.getJSONArray("stuff"); // Get all JSONArray rows

      // System.out.println("row lengths: " + rows.length());

      set2 = new HashSet<>();

      for (int j = 0; j < rows.length(); j++) { // Iterate each element in the elements array
        JSONObject element = rows.getJSONObject(j); // Get the element object

        String defendant = element.getString("Defendant");

        String strarr[] = defendant.split(" ");
        String temp = strarr[1];
        int len = strarr[0].length();
        strarr[0] = strarr[0].substring(0, len - 1);
        strarr[1] = strarr[0];
        strarr[0] = temp;

        String firstLast = strarr[0] + strarr[1];
        firstLast = firstLast.toLowerCase();

        set2.add(firstLast);
        // System.out.println(firstLast);

        int zipCode = 0;
        Boolean isZipCodeNull = element.isNull("ZIP Code");
        if (!isZipCodeNull) zipCode = element.getInt("ZIP Code");
        String dob = element.getString("Date of Birth");
        String caseNumber = element.getString("Case Number");

        String firstLastDOB = firstLast + dob;

        // pick a ssn from list
        String ssn = ssnList.get(ssnCounter);
        ssnCounter--;
        ssnHashMap.put(firstLast, ssn);

        // compute salt
        final Random ran = new SecureRandom();
        byte[] salt = new byte[32];
        ran.nextBytes(salt);
        String saltString = Base64.encodeBase64String(salt);

        // System.out.println("saltstring: " + saltString);
        saltHashMap.put(firstLast, saltString);

        // compute ripemd160 hash of ssn + salt
        String saltPlusSsn = saltString + ssn;
        // System.out.println("salt plus ssn: " + saltPlusSsn);

        String resultingHash = "";
        try {
          byte[] r = saltPlusSsn.getBytes("US-ASCII");
          RIPEMD160Digest d = new RIPEMD160Digest();
          d.update(r, 0, r.length);
          byte[] o = new byte[d.getDigestSize()];
          d.doFinal(o, 0);
          ByteArrayOutputStream baos = new ByteArrayOutputStream(40);
          Hex.encode(o, baos);
          resultingHash = new String(baos.toByteArray(), StandardCharsets.UTF_8);

          hashedssnHashMap.put(firstLast, resultingHash);
        } catch (UnsupportedEncodingException e) {
          System.out.println(e);
        } catch (IOException i) {
          System.out.println(i);
        }

        // compareNames();

        Map<String, AttributeValue> item =
            newWarrantItem(
                firstLast, firstLastDOB, resultingHash, defendant, zipCode, dob, caseNumber);
        PutItemRequest putItemRequest = new PutItemRequest("warrants-table", item);
        PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
      }
    } catch (JSONException e) {
      // JSON Parsing error
      e.printStackTrace();
    }
  }
Ejemplo n.º 14
0
  /**
   * Entry point.
   *
   * @param args
   */
  public static void main(String args[]) {
    Iterator it;
    JSONArray a;
    JSONObject j;
    JSONStringer jj;
    String s;

    /**
     * Obj is a typical class that implements JSONString. It also provides some beanie methods that
     * can be used to construct a JSONObject. It also demonstrates constructing a JSONObject with an
     * array of names.
     */
    class Obj implements JSONString {
      public String aString;
      public double aNumber;
      public boolean aBoolean;

      public Obj(String string, double n, boolean b) {
        this.aString = string;
        this.aNumber = n;
        this.aBoolean = b;
      }

      public double getNumber() {
        return this.aNumber;
      }

      public String getString() {
        return this.aString;
      }

      public boolean isBoolean() {
        return this.aBoolean;
      }

      public String getBENT() {
        return "All uppercase key";
      }

      public String getX() {
        return "x";
      }

      public String toJSONString() {
        return "{"
            + JSONObject.quote(this.aString)
            + ":"
            + JSONObject.doubleToString(this.aNumber)
            + "}";
      }

      public String toString() {
        return this.getString()
            + " "
            + this.getNumber()
            + " "
            + this.isBoolean()
            + "."
            + this.getBENT()
            + " "
            + this.getX();
      }
    }

    Obj obj = new Obj("A beany object", 42, true);

    try {
      j =
          XML.toJSONObject(
              "<![CDATA[This is a collection of test patterns and examples for org.json.]]>  Ignore the stuff past the end.  ");
      System.out.println(j.toString());

      s = "{     \"list of lists\" : [         [1, 2, 3],         [4, 5, 6],     ] }";
      j = new JSONObject(s);
      System.out.println(j.toString(4));
      System.out.println(XML.toString(j));

      s =
          "<recipe name=\"bread\" prep_time=\"5 mins\" cook_time=\"3 hours\"> <title>Basic bread</title> <ingredient amount=\"8\" unit=\"dL\">Flour</ingredient> <ingredient amount=\"10\" unit=\"grams\">Yeast</ingredient> <ingredient amount=\"4\" unit=\"dL\" state=\"warm\">Water</ingredient> <ingredient amount=\"1\" unit=\"teaspoon\">Salt</ingredient> <instructions> <step>Mix all ingredients together.</step> <step>Knead thoroughly.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Knead again.</step> <step>Place in a bread baking tin.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Bake in the oven at 180(degrees)C for 30 minutes.</step> </instructions> </recipe> ";
      j = XML.toJSONObject(s);
      System.out.println(j.toString(4));
      System.out.println();

      j = JSONML.toJSONObject(s);
      System.out.println(j.toString());
      System.out.println(JSONML.toString(j));
      System.out.println();

      a = JSONML.toJSONArray(s);
      System.out.println(a.toString(4));
      System.out.println(JSONML.toString(a));
      System.out.println();

      s =
          "<div id=\"demo\" class=\"JSONML\"><p>JSONML is a transformation between <b>JSON</b> and <b>XML</b> that preserves ordering of document features.</p><p>JSONML can work with JSON arrays or JSON objects.</p><p>Three<br/>little<br/>words</p></div>";
      j = JSONML.toJSONObject(s);
      System.out.println(j.toString(4));
      System.out.println(JSONML.toString(j));
      System.out.println();

      a = JSONML.toJSONArray(s);
      System.out.println(a.toString(4));
      System.out.println(JSONML.toString(a));
      System.out.println();

      s =
          "<person created=\"2006-11-11T19:23\" modified=\"2006-12-31T23:59\">\n <firstName>Robert</firstName>\n <lastName>Smith</lastName>\n <address type=\"home\">\n <street>12345 Sixth Ave</street>\n <city>Anytown</city>\n <state>CA</state>\n <postalCode>98765-4321</postalCode>\n </address>\n </person>";
      j = XML.toJSONObject(s);
      System.out.println(j.toString(4));

      j = new JSONObject(obj);
      System.out.println(j.toString());

      s =
          "{ \"entity\": { \"imageURL\": \"\", \"name\": \"IXXXXXXXXXXXXX\", \"id\": 12336, \"ratingCount\": null, \"averageRating\": null } }";
      j = new JSONObject(s);
      System.out.println(j.toString(2));

      jj = new JSONStringer();
      s =
          jj.object()
              .key("single")
              .value("MARIE HAA'S")
              .key("Johnny")
              .value("MARIE HAA\\'S")
              .key("foo")
              .value("bar")
              .key("baz")
              .array()
              .object()
              .key("quux")
              .value("Thanks, Josh!")
              .endObject()
              .endArray()
              .key("obj keys")
              .value(JSONObject.getNames(obj))
              .endObject()
              .toString();
      System.out.println(s);

      System.out.println(
          new JSONStringer()
              .object()
              .key("a")
              .array()
              .array()
              .array()
              .value("b")
              .endArray()
              .endArray()
              .endArray()
              .endObject()
              .toString());

      jj = new JSONStringer();
      jj.array();
      jj.value(1);
      jj.array();
      jj.value(null);
      jj.array();
      jj.object();
      jj.key("empty-array").array().endArray();
      jj.key("answer").value(42);
      jj.key("null").value(null);
      jj.key("false").value(false);
      jj.key("true").value(true);
      jj.key("big").value(123456789e+88);
      jj.key("small").value(123456789e-88);
      jj.key("empty-object").object().endObject();
      jj.key("long");
      jj.value(9223372036854775807L);
      jj.endObject();
      jj.value("two");
      jj.endArray();
      jj.value(true);
      jj.endArray();
      jj.value(98.6);
      jj.value(-100.0);
      jj.object();
      jj.endObject();
      jj.object();
      jj.key("one");
      jj.value(1.00);
      jj.endObject();
      jj.value(obj);
      jj.endArray();
      System.out.println(jj.toString());

      System.out.println(new JSONArray(jj.toString()).toString(4));

      int ar[] = {1, 2, 3};
      JSONArray ja = new JSONArray(ar);
      System.out.println(ja.toString());

      String sa[] = {"aString", "aNumber", "aBoolean"};
      j = new JSONObject(obj, sa);
      j.put("Testing JSONString interface", obj);
      System.out.println(j.toString(4));

      j =
          new JSONObject(
              "{slashes: '///', closetag: '</script>', backslash:'\\\\', ei: {quotes: '\"\\''},eo: {a: '\"quoted\"', b:\"don't\"}, quotes: [\"'\", '\"']}");
      System.out.println(j.toString(2));
      System.out.println(XML.toString(j));
      System.out.println("");

      j =
          new JSONObject(
              "{foo: [true, false,9876543210,    0.0, 1.00000001,  1.000000000001, 1.00000000000000001,"
                  + " .00000000000000001, 2.00, 0.1, 2e100, -32,[],{}, \"string\"], "
                  + "  to   : null, op : 'Good',"
                  + "ten:10} postfix comment");
      j.put("String", "98.6");
      j.put("JSONObject", new JSONObject());
      j.put("JSONArray", new JSONArray());
      j.put("int", 57);
      j.put("double", 123456789012345678901234567890.);
      j.put("true", true);
      j.put("false", false);
      j.put("null", JSONObject.NULL);
      j.put("bool", "true");
      j.put("zero", -0.0);
      j.put("\\u2028", "\u2028");
      j.put("\\u2029", "\u2029");
      a = j.getJSONArray("foo");
      a.put(666);
      a.put(2001.99);
      a.put("so \"fine\".");
      a.put("so <fine>.");
      a.put(true);
      a.put(false);
      a.put(new JSONArray());
      a.put(new JSONObject());
      j.put("keys", JSONObject.getNames(j));
      System.out.println(j.toString(4));
      System.out.println(XML.toString(j));

      System.out.println("String: " + j.getDouble("String"));
      System.out.println("  bool: " + j.getBoolean("bool"));
      System.out.println("    to: " + j.getString("to"));
      System.out.println("  true: " + j.getString("true"));
      System.out.println("   foo: " + j.getJSONArray("foo"));
      System.out.println("    op: " + j.getString("op"));
      System.out.println("   ten: " + j.getInt("ten"));
      System.out.println("  oops: " + j.optBoolean("oops"));

      s =
          "<xml one = 1 two=' \"2\" '><five></five>First \u0009&lt;content&gt;<five></five> This is \"content\". <three>  3  </three>JSON does not preserve the sequencing of elements and contents.<three>  III  </three>  <three>  T H R E E</three><four/>Content text is an implied structure in XML. <six content=\"6\"/>JSON does not have implied structure:<seven>7</seven>everything is explicit.<![CDATA[CDATA blocks<are><supported>!]]></xml>";
      j = XML.toJSONObject(s);
      System.out.println(j.toString(2));
      System.out.println(XML.toString(j));
      System.out.println("");

      ja = JSONML.toJSONArray(s);
      System.out.println(ja.toString(4));
      System.out.println(JSONML.toString(ja));
      System.out.println("");

      s =
          "<xml do='0'>uno<a re='1' mi='2'>dos<b fa='3'/>tres<c>true</c>quatro</a>cinqo<d>seis<e/></d></xml>";
      ja = JSONML.toJSONArray(s);
      System.out.println(ja.toString(4));
      System.out.println(JSONML.toString(ja));
      System.out.println("");

      s =
          "<mapping><empty/>   <class name = \"Customer\">      <field name = \"ID\" type = \"string\">         <bind-xml name=\"ID\" node=\"attribute\"/>      </field>      <field name = \"FirstName\" type = \"FirstName\"/>      <field name = \"MI\" type = \"MI\"/>      <field name = \"LastName\" type = \"LastName\"/>   </class>   <class name = \"FirstName\">      <field name = \"text\">         <bind-xml name = \"text\" node = \"text\"/>      </field>   </class>   <class name = \"MI\">      <field name = \"text\">         <bind-xml name = \"text\" node = \"text\"/>      </field>   </class>   <class name = \"LastName\">      <field name = \"text\">         <bind-xml name = \"text\" node = \"text\"/>      </field>   </class></mapping>";
      j = XML.toJSONObject(s);

      System.out.println(j.toString(2));
      System.out.println(XML.toString(j));
      System.out.println("");
      ja = JSONML.toJSONArray(s);
      System.out.println(ja.toString(4));
      System.out.println(JSONML.toString(ja));
      System.out.println("");

      j =
          XML.toJSONObject(
              "<?xml version=\"1.0\" ?><Book Author=\"Anonymous\"><Title>Sample Book</Title><Chapter id=\"1\">This is chapter 1. It is not very long or interesting.</Chapter><Chapter id=\"2\">This is chapter 2. Although it is longer than chapter 1, it is not any more interesting.</Chapter></Book>");
      System.out.println(j.toString(2));
      System.out.println(XML.toString(j));
      System.out.println("");

      j =
          XML.toJSONObject(
              "<!DOCTYPE bCard 'http://www.cs.caltech.edu/~adam/schemas/bCard'><bCard><?xml default bCard        firstname = ''        lastname  = '' company   = '' email = '' homepage  = ''?><bCard        firstname = 'Rohit'        lastname  = 'Khare'        company   = 'MCI'        email     = '*****@*****.**'        homepage  = 'http://pest.w3.org/'/><bCard        firstname = 'Adam'        lastname  = 'Rifkin'        company   = 'Caltech Infospheres Project'        email     = '*****@*****.**'        homepage  = 'http://www.cs.caltech.edu/~adam/'/></bCard>");
      System.out.println(j.toString(2));
      System.out.println(XML.toString(j));
      System.out.println("");

      j =
          XML.toJSONObject(
              "<?xml version=\"1.0\"?><customer>    <firstName>        <text>Fred</text>    </firstName>    <ID>fbs0001</ID>    <lastName> <text>Scerbo</text>    </lastName>    <MI>        <text>B</text>    </MI></customer>");
      System.out.println(j.toString(2));
      System.out.println(XML.toString(j));
      System.out.println("");

      j =
          XML.toJSONObject(
              "<!ENTITY tp-address PUBLIC '-//ABC University::Special Collections Library//TEXT (titlepage: name and address)//EN' 'tpspcoll.sgm'><list type='simple'><head>Repository Address </head><item>Special Collections Library</item><item>ABC University</item><item>Main Library, 40 Circle Drive</item><item>Ourtown, Pennsylvania</item><item>17654 USA</item></list>");
      System.out.println(j.toString());
      System.out.println(XML.toString(j));
      System.out.println("");

      j =
          XML.toJSONObject(
              "<test intertag status=ok><empty/>deluxe<blip sweet=true>&amp;&quot;toot&quot;&toot;&#x41;</blip><x>eks</x><w>bonus</w><w>bonus2</w></test>");
      System.out.println(j.toString(2));
      System.out.println(XML.toString(j));
      System.out.println("");

      j =
          HTTP.toJSONObject(
              "GET / HTTP/1.0\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\nAccept-Language: en-us\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90; T312461; Q312461)\nHost: www.nokko.com\nConnection: keep-alive\nAccept-encoding: gzip, deflate\n");
      System.out.println(j.toString(2));
      System.out.println(HTTP.toString(j));
      System.out.println("");

      j =
          HTTP.toJSONObject(
              "HTTP/1.1 200 Oki Doki\nDate: Sun, 26 May 2002 17:38:52 GMT\nServer: Apache/1.3.23 (Unix) mod_perl/1.26\nKeep-Alive: timeout=15, max=100\nConnection: Keep-Alive\nTransfer-Encoding: chunked\nContent-Type: text/html\n");
      System.out.println(j.toString(2));
      System.out.println(HTTP.toString(j));
      System.out.println("");

      j =
          new JSONObject(
              "{nix: null, nux: false, null: 'null', 'Request-URI': '/', Method: 'GET', 'HTTP-Version': 'HTTP/1.0'}");
      System.out.println(j.toString(2));
      System.out.println("isNull: " + j.isNull("nix"));
      System.out.println("   has: " + j.has("nix"));
      System.out.println(XML.toString(j));
      System.out.println(HTTP.toString(j));
      System.out.println("");

      j =
          XML.toJSONObject(
              "<?xml version='1.0' encoding='UTF-8'?>"
                  + "\n\n"
                  + "<SOAP-ENV:Envelope"
                  + " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\""
                  + " xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\""
                  + " xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">"
                  + "<SOAP-ENV:Body><ns1:doGoogleSearch"
                  + " xmlns:ns1=\"urn:GoogleSearch\""
                  + " SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
                  + "<key xsi:type=\"xsd:string\">GOOGLEKEY</key> <q"
                  + " xsi:type=\"xsd:string\">'+search+'</q> <start"
                  + " xsi:type=\"xsd:int\">0</start> <maxResults"
                  + " xsi:type=\"xsd:int\">10</maxResults> <filter"
                  + " xsi:type=\"xsd:boolean\">true</filter> <restrict"
                  + " xsi:type=\"xsd:string\"></restrict> <safeSearch"
                  + " xsi:type=\"xsd:boolean\">false</safeSearch> <lr"
                  + " xsi:type=\"xsd:string\"></lr> <ie"
                  + " xsi:type=\"xsd:string\">latin1</ie> <oe"
                  + " xsi:type=\"xsd:string\">latin1</oe>"
                  + "</ns1:doGoogleSearch>"
                  + "</SOAP-ENV:Body></SOAP-ENV:Envelope>");
      System.out.println(j.toString(2));
      System.out.println(XML.toString(j));
      System.out.println("");

      j =
          new JSONObject(
              "{Envelope: {Body: {\"ns1:doGoogleSearch\": {oe: \"latin1\", filter: true, q: \"'+search+'\", key: \"GOOGLEKEY\", maxResults: 10, \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\", start: 0, ie: \"latin1\", safeSearch:false, \"xmlns:ns1\": \"urn:GoogleSearch\"}}}}");
      System.out.println(j.toString(2));
      System.out.println(XML.toString(j));
      System.out.println("");

      j = CookieList.toJSONObject("  f%oo = b+l=ah  ; o;n%40e = t.wo ");
      System.out.println(j.toString(2));
      System.out.println(CookieList.toString(j));
      System.out.println("");

      j = Cookie.toJSONObject("f%oo=blah; secure ;expires = April 24, 2002");
      System.out.println(j.toString(2));
      System.out.println(Cookie.toString(j));
      System.out.println("");

      j =
          new JSONObject(
              "{script: 'It is not allowed in HTML to send a close script tag in a string<script>because it confuses browsers</script>so we insert a backslash before the /'}");
      System.out.println(j.toString());
      System.out.println("");

      JSONTokener jt =
          new JSONTokener("{op:'test', to:'session', pre:1}{op:'test', to:'session', pre:2}");
      j = new JSONObject(jt);
      System.out.println(j.toString());
      System.out.println("pre: " + j.optInt("pre"));
      int i = jt.skipTo('{');
      System.out.println(i);
      j = new JSONObject(jt);
      System.out.println(j.toString());
      System.out.println("");

      a =
          CDL.toJSONArray(
              "Comma delimited list test, '\"Strip\"Quotes', 'quote, comma', No quotes, 'Single Quotes', \"Double Quotes\"\n1,'2',\"3\"\n,'It is \"good,\"', \"It works.\"\n\n");

      s = CDL.toString(a);
      System.out.println(s);
      System.out.println("");
      System.out.println(a.toString(4));
      System.out.println("");
      a = CDL.toJSONArray(s);
      System.out.println(a.toString(4));
      System.out.println("");

      a = new JSONArray(" [\"<escape>\", next is an implied null , , ok,] ");
      System.out.println(a.toString());
      System.out.println("");
      System.out.println(XML.toString(a));
      System.out.println("");

      j =
          new JSONObject(
              "{ fun => with non-standard forms ; forgiving => This package can be used to parse formats that are similar to but not stricting conforming to JSON; why=To make it easier to migrate existing data to JSON,one = [[1.00]]; uno=[[{1=>1}]];'+':+6e66 ;pluses=+++;empty = '' , 'double':0.666,true: TRUE, false: FALSE, null=NULL;[true] = [[!,@;*]]; string=>  o. k. ; \r oct=0666; hex=0x666; dec=666; o=0999; noh=0x0x}");
      System.out.println(j.toString(4));
      System.out.println("");
      if (j.getBoolean("true") && !j.getBoolean("false")) {
        System.out.println("It's all good");
      }

      System.out.println("");
      j = new JSONObject(j, new String[] {"dec", "oct", "hex", "missing"});
      System.out.println(j.toString(4));

      System.out.println("");
      System.out.println(new JSONStringer().array().value(a).value(j).endArray());

      j =
          new JSONObject(
              "{string: \"98.6\", long: 2147483648, int: 2147483647, longer: 9223372036854775807, double: 9223372036854775808}");
      System.out.println(j.toString(4));

      System.out.println("\ngetInt");
      System.out.println("int    " + j.getInt("int"));
      System.out.println("long   " + j.getInt("long"));
      System.out.println("longer " + j.getInt("longer"));
      System.out.println("double " + j.getInt("double"));
      System.out.println("string " + j.getInt("string"));

      System.out.println("\ngetLong");
      System.out.println("int    " + j.getLong("int"));
      System.out.println("long   " + j.getLong("long"));
      System.out.println("longer " + j.getLong("longer"));
      System.out.println("double " + j.getLong("double"));
      System.out.println("string " + j.getLong("string"));

      System.out.println("\ngetDouble");
      System.out.println("int    " + j.getDouble("int"));
      System.out.println("long   " + j.getDouble("long"));
      System.out.println("longer " + j.getDouble("longer"));
      System.out.println("double " + j.getDouble("double"));
      System.out.println("string " + j.getDouble("string"));

      j.put("good sized", 9223372036854775807L);
      System.out.println(j.toString(4));

      a = new JSONArray("[2147483647, 2147483648, 9223372036854775807, 9223372036854775808]");
      System.out.println(a.toString(4));

      System.out.println("\nKeys: ");
      it = j.keys();
      while (it.hasNext()) {
        s = (String) it.next();
        System.out.println(s + ": " + j.getString(s));
      }

      System.out.println("\naccumulate: ");
      j = new JSONObject();
      j.accumulate("stooge", "Curly");
      j.accumulate("stooge", "Larry");
      j.accumulate("stooge", "Moe");
      a = j.getJSONArray("stooge");
      a.put(5, "Shemp");
      System.out.println(j.toString(4));

      System.out.println("\nwrite:");
      System.out.println(j.write(new StringWriter()));

      s = "<xml empty><a></a><a>1</a><a>22</a><a>333</a></xml>";
      j = XML.toJSONObject(s);
      System.out.println(j.toString(4));
      System.out.println(XML.toString(j));

      s =
          "<book><chapter>Content of the first chapter</chapter><chapter>Content of the second chapter      <chapter>Content of the first subchapter</chapter>      <chapter>Content of the second subchapter</chapter></chapter><chapter>Third Chapter</chapter></book>";
      j = XML.toJSONObject(s);
      System.out.println(j.toString(4));
      System.out.println(XML.toString(j));

      a = JSONML.toJSONArray(s);
      System.out.println(a.toString(4));
      System.out.println(JSONML.toString(a));

      Collection c = null;
      Map m = null;

      j = new JSONObject(m);
      a = new JSONArray(c);
      j.append("stooge", "Joe DeRita");
      j.append("stooge", "Shemp");
      j.accumulate("stooges", "Curly");
      j.accumulate("stooges", "Larry");
      j.accumulate("stooges", "Moe");
      j.accumulate("stoogearray", j.get("stooges"));
      j.put("map", m);
      j.put("collection", c);
      j.put("array", a);
      a.put(m);
      a.put(c);
      System.out.println(j.toString(4));

      s =
          "{plist=Apple; AnimalSmells = { pig = piggish; lamb = lambish; worm = wormy; }; AnimalSounds = { pig = oink; lamb = baa; worm = baa;  Lisa = \"Why is the worm talking like a lamb?\" } ; AnimalColors = { pig = pink; lamb = black; worm = pink; } } ";
      j = new JSONObject(s);
      System.out.println(j.toString(4));

      s = " (\"San Francisco\", \"New York\", \"Seoul\", \"London\", \"Seattle\", \"Shanghai\")";
      a = new JSONArray(s);
      System.out.println(a.toString());

      s =
          "<a ichi='1' ni='2'><b>The content of b</b> and <c san='3'>The content of c</c><d>do</d><e></e><d>re</d><f/><d>mi</d></a>";
      j = XML.toJSONObject(s);

      System.out.println(j.toString(2));
      System.out.println(XML.toString(j));
      System.out.println("");
      ja = JSONML.toJSONArray(s);
      System.out.println(ja.toString(4));
      System.out.println(JSONML.toString(ja));
      System.out.println("");

      s =
          "<Root><MsgType type=\"node\"><BatchType type=\"string\">111111111111111</BatchType></MsgType></Root>";
      j = JSONML.toJSONObject(s);
      System.out.println(j);
      ja = JSONML.toJSONArray(s);
      System.out.println(ja);

      System.out.println("\nTesting Exceptions: ");

      System.out.print("Exception: ");
      try {
        a = new JSONArray();
        a.put(Double.NEGATIVE_INFINITY);
        a.put(Double.NaN);
        System.out.println(a.toString());
      } catch (Exception e) {
        System.out.println(e);
      }
      System.out.print("Exception: ");
      try {
        System.out.println(j.getDouble("stooge"));
      } catch (Exception e) {
        System.out.println(e);
      }
      System.out.print("Exception: ");
      try {
        System.out.println(j.getDouble("howard"));
      } catch (Exception e) {
        System.out.println(e);
      }
      System.out.print("Exception: ");
      try {
        System.out.println(j.put(null, "howard"));
      } catch (Exception e) {
        System.out.println(e);
      }
      System.out.print("Exception: ");
      try {
        System.out.println(a.getDouble(0));
      } catch (Exception e) {
        System.out.println(e);
      }
      System.out.print("Exception: ");
      try {
        System.out.println(a.get(-1));
      } catch (Exception e) {
        System.out.println(e);
      }
      System.out.print("Exception: ");
      try {
        System.out.println(a.put(Double.NaN));
      } catch (Exception e) {
        System.out.println(e);
      }
      System.out.print("Exception: ");
      try {
        j = XML.toJSONObject("<a><b>    ");
      } catch (Exception e) {
        System.out.println(e);
      }
      System.out.print("Exception: ");
      try {
        j = XML.toJSONObject("<a></b>    ");
      } catch (Exception e) {
        System.out.println(e);
      }
      System.out.print("Exception: ");
      try {
        j = XML.toJSONObject("<a></a    ");
      } catch (Exception e) {
        System.out.println(e);
      }
      System.out.print("Exception: ");
      try {
        ja = new JSONArray(new Object());
        System.out.println(ja.toString());
      } catch (Exception e) {
        System.out.println(e);
      }

      System.out.print("Exception: ");
      try {
        s = "[)";
        a = new JSONArray(s);
        System.out.println(a.toString());
      } catch (Exception e) {
        System.out.println(e);
      }

      System.out.print("Exception: ");
      try {
        s = "<xml";
        ja = JSONML.toJSONArray(s);
        System.out.println(ja.toString(4));
      } catch (Exception e) {
        System.out.println(e);
      }

      System.out.print("Exception: ");
      try {
        s = "<right></wrong>";
        ja = JSONML.toJSONArray(s);
        System.out.println(ja.toString(4));
      } catch (Exception e) {
        System.out.println(e);
      }

      System.out.print("Exception: ");
      try {
        s = "{\"koda\": true, \"koda\": true}";
        j = new JSONObject(s);
        System.out.println(j.toString(4));
      } catch (Exception e) {
        System.out.println(e);
      }

      System.out.print("Exception: ");
      try {
        jj = new JSONStringer();
        s =
            jj.object()
                .key("bosanda")
                .value("MARIE HAA'S")
                .key("bosanda")
                .value("MARIE HAA\\'S")
                .endObject()
                .toString();
        System.out.println(j.toString(4));
      } catch (Exception e) {
        System.out.println(e);
      }
    } catch (Exception e) {
      System.out.println(e.toString());
    }
  }
  public JSONObject describeOperator(String clazz) throws Exception {
    TypeGraphVertex tgv = typeGraph.getTypeGraphVertex(clazz);
    if (tgv.isInstantiable()) {
      JSONObject response = new JSONObject();
      JSONArray inputPorts = new JSONArray();
      JSONArray outputPorts = new JSONArray();
      // Get properties from ASM

      JSONObject operatorDescriptor = describeClassByASM(clazz);
      JSONArray properties = operatorDescriptor.getJSONArray("properties");

      properties = enrichProperties(clazz, properties);

      JSONArray portTypeInfo = operatorDescriptor.getJSONArray("portTypeInfo");

      List<CompactFieldNode> inputPortfields = typeGraph.getAllInputPorts(clazz);
      List<CompactFieldNode> outputPortfields = typeGraph.getAllOutputPorts(clazz);

      try {
        for (CompactFieldNode field : inputPortfields) {
          JSONObject inputPort = setFieldAttributes(clazz, field);
          if (!inputPort.has("optional")) {
            inputPort.put(
                "optional",
                false); // input port that is not annotated is default to be not optional
          }
          if (!inputPort.has(SCHEMA_REQUIRED_KEY)) {
            inputPort.put(SCHEMA_REQUIRED_KEY, false);
          }
          inputPorts.put(inputPort);
        }

        for (CompactFieldNode field : outputPortfields) {
          JSONObject outputPort = setFieldAttributes(clazz, field);

          if (!outputPort.has("optional")) {
            outputPort.put(
                "optional", true); // output port that is not annotated is default to be optional
          }
          if (!outputPort.has("error")) {
            outputPort.put("error", false);
          }
          if (!outputPort.has(SCHEMA_REQUIRED_KEY)) {
            outputPort.put(SCHEMA_REQUIRED_KEY, false);
          }
          outputPorts.put(outputPort);
        }

        response.put("name", clazz);
        response.put("properties", properties);
        response.put(PORT_TYPE_INFO_KEY, portTypeInfo);
        response.put("inputPorts", inputPorts);
        response.put("outputPorts", outputPorts);

        OperatorClassInfo oci = classInfo.get(clazz);

        if (oci != null) {
          if (oci.comment != null) {
            String[] descriptions;
            // first look for a <p> tag
            String keptPrefix = "<p>";
            descriptions = oci.comment.split("<p>", 2);
            if (descriptions.length == 0) {
              keptPrefix = "";
              // if no <p> tag, then look for a blank line
              descriptions = oci.comment.split("\n\n", 2);
            }
            if (descriptions.length > 0) {
              response.put("shortDesc", descriptions[0]);
            }
            if (descriptions.length > 1) {
              response.put("longDesc", keptPrefix + descriptions[1]);
            }
          }
          response.put("category", oci.tags.get("@category"));
          String displayName = oci.tags.get("@displayName");
          if (displayName == null) {
            displayName = decamelizeClassName(ClassUtils.getShortClassName(clazz));
          }
          response.put("displayName", displayName);
          String tags = oci.tags.get("@tags");
          if (tags != null) {
            JSONArray tagArray = new JSONArray();
            for (String tag : StringUtils.split(tags, ',')) {
              tagArray.put(tag.trim().toLowerCase());
            }
            response.put("tags", tagArray);
          }
          String doclink = oci.tags.get("@doclink");
          if (doclink != null) {
            response.put("doclink", doclink + "?" + getDocName(clazz));
          } else if (clazz.startsWith("com.datatorrent.lib.")
              || clazz.startsWith("com.datatorrent.contrib.")) {
            response.put("doclink", DT_OPERATOR_DOCLINK_PREFIX + "?" + getDocName(clazz));
          }
        }
      } catch (JSONException ex) {
        throw new RuntimeException(ex);
      }
      return response;
    } else {
      throw new UnsupportedOperationException();
    }
  }
  /**
   * Enrich portClassHier with class/interface names that map to a list of parent
   * classes/interfaces. For any class encountered, find its parents too.<br>
   * Also find the port types which have assignable schema classes.
   *
   * @param oper Operator to work on
   * @param portClassHierarchy In-Out param that contains a mapping of class/interface to its
   *     parents
   * @param portTypesWithSchemaClasses Json that will contain all the ports which have any schema
   *     classes.
   */
  public void buildAdditionalPortInfo(
      JSONObject oper, JSONObject portClassHierarchy, JSONObject portTypesWithSchemaClasses) {
    try {
      JSONArray ports = oper.getJSONArray(OperatorDiscoverer.PORT_TYPE_INFO_KEY);
      for (int i = 0; i < ports.length(); i++) {
        JSONObject port = ports.getJSONObject(i);

        String portType = port.optString("type");
        if (portType == null) {
          // skipping if port type is null
          continue;
        }

        if (typeGraph.size() == 0) {
          buildTypeGraph();
        }

        try {
          // building port class hierarchy
          LinkedList<String> queue = Lists.newLinkedList();
          queue.add(portType);
          while (!queue.isEmpty()) {
            String currentType = queue.remove();
            if (portClassHierarchy.has(currentType)) {
              // already present in the json so we skip.
              continue;
            }
            List<String> immediateParents = typeGraph.getParents(currentType);
            if (immediateParents == null) {
              portClassHierarchy.put(currentType, Lists.<String>newArrayList());
              continue;
            }
            portClassHierarchy.put(currentType, immediateParents);
            queue.addAll(immediateParents);
          }
        } catch (JSONException e) {
          LOG.warn("building port type hierarchy {}", portType, e);
        }

        // finding port types with schema classes
        if (portTypesWithSchemaClasses.has(portType)) {
          // already present in the json so skipping
          continue;
        }
        if (portType.equals("byte")
            || portType.equals("short")
            || portType.equals("char")
            || portType.equals("int")
            || portType.equals("long")
            || portType.equals("float")
            || portType.equals("double")
            || portType.equals("java.lang.String")
            || portType.equals("java.lang.Object")) {
          // ignoring primitives, strings and object types as this information is needed only for
          // complex types.
          continue;
        }
        if (port.has("typeArgs")) {
          // ignoring any type with generics
          continue;
        }
        boolean hasSchemaClasses = false;
        List<String> instantiableDescendants = typeGraph.getInstantiableDescendants(portType);
        if (instantiableDescendants != null) {
          for (String descendant : instantiableDescendants) {
            try {
              if (typeGraph.isInstantiableBean(descendant)) {
                hasSchemaClasses = true;
                break;
              }
            } catch (JSONException ex) {
              LOG.warn("checking descendant is instantiable {}", descendant);
            }
          }
        }
        portTypesWithSchemaClasses.put(portType, hasSchemaClasses);
      }
    } catch (JSONException e) {
      // should not reach this
      LOG.error("JSON Exception {}", e);
      throw new RuntimeException(e);
    }
  }
Ejemplo n.º 17
0
  /** This is internal stuff. Don't call this directly! */
  public void run() {
    if (!plugin.isEnabled()) {
      plugin.getServer().getScheduler().cancelTask(pid);
      return;
    }
    try {
      while (!lock.compareAndSet(false, true)) {
        try {
          Thread.sleep(1L);
        } catch (InterruptedException e) {
        }
        continue;
      }
      try {
        InputStreamReader ir;
        try {
          URL url = new URL(bukget);
          ir = new InputStreamReader(url.openStream());
        } catch (Exception e) {
          URL url = new URL(bukgetFallback);
          ir = new InputStreamReader(url.openStream());
        }

        String nv;
        try {
          JSONObject jo = new JSONObject(new JSONTokener(ir));
          JSONArray ja = jo.getJSONArray("versions");
          pluginURL = jo.getString("bukkitdev_link");
          jo = ja.getJSONObject(0);
          nv = bukkitdevPrefix + jo.getString("name") + bukkitdevSuffix;
          if (av.equals(nv) || (updateVersion != null && updateVersion.equals(nv))) {
            lock.set(false);
            return;
          }
          updateURL = jo.getString("dl_link");
          updateVersion = nv;
          type = jo.getString("type");
          needUpdate = true;
          ir.close();
        } catch (JSONException e) {
          lock.set(false);
          printStackTraceSync(e, true);
          ir.close();
          return;
        }
        final String[] out =
            new String[] {
              "[" + plugin.getName() + "] New " + type + " available!",
              "If you want to update from "
                  + av
                  + " to "
                  + updateVersion
                  + " use /update "
                  + plugin.getName(),
              "See " + pluginURL + " for more information."
            };
        plugin
            .getServer()
            .getScheduler()
            .scheduleSyncDelayedTask(plugin, new SyncMessageDelayer(null, out));
        plugin
            .getServer()
            .getScheduler()
            .scheduleSyncDelayedTask(
                plugin,
                new Runnable() {
                  public void run() {
                    String[] rout = new String[3];
                    for (int i = 0; i < 3; i++) rout[i] = COLOR_INFO + out[i];
                    for (Player p : plugin.getServer().getOnlinePlayers())
                      if (hasPermission(p, "autoupdate.announce")) p.sendMessage(rout);
                  }
                });
      } catch (Exception e) {
        printStackTraceSync(e, true);
      }
      lock.set(false);
    } catch (Throwable t) {
      printStackTraceSync(t, false);
    }
  }
Ejemplo n.º 18
0
  //
  // ONE TIME ADD CITATIONS
  //
  //
  private static void oneTimeAddCitations() {

    String jsonString = "";

    try {
      jsonString = readFile("./json/citations.json", StandardCharsets.UTF_8);
    } catch (IOException e) {
      System.out.println(e);
    }

    try {
      JSONObject rootObject = new JSONObject(jsonString); // Parse the JSON to a JSONObject
      JSONArray rows = rootObject.getJSONArray("stuff"); // Get all JSONArray rows

      System.out.println("row lengths: " + rows.length());

      set1 = new HashSet<>();

      for (int j = 0; j < rows.length(); j++) { // Iterate each element in the elements array
        JSONObject element = rows.getJSONObject(j); // Get the element object

        int id = element.getInt("id");
        int citationNumber = element.getInt("citation_number");
        String citationDate = " ";
        Boolean isCitationDateNull = element.isNull("citation_date");
        if (!isCitationDateNull) citationDate = element.getString("citation_date");
        String firstName = element.getString("first_name");
        String lastName = element.getString("last_name");
        String firstLastName = firstName + lastName;
        firstLastName = firstLastName.toLowerCase();
        set1.add(firstLastName);

        // System.out.println(firstLastName);
        String dob = " ";
        Boolean isDobNull = element.isNull("date_of_birth");
        if (!isDobNull) {
          dob = element.getString("date_of_birth");
          dob = (dob.split(" "))[0];
        }

        // pick a ssn from list
        String ssn = ssnList.get(ssnCounter);
        ssnCounter--;
        ssnHashMap.put(firstLastName, ssn);

        System.out.println(firstLastName + " " + ssn);

        // compute salt
        final Random ran = new SecureRandom();
        byte[] salt = new byte[32];
        ran.nextBytes(salt);
        String saltString = Base64.encodeBase64String(salt);

        // System.out.println("saltstring: " + saltString);
        saltHashMap.put(firstLastName, saltString);

        // compute ripemd160 hash of ssn + salt
        String saltPlusSsn = saltString + ssn;
        // System.out.println("salt plus ssn: " + saltPlusSsn);

        String resultingHash = "";
        try {
          byte[] r = saltPlusSsn.getBytes("US-ASCII");
          RIPEMD160Digest d = new RIPEMD160Digest();
          d.update(r, 0, r.length);
          byte[] o = new byte[d.getDigestSize()];
          d.doFinal(o, 0);
          ByteArrayOutputStream baos = new ByteArrayOutputStream(40);
          Hex.encode(o, baos);
          resultingHash = new String(baos.toByteArray(), StandardCharsets.UTF_8);

          hashedssnHashMap.put(firstLastName, resultingHash);
        } catch (UnsupportedEncodingException e) {
          System.out.println(e);
        } catch (IOException i) {
          System.out.println(i);
        }

        String fldob = firstLastName + dob;
        String da = " ";
        Boolean isDaNull = element.isNull("defendant_address");
        if (!isDaNull) da = element.getString("defendant_address");
        String dc = " ";
        Boolean isDcNull = element.isNull("defendant_city");
        if (!isDcNull) dc = element.getString("defendant_city");
        String ds = " ";
        Boolean isDsNull = element.isNull("defendant_state");
        if (!isDsNull) ds = element.getString("defendant_state");
        String dln = " ";
        Boolean isDlnNull = element.isNull("drivers_license_number");
        if (!isDlnNull) dln = element.getString("drivers_license_number");
        String cd = " ";
        Boolean isCdNull = element.isNull("court_date");
        if (!isCdNull) cd = element.getString("court_date");
        String cl = " ";
        Boolean isClNull = element.isNull("court_location");
        if (!isClNull) cl = element.getString("court_location");
        String ca = " ";
        Boolean isCaNull = element.isNull("court_address");
        if (!isCaNull) ca = element.getString("court_address");
        /*
        Map<String, AttributeValue> item = newCitationItem(citationNumber, citationDate, firstName, lastName, firstLastName, dob, fldob, resultingHash, da, dc, ds, dln, cd, cl, ca);
        PutItemRequest putItemRequest = new PutItemRequest("citations-table", item);
        PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
        */
      }
    } catch (JSONException e) {
      // JSON Parsing error
      e.printStackTrace();
    }
  }