示例#1
1
  /** Return the next element of the json document */
  private String getNextNode() {
    final int popState;

    switch (states.peek()) {
      case ARRAY:
        switch (rand.nextInt(9)) {
          case 0: // String case
            final String val = "stepha" + this.getWhitespace() + "n" + this.getWhitespace() + "e";
            this.addToLastNode(1);
            nodes.add(IntsRef.deepCopyOf(curNodePath));
            images.add(val);
            types.add(ExtendedJsonTokenizer.getTokenTypes()[LITERAL]);
            incr.add(1);
            datatypes.add(XSDDatatype.XSD_STRING);
            return "\"" + val + "\"" + this.getWhitespace() + ",";
          case 1: // DOUBLE case
            this.addToLastNode(1);
            nodes.add(IntsRef.deepCopyOf(curNodePath));
            images.add("34.560e-9");
            types.add(ExtendedJsonTokenizer.getTokenTypes()[NUMBER]);
            incr.add(1);
            datatypes.add(XSDDatatype.XSD_DOUBLE);
            return "34.560e-9" + this.getWhitespace() + ",";
          case 2: // LONG case
            this.addToLastNode(1);
            nodes.add(IntsRef.deepCopyOf(curNodePath));
            images.add("34560e-9");
            types.add(ExtendedJsonTokenizer.getTokenTypes()[NUMBER]);
            incr.add(1);
            datatypes.add(XSDDatatype.XSD_LONG);
            return "34560e-9" + this.getWhitespace() + ",";
          case 3: // true case
            this.addToLastNode(1);
            nodes.add(IntsRef.deepCopyOf(curNodePath));
            images.add("true");
            types.add(ExtendedJsonTokenizer.getTokenTypes()[TRUE]);
            incr.add(1);
            datatypes.add(XSDDatatype.XSD_BOOLEAN);
            return "true" + this.getWhitespace() + ",";
          case 4: // false case
            this.addToLastNode(1);
            nodes.add(IntsRef.deepCopyOf(curNodePath));
            images.add("false");
            types.add(ExtendedJsonTokenizer.getTokenTypes()[FALSE]);
            incr.add(1);
            datatypes.add(XSDDatatype.XSD_BOOLEAN);
            return "false" + this.getWhitespace() + ",";
          case 5: // null case
            this.addToLastNode(1);
            nodes.add(IntsRef.deepCopyOf(curNodePath));
            images.add("null");
            types.add(ExtendedJsonTokenizer.getTokenTypes()[NULL]);
            incr.add(1);
            datatypes.add(XSDDatatype.XSD_STRING);
            return "null" + this.getWhitespace() + ",";
          case 6: // nested array case
            if (states.size() <= MAX_DEPTH) {
              this.addToLastNode(1);
              this.incrNodeObjectPath();
              states.add(ARRAY);
              return "[";
            }
            return "";
          case 7: // nested object case
            if (states.size() <= MAX_DEPTH) {
              this.addToLastNode(1);
              this.incrNodeObjectPath();
              states.add(ARRAY_OBJECT);
              return "{";
            }
            return "";
          case 8: // closing array case
            this.decrNodeObjectPath();
            popState = states.pop();
            if (popState != ARRAY) {
              shouldFail = true;
            }
            // Remove previous comma, this is not allowed
            final int comma = sb.lastIndexOf(",");
            if (comma != -1 && sb.substring(comma + 1).matches("\\s*")) {
              sb.deleteCharAt(comma);
            }
            return "],";
        }
      case ARRAY_OBJECT:
      case OBJECT_ATT:
        switch (rand.nextInt(3)) {
          case 0: // new object field
            types.add(ExtendedJsonTokenizer.getTokenTypes()[LITERAL]);
            images.add("ste ph ane");
            incr.add(1);
            this.addToLastNode(1);
            nodes.add(IntsRef.deepCopyOf(curNodePath));
            datatypes.add(JSONDatatype.JSON_FIELD);

            states.push(OBJECT_VAL);
            return "\"ste ph ane\"" + this.getWhitespace() + ":";
          case 1: // close object
            if (states.peek() == OBJECT_ATT && nestedObjs > 0) {
              this.decrNodeObjectPath();
              nestedObjs--;
            }
            this.decrNodeObjectPath();
            popState = states.pop();
            if (popState != OBJECT_ATT && popState != ARRAY_OBJECT) {
              shouldFail = true;
            }
            // Remove previous comma, this is not allowed
            final int comma = sb.lastIndexOf(",");
            if (comma != -1 && sb.substring(comma + 1).matches("\\s*")) {
              sb.deleteCharAt(comma);
            }
            return states.empty() ? "}" : "},";
          case 2: // Datatype
            if (getLastNode() >= 0) {
              // this nested object cannot be a datatype object because other things have been added
              // to it
              return "";
            }
            final String field;
            if (states.isEmpty()) {
              // datatype object at the root are not possible
              shouldFail = true;
              field = "{";
            } else if (states.peek() == OBJECT_ATT) {
              // field name
              this.addToLastNode(1);
              field = "\"field\":{";
              types.add(ExtendedJsonTokenizer.getTokenTypes()[LITERAL]);
              images.add("field");
              incr.add(1);
              nodes.add(IntsRef.deepCopyOf(curNodePath));
              datatypes.add(JSONDatatype.JSON_FIELD);
              // value
              this.incrNodeObjectPath();
              this.setLastNode(0);
            } else if (states.peek() == ARRAY) {
              this.addToLastNode(1);
              field = "{";
            } else if (states.peek() == ARRAY_OBJECT) {
              this.decrNodeObjectPath();
              field = "";
            } else {
              // should not happen
              throw new IllegalStateException("Received unknown state=" + states.peek());
            }

            types.add(ExtendedJsonTokenizer.getTokenTypes()[LITERAL]);
            images.add("Luke Skywalker");
            incr.add(1);
            nodes.add(IntsRef.deepCopyOf(curNodePath));
            datatypes.add("jedi");
            // close datatype object
            if (states.peek() == ARRAY_OBJECT) {
              popState = states.pop();
            } else {
              this.decrNodeObjectPath();
            }
            return field
                + this.getWhitespace()
                + "\""
                + ExtendedJsonTokenizer.DATATYPE_LABEL
                + "\":"
                + this.getWhitespace()
                + "\"jedi\","
                + "\""
                + ExtendedJsonTokenizer.DATATYPE_VALUES
                + "\":"
                + this.getWhitespace()
                + "\"Luke Skywalker\""
                + this.getWhitespace()
                + "},";
        }
      case OBJECT_VAL:
        switch (rand.nextInt(8)) {
          case 0: // String
            return this.doValString(
                "stepha" + this.getWhitespace() + "n" + this.getWhitespace() + "e");
          case 1: // DOUBLE case
            images.add("34.560e-9");
            types.add(ExtendedJsonTokenizer.getTokenTypes()[NUMBER]);
            incr.add(1);
            this.incrNodeObjectPath();
            this.setLastNode(0);
            nodes.add(IntsRef.deepCopyOf(curNodePath));
            this.decrNodeObjectPath();
            datatypes.add(XSDDatatype.XSD_DOUBLE);

            states.pop(); // remove OBJECT_VAL state
            return "34.560e-9" + this.getWhitespace() + ",";
          case 2: // LONG case
            images.add("34560e-9");
            types.add(ExtendedJsonTokenizer.getTokenTypes()[NUMBER]);
            incr.add(1);
            this.incrNodeObjectPath();
            this.setLastNode(0);
            nodes.add(IntsRef.deepCopyOf(curNodePath));
            this.decrNodeObjectPath();
            datatypes.add(XSDDatatype.XSD_LONG);

            states.pop(); // remove OBJECT_VAL state
            return "34560e-9" + this.getWhitespace() + ",";
          case 3: // True
            images.add("true");
            types.add(ExtendedJsonTokenizer.getTokenTypes()[TRUE]);
            incr.add(1);
            this.incrNodeObjectPath();
            this.setLastNode(0);
            nodes.add(IntsRef.deepCopyOf(curNodePath));
            this.decrNodeObjectPath();
            datatypes.add(XSDDatatype.XSD_BOOLEAN);

            states.pop(); // remove OBJECT_VAL state
            return "true" + this.getWhitespace() + ",";
          case 4: // False
            images.add("false");
            types.add(ExtendedJsonTokenizer.getTokenTypes()[FALSE]);
            incr.add(1);
            this.incrNodeObjectPath();
            this.setLastNode(0);
            nodes.add(IntsRef.deepCopyOf(curNodePath));
            this.decrNodeObjectPath();
            datatypes.add(XSDDatatype.XSD_BOOLEAN);

            states.pop(); // remove OBJECT_VAL state
            return "false" + this.getWhitespace() + ",";
          case 5: // NULL
            images.add("null");
            types.add(ExtendedJsonTokenizer.getTokenTypes()[NULL]);
            incr.add(1);
            this.incrNodeObjectPath();
            this.setLastNode(0);
            nodes.add(IntsRef.deepCopyOf(curNodePath));
            this.decrNodeObjectPath();
            datatypes.add(XSDDatatype.XSD_STRING);

            states.pop(); // remove OBJECT_VAL state
            return "null" + this.getWhitespace() + ",";
          case 6: // New array
            if (states.size() <= MAX_DEPTH) {
              states.pop(); // remove OBJECT_VAL state
              this.incrNodeObjectPath();
              states.add(ARRAY);
              return "[";
            }
            return this.doValString("");
          case 7: // new Object
            if (states.size() <= MAX_DEPTH) {
              states.pop(); // remove OBJECT_VAL state
              // Two incrementations, because the object introduce a "blank" node
              nestedObjs++;
              this.incrNodeObjectPath();
              this.setLastNode(0);
              this.incrNodeObjectPath();
              states.add(OBJECT_ATT);
              return "{";
            }
            return this.doValString("");
        }
      default:
        throw new IllegalStateException("Got unknown lexical state: " + states.peek());
    }
  }
示例#2
0
  /** Add a string value to an object entry */
  private String doValString(final String val) {
    images.add(val);
    types.add(ExtendedJsonTokenizer.getTokenTypes()[LITERAL]);
    incr.add(1);
    this.incrNodeObjectPath();
    this.setLastNode(0);
    nodes.add(IntsRef.deepCopyOf(curNodePath));
    this.decrNodeObjectPath();
    datatypes.add(XSDDatatype.XSD_STRING);

    states.pop(); // remove OBJECT_VAL state
    return "\"" + val + "\"" + this.getWhitespace() + ",";
  }