예제 #1
0
파일: Text.java 프로젝트: aboutdata/NEFS
  /**
   * Insert the text of the file into the result tree
   *
   * <p>Processing this element inserts the contents of the URL named by the href attribute into the
   * result tree as plain text.
   */
  public void process(Context context) throws TransformerException {
    Outputter out = context.getOutputter();

    String hrefAtt = getAttribute("href");
    Expression hrefExpr = makeAttributeValueTemplate(hrefAtt);
    String href = hrefExpr.evaluateAsString(context);
    URL fileURL = null;

    try {
      try {
        fileURL = new URL(href);
      } catch (MalformedURLException e1) {
        try {
          fileURL = new URL("file:" + href);
        } catch (MalformedURLException e2) {
          System.out.println("Cannot open " + href);
          return;
        }
      }

      InputStreamReader isr = new InputStreamReader(fileURL.openStream());
      BufferedReader is = new BufferedReader(isr);

      char chars[] = new char[4096];
      int len = 0;
      while ((len = is.read(chars)) > 0) {
        out.writeContent(chars, 0, len);
      }
      is.close();
    } catch (Exception e) {
      System.out.println("Cannot read " + href);
    }
  }