Ejemplo n.º 1
0
  /**
   * Writes an XML element.
   *
   * @param xml the non-null XML element to write.
   * @param indent how many spaces to indent the element.
   */
  public void write(XMLElement xml, int indent) throws IOException {
    for (int i = 0; i < indent; i++) {
      this.writer.print(' ');
    }

    if (xml.getName() == null) {
      if (xml.getContent() != null) {
        this.writeEncoded(xml.getContent()); // aspect(xml.getContent(), "xml.getContent() - 111");
        this.writer.println();
      }
    } else {
      this.writer.print('<');
      this.writer.print(xml.getName()); // aspect(xml.getName(), "xml.getName() - 116");
      Enumeration _enum = xml._enumerateAttributeNames();

      while (_enum.hasMoreElements()) {
        String key = (String) _enum.nextElement();
        String value = xml.getAttribute(key);
        this.writer.print(" " + key + "=\""); // aspect(key, "key - 122");
        this.writeEncoded(value); // aspect(value, "value - 123");
        this.writer.print('"');
      }

      if ((xml.getContent() != null) && (xml.getContent().length() > 0)) {
        writer.print('>');
        this.writeEncoded(xml.getContent()); // aspect(xml.getContent(), "xml.getContent() - 130");
        writer.println("</" + xml.getName() + '>'); // aspect(xml.getName(), "xml.getName() - 131");
      } else if (xml.hasChildren()) {
        writer.println('>');
        _enum = xml._enumerateChildren();

        while (_enum.hasMoreElements()) {
          XMLElement child = (XMLElement) _enum.nextElement();
          this.write(child, indent + 4);
        }

        for (int i = 0; i < indent; i++) {
          this.writer.print(' ');
        }

        this.writer.println(
            "</" + xml.getName() + ">"); // aspect(xml.getName(), "xml.getName() - 138");
      } else {
        this.writer.println("/>");
      }
    }

    this.writer.flush();
  }
Ejemplo n.º 2
0
  /**
   * This method is called when the end of an XML elemnt is encountered.
   *
   * @see #startElement
   * @param name the name of the element
   * @param nsPrefix the prefix used to identify the namespace
   * @param nsSystemID the system ID associated with the namespace
   */
  public void endElement(String name, String nsPrefix, String nsSystemID) {
    XMLElement elt = (XMLElement) this.stack.pop();

    if (elt.getChildrenCount() == 1) {
      XMLElement child = elt.getChildAtIndex(0);

      if (child.getName() == null) {
        elt.setContent(child.getContent());
        elt.removeChildAtIndex(0);
      }
    }
  }