Search in sources :

Example 26 with ElemTemplateElement

use of org.apache.xalan.templates.ElemTemplateElement in project robovm by robovm.

the class KeyManager method getNodeSetDTMByKey.

/**
   * Given a valid element key, return the corresponding node list.
   *
   * @param xctxt The XPath runtime state
   * @param doc The document node
   * @param name The key element name
   * @param ref The key value we're looking for 
   * @param nscontext The prefix resolver for the execution context
   *
   * @return A nodelist of nodes mathing the given key
   *
   * @throws javax.xml.transform.TransformerException
   */
public XNodeSet getNodeSetDTMByKey(XPathContext xctxt, int doc, QName name, XMLString ref, PrefixResolver nscontext) throws javax.xml.transform.TransformerException {
    XNodeSet nl = null;
    // yuck -sb
    ElemTemplateElement template = (ElemTemplateElement) nscontext;
    if ((null != template) && null != template.getStylesheetRoot().getKeysComposed()) {
        boolean foundDoc = false;
        if (null == m_key_tables) {
            m_key_tables = new Vector(4);
        } else {
            int nKeyTables = m_key_tables.size();
            for (int i = 0; i < nKeyTables; i++) {
                KeyTable kt = (KeyTable) m_key_tables.elementAt(i);
                if (kt.getKeyTableName().equals(name) && doc == kt.getDocKey()) {
                    nl = kt.getNodeSetDTMByKey(name, ref);
                    if (nl != null) {
                        foundDoc = true;
                        break;
                    }
                }
            }
        }
        if ((null == nl) && !foundDoc) /* && m_needToBuildKeysTable */
        {
            KeyTable kt = new KeyTable(doc, nscontext, name, template.getStylesheetRoot().getKeysComposed(), xctxt);
            m_key_tables.addElement(kt);
            if (doc == kt.getDocKey()) {
                foundDoc = true;
                nl = kt.getNodeSetDTMByKey(name, ref);
            }
        }
    }
    return nl;
}
Also used : Vector(java.util.Vector) ElemTemplateElement(org.apache.xalan.templates.ElemTemplateElement) XNodeSet(org.apache.xpath.objects.XNodeSet)

Example 27 with ElemTemplateElement

use of org.apache.xalan.templates.ElemTemplateElement in project robovm by robovm.

the class TransformerImpl method transformNode.

/**
   * Process the source node to the output result, if the
   * processor supports the "http://xml.org/trax/features/dom/input"
   * feature.
   * %REVIEW% Do we need a Node version of this?
   * @param node  The input source node, which can be any valid DTM node.
   *
   * @throws TransformerException
   */
