Search in sources :

Example 6 with XPathContext

use of org.apache.xpath.XPathContext in project robovm by robovm.

the class ElemNumber method getLocale.

// end getMatchingAncestors method
/**
   * Get the locale we should be using.
   *
   * @param transformer non-null reference to the the current transform-time state.
   * @param contextNode The node that "." expresses.
   *
   * @return The locale to use. May be specified by "lang" attribute,
   * but if not, use default locale on the system. 
   *
   * @throws TransformerException
   */
Locale getLocale(TransformerImpl transformer, int contextNode) throws TransformerException {
    Locale locale = null;
    if (null != m_lang_avt) {
        XPathContext xctxt = transformer.getXPathContext();
        String langValue = m_lang_avt.evaluate(xctxt, contextNode, this);
        if (null != langValue) {
            // Not really sure what to do about the country code, so I use the
            // default from the system.
            // TODO: fix xml:lang handling.
            locale = new Locale(langValue.toUpperCase(), "");
            //Locale.getDefault().getDisplayCountry());
            if (null == locale) {
                transformer.getMsgMgr().warn(this, null, xctxt.getDTM(contextNode).getNode(contextNode), XSLTErrorResources.WG_LOCALE_NOT_FOUND, //"Warning: Could not find locale for xml:lang="+langValue);
                new Object[] { langValue });
                locale = Locale.getDefault();
            }
        }
    } else {
        locale = Locale.getDefault();
    }
    return locale;
}
Also used : Locale(java.util.Locale) XPathContext(org.apache.xpath.XPathContext)

Example 7 with XPathContext

use of org.apache.xpath.XPathContext in project robovm by robovm.

the class ElemExsltFunction method execute.

public void execute(TransformerImpl transformer, XObject[] args) throws TransformerException {
    XPathContext xctxt = transformer.getXPathContext();
    VariableStack vars = xctxt.getVarStack();
    // Increment the frame bottom of the variable stack by the
    // frame size
    int thisFrame = vars.getStackFrame();
    int nextFrame = vars.link(m_frameSize);
    if (m_inArgsSize < args.length) {
        throw new TransformerException("function called with too many args");
    }
    // have to clear the section of the stack frame that has params.
    if (m_inArgsSize > 0) {
        vars.clearLocalSlots(0, m_inArgsSize);
        if (args.length > 0) {
            vars.setStackFrame(thisFrame);
            NodeList children = this.getChildNodes();
            for (int i = 0; i < args.length; i++) {
                Node child = children.item(i);
                if (children.item(i) instanceof ElemParam) {
                    ElemParam param = (ElemParam) children.item(i);
                    vars.setLocalVariable(param.getIndex(), args[i], nextFrame);
                }
            }
            vars.setStackFrame(nextFrame);
        }
    }
    //  Removed ElemTemplate 'push' and 'pop' of RTFContext, in order to avoid losing the RTF context 
    //  before a value can be returned. ElemExsltFunction operates in the scope of the template that called 
    //  the function.
    //  xctxt.pushRTFContext();
    vars.setStackFrame(nextFrame);
    transformer.executeChildTemplates(this, true);
    // Reset the stack frame after the function call
    vars.unlink(thisFrame);
// Following ElemTemplate 'pop' removed -- see above.
// xctxt.popRTFContext(); 
}
Also used : VariableStack(org.apache.xpath.VariableStack) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) XPathContext(org.apache.xpath.XPathContext) TransformerException(javax.xml.transform.TransformerException)

Example 8 with XPathContext

use of org.apache.xpath.XPathContext in project robovm by robovm.

the class ElemIf method execute.

/**
   * Conditionally execute a sub-template.
   * The expression is evaluated and the resulting object is converted
   * to a boolean as if by a call to the boolean function. If the result
   * is true, then the content template is instantiated; otherwise, nothing
   * is created.
   *
   * @param transformer non-null reference to the the current transform-time state.
   *
   * @throws TransformerException
   */
public void execute(TransformerImpl transformer) throws TransformerException {
    XPathContext xctxt = transformer.getXPathContext();
    int sourceNode = xctxt.getCurrentNode();
    if (m_test.bool(xctxt, sourceNode, this)) {
        transformer.executeChildTemplates(this, true);
    }
}
Also used : XPathContext(org.apache.xpath.XPathContext)

Example 9 with XPathContext

use of org.apache.xpath.XPathContext in project robovm by robovm.

the class ElemLiteralResult method execute.

/**
   * Copy a Literal Result Element into the Result tree, copy the
   * non-excluded namespace attributes, copy the attributes not
   * of the XSLT namespace, and execute the children of the LRE.
   * @see <a href="http://www.w3.org/TR/xslt#literal-result-element">literal-result-element in XSLT Specification</a>
   *
   * @param transformer non-null reference to the the current transform-time state.
   *
   * @throws TransformerException
   */
