示例#1
0
  /**
   * Writes the given {@link Node}.
   *
   * @param node <code>Node</code> to output.
   * @throws IOException DOCUMENT ME!
   */
  public void write(Node node) throws IOException {
    writeNode(node);

    if (autoFlush) {
      flush();
    }
  }
示例#2
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();
    }
  }
示例#3
0
  /**
   * Outputs the content of the given element. If whitespace trimming is enabled then all adjacent
   * text nodes are appended together before the whitespace trimming occurs to avoid problems with
   * multiple text nodes being created due to text content that spans parser buffers in a SAX
   * parser.
   *
   * @param element DOCUMENT ME!
   * @throws IOException DOCUMENT ME!
   */
  protected void writeElementContent(Element element) throws IOException {
    boolean trim = format.isTrimText();
    boolean oldPreserve = preserve;

    if (trim) { // verify we have to before more expensive test
      preserve = isElementSpacePreserved(element);
      trim = !preserve;
    }

    if (trim) {
      // concatenate adjacent text nodes together
      // so that whitespace trimming works properly
      Text lastTextNode = null;
      StringBuilder buff = null;
      boolean textOnly = true;

      for (Node node : element.content()) {
        if (node instanceof Text) {
          if (lastTextNode == null) {
            lastTextNode = (Text) node;
          } else {
            if (buff == null) {
              buff = new StringBuilder(lastTextNode.getText());
            }

            buff.append((node).getText());
          }
        } else {
          if (!textOnly && format.isPadText()) {
            // only add the PAD_TEXT if the text itself starts with
            // whitespace
            char firstChar = 'a';
            if (buff != null) {
              firstChar = buff.charAt(0);
            } else if (lastTextNode != null) {
              firstChar = lastTextNode.getText().charAt(0);
            }

            if (Character.isWhitespace(firstChar)) {
              writer.write(PAD_TEXT);
            }
          }

          if (lastTextNode != null) {
            if (buff != null) {
              writeString(buff.toString());
              buff = null;
            } else {
              writeString(lastTextNode.getText());
            }

            if (format.isPadText()) {
              // only add the PAD_TEXT if the text itself ends
              // with whitespace
              char lastTextChar = 'a';
              if (buff != null) {
                lastTextChar = buff.charAt(buff.length() - 1);
              } else if (lastTextNode != null) {
                String txt = lastTextNode.getText();
                lastTextChar = txt.charAt(txt.length() - 1);
              }

              if (Character.isWhitespace(lastTextChar)) {
                writer.write(PAD_TEXT);
              }
            }

            lastTextNode = null;
          }

          textOnly = false;
          writeNode(node);
        }
      }

      if (lastTextNode != null) {
        if (!textOnly && format.isPadText()) {
          // only add the PAD_TEXT if the text itself starts with
          // whitespace
          char firstChar = 'a';
          if (buff != null) {
            firstChar = buff.charAt(0);
          } else {
            firstChar = lastTextNode.getText().charAt(0);
          }

          if (Character.isWhitespace(firstChar)) {
            writer.write(PAD_TEXT);
          }
        }

        if (buff != null) {
          writeString(buff.toString());
          buff = null;
        } else {
          writeString(lastTextNode.getText());
        }

        lastTextNode = null;
      }
    } else {
      Node lastTextNode = null;

      for (Node node : element.content()) {
        if (node instanceof Text) {
          writeNode(node);
          lastTextNode = node;
        } else {
          if ((lastTextNode != null) && format.isPadText()) {
            // only add the PAD_TEXT if the text itself ends with
            // whitespace
            String txt = lastTextNode.getText();
            char lastTextChar = txt.charAt(txt.length() - 1);

            if (Character.isWhitespace(lastTextChar)) {
              writer.write(PAD_TEXT);
            }
          }

          writeNode(node);

          // if ((lastTextNode != null) && format.isPadText()) {
          // writer.write(PAD_TEXT);
          // }

          lastTextNode = null;
        }
      }
    }

    preserve = oldPreserve;
  }