Search in sources :

Example 1 with DTMManager

use of org.apache.xml.dtm.DTMManager in project j2objc by google.

the class VariableSafeAbsRef method execute.

/**
   * Dereference the variable, and return the reference value.  Note that lazy 
   * evaluation will occur.  If a variable within scope is not found, a warning 
   * will be sent to the error listener, and an empty nodeset will be returned.
   *
   *
   * @param xctxt The runtime execution context.
   *
   * @return The evaluated variable, or an empty nodeset if not found.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject execute(XPathContext xctxt, boolean destructiveOK) throws javax.xml.transform.TransformerException {
    XNodeSet xns = (XNodeSet) super.execute(xctxt, destructiveOK);
    DTMManager dtmMgr = xctxt.getDTMManager();
    int context = xctxt.getContextNode();
    if (dtmMgr.getDTM(xns.getRoot()).getDocument() != dtmMgr.getDTM(context).getDocument()) {
        Expression expr = (Expression) xns.getContainedIter();
        xns = (XNodeSet) expr.asIterator(xctxt, context);
    }
    return xns;
}
Also used : DTMManager(org.apache.xml.dtm.DTMManager) Expression(org.apache.xpath.Expression) XNodeSet(org.apache.xpath.objects.XNodeSet)

Example 2 with DTMManager

use of org.apache.xml.dtm.DTMManager in project j2objc by google.

the class TransformerImpl method transform.

/**
   * Process the source tree to SAX parse events.
   * @param source  The input for the source tree.
   * @param shouldRelease  Flag indicating whether to release DTMManager.
   *
   * @throws TransformerException
   */
