@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); }
public static int navigateTree(JsonValue tree, String skip) { int ret = 0; switch (tree.getValueType()) { case OBJECT: // System.out.println("OBJECT"); JsonArrayBuilder dirtyResult = Json.createArrayBuilder(); JsonObject object = (JsonObject) tree; for (String name : object.keySet()) { if (object.get(name).getValueType() == JsonValue.ValueType.STRING && object.getString(name).equals(skip)) { return 0; } dirtyResult.add(object.get(name)); } ret = ret + navigateTree(dirtyResult.build(), skip); break; case ARRAY: // System.out.println("ARRAY"); JsonArray array = (JsonArray) tree; for (JsonValue val : array) ret = ret + navigateTree(val, skip); break; case STRING: JsonString st = (JsonString) tree; // System.out.println("STRING " + st.getString()); break; case NUMBER: JsonNumber num = (JsonNumber) tree; return num.intValue(); case TRUE: case FALSE: case NULL: // System.out.println(tree.getValueType().toString()); break; } return ret; }