예제 #1
0
파일: XMLWriter.java 프로젝트: dom4j/dom4j
  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);
    }
  }
예제 #2
0
파일: XMLWriter.java 프로젝트: dom4j/dom4j
  protected void writeComment(String text) throws IOException {
    if (format.isNewlines()) {
      println();
      indent();
    }

    writer.write("<!--");
    writer.write(text);
    writer.write("-->");

    lastOutputNodeType = Node.COMMENT_NODE;
  }
예제 #3
0
파일: XMLWriter.java 프로젝트: dom4j/dom4j
  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);
    }
  }
예제 #4
0
파일: XMLWriter.java 프로젝트: dom4j/dom4j
  /**
   * 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();
    }
  }
예제 #5
0
파일: XMLWriter.java 프로젝트: dom4j/dom4j
  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);
    }
  }
예제 #6
0
파일: XMLWriter.java 프로젝트: dom4j/dom4j
  // 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;
  }