public void transform(Source source, boolean shouldRelease) throws TransformerException {
    try {
        // here or in reset(). -is  
        if (getXPathContext().getNamespaceContext() == null) {
            getXPathContext().setNamespaceContext(getStylesheet());
        }
        String base = source.getSystemId();
        // If no systemID of the source, use the base of the stylesheet.
        if (null == base) {
            base = m_stylesheetRoot.getBaseIdentifier();
        }
        // As a last resort, use the current user dir.
        if (null == base) {
            String currentDir = "";
            try {
                currentDir = System.getProperty("user.dir");
            }// user.dir not accessible from applet
             catch (SecurityException se) {
            }
            if (currentDir.startsWith(java.io.File.separator))
                base = "file://" + currentDir;
            else
                base = "file:///" + currentDir;
            base = base + java.io.File.separatorChar + source.getClass().getName();
        }
        setBaseURLOfSource(base);
        DTMManager mgr = m_xcontext.getDTMManager();
        /*
       * According to JAXP1.2, new SAXSource()/StreamSource()
       * should create an empty input tree, with a default root node. 
       * new DOMSource()creates an empty document using DocumentBuilder.
       * newDocument(); Use DocumentBuilder.newDocument() for all 3 situations,
       * since there is no clear spec. how to create an empty tree when
       * both SAXSource() and StreamSource() are used.
       */
        if ((source instanceof StreamSource && source.getSystemId() == null && ((StreamSource) source).getInputStream() == null && ((StreamSource) source).getReader() == null) || (source instanceof SAXSource && ((SAXSource) source).getInputSource() == null && ((SAXSource) source).getXMLReader() == null) || (source instanceof DOMSource && ((DOMSource) source).getNode() == null)) {
            try {
                DocumentBuilderFactory builderF = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = builderF.newDocumentBuilder();
                String systemID = source.getSystemId();
                source = new DOMSource(builder.newDocument());
                // Copy system ID from original, empty Source to new Source
                if (systemID != null) {
                    source.setSystemId(systemID);
                }
            } catch (ParserConfigurationException e) {
                fatalError(e);
            }
        }
        DTM dtm = mgr.getDTM(source, false, this, true, true);
        dtm.setDocumentBaseURI(base);
        // %REVIEW% I have to think about this. -sb
        boolean hardDelete = true;
        try {
            // NOTE: This will work because this is _NOT_ a shared DTM, and thus has
            // only a single Document node. If it could ever be an RTF or other
            // shared DTM, look at dtm.getDocumentRoot(nodeHandle).
            this.transformNode(dtm.getDocument());
        } finally {
            if (shouldRelease)
                mgr.release(dtm, hardDelete);
        }
        // Kick off the parse.  When the ContentHandler gets 
        // the startDocument event, it will call transformNode( node ).
        // reader.parse( xmlSource );
        // This has to be done to catch exceptions thrown from 
        // the transform thread spawned by the STree handler.
        Exception e = getExceptionThrown();
        if (null != e) {
            if (e instanceof javax.xml.transform.TransformerException) {
                throw (javax.xml.transform.TransformerException) e;
            } else if (e instanceof org.apache.xml.utils.WrappedRuntimeException) {
                fatalError(((org.apache.xml.utils.WrappedRuntimeException) e).getException());
            } else {
                throw new javax.xml.transform.TransformerException(e);
            }
        } else if (null != m_serializationHandler) {
            m_serializationHandler.endDocument();
        }
    } catch (org.apache.xml.utils.WrappedRuntimeException wre) {
        Throwable throwable = wre.getException();
        while (throwable instanceof org.apache.xml.utils.WrappedRuntimeException) {
            throwable = ((org.apache.xml.utils.WrappedRuntimeException) throwable).getException();
        }
        fatalError(throwable);
    }// Patch attributed to David Eisenberg <david@catcode.com>
     catch (org.xml.sax.SAXParseException spe) {
        fatalError(spe);
    } catch (org.xml.sax.SAXException se) {
        m_errorHandler.fatalError(new TransformerException(se));
    } finally {
        m_hasTransformThreadErrorCatcher = false;
        // This looks to be redundent to the one done in TransformNode.
        reset();
    }
}
Also used : DTMManager(org.apache.xml.dtm.DTMManager) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerException(javax.xml.transform.TransformerException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException) StreamSource(javax.xml.transform.stream.StreamSource) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXSource(javax.xml.transform.sax.SAXSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXException(org.xml.sax.SAXException) DTM(org.apache.xml.dtm.DTM)

Example 3 with DTMManager

use of org.apache.xml.dtm.DTMManager in project robovm by robovm.

the class TransformerImpl method transform.

/**
   * Process the source tree to SAX parse events.
   * @param source  The input for the source tree.
   * @param shouldRelease  Flag indicating whether to release DTMManager.
   *
   * @throws TransformerException
   */
public void transform(Source source, boolean shouldRelease) throws TransformerException {
    try {
        // here or in reset(). -is  
        if (getXPathContext().getNamespaceContext() == null) {
            getXPathContext().setNamespaceContext(getStylesheet());
        }
        String base = source.getSystemId();
        // If no systemID of the source, use the base of the stylesheet.
        if (null == base) {
            base = m_stylesheetRoot.getBaseIdentifier();
        }
        // As a last resort, use the current user dir.
        if (null == base) {
            String currentDir = "";
            try {
                currentDir = System.getProperty("user.dir");
            }// user.dir not accessible from applet
             catch (SecurityException se) {
            }
            if (currentDir.startsWith(java.io.File.separator))
                base = "file://" + currentDir;
            else
                base = "file:///" + currentDir;
            base = base + java.io.File.separatorChar + source.getClass().getName();
        }
        setBaseURLOfSource(base);
        DTMManager mgr = m_xcontext.getDTMManager();
        /*
       * According to JAXP1.2, new SAXSource()/StreamSource()
       * should create an empty input tree, with a default root node. 
       * new DOMSource()creates an empty document using DocumentBuilder.
       * newDocument(); Use DocumentBuilder.newDocument() for all 3 situations,
       * since there is no clear spec. how to create an empty tree when
       * both SAXSource() and StreamSource() are used.
       */
        if ((source instanceof StreamSource && source.getSystemId() == null && ((StreamSource) source).getInputStream() == null && ((StreamSource) source).getReader() == null) || (source instanceof SAXSource && ((SAXSource) source).getInputSource() == null && ((SAXSource) source).getXMLReader() == null) || (source instanceof DOMSource && ((DOMSource) source).getNode() == null)) {
            try {
                DocumentBuilderFactory builderF = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = builderF.newDocumentBuilder();
                String systemID = source.getSystemId();
                source = new DOMSource(builder.newDocument());
                // Copy system ID from original, empty Source to new Source
                if (systemID != null) {
                    source.setSystemId(systemID);
                }
            } catch (ParserConfigurationException e) {
                fatalError(e);
            }
        }
        DTM dtm = mgr.getDTM(source, false, this, true, true);
        dtm.setDocumentBaseURI(base);
        // %REVIEW% I have to think about this. -sb
        boolean hardDelete = true;
        try {
            // NOTE: This will work because this is _NOT_ a shared DTM, and thus has
            // only a single Document node. If it could ever be an RTF or other
            // shared DTM, look at dtm.getDocumentRoot(nodeHandle).
            this.transformNode(dtm.getDocument());
        } finally {
            if (shouldRelease)
                mgr.release(dtm, hardDelete);
        }
        // Kick off the parse.  When the ContentHandler gets 
        // the startDocument event, it will call transformNode( node ).
        // reader.parse( xmlSource );
        // This has to be done to catch exceptions thrown from 
        // the transform thread spawned by the STree handler.
        Exception e = getExceptionThrown();
        if (null != e) {
            if (e instanceof javax.xml.transform.TransformerException) {
                throw (javax.xml.transform.TransformerException) e;
            } else if (e instanceof org.apache.xml.utils.WrappedRuntimeException) {
                fatalError(((org.apache.xml.utils.WrappedRuntimeException) e).getException());
            } else {
                throw new javax.xml.transform.TransformerException(e);
            }
        } else if (null != m_serializationHandler) {
            m_serializationHandler.endDocument();
        }
    } catch (org.apache.xml.utils.WrappedRuntimeException wre) {
        Throwable throwable = wre.getException();
        while (throwable instanceof org.apache.xml.utils.WrappedRuntimeException) {
            throwable = ((org.apache.xml.utils.WrappedRuntimeException) throwable).getException();
        }
        fatalError(throwable);
    }// Patch attributed to David Eisenberg <david@catcode.com>
     catch (org.xml.sax.SAXParseException spe) {
        fatalError(spe);
    } catch (org.xml.sax.SAXException se) {
        m_errorHandler.fatalError(new TransformerException(se));
    } finally {
        m_hasTransformThreadErrorCatcher = false;
        // This looks to be redundent to the one done in TransformNode.
        reset();
    }
}
Also used : DTMManager(org.apache.xml.dtm.DTMManager) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerException(javax.xml.transform.TransformerException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException) StreamSource(javax.xml.transform.stream.StreamSource) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXSource(javax.xml.transform.sax.SAXSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXException(org.xml.sax.SAXException) DTM(org.apache.xml.dtm.DTM)

Example 4 with DTMManager

use of org.apache.xml.dtm.DTMManager in project robovm by robovm.

the class VariableSafeAbsRef method execute.

/**
   * Dereference the variable, and return the reference value.  Note that lazy 
   * evaluation will occur.  If a variable within scope is not found, a warning 
   * will be sent to the error listener, and an empty nodeset will be returned.
   *
   *
   * @param xctxt The runtime execution context.
   *
   * @return The evaluated variable, or an empty nodeset if not found.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject execute(XPathContext xctxt, boolean destructiveOK) throws javax.xml.transform.TransformerException {
    XNodeSet xns = (XNodeSet) super.execute(xctxt, destructiveOK);
    DTMManager dtmMgr = xctxt.getDTMManager();
    int context = xctxt.getContextNode();
    if (dtmMgr.getDTM(xns.getRoot()).getDocument() != dtmMgr.getDTM(context).getDocument()) {
        Expression expr = (Expression) xns.getContainedIter();
        xns = (XNodeSet) expr.asIterator(xctxt, context);
    }
    return xns;
}
Also used : DTMManager(org.apache.xml.dtm.DTMManager) Expression(org.apache.xpath.Expression) XNodeSet(org.apache.xpath.objects.XNodeSet)

Aggregations

DTMManager (org.apache.xml.dtm.DTMManager)4 IOException (java.io.IOException)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 TransformerException (javax.xml.transform.TransformerException)2 DOMSource (javax.xml.transform.dom.DOMSource)2 SAXSource (javax.xml.transform.sax.SAXSource)2 StreamSource (javax.xml.transform.stream.StreamSource)2 DTM (org.apache.xml.dtm.DTM)2 Expression (org.apache.xpath.Expression)2 XNodeSet (org.apache.xpath.objects.XNodeSet)2 SAXException (org.xml.sax.SAXException)2 SAXNotRecognizedException (org.xml.sax.SAXNotRecognizedException)2 SAXNotSupportedException (org.xml.sax.SAXNotSupportedException)2