View Javadoc

    1   package sharin.doc.builder;
    2
    3   import java.util.List;
    4
    5   import sharin.util.StringUtils;
    6
    7   public class GenericXmlElem<E extends Elem> extends Elem {
    8
    9       public GenericXmlElem(String name, Object... nodes) {
    10          super(name, nodes);
    11      }
    12
    13      @SuppressWarnings("unchecked")
    14      @Override
    15      public E attr(String name, Object value) {
    16          return (E) super.attr(name, value);
    17      }
    18
    19      @SuppressWarnings("unchecked")
    20      @Override
    21      public E _(Object... nodes) {
    22          return (E) super._(nodes);
    23      }
    24
    25      @Override
    26      public String toString() {
    27          StringBuilder builder = new StringBuilder();
    28          builder.append("<");
    29          builder.append(StringUtils.escapeXml(getName()));
    30
    31          for (Attr attr : getAttrList()) {
    32              builder.append(" ");
    33              builder.append(StringUtils.escapeXml(attr.getName()));
    34              builder.append("=\"");
    35              builder.append(StringUtils.escapeXml(String
    36                      .valueOf(attr.getValue())));
    37              builder.append('"');
    38          }
    39
    40          List<Object> nodeList = getNodeList();
    41
    42          if (nodeList.size() > 0) {
    43              builder.append(">");
    44
    45              for (Object node : nodeList) {
    46                  String s = node.toString();
    47
    48                  if (!(node instanceof Elem)) {
    49                      s = StringUtils.escapeXml(s);
    50                  }
    51
    52                  builder.append(s);
    53              }
    54
    55              builder.append("</");
    56              builder.append(StringUtils.escapeXml(getName()));
    57              builder.append(">");
    58
    59          } else {
    60              builder.append(" />");
    61          }
    62
    63          return builder.toString();
    64      }
    65  }