Esempio n. 1
0
  /**
   * Returns the result of an XSL Transformation as a JDOM document.
   *
   * <p>If the result of the transformation is a list of nodes, this method attempts to convert it
   * into a JDOM document. If successful, any subsequent call to {@link #getResult} will return an
   * empty list.
   *
   * <p><strong>Warning</strong>: The XSLT 1.0 specification states that the output of an XSL
   * transformation is not a well-formed XML document but a list of nodes. Applications should thus
   * use {@link #getResult} instead of this method or at least expect <code>null</code> documents to
   * be returned.
   *
   * @return the transformation result as a JDOM document or <code>null</code> if the result of the
   *     transformation can not be converted into a well-formed document.
   * @see #getResult
   */
  public Document getDocument() {
    Document doc = null;

    // Retrieve result from the document builder if not set.
    this.retrieveResult();

    if (result instanceof Document) {
      doc = (Document) result;
    } else {
      if ((result instanceof List) && (queried == false)) {
        // Try to create a document from the result nodes
        try {
          JDOMFactory f = this.getFactory();
          if (f == null) {
            f = new DefaultJDOMFactory();
          }

          doc = f.document(null);
          doc.setContent((List) result);

          result = doc;
        } catch (RuntimeException ex1) {
          // Some of the result nodes are not valid children of a
          // Document node. => return null.
        }
      }
    }
    queried = true;

    return (doc);
  }
  @Override
  public void endDocument() throws SAXException {
    // System.out.println("\nContent :: "+content);
    // System.out.println("\nTitle :: "+title);
    // System.out.println("\nAuthor :: "+author);
    // System.out.println("\nSource :: "+source);
    // System.out.println("\nCategories :: ");
    // for(String cat:categories){
    // System.out.println(cat);
    // }
    // System.out.println("\nTags :: "+tags.size());
    // for(String tag:tags){
    // System.out.println(tag);
    // }
    // System.out.println("\nEnded document");

    document.setAuthor(author);
    document.setCategories(categories);
    document.setContent(content);
    document.setPublishedDate(date);
    document.setPlace(place);
    document.setTags(tags);
    document.setTitle(title);
    document.setSource(source);
    document.setSummary(summary);
    document.setSnippet(snippet);
    document.setSource(CommonConstants.SRC_NYT);
  }
  @Test
  public void testPrintEmptyContent() {
    printer.print("");
    replayAll();

    document.setContent("");
    document.print();

    verifyAll(); // make sure Printer.print was called
  }
  /** Delete '\r' in combination CRLF or LFCR in document content */
  private void removeExtraNewLine(Document doc) {
    String content = doc.getContent().toString();
    StringBuffer buff = new StringBuffer(content);

    char ch = ' ';
    char lastch = ' ';
    for (int i = content.length() - 1; i > -1; --i) {
      ch = content.charAt(i);
      if (ch == '\n' && lastch == '\r') {
        buff.deleteCharAt(i + 1);
      }
      if (ch == '\r' && lastch == '\n') {
        buff.deleteCharAt(i);
        ch = lastch;
      }
      lastch = ch;
    } // for

    doc.setContent(new DocumentContentImpl(buff.toString()));
  } // removeExtraNewLine(Document doc)