Example #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);
  }
Example #2
0
  /**
   * This will signify that a DTD is being parsed, and can be used to ensure that comments and other
   * lexical structures in the DTD are not added to the JDOM <code>Document</code> object.
   *
   * @param name <code>String</code> name of element listed in DTD
   * @param publicID <code>String</code> public ID of DTD
   * @param systemID <code>String</code> system ID of DTD
   */
  public void startDTD(String name, String publicID, String systemID) throws SAXException {

    flushCharacters(); // Is this needed here?

    factory.addContent(document, factory.docType(name, publicID, systemID));
    inDTD = true;
    inInternalSubset = true;
  }
Example #3
0
  /**
   * This indicates that an unresolvable entity reference has been encountered, normally because the
   * external DTD subset has not been read.
   *
   * @param name <code>String</code> name of entity
   * @throws SAXException when things go wrong
   */
  public void skippedEntity(String name) throws SAXException {

    // We don't handle parameter entity references.
    if (name.startsWith("%")) return;

    flushCharacters();

    factory.addContent(getCurrentElement(), factory.entityRef(name));
  }
Example #4
0
  /**
   * This will indicate that a processing instruction has been encountered. (The XML declaration is
   * not a processing instruction and will not be reported.)
   *
   * @param target <code>String</code> target of PI
   * @param data <code>String</code> containing all data sent to the PI. This typically looks like
   *     one or more attribute value pairs.
   * @throws SAXException when things go wrong
   */
  public void processingInstruction(String target, String data) throws SAXException {

    if (suppress) return;

    flushCharacters();

    if (atRoot) {
      factory.addContent(document, factory.processingInstruction(target, data));
    } else {
      factory.addContent(getCurrentElement(), factory.processingInstruction(target, data));
    }
  }
Example #5
0
  public void startEntity(String name) throws SAXException {
    entityDepth++;

    if (expand || entityDepth > 1) {
      // Short cut out if we're expanding or if we're nested
      return;
    }

    // A "[dtd]" entity indicates the beginning of the external subset
    if (name.equals("[dtd]")) {
      inInternalSubset = false;
      return;
    }

    // Ignore DTD references, and translate the standard 5
    if ((!inDTD)
        && (!name.equals("amp"))
        && (!name.equals("lt"))
        && (!name.equals("gt"))
        && (!name.equals("apos"))
        && (!name.equals("quot"))) {

      if (!expand) {
        String pub = null;
        String sys = null;
        String[] ids = (String[]) externalEntities.get(name);
        if (ids != null) {
          pub = ids[0]; // may be null, that's OK
          sys = ids[1]; // may be null, that's OK
        }
        /**
         * if no current element, this entity belongs to an attribute in these cases, it is an error
         * on the part of the parser to call startEntity but this will help in some cases. See
         * org/xml/sax/ext/LexicalHandler.html#startEntity(java.lang.String) for more information
         */
        if (!atRoot) {
          flushCharacters();
          EntityRef entity = factory.entityRef(name, pub, sys);

          // no way to tell if the entity was from an attribute or element so just assume element
          factory.addContent(getCurrentElement(), entity);
        }
        suppress = true;
      }
    }
  }
Example #6
0
 /**
  * Pushes an element onto the tree under construction. Allows subclasses to put content under a
  * dummy root element which is useful for building content that would otherwise be a non-well
  * formed document.
  *
  * @param element root element under which content will be built
  */
 protected void pushElement(Element element) {
   if (atRoot) {
     document.setRootElement(element); // XXX should we use a factory call?
     atRoot = false;
   } else {
     factory.addContent(currentElement, element);
   }
   currentElement = element;
 }
Example #7
0
  /**
   * This reports that a comments is parsed. If not in the DTD, this comment is added to the current
   * JDOM <code>Element</code>, or the <code>Document</code> itself if at that level.
   *
   * @param ch <code>ch[]</code> array of comment characters.
   * @param start <code>int</code> index to start reading from.
   * @param length <code>int</code> length of data.
   * @throws SAXException
   */
  public void comment(char[] ch, int start, int length) throws SAXException {

    if (suppress) return;

    flushCharacters();

    String commentText = new String(ch, start, length);
    if (inDTD && inInternalSubset && (expand == false)) {
      internalSubset.append("  <!--").append(commentText).append("-->\n");
      return;
    }
    if ((!inDTD) && (!commentText.equals(""))) {
      if (atRoot) {
        factory.addContent(document, factory.comment(commentText));
      } else {
        factory.addContent(getCurrentElement(), factory.comment(commentText));
      }
    }
  }
