예제 #1
0
  public static void main(String[] args) throws IOException {
    String workingDir = System.getProperty("user.dir");
    Path filePath = FileSystems.getDefault().getPath(workingDir + "/resources/Day12");

    JsonReader reader = Json.createReader(Files.newInputStream(filePath));
    JsonStructure jsonst = reader.read();

    System.out.printf("Value is %d\n", navigateTree(jsonst, null));
    System.out.printf("Value is %d\n", navigateTree(jsonst, "red"));
  }
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    resp.setCharacterEncoding("UTF-8");
    HttpSession user = req.getSession();
    PrintWriter out = resp.getWriter();
    SpejdResultAnalyser result = null;
    JsonObject response;

    JsonReader jsonReader =
        Json.createReader(new InputStreamReader(req.getInputStream(), StandardCharsets.ISO_8859_1));
    JsonObject jsonObject = jsonReader.readObject();
    jsonReader.close();
    System.out.println("REQUEST");
    System.out.println(jsonObject);
    String originalText = jsonObject.getString("text");
    int space = jsonObject.getInt("space");

    String xml = getXml(originalText, user.getId());
    try {
      result = new SpejdResultAnalyser(xml.replace("SYSTEM \"xcesAnaIPI.dtd\"", ""));
    } catch (ParserConfigurationException | SAXException e) {
      out.println(e.toString());
    }

    assert result != null;
    /** do analysis of xml spejd result */
    SubstantivePairs pairs = result.doAnalysis().makePair(space);

    /**
     * create json response { text: ... xml: ... pairs: [ { subst1: ... subst2: ... quantity: ... },
     * ...] }
     */
    JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
    for (SubstantivePair pair : pairs) {
      arrayBuilder.add(
          Json.createObjectBuilder()
              .add("subst1", pair.getSubst1())
              .add("subst2", pair.getSubst2())
              .add("quantity", pair.getQuantity()));
    }

    Charset.forName("UTF-8").encode(originalText);
    JsonArray array = arrayBuilder.build();
    response =
        Json.createObjectBuilder()
            .add("text", originalText)
            .add("xml", xml)
            .add("pairs", array)
            .build();
    System.out.println("RESPONSE");
    System.out.println(response);
    out.print(response);
  }
  private void jsonReadCont(JsonReader jReader) throws IOException {
    jReader.beginObject();
    assertNotNull("cont1 is missing.", jReader.hasNext());

    // Cont dataFromJson = new Cont(jReader.nextName());
    jReader.nextName();
    jsonReadContElements(jReader);

    assertFalse("cont shouldn't have other element.", jReader.hasNext());
    jReader.endObject();
    // return dataFromJson;
  }
예제 #4
0
 public String asString() {
   if (isString()) {
     return StringUtils.unescapeString(buffer.substring(start + 1, end - 1));
   } else {
     throw new RuntimeException("cant cast to boolean");
   }
 }
예제 #5
0
 public int rewind() {
   if (symbol >= 0) {
     return reader.rewind();
   } else {
     return symbol;
   }
 }
예제 #6
0
 public String value() {
   return buffer.substring(start, end);
 }