public void execute(TransformerImpl transformer) throws TransformerException {
    SerializationHandler rhandler = transformer.getSerializationHandler();
    try {
        // JJK Bugzilla 3464, test namespace85 -- make sure LRE's
        // namespace is asserted even if default, since xsl:element
        // may have changed the context.
        rhandler.startPrefixMapping(getPrefix(), getNamespace());
        // Add namespace declarations.
        executeNSDecls(transformer);
        rhandler.startElement(getNamespace(), getLocalName(), getRawName());
    } catch (SAXException se) {
        throw new TransformerException(se);
    }
    /*
         * If we make it to here we have done a successful startElement()
         * we will do an endElement() call for balance, no matter what happens
         * in the middle.  
         */
    // tException remembers if we had an exception "in the middle"
    TransformerException tException = null;
    try {
        // Process any possible attributes from xsl:use-attribute-sets first
        super.execute(transformer);
        // Process the list of avts next
        if (null != m_avts) {
            int nAttrs = m_avts.size();
            for (int i = (nAttrs - 1); i >= 0; i--) {
                AVT avt = (AVT) m_avts.get(i);
                XPathContext xctxt = transformer.getXPathContext();
                int sourceNode = xctxt.getCurrentNode();
                String stringedValue = avt.evaluate(xctxt, sourceNode, this);
                if (null != stringedValue) {
                    // Important Note: I'm not going to check for excluded namespace 
                    // prefixes here.  It seems like it's too expensive, and I'm not 
                    // even sure this is right.  But I could be wrong, so this needs 
                    // to be tested against other implementations.
                    rhandler.addAttribute(avt.getURI(), avt.getName(), avt.getRawName(), "CDATA", stringedValue, false);
                }
            }
        // end for
        }
        // Now process all the elements in this subtree
        // TODO: Process m_extensionElementPrefixes && m_attributeSetsNames
        transformer.executeChildTemplates(this, true);
    } catch (TransformerException te) {
        // thrown in finally to prevent original exception consumed by subsequent exceptions
        tException = te;
    } catch (SAXException se) {
        tException = new TransformerException(se);
    }
    try {
        /* we need to do this endElement() to balance the
             * successful startElement() call even if 
             * there was an exception in the middle.
             * Otherwise an exception in the middle could cause a system to hang.
             */
        rhandler.endElement(getNamespace(), getLocalName(), getRawName());
    } catch (SAXException se) {
        /* we did call endElement(). If thee was an exception
             * in the middle throw that one, otherwise if there
             * was an exception from endElement() throw that one.
             */
        if (tException != null)
            throw tException;
        else
            throw new TransformerException(se);
    }
    /* If an exception was thrown in the middle but not with startElement() or
         * or endElement() then its time to let it percolate.
         */
    if (tException != null)
        throw tException;
    unexecuteNSDecls(transformer);
    // JJK Bugzilla 3464, test namespace85 -- balance explicit start.
    try {
        rhandler.endPrefixMapping(getPrefix());
    } catch (SAXException se) {
        throw new TransformerException(se);
    }
}
Also used : SerializationHandler(org.apache.xml.serializer.SerializationHandler) XPathContext(org.apache.xpath.XPathContext) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 10 with XPathContext

use of org.apache.xpath.XPathContext in project robovm by robovm.

the class ElemVariable method getValue.

/**
   * Get the XObject representation of the variable.
   *
   * @param transformer non-null reference to the the current transform-time state.
   * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
   *
   * @return the XObject representation of the variable.
   *
   * @throws TransformerException
   */
public XObject getValue(TransformerImpl transformer, int sourceNode) throws TransformerException {
    XObject var;
    XPathContext xctxt = transformer.getXPathContext();
    xctxt.pushCurrentNode(sourceNode);
    try {
        if (null != m_selectPattern) {
            var = m_selectPattern.execute(xctxt, sourceNode, this);
            var.allowDetachToRelease(false);
        } else if (null == getFirstChildElem()) {
            var = XString.EMPTYSTRING;
        } else {
            // Use result tree fragment.
            // Global variables may be deferred (see XUnresolvedVariable) and hence
            // need to be assigned to a different set of DTMs than local variables
            // so they aren't popped off the stack on return from a template.
            int df;
            ////// problem in parameters. Needs more study.
            try {
                //////////xctxt.getVarStack().link(0);
                if (// Global variable
                m_parentNode instanceof Stylesheet)
                    df = transformer.transformToGlobalRTF(this);
                else
                    df = transformer.transformToRTF(this);
            } finally {
            //////////////xctxt.getVarStack().unlink(); 
            }
            var = new XRTreeFrag(df, xctxt, this);
        }
    } finally {
        xctxt.popCurrentNode();
    }
    return var;
}
Also used : XRTreeFrag(org.apache.xpath.objects.XRTreeFrag) XPathContext(org.apache.xpath.XPathContext) XObject(org.apache.xpath.objects.XObject)

Aggregations

XPathContext (org.apache.xpath.XPathContext)73 TransformerException (javax.xml.transform.TransformerException)30 XObject (org.apache.xpath.objects.XObject)29 DTM (org.apache.xml.dtm.DTM)16 SAXException (org.xml.sax.SAXException)16 SerializationHandler (org.apache.xml.serializer.SerializationHandler)14 DTMIterator (org.apache.xml.dtm.DTMIterator)12 Vector (java.util.Vector)10 VariableStack (org.apache.xpath.VariableStack)8 NodeVector (org.apache.xml.utils.NodeVector)6 QName (org.apache.xml.utils.QName)6 ElemTemplateElement (org.apache.xalan.templates.ElemTemplateElement)4 StylesheetRoot (org.apache.xalan.templates.StylesheetRoot)4 IntStack (org.apache.xml.utils.IntStack)4 Expression (org.apache.xpath.Expression)4 XNodeSet (org.apache.xpath.objects.XNodeSet)4 XRTreeFrag (org.apache.xpath.objects.XRTreeFrag)4 Node (org.w3c.dom.Node)3 IOException (java.io.IOException)2 Hashtable (java.util.Hashtable)2