Example #1
0
  /** Print the tag formatted to a PrintWriter. */
  public void print(int indent, PrintWriter pw) {
    pw.print("\n");
    spaces(pw, indent);
    pw.print('<');
    pw.print(name);

    for (Map.Entry<String, String> e : attributes.entrySet()) {
      String key = e.getKey();
      String value = escape(e.getValue());
      pw.print(' ');
      pw.print(key);
      pw.print("=");
      String quote = "'";
      if (value.indexOf(quote) >= 0) quote = "\"";
      pw.print(quote);
      pw.print(value);
      pw.print(quote);
    }

    if (content.size() == 0) pw.print('/');
    else {
      pw.print('>');
      for (Object content : this.content) {
        if (content instanceof String) {
          formatted(pw, indent + 2, 60, escape((String) content));
        } else if (content instanceof Tag) {
          Tag tag = (Tag) content;
          tag.print(indent + 2, pw);
        }
      }
      pw.print("\n");
      spaces(pw, indent);
      pw.print("</");
      pw.print(name);
    }
    pw.print('>');
  }
Example #2
0
 /** Return a string representation of this Tag and all its children recursively. */
 public String toString() {
   StringWriter sw = new StringWriter();
   print(0, new PrintWriter(sw));
   return sw.toString();
 }