예제 #7
0
  public Token next(boolean skipWhitespace) {
    int from = reader.position();
    int ch = read();

    switch (ch) {
      case '{':
        token = Token.create(Token.Type.OBJECT_START, reader, from, reader.position());
        break;
      case '}':
        token = Token.create(Token.Type.OBJECT_END, reader, from, reader.position());
        break;
      case '[':
        token = Token.create(Token.Type.ARRAY_START, reader, from, reader.position());
        break;
      case ']':
        token = Token.create(Token.Type.ARRAY_END, reader, from, reader.position());
        break;
      case ':':
        token = Token.create(Token.Type.COLON, reader, from, reader.position());
        break;
      case ',':
        token = Token.create(Token.Type.COMMA, reader, from, reader.position());
        break;
      default:
        if (isWhitespaceStart(ch)) {
          readWhitespace();
          token = Token.create(Token.Type.WS, reader, from, reader.position());
        } else if (isStringStart(ch)) {
          readString();
          token = Token.create(Token.Type.STRING, reader, from, reader.position());
        } else if (isNullStart(ch)) {
          readNull();
          token = Token.create(Token.Type.NULL, reader, from, reader.position());
        } else if (isTrueStart(ch)) {
          readTrue();
          token = Token.create(Token.Type.TRUE, reader, from, reader.position());
        } else if (isFalseStart(ch)) {
          readFalse();
          token = Token.create(Token.Type.FALSE, reader, from, reader.position());
        } else if (isNumberStart(ch)) {
          readNumber();
          token = Token.create(Token.Type.NUMBER, reader, from, reader.position());
        } else if (ch == -1) {
          token = Token.create(Token.Type.EOF, reader, from, reader.position());
        } else {
          throw new RuntimeException("Invalid Json Data\n" + reader.toString());
        }
    }
    while (skipWhitespace && Token.Type.WS.equals(token.type)) {
      token = next();
    }
    return token;
  }
