Exemple #1
0
  public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    try {
      charsAdded = false;
      --indentLevel;

      if (lastElementClosed) {
        writePrintln();
        indent();
      }

      // XXXX: need to determine this using a stack and checking for
      // content / children
      boolean hadContent = true;

      if (hadContent) {
        writeClose(qName);
      } else {
        writeEmptyElementClose(qName);
      }

      lastOutputNodeType = Node.ELEMENT_NODE;
      lastElementClosed = true;

      super.endElement(namespaceURI, localName, qName);
    } catch (IOException e) {
      handleException(e);
    }
  }
Exemple #2
0
  protected void writeDocType(String name, String publicID, String systemID) throws IOException {
    boolean hasPublic = false;

    writer.write("<!DOCTYPE ");
    writer.write(name);

    if ((publicID != null) && (!publicID.equals(""))) {
      writer.write(" PUBLIC \"");
      writer.write(publicID);
      writer.write("\"");
      hasPublic = true;
    }

    if ((systemID != null) && (!systemID.equals(""))) {
      if (!hasPublic) {
        writer.write(" SYSTEM");
      }

      writer.write(" \"");
      writer.write(systemID);
      writer.write("\"");
    }

    writer.write(">");
    writePrintln();
  }
Exemple #3
0
  protected void writeProcessingInstruction(ProcessingInstruction pi) throws IOException {
    // indent();
    writer.write("<?");
    writer.write(pi.getName());
    writer.write(" ");
    writer.write(pi.getText());
    writer.write("?>");
    writePrintln();

    lastOutputNodeType = Node.PROCESSING_INSTRUCTION_NODE;
  }
Exemple #4
0
  public void processingInstruction(String target, String data) throws SAXException {
    try {
      indent();
      writer.write("<?");
      writer.write(target);
      writer.write(" ");
      writer.write(data);
      writer.write("?>");
      writePrintln();
      lastOutputNodeType = Node.PROCESSING_INSTRUCTION_NODE;

      super.processingInstruction(target, data);
    } catch (IOException e) {
      handleException(e);
    }
  }
Exemple #5
0
  /**
   * This will print the <code>Document</code> to the current Writer.
   *
   * <p>Warning: using your own Writer may cause the writer's preferred character encoding to be
   * ignored. If you use encodings other than UTF8, we recommend using the method that takes an
   * OutputStream instead.
   *
   * <p>Note: as with all Writers, you may need to flush() yours after this method returns.
   *
   * @param doc <code>Document</code> to format.
   * @throws IOException if there's any problem writing.
   */
  public void write(Document doc) throws IOException {
    writeDeclaration();

    if (doc.getDocType() != null) {
      indent();
      writeDocType(doc.getDocType());
    }

    for (int i = 0, size = doc.nodeCount(); i < size; i++) {
      Node node = doc.node(i);
      writeNode(node);
    }

    writePrintln();

    if (autoFlush) {
      flush();
    }
  }
Exemple #6
0
  public void startElement(
      String namespaceURI, String localName, String qName, Attributes attributes)
      throws SAXException {
    try {
      charsAdded = false;

      writePrintln();
      indent();
      writer.write("<");
      writer.write(qName);
      writeNamespaces();
      writeAttributes(attributes);
      writer.write(">");
      ++indentLevel;
      lastOutputNodeType = Node.ELEMENT_NODE;
      lastElementClosed = false;

      super.startElement(namespaceURI, localName, qName, attributes);
    } catch (IOException e) {
      handleException(e);
    }
  }
Exemple #7
0
  // Implementation methods
  // -------------------------------------------------------------------------
  protected void writeElement(Element element) throws IOException {
    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

    writePrintln();
    indent();

    writer.write("<");
    writer.write(qualifiedName);

    int previouslyDeclaredNamespaces = namespaceStack.size();
    Namespace ns = element.getNamespace();

    if (isNamespaceDeclaration(ns)) {
      namespaceStack.push(ns);
      writeNamespace(ns);
    }

    // Print out additional namespace declarations
    boolean textOnly = true;

    for (int i = 0; i < size; i++) {
      Node node = element.node(i);

      if (node instanceof Namespace) {
        Namespace additional = (Namespace) node;

        if (isNamespaceDeclaration(additional)) {
          namespaceStack.push(additional);
          writeNamespace(additional);
        }
      } else if (node instanceof Element) {
        textOnly = false;
      } else if (node instanceof Comment) {
        textOnly = false;
      }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
      writeEmptyElementClose(qualifiedName);
    } else {
      writer.write(">");

      if (textOnly) {
        // we have at least one text node so lets assume
        // that its non-empty
        writeElementContent(element);
      } else {
        // we know it's not null or empty from above
        ++indentLevel;

        writeElementContent(element);

        --indentLevel;

        writePrintln();
        indent();
      }

      writer.write("</");
      writer.write(qualifiedName);
      writer.write(">");
    }

    // remove declared namespaceStack from stack
    while (namespaceStack.size() > previouslyDeclaredNamespaces) {
      namespaceStack.pop();
    }

    lastOutputNodeType = Node.ELEMENT_NODE;
  }
Exemple #8
0
 protected void writeDocType(DocumentType docType) throws IOException {
   if (docType != null) {
     docType.write(writer);
     writePrintln();
   }
 }