List of usage examples for org.json.simple JSONValue parseWithException
public static Object parseWithException(String s) throws ParseException
From source file:i5.las2peer.services.mobsos.SurveyService.java
/** * Parses incoming content to a survey JSON representation including checks for completeness, illegal fields and values. *///from www. j av a 2 s. c o m private JSONObject parseSurvey(String content) throws IllegalArgumentException { JSONObject o; try { o = (JSONObject) JSONValue.parseWithException(content); } catch (ParseException e1) { throw new IllegalArgumentException("Survey data *" + content + "* is not valid JSON!"); } // check result for unknown illegal fields. If so, parsing fails. String[] fields = { "id", "owner", "organization", "logo", "name", "description", "resource", "start", "end", "lang", "qid" }; for (Object key : o.keySet()) { if (!Arrays.asList(fields).contains(key)) { throw new IllegalArgumentException("Illegal survey field '" + key + "' detected!"); } else { if (key.equals("name") && !(o.get(key) instanceof String)) { throw new IllegalArgumentException( "Illegal value for survey field 'name'. Should be a string."); } else if (key.equals("description") && !(o.get(key) instanceof String)) { throw new IllegalArgumentException( "Illegal value for survey field 'description'. Should be a string."); } else if (key.equals("organization") && !(o.get(key) instanceof String)) { throw new IllegalArgumentException( "Illegal value for survey field 'organization'. Should be a string."); } else if (key.equals("logo")) { try { URL u = new URL((String) o.get(key)); HttpURLConnection con = (HttpURLConnection) u.openConnection(); if (404 == con.getResponseCode()) { throw new IllegalArgumentException( "Illegal value for survey field logo. Should be a valid URL to an image resource."); } if (!con.getContentType().matches("image/.*")) { throw new IllegalArgumentException( "Illegal value for survey field logo. Should be a valid URL to an image resource."); } } catch (MalformedURLException e) { throw new IllegalArgumentException( "Illegal value for survey field 'logo'. Should be a valid URL to an image resource."); } catch (IOException e) { throw new IllegalArgumentException( "Illegal value for survey field 'logo'. Should be a valid URL to an image resource."); } } else if (key.equals("resource")) { HttpResponse h = getClientMetadata((String) o.get(key)); if (h.getStatus() == 404) { throw new IllegalArgumentException( "Illegal value for survey field 'resource'. Should be an existing OpenID Client ID."); } /*try { URL u = new URL((String) o.get(key)); HttpURLConnection con = (HttpURLConnection) u.openConnection(); if(404 == con.getResponseCode()){ throw new IllegalArgumentException("Illegal value for survey field 'resource'. Should be a valid URL."); } } catch (MalformedURLException e) { throw new IllegalArgumentException("Illegal value for survey field 'resource'. Should be a valid URL."); } catch (IOException e) { throw new IllegalArgumentException("Illegal value for survey field 'resource'. Should be a valid URL."); }*/ } else if (key.equals("start")) { try { DatatypeConverter.parseDateTime((String) o.get("start")); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( "Illegal value for survey field 'start'. Should be an ISO-8601 formatted time string."); } } else if (key.equals("end")) { try { DatatypeConverter.parseDateTime((String) o.get("end")); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( "Illegal value for survey field 'end'. Should be an ISO-8601 formatted time string."); } } else if (key.equals("lang")) { String lang = (String) o.get(key); Pattern p = Pattern.compile("[a-z]+-[A-Z]+"); Matcher m = p.matcher(lang); // do not iterate over all locales found, but only use first option with highest preference. Locale l = null; if (m.find()) { String[] tokens = m.group().split("-"); l = new Locale(tokens[0], tokens[1]); } else { throw new IllegalArgumentException( "Illegal value for survey field 'lang'. Should be a valid locale such as 'en-US' or 'de-DE'."); } } } } // check if all necessary fields are specified. if (o.get("name") == null || o.get("organization") == null || o.get("logo") == null || o.get("description") == null || o.get("resource") == null || o.get("start") == null || o.get("end") == null || o.get("lang") == null) { throw new IllegalArgumentException( "Survey data incomplete! All fields name, organization, logo, description, resource, start, end, and lang must be defined!"); } // finally check time integrity constraint: start must be before end (possibly not enforced by database; mySQL does not support this check) long d_start = DatatypeConverter.parseDateTime((String) o.get("start")).getTimeInMillis(); long d_end = DatatypeConverter.parseDateTime((String) o.get("end")).getTimeInMillis(); if (d_start >= d_end) { throw new IllegalArgumentException("Survey data invalid! Start time must be before end time!"); } return o; }
From source file:i5.las2peer.services.mobsos.SurveyService.java
/** * Parses incoming content to a questionnaire JSON representation including checks for completeness, illegal fields and values. *//* ww w. j a v a 2s. c o m*/ private JSONObject parseQuestionnaire(String content) throws IllegalArgumentException { JSONObject o; try { o = (JSONObject) JSONValue.parseWithException(content); } catch (ParseException e1) { throw new IllegalArgumentException("Questionnaire data *" + content + "* is not valid JSON!"); } // check result for unknown illegal fields. If so, parsing fails. String[] fields = { "id", "owner", "organization", "logo", "name", "description", "lang" }; for (Object key : o.keySet()) { if (!Arrays.asList(fields).contains(key)) { throw new IllegalArgumentException("Illegal questionnaire field '" + key + "' detected!"); } else { if (key.equals("name") && !(o.get(key) instanceof String)) { throw new IllegalArgumentException( "Illegal value for questionnaire field 'name'. Should be a string."); } else if (key.equals("description") && !(o.get(key) instanceof String)) { throw new IllegalArgumentException( "Illegal value for questionnaire field 'description'. Should be a string."); } else if (key.equals("organization") && !(o.get(key) instanceof String)) { throw new IllegalArgumentException( "Illegal value for questionnaire field 'organization'. Should be a string."); } else if (key.equals("logo")) { try { URL u = new URL((String) o.get(key)); HttpURLConnection con = (HttpURLConnection) u.openConnection(); if (404 == con.getResponseCode()) { throw new IllegalArgumentException( "Illegal value for questionnaire field 'logo'. Should be a valid URL to an image resource."); } if (!con.getContentType().matches("image/.*")) { throw new IllegalArgumentException( "Illegal value for questionnaire field 'logo'. Should be a valid URL to an image resource."); } } catch (MalformedURLException e) { throw new IllegalArgumentException( "Illegal value for questionnaire field 'logo'. Should be a valid URL to an image resource."); } catch (IOException e) { throw new IllegalArgumentException( "Illegal value for questionnaire field 'logo'. Should be a valid URL to an image resource."); } } else if (key.equals("lang")) { String lang = (String) o.get(key); Pattern p = Pattern.compile("[a-z]+-[A-Z]+"); Matcher m = p.matcher(lang); // do not iterate over all locales found, but only use first option with highest preference. Locale l = null; if (m.find()) { String[] tokens = m.group().split("-"); l = new Locale(tokens[0], tokens[1]); //l = new Locale("zz","ZZ"); //System.out.println("Locale: " + l.getDisplayCountry() + " " + l.getDisplayLanguage()); } else { throw new IllegalArgumentException( "Illegal value for questionnaire field 'lang'. Should be a valid locale such as en-US or de-DE"); } } } } // check if all necessary fields are specified. if (o.get("name") == null || o.get("organization") == null || o.get("logo") == null || o.get("description") == null || o.get("lang") == null) { throw new IllegalArgumentException( "Questionnaire data incomplete! All fields name, organization, logo, description, and lang must be defined!"); } return o; }
From source file:i5.las2peer.services.mobsos.SurveyService.java
private JSONObject getActiveUserInfo() throws ParseException { if (this.getActiveAgent() instanceof UserAgent) { UserAgent me = (UserAgent) this.getActiveAgent(); JSONObject o;// w ww .ja va2 s . c o m if (me.getUserData() != null) { System.err.println(me.getUserData()); o = (JSONObject) JSONValue.parseWithException((String) me.getUserData()); } else { o = new JSONObject(); if (getActiveNode().getAnonymous().getId() == getActiveAgent().getId()) { o.put("sub", "anonymous"); } else { String md5ide = new String("" + me.getId()); o.put("sub", md5ide); } } return o; } else { return new JSONObject(); } }
From source file:net.vidainc.home.server.xmpp.CcsClient.java
/** * Connects to GCM Cloud Connection Server using the supplied credentials. * /* w w w .ja va 2s . c om*/ * @throws XMPPException */ public void connect() throws XMPPException { config = new ConnectionConfiguration(GCM_SERVER, GCM_PORT); config.setSecurityMode(SecurityMode.enabled); config.setReconnectionAllowed(true); config.setRosterLoadedAtLogin(false); config.setSendPresence(false); config.setSocketFactory(SSLSocketFactory.getDefault()); // NOTE: Set to true to launch a window with information about packets // sent and received //config.setDebuggerEnabled(mDebuggable); // -Dsmack.debugEnabled=true //XMPPConnection.DEBUG_ENABLED = true; connection = new XMPPConnection(config); connection.connect(); connection.addConnectionListener(new ConnectionListener() { @Override public void reconnectionSuccessful() { logger.info("Reconnecting.."); } @Override public void reconnectionFailed(Exception e) { logger.log(Level.INFO, "Reconnection failed.. ", e); } @Override public void reconnectingIn(int seconds) { logger.log(Level.INFO, "Reconnecting in %d secs", seconds); } @Override public void connectionClosedOnError(Exception e) { logger.log(Level.INFO, "Connection closed on error."); } @Override public void connectionClosed() { logger.info("Connection closed."); } }); // Handle incoming packets connection.addPacketListener(new PacketListener() { @Override public void processPacket(Packet packet) { logger.log(Level.INFO, "Received: " + packet.toXML()); Message incomingMessage = (Message) packet; GcmPacketExtension gcmPacket = (GcmPacketExtension) incomingMessage.getExtension(GCM_NAMESPACE); String json = gcmPacket.getJson(); try { @SuppressWarnings("unchecked") Map<String, Object> jsonMap = (Map<String, Object>) JSONValue.parseWithException(json); handleMessage(jsonMap); } catch (ParseException e) { logger.log(Level.SEVERE, "Error parsing JSON " + json, e); } catch (Exception e) { logger.log(Level.SEVERE, "Couldn't send echo.", e); } } }, new PacketTypeFilter(Message.class)); // Log all outgoing packets connection.addPacketInterceptor(new PacketInterceptor() { @Override public void interceptPacket(Packet packet) { logger.log(Level.INFO, "Sent: {0}", packet.toXML()); } }, new PacketTypeFilter(Message.class)); connection.login(mProjectId + "@gcm.googleapis.com", mApiKey); logger.log(Level.INFO, "logged in: " + mProjectId); }
From source file:netbeanstypescript.tsconfig.TSConfigParser.java
@Override public void parse(Snapshot snapshot, Task task, SourceModificationEvent sme) throws ParseException { final TokenSequence<JsTokenId> ts = snapshot.getTokenHierarchy() .tokenSequence(JsTokenId.javascriptLanguage()); final Result res = new Result(snapshot); res.root = (new Object() { private JsTokenId advance() { while (ts.moveNext()) { JsTokenId id = ts.token().id(); String category = id.primaryCategory(); if (!("comment".equals(category) || "whitespace".equals(category))) { return id; }/*w ww. j a va 2 s. c o m*/ } return null; } private void error(String msg) { res.addError(msg, ts.offset(), ts.offset() + ts.token().length()); } private boolean nextListItem(boolean previous, JsTokenId endToken, String type) { if (!previous) { JsTokenId id = advance(); if (id == null || id == JsTokenId.BRACKET_RIGHT_BRACKET || id == JsTokenId.BRACKET_RIGHT_CURLY) { if (id != endToken) { if (id != null) ts.movePrevious(); error("Unterminated " + type + "."); } return false; } return true; } else { JsTokenId id = advance(); if (id == JsTokenId.OPERATOR_COMMA) { id = advance(); if (id == null) { error("Unterminated " + type + "."); return false; } return true; } else if (id == null || id == JsTokenId.BRACKET_RIGHT_BRACKET || id == JsTokenId.BRACKET_RIGHT_CURLY) { if (id != endToken) { if (id != null) ts.movePrevious(); error("Unterminated " + type + "."); } return false; } else { error("Missing comma."); return true; } } } private ConfigNode value() { ConfigNode node = new ConfigNode(); node.startOffset = ts.offset(); switch (ts.token().id()) { case KEYWORD_NULL: break; case KEYWORD_TRUE: node.value = Boolean.TRUE; break; case KEYWORD_FALSE: node.value = Boolean.FALSE; break; case STRING: case NUMBER: // TODO: negative numbers try { node.value = JSONValue.parseWithException(ts.token().text().toString()); } catch (org.json.simple.parser.ParseException e) { error("Invalid JSON literal: " + e); return null; } break; case BRACKET_LEFT_BRACKET: node.elements = new ArrayList<>(); for (boolean in = false; (in = nextListItem(in, JsTokenId.BRACKET_RIGHT_BRACKET, "array"));) { ConfigNode element = value(); if (element != null) { node.elements.add(element); } } break; case BRACKET_LEFT_CURLY: node.properties = new LinkedHashMap<>(); for (boolean in = false; (in = nextListItem(in, JsTokenId.BRACKET_RIGHT_CURLY, "object"));) { ConfigNode keyNode = value(); if (keyNode == null) continue; String key = null; if (keyNode.value instanceof String) { key = (String) keyNode.value; } else { res.addError("JSON object key must be a string.", keyNode); } int colonOffset = keyNode.endOffset; ConfigNode valueNode; if (advance() == JsTokenId.OPERATOR_COLON) { colonOffset = ts.offset() + 1; advance(); } else { res.addError("JSON object must consist of '\"key\": value' pairs.", keyNode); } valueNode = value(); if (valueNode == null) { valueNode = new ConfigNode(); valueNode.missing = true; valueNode.startOffset = colonOffset; valueNode.endOffset = ts.offset() + ts.token().length(); } valueNode.keyOffset = keyNode.startOffset; if (key != null) { ConfigNode oldValue = node.properties.put(key, valueNode); if (oldValue != null) { res.addError("Duplicate key '" + key + "' will be ignored.", oldValue.keyOffset, oldValue.endOffset, Severity.WARNING); } } } break; case OPERATOR_COMMA: case BRACKET_RIGHT_BRACKET: case BRACKET_RIGHT_CURLY: error("Missing value."); ts.movePrevious(); return null; default: error("Unexpected token type " + ts.token().id()); return null; } node.endOffset = ts.offset() + ts.token().length(); return node; } ConfigNode root() { if (advance() == null) { return null; } ConfigNode root = value(); if (advance() != null) { error("Extra text at end of JSON."); } return root; } }).root(); ROOT: if (res.root != null) { if (res.root.properties == null) { res.addError("tsconfig.json value should be an object", res.root); break ROOT; } Map<String, TSConfigElementHandle> rootCompletions = res.root.validMap = new HashMap<>(); rootCompletions.put("compileOnSave", new TSConfigElementHandle("compileOnSave", "boolean", "If true, any TypeScript files modified during the IDE session are automatically transpiled to JS.")); ConfigNode compileOnSave = res.root.properties.get("compileOnSave"); if (compileOnSave != null && !(compileOnSave.value instanceof Boolean)) { res.addError("'compileOnSave' value must be a boolean.", compileOnSave); } ConfigNode files = res.root.properties.get("files"); rootCompletions.put("files", new TSConfigElementHandle("files", "list", "Array of files to include in the project.")); if (files != null) { if (files.elements == null) { res.addError("'files' value must be an array of strings.", files); } else { for (ConfigNode file : files.elements) { if (!(file.value instanceof String)) { res.addError("'files' element should be a string.", file); } } } } ConfigNode include = res.root.properties.get("include"); rootCompletions.put("include", new TSConfigElementHandle("include", "list", "Array of files and directories to include in the project.")); if (include != null) { if (include.elements == null) { res.addError("'include' value must be an array of strings.", files); } else { for (ConfigNode file : include.elements) { if (!(file.value instanceof String)) { res.addError("'include' element should be a string.", file); } } } } ConfigNode exclude = res.root.properties.get("exclude"); rootCompletions.put("exclude", new TSConfigElementHandle("exclude", "list", "Array of files and directories not to include in the project.")); if (exclude != null) { if (exclude.elements == null) { res.addError("'exclude' value must be an array of strings.", exclude); } else { for (ConfigNode file : exclude.elements) { if (!(file.value instanceof String)) { res.addError("'exclude' element should be a string.", file); } } } } rootCompletions.put("compilerOptions", new TSConfigElementHandle("compilerOptions", "object", null)); ConfigNode compilerOptions = res.root.properties.get("compilerOptions"); OPTIONS: if (compilerOptions != null) { if (compilerOptions.properties == null) { res.addError("'compilerOptions' value should be an object.", compilerOptions); break OPTIONS; } JSONArray validArray = (JSONArray) TSService.call("getCompilerOptions", res.fileObj); if (validArray == null) { res.addError("Error communicating with Node.js process", compilerOptions); break OPTIONS; } HashMap<String, TSConfigElementHandle> validMap = new HashMap<>(); for (Object obj : validArray) { TSConfigElementHandle eh = new TSConfigElementHandle((JSONObject) obj); validMap.put(eh.getName(), eh); } compilerOptions.validMap = validMap; for (Map.Entry<String, ConfigNode> entry : compilerOptions.properties.entrySet()) { String key = entry.getKey(); ConfigNode value = entry.getValue(); TSConfigElementHandle optionInfo = validMap.get(key); if (optionInfo == null) { res.addError("Unknown compiler option '" + key + "'.", value.keyOffset, value.endOffset); continue; } checkType(res, value, optionInfo); if (key.equals("out")) { res.addError("'out' option is deprecated. Use 'outFile' instead.", value.keyOffset, value.endOffset, Severity.WARNING); } if (optionInfo.commandLineOnly) { res.addError("Option '" + key + "' is only meaningful when used from the command line.", value.keyOffset, value.endOffset, Severity.WARNING); } } } } this.result = res; }
From source file:nl.uva.sne.disambiguators.BabelNet.java
private List<String> getcandidateWordIDs(String language, String word) throws IOException, ParseException, FileNotFoundException, InterruptedException { // if (db == null || db.isClosed()) { // loadCache(); // }//from w ww.j a v a 2 s .co m List<String> ids = getFromWordIDCache(word); if (ids != null && ids.size() == 1 && ids.get(0).equals("NON-EXISTING")) { return null; } language = language.toUpperCase(); if (ids == null || ids.isEmpty()) { ids = new ArrayList<>(); URL url = new URL( "http://babelnet.io/v2/getSynsetIds?word=" + word + "&langs=" + language + "&key=" + this.key); System.err.println(url); String genreJson = IOUtils.toString(url); int count = 0; try { handleKeyLimitException(genreJson); } catch (IOException ex) { if (ex.getMessage().contains("Your key is not valid or the daily requests limit has been reached") && count < keys.length - 1) { count++; return getcandidateWordIDs(language, word); } else { throw ex; } } Object obj = JSONValue.parseWithException(genreJson); if (obj instanceof JSONArray) { JSONArray jsonArray = (JSONArray) obj; for (Object o : jsonArray) { JSONObject jo = (JSONObject) o; if (jo != null) { String id = (String) jo.get("id"); if (id != null) { ids.add(id); } } } } else if (obj instanceof JSONObject) { JSONObject jsonObj = (JSONObject) obj; String id = (String) jsonObj.get("id"); if (id != null) { ids.add(id); } } // if (db.isClosed()) { // loadCache(); // } if (ids.isEmpty()) { ids.add("NON-EXISTING"); putInWordIDCache(word, ids); return null; } putInWordIDCache(word, ids); } return ids; }
From source file:nl.uva.sne.disambiguators.BabelNet.java
private Map<String, Double> getEdgeIDs(String language, String id, String relation) throws MalformedURLException, IOException, ParseException, Exception { // if (db == null || db.isClosed()) { // loadCache(); // }//from ww w . j av a 2 s. co m String genreJson = getFromEdgesCache(id); if (genreJson == null) { URL url = new URL("http://babelnet.io/v2/getEdges?id=" + id + "&key=" + this.key); System.err.println(url); genreJson = IOUtils.toString(url); handleKeyLimitException(genreJson); if (genreJson != null) { putInEdgesCache(id, genreJson); } if (genreJson == null) { putInEdgesCache(id, "NON-EXISTING"); } } Object obj = JSONValue.parseWithException(genreJson); JSONArray edgeArray = (JSONArray) obj; Map<String, Double> map = new HashMap<>(); for (Object o : edgeArray) { JSONObject pointer = (JSONObject) ((JSONObject) o).get("pointer"); String relationGroup = (String) pointer.get("relationGroup"); String target = (String) ((JSONObject) o).get("target"); Double normalizedWeight = (Double) ((JSONObject) o).get("normalizedWeight"); Double weight = (Double) ((JSONObject) o).get("weight"); if (relationGroup.equals(relation)) { map.put(target, ((normalizedWeight + weight) / 2.0)); } } return map; }
From source file:nl.uva.sne.disambiguators.BabelNet.java
private Pair<Term, Double> babelNetDisambiguation(String language, String lemma, String sentence) throws IOException, ParseException, Exception { if (lemma == null || lemma.length() < 1) { return null; }/*from ww w. ja v a 2 s. c o m*/ // if (db == null || db.isClosed()) { // loadCache(); // } String query = lemma + " " + sentence.replaceAll("_", " "); query = URLEncoder.encode(query, "UTF-8"); String genreJson; genreJson = getFromDisambiguateCache(sentence); if (genreJson != null && genreJson.equals("NON-EXISTING")) { return null; } if (genreJson == null) { URL url = new URL( "http://babelfy.io/v1/disambiguate?text=" + query + "&lang=" + language + "&key=" + key); System.err.println(url); genreJson = IOUtils.toString(url); handleKeyLimitException(genreJson); // if (db.isClosed()) { // loadCache(); // } if (!genreJson.isEmpty() || genreJson.length() < 1) { putInDisambiguateCache(sentence, genreJson); } else { putInDisambiguateCache(sentence, "NON-EXISTING"); } } Object obj = JSONValue.parseWithException(genreJson); // Term term = null; if (obj instanceof JSONArray) { JSONArray ja = (JSONArray) obj; for (Object o : ja) { JSONObject jo = (JSONObject) o; String id = (String) jo.get("babelSynsetID"); Double score = (Double) jo.get("score"); Double globalScore = (Double) jo.get("globalScore"); Double coherenceScore = (Double) jo.get("coherenceScore"); double someScore = (score + globalScore + coherenceScore) / 3.0; String synet = getBabelnetSynset(id, language); String url = "http://babelnet.org/synset?word=" + URLEncoder.encode(id, "UTF-8"); Term t = TermFactory.create(synet, language, lemma, null, url); if (t != null) { List<Term> h = getHypernyms(language, t); t.setBroader(h); return new Pair<>(t, someScore); } } } return null; }
From source file:nl.uva.sne.disambiguators.Wikidata.java
private Set<Term> getCandidateTerms(String jsonString, String originalTerm) throws ParseException, IOException, JWNLException, MalformedURLException, InterruptedException, ExecutionException { Set<Term> terms = new HashSet<>(); JSONObject jsonObj = (JSONObject) JSONValue.parseWithException(jsonString); JSONArray search = (JSONArray) jsonObj.get("search"); for (Object obj : search) { JSONObject jObj = (JSONObject) obj; String label = (String) jObj.get("label"); if (label != null && !label.toLowerCase().contains("(disambiguation)")) { label = label.replaceAll("%(?![0-9a-fA-F]{2})", "%25"); label = label.replaceAll("\\+", "%2B"); label = java.net.URLDecoder.decode(label, "UTF-8"); label = label.replaceAll("_", " ").toLowerCase(); originalTerm = java.net.URLDecoder.decode(originalTerm, "UTF-8"); originalTerm = originalTerm.replaceAll("_", " "); String stemTitle = SemanticUtils.stem(label); String stemLema = SemanticUtils.stem(originalTerm); int dist = edu.stanford.nlp.util.StringUtils.editDistance(stemLema, stemTitle); if (stemTitle.contains(stemLema) && dist <= 10) { String url = null; Term t = new Term(label, url); t.setOriginalTerm(originalTerm); JSONArray aliases = (JSONArray) jObj.get("aliases"); if (aliases != null) { List<String> altLables = new ArrayList<>(); for (Object aObj : aliases) { String alt = (String) aObj; altLables.add(alt); }// w w w. ja v a 2s . com t.setAlternativeLables(altLables); } String description = (String) jObj.get("description"); if (description == null || !description.toLowerCase().contains("wikipedia disambiguation page")) { String id = (String) jObj.get("id"); url = "https://www.wikidata.org/wiki/" + id; t.setUrl(url); List<String> glosses = new ArrayList<>(); glosses.add(description); t.setGlosses(glosses); t.setUID(id); // List<String> broaderID = getBroaderID(id); // t.setBroaderUIDS(broaderID); // List<String> cat = getCategories(id); // t.setCategories(cat); terms.add(t); } } } } Set<Term> catTerms = new HashSet<>(); Map<String, List<String>> cats = getCategories(terms); for (Term t : terms) { List<String> cat = cats.get(t.getUID()); t.setCategories(cat); catTerms.add(t); } Map<String, List<String>> broaderIDs = getbroaderIDS(terms); Set<Term> returnTerms = new HashSet<>(); for (Term t : catTerms) { List<String> broaderID = broaderIDs.get(t.getUID()); t.setBroaderUIDS(broaderID); returnTerms.add(t); } return returnTerms; }
From source file:nl.uva.sne.disambiguators.Wikidata.java
private List<String> getNumProperty(String id, String prop) throws MalformedURLException, IOException, ParseException { URL url = new URL(page + "?action=wbgetclaims&format=json&props=&property=" + prop + "&entity=" + id); System.err.println(url);/*from w w w . j a v a 2 s. c o m*/ String jsonString = IOUtils.toString(url); JSONObject jsonObj = (JSONObject) JSONValue.parseWithException(jsonString); JSONObject claims = (JSONObject) jsonObj.get("claims"); JSONArray Jprop = (JSONArray) claims.get(prop); List<String> ids = new ArrayList<>(); if (Jprop != null) { for (Object obj : Jprop) { JSONObject jobj = (JSONObject) obj; JSONObject mainsnak = (JSONObject) jobj.get("mainsnak"); // System.err.println(mainsnak); JSONObject datavalue = (JSONObject) mainsnak.get("datavalue"); // System.err.println(datavalue); if (datavalue != null) { JSONObject value = (JSONObject) datavalue.get("value"); // System.err.println(value); java.lang.Long numericID = (java.lang.Long) value.get("numeric-id"); // System.err.println(id + " -> Q" + numericID); ids.add("Q" + numericID); } } } return ids; }