public void transformNode(int node) throws TransformerException {
    //dml
    setExtensionsTable(getStylesheet());
    // Make sure we're not writing to the same output content handler.
    synchronized (m_serializationHandler) {
        m_hasBeenReset = false;
        XPathContext xctxt = getXPathContext();
        DTM dtm = xctxt.getDTM(node);
        try {
            pushGlobalVars(node);
            // ==========
            // Give the top-level templates a chance to pass information into 
            // the context (this is mainly for setting up tables for extensions).
            StylesheetRoot stylesheet = this.getStylesheet();
            int n = stylesheet.getGlobalImportCount();
            for (int i = 0; i < n; i++) {
                StylesheetComposed imported = stylesheet.getGlobalImport(i);
                int includedCount = imported.getIncludeCountComposed();
                for (int j = -1; j < includedCount; j++) {
                    Stylesheet included = imported.getIncludeComposed(j);
                    included.runtimeInit(this);
                    for (ElemTemplateElement child = included.getFirstChildElem(); child != null; child = child.getNextSiblingElem()) {
                        child.runtimeInit(this);
                    }
                }
            }
            // ===========        
            // System.out.println("Calling applyTemplateToNode - "+Thread.currentThread().getName());
            DTMIterator dtmIter = new org.apache.xpath.axes.SelfIteratorNoPredicate();
            dtmIter.setRoot(node, xctxt);
            xctxt.pushContextNodeList(dtmIter);
            try {
                this.applyTemplateToNode(null, null, node);
            } finally {
                xctxt.popContextNodeList();
            }
            // System.out.println("Done with applyTemplateToNode - "+Thread.currentThread().getName());
            if (null != m_serializationHandler) {
                m_serializationHandler.endDocument();
            }
        } catch (Exception se) {
            // SAXSourceLocator
            while (se instanceof org.apache.xml.utils.WrappedRuntimeException) {
                Exception e = ((org.apache.xml.utils.WrappedRuntimeException) se).getException();
                if (null != e)
                    se = e;
            }
            if (null != m_serializationHandler) {
                try {
                    if (se instanceof org.xml.sax.SAXParseException)
                        m_serializationHandler.fatalError((org.xml.sax.SAXParseException) se);
                    else if (se instanceof TransformerException) {
                        TransformerException te = ((TransformerException) se);
                        SAXSourceLocator sl = new SAXSourceLocator(te.getLocator());
                        m_serializationHandler.fatalError(new org.xml.sax.SAXParseException(te.getMessage(), sl, te));
                    } else {
                        m_serializationHandler.fatalError(new org.xml.sax.SAXParseException(se.getMessage(), new SAXSourceLocator(), se));
                    }
                } catch (Exception e) {
                }
            }
            if (se instanceof TransformerException) {
                m_errorHandler.fatalError((TransformerException) se);
            } else if (se instanceof org.xml.sax.SAXParseException) {
                m_errorHandler.fatalError(new TransformerException(se.getMessage(), new SAXSourceLocator((org.xml.sax.SAXParseException) se), se));
            } else {
                m_errorHandler.fatalError(new TransformerException(se));
            }
        } finally {
            this.reset();
        }
    }
}
Also used : StylesheetComposed(org.apache.xalan.templates.StylesheetComposed) Stylesheet(org.apache.xalan.templates.Stylesheet) ElemTemplateElement(org.apache.xalan.templates.ElemTemplateElement) 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) DTMIterator(org.apache.xml.dtm.DTMIterator) StylesheetRoot(org.apache.xalan.templates.StylesheetRoot) XPathContext(org.apache.xpath.XPathContext) DTM(org.apache.xml.dtm.DTM) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) TransformerException(javax.xml.transform.TransformerException)

Example 28 with ElemTemplateElement

use of org.apache.xalan.templates.ElemTemplateElement in project robovm by robovm.

the class TransformerImpl method executeChildTemplates.

/**
   * Execute each of the children of a template element.
   *
   * @param elem The ElemTemplateElement that contains the children
   * that should execute.
   * @param shouldAddAttrs true if xsl:attributes should be executed.
   *
   * @throws TransformerException
   * @xsl.usage advanced
   */
public void executeChildTemplates(ElemTemplateElement elem, boolean shouldAddAttrs) throws TransformerException {
    // Does this element have any children?
    ElemTemplateElement t = elem.getFirstChildElem();
    if (null == t)
        return;
    if (elem.hasTextLitOnly() && m_optimizer) {
        char[] chars = ((ElemTextLiteral) t).getChars();
        try {
            // Have to push stuff on for tooling...
            this.pushElemTemplateElement(t);
            m_serializationHandler.characters(chars, 0, chars.length);
        } catch (SAXException se) {
            throw new TransformerException(se);
        } finally {
            this.popElemTemplateElement();
        }
        return;
    }
    //    // Check for infinite loops if we have to.
    //    boolean check = (m_stackGuard.m_recursionLimit > -1);
    //
    //    if (check)
    //      getStackGuard().push(elem, xctxt.getCurrentNode());
    XPathContext xctxt = m_xcontext;
    xctxt.pushSAXLocatorNull();
    int currentTemplateElementsTop = m_currentTemplateElements.size();
    m_currentTemplateElements.push(null);
    try {
        // each of them.
        for (; t != null; t = t.getNextSiblingElem()) {
            if (!shouldAddAttrs && t.getXSLToken() == Constants.ELEMNAME_ATTRIBUTE)
                continue;
            xctxt.setSAXLocator(t);
            m_currentTemplateElements.setElementAt(t, currentTemplateElementsTop);
            t.execute(this);
        }
    } catch (RuntimeException re) {
        TransformerException te = new TransformerException(re);
        te.setLocator(t);
        throw te;
    } finally {
        m_currentTemplateElements.pop();
        xctxt.popSAXLocator();
    }
// Check for infinite loops if we have to
//    if (check)
//      getStackGuard().pop();
}
Also used : ElemTextLiteral(org.apache.xalan.templates.ElemTextLiteral) XPathContext(org.apache.xpath.XPathContext) ElemTemplateElement(org.apache.xalan.templates.ElemTemplateElement) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 29 with ElemTemplateElement

