/**
   * Loads words from a given {@link IResource} (UTF-8, one word per line, #-starting lines are
   * considered comments).
   */
  public static HashSet<String> load(IResource resource) throws IOException {
    final HashSet<String> words = Sets.newHashSet();

    final InputStream is = resource.open();
    if (is == null) throw new IOException("Resource returned null stream: " + resource);

    final BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

    try {

      String line;
      while ((line = reader.readLine()) != null) {
        line = line.trim();
        if (line.startsWith("#") || line.length() == 0) {
          continue;
        }

        words.add(line);
      }
    } finally {
      reader.close();
    }

    return words;
  }
 /** Attempts to load <code>resourceName</code> from the provided {@link ResourceLookup}. */
 private static HashSet<String> load(ResourceLookup resourceLookup, String resourceName) {
   final IResource resource = resourceLookup.getFirst(resourceName);
   if (resource == null) {
     throw new RuntimeException(
         "No resource named "
             + resourceName
             + " in resource lookup locations: "
             + Arrays.toString(resourceLookup.getLocators()));
   } else {
     try {
       return load(resource);
     } catch (IOException e) {
       throw new RuntimeException(
           "Resource named " + resourceName + " failed to load from: " + resource.toString());
     }
   }
 }
Exemplo n.º 3
0
  /**
   * Read and deserialize an XML resource of class <code>clazz</code>.
   *
   * @param <T> Class to be deserialized.
   * @param required If <code>true</code>, missing resources will throw an IOException.
   * @return Returns the deserialized resource or <code>null</code> if <code>required</code> is
   *     <code>false</code>.
   */
  public static <T> T read(
      ResourceLookup resourceLookup, String resource, Class<T> clazz, boolean required)
      throws IOException {
    IResource res = resourceLookup.getFirst(resource);
    if (res == null) {
      if (required) throw new IOException("Required resource not found: " + resource);
      return null;
    }

    InputStream inputStream = null;
    try {
      inputStream = new BufferedInputStream(res.open());
      try {
        return PersisterHelpers.createPersister(resourceLookup, new AnnotationStrategy())
            .read(clazz, inputStream);
      } catch (IOException e) {
        throw e;
      } catch (Exception e) {
        throw new IOException(e);
      }
    } finally {
      CloseableUtils.close(inputStream);
    }
  }