コード例 #1
0
  /**
   * 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;
  }
コード例 #2
0
ファイル: PersisterHelpers.java プロジェクト: nkabir/carrot2
  /**
   * 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);
    }
  }