Exemplo 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);
  }
  /**
   * Test the removal of 'px' if no value for pixels and there is a previous non-empty value in the
   * list.
   */
  public void testFormatAttributesNoOtherElements() {
    JDOMFactory factory = new DefaultJDOMFactory();
    Attribute attr;
    List attributes = new ArrayList();
    attr = factory.attribute("pixelsX", "100");
    attributes.add(attr);

    String message = "{pixelDepth}bits, {pixelsX}x{pixelsY}px";

    PolicyAttributesDetails details = new PolicyAttributesDetails("imageComponent", true);
    AttributesMessageFormatter attributesMessageFormatter = new AttributesMessageFormatter(details);
    String formatted = attributesMessageFormatter.format(attributes, message, "");

    assertEquals("Result is: '" + formatted + "'", "100x", formatted);
  }
  /** Test the missing first bracket (or char) if no value present. */
  public void testFormatAttributesMissingGridOpenBracket() {
    JDOMFactory factory = new DefaultJDOMFactory();
    Attribute attr;
    List attributes = new ArrayList();
    attr = factory.attribute("rows", "1");
    attributes.add(attr);
    attr = factory.attribute("columns", "2");
    attributes.add(attr);

    String message = "{element}: ''{name}'' ({rows}x{columns})";

    PolicyAttributesDetails details = new PolicyAttributesDetails("imageComponent", true);
    AttributesMessageFormatter attributesMessageFormatter = new AttributesMessageFormatter(details);
    String formatted = attributesMessageFormatter.format(attributes, message, "elementName");

    assertEquals("Result is: '" + formatted + "'", "elementName: ''(1x2)", formatted);
  }
  /** Test format with attributes and an element. */
  public void testFormatAttributesAndElement() {
    JDOMFactory factory = new DefaultJDOMFactory();
    Attribute attr;
    List attributes = new ArrayList();
    attr = factory.attribute("device", "PC");
    attributes.add(attr);
    attr = factory.attribute("pixelsX", "100");
    attributes.add(attr);
    attr = factory.attribute("pixelsY", "200");
    attributes.add(attr);
    attr = factory.attribute("pixelDepth", "24");
    attributes.add(attr);
    attr = factory.attribute("rendering", "color");
    attributes.add(attr);

    String message =
        "{value}, {element}, {assetGroup}, {device}, "
            + "{rendering}, {pixelDepth}bits, {pixelsX}x{pixelsY}px, "
            + "{widthHint}";

    PolicyAttributesDetails details = new PolicyAttributesDetails("imageComponent", true);
    AttributesMessageFormatter attributesMessageFormatter = new AttributesMessageFormatter(details);
    String formatted =
        attributesMessageFormatter.format(attributes, message, "Device Specific Image");

    assertEquals("Device Specific Image, PC, Color, 24bits, 100x200px", formatted);
  }