Search in sources :

Example 1 with ElemTextLiteral

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

the class ProcessorCharacters method startNonText.

/**
   * Receive notification of the start of the non-text event.  This
   * is sent to the current processor when any non-text event occurs.
   *
   * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
   */
public void startNonText(StylesheetHandler handler) throws org.xml.sax.SAXException {
    if (this == handler.getCurrentProcessor()) {
        handler.popProcessor();
    }
    int nChars = m_accumulator.length();
    if ((nChars > 0) && ((null != m_xslTextElement) || !XMLCharacterRecognizer.isWhiteSpace(m_accumulator)) || handler.isSpacePreserve()) {
        ElemTextLiteral elem = new ElemTextLiteral();
        elem.setDOMBackPointer(m_firstBackPointer);
        elem.setLocaterInfo(handler.getLocator());
        try {
            elem.setPrefixes(handler.getNamespaceSupport());
        } catch (TransformerException te) {
            throw new org.xml.sax.SAXException(te);
        }
        boolean doe = (null != m_xslTextElement) ? m_xslTextElement.getDisableOutputEscaping() : false;
        elem.setDisableOutputEscaping(doe);
        elem.setPreserveSpace(true);
        char[] chars = new char[nChars];
        m_accumulator.getChars(0, nChars, chars, 0);
        elem.setChars(chars);
        ElemTemplateElement parent = handler.getElemTemplateElement();
        parent.appendChild(elem);
    }
    m_accumulator.setLength(0);
    m_firstBackPointer = null;
}
Also used : ElemTextLiteral(org.apache.xalan.templates.ElemTextLiteral) TransformerException(javax.xml.transform.TransformerException) ElemTemplateElement(org.apache.xalan.templates.ElemTemplateElement)

Example 2 with ElemTextLiteral

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

the class ProcessorExsltFunction method validate.

/**
   * Non-recursive traversal of FunctionElement tree based on TreeWalker to verify that
   * there are no literal result elements except within a func:result element and that
   * the func:result element does not contain any following siblings except xsl:fallback.
   */
public void validate(ElemTemplateElement elem, StylesheetHandler handler) throws SAXException {
    String msg = "";
    while (elem != null) {
        //System.out.println("elem " + elem);
        if (elem instanceof ElemExsltFuncResult && elem.getNextSiblingElem() != null && !(elem.getNextSiblingElem() instanceof ElemFallback)) {
            msg = "func:result has an illegal following sibling (only xsl:fallback allowed)";
            handler.error(msg, new SAXException(msg));
        }
        if ((elem instanceof ElemApplyImport || elem instanceof ElemApplyTemplates || elem instanceof ElemAttribute || elem instanceof ElemCallTemplate || elem instanceof ElemComment || elem instanceof ElemCopy || elem instanceof ElemCopyOf || elem instanceof ElemElement || elem instanceof ElemLiteralResult || elem instanceof ElemNumber || elem instanceof ElemPI || elem instanceof ElemText || elem instanceof ElemTextLiteral || elem instanceof ElemValueOf) && !(ancestorIsOk(elem))) {
            msg = "misplaced literal result in a func:function container.";
            handler.error(msg, new SAXException(msg));
        }
        ElemTemplateElement nextElem = elem.getFirstChildElem();
        while (nextElem == null) {
            nextElem = elem.getNextSiblingElem();
            if (nextElem == null)
                elem = elem.getParentElem();
            if (elem == null || elem instanceof ElemExsltFunction)
                // ok
                return;
        }
        elem = nextElem;
    }
}
Also used : ElemApplyImport(org.apache.xalan.templates.ElemApplyImport) ElemFallback(org.apache.xalan.templates.ElemFallback) ElemExsltFuncResult(org.apache.xalan.templates.ElemExsltFuncResult) ElemCallTemplate(org.apache.xalan.templates.ElemCallTemplate) ElemApplyTemplates(org.apache.xalan.templates.ElemApplyTemplates) ElemComment(org.apache.xalan.templates.ElemComment) ElemTemplateElement(org.apache.xalan.templates.ElemTemplateElement) SAXException(org.xml.sax.SAXException) ElemCopy(org.apache.xalan.templates.ElemCopy) ElemText(org.apache.xalan.templates.ElemText) ElemTextLiteral(org.apache.xalan.templates.ElemTextLiteral) ElemLiteralResult(org.apache.xalan.templates.ElemLiteralResult) ElemAttribute(org.apache.xalan.templates.ElemAttribute) ElemCopyOf(org.apache.xalan.templates.ElemCopyOf) ElemElement(org.apache.xalan.templates.ElemElement) ElemPI(org.apache.xalan.templates.ElemPI) ElemExsltFunction(org.apache.xalan.templates.ElemExsltFunction) ElemNumber(org.apache.xalan.templates.ElemNumber) ElemValueOf(org.apache.xalan.templates.ElemValueOf)