use of org.apache.xalan.templates.ElemTemplateElement in project robovm by robovm.

the class TransformerImpl method transformToString.

/**
   * Take the contents of a template element, process it, and
   * convert it to a string.
   *
   * @param elem The parent element whose children will be output
   * as a string.
   *
   * @return The stringized result of executing the elements children.
   *
   * @throws TransformerException
   * @xsl.usage advanced
   */
public String transformToString(ElemTemplateElement elem) throws TransformerException {
    ElemTemplateElement firstChild = elem.getFirstChildElem();
    if (null == firstChild)
        return "";
    if (elem.hasTextLitOnly() && m_optimizer) {
        return ((ElemTextLiteral) firstChild).getNodeValue();
    }
    // Save the current result tree handler.
    SerializationHandler savedRTreeHandler = this.m_serializationHandler;
    // Create a Serializer object that will handle the SAX events 
    // and build the ResultTreeFrag nodes.
    StringWriter sw = (StringWriter) m_stringWriterObjectPool.getInstance();
    m_serializationHandler = (ToTextStream) m_textResultHandlerObjectPool.getInstance();
    if (null == m_serializationHandler) {
        // if we didn't get one from the pool, go make a new one
        Serializer serializer = org.apache.xml.serializer.SerializerFactory.getSerializer(m_textformat.getProperties());
        m_serializationHandler = (SerializationHandler) serializer;
    }
    m_serializationHandler.setTransformer(this);
    m_serializationHandler.setWriter(sw);
    String result;
    try {
        /* Don't call startDocument, the SerializationHandler  will
         * generate its own internal startDocument call anyways
         */
        // this.m_serializationHandler.startDocument();
        // Do the transformation of the child elements.
        executeChildTemplates(elem, true);
        this.m_serializationHandler.endDocument();
        result = sw.toString();
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerException(se);
    } finally {
        sw.getBuffer().setLength(0);
        try {
            sw.close();
        } catch (Exception ioe) {
        }
        m_stringWriterObjectPool.freeInstance(sw);
        m_serializationHandler.reset();
        m_textResultHandlerObjectPool.freeInstance(m_serializationHandler);
        // Restore the previous result tree handler.
        m_serializationHandler = savedRTreeHandler;
    }
    return result;
}
Also used : ElemTextLiteral(org.apache.xalan.templates.ElemTextLiteral) StringWriter(java.io.StringWriter) SAXException(org.xml.sax.SAXException) SerializationHandler(org.apache.xml.serializer.SerializationHandler) ElemTemplateElement(org.apache.xalan.templates.ElemTemplateElement) TransformerException(javax.xml.transform.TransformerException) 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) Serializer(org.apache.xml.serializer.Serializer)

Example 30 with ElemTemplateElement

use of org.apache.xalan.templates.ElemTemplateElement in project robovm by robovm.

the class ProcessorLRE method startElement.

/**
   * Receive notification of the start of an element.
   *
   * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
   * @param uri The Namespace URI, or an empty string.
   * @param localName The local name (without prefix), or empty string if not namespace processing.
   * @param rawName The qualified name (with prefix).
   * @param attributes The specified or defaulted attributes.
   */
