Пример #1
0
 /**
  * Gets the definition of a word
  *
  * @param word the word to get
  * @param dictionary the dictionary to lookup with
  * @return the definition of the word
  * @throws IOException if an error occurred whilst reading the definition
  */
 public static Dictionary.Definition getDefinition(String word, Dictionary dictionary)
     throws IOException {
   if (dictionary == Dictionary.URBAN_DICTIONARY) {
     JsonObject obj =
         new JsonParser()
             .parse(readURLToString(dictionary.getFormattedURL(word)))
             .getAsJsonObject();
     if (!obj.get("result_type").getAsString().equals("no_results")
         && !obj.getAsJsonArray("list").isJsonNull())
       return new Dictionary.Definition(
           obj.getAsJsonArray("list").get(0).getAsJsonObject().get("definition").getAsString(),
           shortenURL("http://www.urbandictionary.com/define.php?term=" + word));
   } else if (dictionary == Dictionary.DUCK_DUCK_GO) {
     JsonObject obj =
         new JsonParser()
             .parse(readURLToString(dictionary.getFormattedURL(word)))
             .getAsJsonObject();
     if (!obj.get("AbstractText").getAsString().equals(""))
       return new Dictionary.Definition(
           obj.get("AbstractText").getAsString(),
           shortenURL("http://www.thefreedictionary.com/" + word));
   }
   return null;
 }
  @Override
  public JsonElement serialize(
      Map<GuildLocation, Set<String>> src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject obj = new JsonObject();

    try {
      if (src != null) {
        GuildLocation loc;
        String locWorld;
        Set<String> nameSet;
        Iterator<String> iter;
        JsonArray nameArray;
        JsonPrimitive nameElement;

        for (Entry<GuildLocation, Set<String>> entry : src.entrySet()) {
          loc = entry.getKey();
          locWorld = loc.getWorldName();
          nameSet = entry.getValue();

          if (nameSet == null || nameSet.isEmpty()) {
            continue;
          }

          nameArray = new JsonArray();
          iter = nameSet.iterator();
          while (iter.hasNext()) {
            nameElement = new JsonPrimitive(iter.next());
            nameArray.add(nameElement);
          }

          if (!obj.has(locWorld)) {
            obj.add(locWorld, new JsonObject());
          }

          obj.get(locWorld).getAsJsonObject().add(loc.getCoordString(), nameArray);
        }
      }
      return obj;

    } catch (Exception ex) {
      ex.printStackTrace();
      GuildsMain.p.log(
          Level.WARNING, "Error encountered while serializing a Map of FLocations to String Sets.");
      return obj;
    }
  }