예제 #8
0
 public int read() {
   symbol = reader.read();
   return symbol;
 }
  private void jsonReadContElements(JsonReader jReader) throws IOException {
    jReader.beginObject();
    List<String> loadedLfs = new ArrayList<>();
    boolean exceptForDecimal5Raised = false;
    boolean enumChecked = false;
    boolean bitsChecked = false;
    boolean lfdecimal6Checked = false;
    boolean lfdecimal4Checked = false;
    boolean lfdecimal3Checked = false;
    boolean lfdecimal2Checked = false;
    boolean lfdecimal1Checked = false;
    boolean lfbool1Checked = false;
    boolean lfbool2Checked = false;
    boolean lfstrChecked = false;
    boolean lfbinaryChecked = false;
    // boolean lfref1Checked = false;
    boolean lfemptyChecked = false;
    boolean lfstr1Checked = false;

    while (jReader.hasNext()) {
      String keyName = jReader.nextName();
      JsonToken peek = null;
      try {
        peek = jReader.peek();
      } catch (IOException e) {
        if (keyName.equals("lfdecimal5")) {
          exceptForDecimal5Raised = true;
        } else {
          assertTrue("Key " + keyName + " has incorrect value for specifed type", false);
        }
      }

      if (keyName.startsWith("lfnint") || keyName.startsWith("lfnuint")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
        try {
          jReader.nextLong();
        } catch (NumberFormatException e) {
          assertTrue("Key " + keyName + " has incorrect value - " + e.getMessage(), false);
        }
        loadedLfs.add(keyName.substring(3));
      } else if (keyName.equals("lfstr")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
        assertEquals("lfstr", jReader.nextString());
        lfstrChecked = true;
      } else if (keyName.equals("lfstr1")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
        assertEquals("", jReader.nextString());
        lfstr1Checked = true;
      } else if (keyName.equals("lfbool1")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek);
        assertEquals(true, jReader.nextBoolean());
        lfbool1Checked = true;
      } else if (keyName.equals("lfbool2")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek);
        assertEquals(false, jReader.nextBoolean());
        lfbool2Checked = true;
      } else if (keyName.equals("lfbool3")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek);
        assertEquals(false, jReader.nextBoolean());
      } else if (keyName.equals("lfdecimal1")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
        assertEquals(new Double(43.32), (Double) jReader.nextDouble());
        lfdecimal1Checked = true;
      } else if (keyName.equals("lfdecimal2")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
        assertEquals(new Double(-0.43), (Double) jReader.nextDouble());
        lfdecimal2Checked = true;
      } else if (keyName.equals("lfdecimal3")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
        assertEquals(new Double(43), (Double) jReader.nextDouble());
        lfdecimal3Checked = true;
      } else if (keyName.equals("lfdecimal4")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
        assertEquals(new Double(43E3), (Double) jReader.nextDouble());
        lfdecimal4Checked = true;
      } else if (keyName.equals("lfdecimal6")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
        assertEquals(new Double(33.12345), (Double) jReader.nextDouble());
        lfdecimal6Checked = true;
      } else if (keyName.equals("lfenum")) {
        assertEquals("enum3", jReader.nextString());
        enumChecked = true;
      } else if (keyName.equals("lfbits")) {
        assertEquals("bit3", jReader.nextString());
        bitsChecked = true;
      } else if (keyName.equals("lfbinary")) {
        assertEquals(
            "AAaacdabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%%-#^",
            jReader.nextString());
        lfbinaryChecked = true;
      } else if (keyName.equals("lfempty")) {
        jReader.beginArray();
        jReader.nextNull();
        jReader.endArray();
        lfemptyChecked = true;
      } else if (keyName.startsWith("lfunion")) {
        checkLfUnion(jReader, keyName, peek);
      } else {
        assertTrue("Key " + keyName + " doesn't exists in yang file.", false);
      }
    }
    Collections.sort(loadedLfs);
    String expectedLfsStr =
        "[int16Max, int16Min, int32Max, int32Min, int64Max, int64Min, int8Max, int8Min, uint16Max, uint32Max, uint8Max]";
    String actualLfsStr = loadedLfs.toString();
    assertEquals("Some leaves are missing", expectedLfsStr, actualLfsStr);
    // assertTrue("For lfdecimal5 wasn't catch error",exceptForDecimal5Raised);
    assertTrue("Enum wasn't checked", enumChecked);
    assertTrue("Bits wasn't checked", bitsChecked);
    assertTrue("Decimal1 wasn't checked", lfdecimal1Checked);
    assertTrue("Decimal2 wasn't checked", lfdecimal2Checked);
    assertTrue("Decimal3 wasn't checked", lfdecimal3Checked);
    assertTrue("Decimal4 wasn't checked", lfdecimal4Checked);
    assertTrue("Decimal5 wasn't checked", lfdecimal6Checked);
    assertTrue("lfbool1 wasn't checked", lfbool1Checked);
    assertTrue("lfbool2 wasn't checked", lfbool2Checked);
    assertTrue("lfstr wasn't checked", lfstrChecked);
    assertTrue("lfstr1 wasn't checked", lfstr1Checked);
    assertTrue("lfbinary wasn't checked", lfbinaryChecked);
    assertTrue("lfempty wasn't checked", lfemptyChecked);
    // assertTrue("lfref1 wasn't checked", lfref1Checked);

    jReader.endObject();
  }
 private void checkLfUnion(JsonReader jReader, String keyName, JsonToken peek) throws IOException {
   if (keyName.equals("lfunion1")) {
     assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
     jReader.nextString();
   } else if (keyName.equals("lfunion2")) {
     assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
     jReader.nextString();
   } else if (keyName.equals("lfunion3")) {
     assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
     jReader.nextInt();
   } else if (keyName.equals("lfunion4")) {
     assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek);
     jReader.nextBoolean();
   } else if (keyName.equals("lfunion5")) {
     assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
     jReader.nextString();
   } else if (keyName.equals("lfunion6")) {
     assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
     jReader.nextString();
   } else if (keyName.equals("lfunion7")) {
     assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
     jReader.nextString();
   } else if (keyName.equals("lfunion8")) {
     assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
     jReader.nextString();
   } else if (keyName.equals("lfunion9")) {
     assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
     jReader.nextString();
   } else if (keyName.equals("lfunion10")) {
     assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
     jReader.nextString();
   } else if (keyName.equals("lfunion11")) {
     assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
     jReader.nextString();
   } else if (keyName.equals("lfunion12")) {
     assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek);
     jReader.nextBoolean();
   }
 }