Example 3 with ElemTextLiteral

use of org.apache.xalan.templates.ElemTextLiteral in project j2objc by google.

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 4 with ElemTextLiteral

use of org.apache.xalan.templates.ElemTextLiteral in project j2objc by google.

the class ProcessorExsltFunction method validate.

/**
 * Non-recursive traversal of FunctionElement tree based on TreeWalker to verify that
 * there are no literal result elements except within a func:result element and that
 * the func:result element does not contain any following siblings except xsl:fallback.
 */
public void validate(ElemTemplateElement elem, StylesheetHandler handler) throws SAXException {
    String msg = "";
    while (elem != null) {
        // System.out.println("elem " + elem);
        if (elem instanceof ElemExsltFuncResult && elem.getNextSiblingElem() != null && !(elem.getNextSiblingElem() instanceof ElemFallback)) {
            msg = "func:result has an illegal following sibling (only xsl:fallback allowed)";
            handler.error(msg, new SAXException(msg));
        }
        if ((elem instanceof ElemApplyImport || elem instanceof ElemApplyTemplates || elem instanceof ElemAttribute || elem instanceof ElemCallTemplate || elem instanceof ElemComment || elem instanceof ElemCopy || elem instanceof ElemCopyOf || elem instanceof ElemElement || elem instanceof ElemLiteralResult || elem instanceof ElemNumber || elem instanceof ElemPI || elem instanceof ElemText || elem instanceof ElemTextLiteral || elem instanceof ElemValueOf) && !(ancestorIsOk(elem))) {
            msg = "misplaced literal result in a func:function container.";
            handler.error(msg, new SAXException(msg));
        }
        ElemTemplateElement nextElem = elem.getFirstChildElem();
        while (nextElem == null) {
            nextElem = elem.getNextSiblingElem();
            if (nextElem == null)
                elem = elem.getParentElem();
            if (elem == null || elem instanceof ElemExsltFunction)
                // ok
                return;
        }
        elem = nextElem;
    }
}
Also used : ElemApplyImport(org.apache.xalan.templates.ElemApplyImport) ElemFallback(org.apache.xalan.templates.ElemFallback) ElemExsltFuncResult(org.apache.xalan.templates.ElemExsltFuncResult) ElemCallTemplate(org.apache.xalan.templates.ElemCallTemplate) ElemApplyTemplates(org.apache.xalan.templates.ElemApplyTemplates) ElemComment(org.apache.xalan.templates.ElemComment) ElemTemplateElement(org.apache.xalan.templates.ElemTemplateElement) SAXException(org.xml.sax.SAXException) ElemCopy(org.apache.xalan.templates.ElemCopy) ElemText(org.apache.xalan.templates.ElemText) ElemTextLiteral(org.apache.xalan.templates.ElemTextLiteral) ElemLiteralResult(org.apache.xalan.templates.ElemLiteralResult) ElemAttribute(org.apache.xalan.templates.ElemAttribute) ElemCopyOf(org.apache.xalan.templates.ElemCopyOf) ElemElement(org.apache.xalan.templates.ElemElement) ElemPI(org.apache.xalan.templates.ElemPI) ElemExsltFunction(org.apache.xalan.templates.ElemExsltFunction) ElemNumber(org.apache.xalan.templates.ElemNumber) ElemValueOf(org.apache.xalan.templates.ElemValueOf)

Example 5 with ElemTextLiteral

use of org.apache.xalan.templates.ElemTextLiteral 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)

Aggregations

ElemTextLiteral (org.apache.xalan.templates.ElemTextLiteral)9 ElemTemplateElement (org.apache.xalan.templates.ElemTemplateElement)8 TransformerException (javax.xml.transform.TransformerException)6 SAXException (org.xml.sax.SAXException)6 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 ElemApplyImport (org.apache.xalan.templates.ElemApplyImport)2 ElemApplyTemplates (org.apache.xalan.templates.ElemApplyTemplates)2 ElemAttribute (org.apache.xalan.templates.ElemAttribute)2 ElemCallTemplate (org.apache.xalan.templates.ElemCallTemplate)2 ElemComment (org.apache.xalan.templates.ElemComment)2 ElemCopy (org.apache.xalan.templates.ElemCopy)2 ElemCopyOf (org.apache.xalan.templates.ElemCopyOf)2 ElemElement (org.apache.xalan.templates.ElemElement)2 ElemExsltFuncResult (org.apache.xalan.templates.ElemExsltFuncResult)2 ElemExsltFunction (org.apache.xalan.templates.ElemExsltFunction)2 ElemFallback (org.apache.xalan.templates.ElemFallback)2 ElemLiteralResult (org.apache.xalan.templates.ElemLiteralResult)2 ElemNumber (org.apache.xalan.templates.ElemNumber)2