예제 #1
0
  public static void writeEncodeLinks(XMLWriter xmlw, String line) {
    int linkpos = line.indexOf("://");
    if (linkpos == -1) {
      xmlw.write(line);
      return;
    }
    int backpos = linkpos - 1;
    for (; backpos >= 0; --backpos) {
      if (Character.isWhitespace(line.charAt(backpos))) break;
    }
    ++backpos;
    if (backpos == linkpos - 1) { // require non-empty protocol
      xmlw.write(line);
      return;
    }

    int frontpos = linkpos + 3;
    for (; frontpos < line.length(); ++frontpos) {
      if (Character.isWhitespace(line.charAt(backpos))) break;
    }
    String url = line.substring(backpos, frontpos);
    xmlw.write(line.substring(0, backpos));
    xmlw.writeRaw("<a target=\"_top\" href=\"");
    xmlw.write(url);
    xmlw.writeRaw("\">");
    xmlw.write(url);
    xmlw.writeRaw("</a>");
    xmlw.write(line.substring(frontpos));
  }
예제 #2
0
  /**
   * Copies text from input to output, converting newlines into XHTML &lt;br/&gt; elements. The
   * supplied streams WILL be closed!
   */
  public void copyReader(Reader r, PrintOutputStream pos) {
    BufferedReader br = new BufferedReader(r);
    XMLWriter xmlw = new XMLWriter(pos);
    try {
      while (true) {
        String line = br.readLine();

        if (line == null) break;
        xmlw.write(line);
        // TODO: make some kind of "XMLFilterWriter" architecture if necessary
        //        writeEncodeLinks(xmlw, line);
        xmlw.writeRaw("<br/>");
      }
    } catch (Throwable t) {
      throw UniversalRuntimeException.accumulate(t, "Error rendering text as HTML");
    } finally {
      StreamCloseUtil.closeReader(r);
      pos.close();
    }
  }