Example #8
0
  /**
   * Flush the given string into the document. This is a protected method so subclassers can control
   * text handling without knowledge of the internals of this class.
   *
   * @param data string to flush
   */
  protected void flushCharacters(String data) throws SAXException {
    if (data.length() == 0) {
      previousCDATA = inCDATA;
      return;
    }

    /**
     * This is commented out because of some problems with the inline DTDs that Xerces seems to
     * have. if (!inDTD) { if (inEntity) { getCurrentElement().setContent(factory.text(data)); }
     * else { getCurrentElement().addContent(factory.text(data)); }
     */
    if (previousCDATA) {
      factory.addContent(getCurrentElement(), factory.cdata(data));
    } else {
      factory.addContent(getCurrentElement(), factory.text(data));
    }

    previousCDATA = inCDATA;
  }
Example #9
0
  /**
   * This reports the occurrence of an actual element. It will include the element's attributes,
   * with the exception of XML vocabulary specific attributes, such as <code>
   * xmlns:[namespace prefix]</code> and <code>xsi:schemaLocation</code>.
   *
   * @param namespaceURI <code>String</code> namespace URI this element is associated with, or an
   *     empty <code>String</code>
   * @param localName <code>String</code> name of element (with no namespace prefix, if one is
   *     present)
   * @param qName <code>String</code> XML 1.0 version of element name: [namespace
   *     prefix]:[localName]
   * @param atts <code>Attributes</code> list for this element
   * @throws SAXException when things go wrong
   */
  public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
      throws SAXException {
    if (suppress) return;

    Element element = null;

    if ((namespaceURI != null) && (!namespaceURI.equals(""))) {
      String prefix = "";

      // Determine any prefix on the Element
      if (!qName.equals(localName)) {
        int split = qName.indexOf(":");
        prefix = qName.substring(0, split);
      }
      Namespace elementNamespace = Namespace.getNamespace(prefix, namespaceURI);
      element = factory.element(localName, elementNamespace);
    } else {
      element = factory.element(localName);
    }

    // Take leftover declared namespaces and add them to this element's
    // map of namespaces
    if (declaredNamespaces.size() > 0) {
      transferNamespaces(element);
    }

    // Handle attributes
    for (int i = 0, len = atts.getLength(); i < len; i++) {
      Attribute attribute = null;

      String attLocalName = atts.getLocalName(i);
      String attQName = atts.getQName(i);
      int attType = getAttributeType(atts.getType(i));

      // Bypass any xmlns attributes which might appear, as we got
      // them already in startPrefixMapping().
      // This is sometimes necessary when SAXHandler is used with
      // another source than SAXBuilder, as with JDOMResult.
      if (attQName.startsWith("xmlns:") || attQName.equals("xmlns")) {
        continue;
      }

      // First clause per http://markmail.org/message/2p245ggcjst27xe6
      // patch from Mattias Jiderhamn
      if ("".equals(attLocalName) && attQName.indexOf(":") == -1) {
        attribute = factory.attribute(attQName, atts.getValue(i), attType);
      } else if (!attQName.equals(attLocalName)) {
        String attPrefix = attQName.substring(0, attQName.indexOf(":"));
        Namespace attNs = Namespace.getNamespace(attPrefix, atts.getURI(i));

        attribute = factory.attribute(attLocalName, atts.getValue(i), attType, attNs);
      } else {
        attribute = factory.attribute(attLocalName, atts.getValue(i), attType);
      }
      factory.setAttribute(element, attribute);
    }

    flushCharacters();

    if (atRoot) {
      document.setRootElement(element); // XXX should we use a factory call?
      atRoot = false;
    } else {
      factory.addContent(getCurrentElement(), element);
    }
    currentElement = element;
  }