public void startElement(StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes) throws org.xml.sax.SAXException {
    try {
        ElemTemplateElement p = handler.getElemTemplateElement();
        boolean excludeXSLDecl = false;
        boolean isLREAsStyleSheet = false;
        if (null == p) {
            // Literal Result Template as stylesheet.
            XSLTElementProcessor lreProcessor = handler.popProcessor();
            XSLTElementProcessor stylesheetProcessor = handler.getProcessorFor(Constants.S_XSLNAMESPACEURL, "stylesheet", "xsl:stylesheet");
            handler.pushProcessor(lreProcessor);
            Stylesheet stylesheet;
            try {
                stylesheet = getStylesheetRoot(handler);
            } catch (TransformerConfigurationException tfe) {
                throw new TransformerException(tfe);
            }
            // stylesheet.setDOMBackPointer(handler.getOriginatingNode());
            // ***** Note that we're assigning an empty locator. Is this necessary?
            SAXSourceLocator slocator = new SAXSourceLocator();
            Locator locator = handler.getLocator();
            if (null != locator) {
                slocator.setLineNumber(locator.getLineNumber());
                slocator.setColumnNumber(locator.getColumnNumber());
                slocator.setPublicId(locator.getPublicId());
                slocator.setSystemId(locator.getSystemId());
            }
            stylesheet.setLocaterInfo(slocator);
            stylesheet.setPrefixes(handler.getNamespaceSupport());
            handler.pushStylesheet(stylesheet);
            isLREAsStyleSheet = true;
            AttributesImpl stylesheetAttrs = new AttributesImpl();
            AttributesImpl lreAttrs = new AttributesImpl();
            int n = attributes.getLength();
            for (int i = 0; i < n; i++) {
                String attrLocalName = attributes.getLocalName(i);
                String attrUri = attributes.getURI(i);
                String value = attributes.getValue(i);
                if ((null != attrUri) && attrUri.equals(Constants.S_XSLNAMESPACEURL)) {
                    stylesheetAttrs.addAttribute(null, attrLocalName, attrLocalName, attributes.getType(i), attributes.getValue(i));
                } else if ((attrLocalName.startsWith("xmlns:") || attrLocalName.equals("xmlns")) && value.equals(Constants.S_XSLNAMESPACEURL)) {
                // ignore
                } else {
                    lreAttrs.addAttribute(attrUri, attrLocalName, attributes.getQName(i), attributes.getType(i), attributes.getValue(i));
                }
            }
            attributes = lreAttrs;
            // allowed on a stylesheet.
            try {
                stylesheetProcessor.setPropertiesFromAttributes(handler, "stylesheet", stylesheetAttrs, stylesheet);
            } catch (Exception e) {
                if (stylesheet.getDeclaredPrefixes() == null || !declaredXSLNS(stylesheet)) {
                    throw new org.xml.sax.SAXException(XSLMessages.createWarning(XSLTErrorResources.WG_OLD_XSLT_NS, null));
                } else {
                    throw new org.xml.sax.SAXException(e);
                }
            }
            handler.pushElemTemplateElement(stylesheet);
            ElemTemplate template = new ElemTemplate();
            if (slocator != null)
                template.setLocaterInfo(slocator);
            appendAndPush(handler, template);
            XPath rootMatch = new XPath("/", stylesheet, stylesheet, XPath.MATCH, handler.getStylesheetProcessor().getErrorListener());
            template.setMatch(rootMatch);
            // template.setDOMBackPointer(handler.getOriginatingNode());
            stylesheet.setTemplate(template);
            p = handler.getElemTemplateElement();
            excludeXSLDecl = true;
        }
        XSLTElementDef def = getElemDef();
        Class classObject = def.getClassObject();
        boolean isExtension = false;
        boolean isComponentDecl = false;
        boolean isUnknownTopLevel = false;
        while (null != p) {
            // System.out.println("Checking: "+p);
            if (p instanceof ElemLiteralResult) {
                ElemLiteralResult parentElem = (ElemLiteralResult) p;
                isExtension = parentElem.containsExtensionElementURI(uri);
            } else if (p instanceof Stylesheet) {
                Stylesheet parentElem = (Stylesheet) p;
                isExtension = parentElem.containsExtensionElementURI(uri);
                if ((false == isExtension) && (null != uri) && (uri.equals(Constants.S_BUILTIN_EXTENSIONS_URL) || uri.equals(Constants.S_BUILTIN_OLD_EXTENSIONS_URL))) {
                    isComponentDecl = true;
                } else {
                    isUnknownTopLevel = true;
                }
            }
            if (isExtension)
                break;
            p = p.getParentElem();
        }
        ElemTemplateElement elem = null;
        try {
            if (isExtension) {
                // System.out.println("Creating extension(1): "+uri);
                elem = new ElemExtensionCall();
            } else if (isComponentDecl) {
                elem = (ElemTemplateElement) classObject.newInstance();
            } else if (isUnknownTopLevel) {
                // TBD: Investigate, not sure about this.  -sb
                elem = (ElemTemplateElement) classObject.newInstance();
            } else {
                elem = (ElemTemplateElement) classObject.newInstance();
            }
            elem.setDOMBackPointer(handler.getOriginatingNode());
            elem.setLocaterInfo(handler.getLocator());
            elem.setPrefixes(handler.getNamespaceSupport(), excludeXSLDecl);
            if (elem instanceof ElemLiteralResult) {
                ((ElemLiteralResult) elem).setNamespace(uri);
                ((ElemLiteralResult) elem).setLocalName(localName);
                ((ElemLiteralResult) elem).setRawName(rawName);
                ((ElemLiteralResult) elem).setIsLiteralResultAsStylesheet(isLREAsStyleSheet);
            }
        } catch (InstantiationException ie) {
            //"Failed creating ElemLiteralResult instance!", ie);
            handler.error(XSLTErrorResources.ER_FAILED_CREATING_ELEMLITRSLT, null, ie);
        } catch (IllegalAccessException iae) {
            //"Failed creating ElemLiteralResult instance!", iae);
            handler.error(XSLTErrorResources.ER_FAILED_CREATING_ELEMLITRSLT, null, iae);
        }
        setPropertiesFromAttributes(handler, rawName, attributes, elem);
        // bit of a hack here...
        if (!isExtension && (elem instanceof ElemLiteralResult)) {
            isExtension = ((ElemLiteralResult) elem).containsExtensionElementURI(uri);
            if (isExtension) {
                // System.out.println("Creating extension(2): "+uri);
                elem = new ElemExtensionCall();
                elem.setLocaterInfo(handler.getLocator());
                elem.setPrefixes(handler.getNamespaceSupport());
                ((ElemLiteralResult) elem).setNamespace(uri);
                ((ElemLiteralResult) elem).setLocalName(localName);
                ((ElemLiteralResult) elem).setRawName(rawName);
                setPropertiesFromAttributes(handler, rawName, attributes, elem);
            }
        }
        appendAndPush(handler, elem);
    } catch (TransformerException te) {
        throw new org.xml.sax.SAXException(te);
    }
}
Also used : XPath(org.apache.xpath.XPath) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ElemTemplate(org.apache.xalan.templates.ElemTemplate) ElemTemplateElement(org.apache.xalan.templates.ElemTemplateElement) Stylesheet(org.apache.xalan.templates.Stylesheet) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Locator(org.xml.sax.Locator) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) AttributesImpl(org.xml.sax.helpers.AttributesImpl) ElemLiteralResult(org.apache.xalan.templates.ElemLiteralResult) ElemExtensionCall(org.apache.xalan.templates.ElemExtensionCall) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) TransformerException(javax.xml.transform.TransformerException)

Aggregations

ElemTemplateElement (org.apache.xalan.templates.ElemTemplateElement)35 TransformerException (javax.xml.transform.TransformerException)18 SAXException (org.xml.sax.SAXException)10 ElemTextLiteral (org.apache.xalan.templates.ElemTextLiteral)8 ElemExsltFuncResult (org.apache.xalan.templates.ElemExsltFuncResult)6 ElemExsltFunction (org.apache.xalan.templates.ElemExsltFunction)6 ElemLiteralResult (org.apache.xalan.templates.ElemLiteralResult)6 ElemVariable (org.apache.xalan.templates.ElemVariable)5 IOException (java.io.IOException)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)4 ElemParam (org.apache.xalan.templates.ElemParam)4 Stylesheet (org.apache.xalan.templates.Stylesheet)4 SAXSourceLocator (org.apache.xml.utils.SAXSourceLocator)4 XPathContext (org.apache.xpath.XPathContext)4 SAXNotRecognizedException (org.xml.sax.SAXNotRecognizedException)4 SAXNotSupportedException (org.xml.sax.SAXNotSupportedException)4 StringWriter (java.io.StringWriter)2 Vector (java.util.Vector)2 ElemApplyImport (org.apache.xalan.templates.ElemApplyImport)2