Search in sources :

Example 11 with Expression

use of org.apache.xpath.Expression in project j2objc by google.

the class FuncExtFunction method execute.

/**
   * Execute the function.  The function must return
   * a valid object.
   * @param xctxt The current execution context.
   * @return A valid XObject.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    if (xctxt.isSecureProcessing())
        throw new javax.xml.transform.TransformerException(XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED, new Object[] { toString() }));
    XObject result;
    Vector argVec = new Vector();
    int nArgs = m_argVec.size();
    for (int i = 0; i < nArgs; i++) {
        Expression arg = (Expression) m_argVec.elementAt(i);
        XObject xobj = arg.execute(xctxt);
        /*
       * Should cache the arguments for func:function
       */
        xobj.allowDetachToRelease(false);
        argVec.addElement(xobj);
    }
    //dml
    ExtensionsProvider extProvider = (ExtensionsProvider) xctxt.getOwnerObject();
    Object val = extProvider.extFunction(this, argVec);
    if (null != val) {
        result = XObject.create(val, xctxt);
    } else {
        result = new XNull();
    }
    return result;
}
Also used : ExtensionsProvider(org.apache.xpath.ExtensionsProvider) XNull(org.apache.xpath.objects.XNull) Expression(org.apache.xpath.Expression) XObject(org.apache.xpath.objects.XObject) Vector(java.util.Vector) XObject(org.apache.xpath.objects.XObject)

Example 12 with Expression

use of org.apache.xpath.Expression in project j2objc by google.

the class FuncExtFunction method callArgVisitors.

/**
   * Call the visitors for the function arguments.
   */
public void callArgVisitors(XPathVisitor visitor) {
    for (int i = 0; i < m_argVec.size(); i++) {
        Expression exp = (Expression) m_argVec.elementAt(i);
        exp.callVisitors(new ArgExtOwner(exp), visitor);
    }
}
Also used : Expression(org.apache.xpath.Expression)

Example 13 with Expression

use of org.apache.xpath.Expression 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 14 with Expression

use of org.apache.xpath.Expression in project j2objc by google.

the class FuncFormatNumb method execute.

/**
   * Execute the function.  The function must return
   * a valid object.
   * @param xctxt The current execution context.
   * @return A valid XObject.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    // A bit of an ugly hack to get our context.
    ElemTemplateElement templElem = (ElemTemplateElement) xctxt.getNamespaceContext();
    StylesheetRoot ss = templElem.getStylesheetRoot();
    java.text.DecimalFormat formatter = null;
    java.text.DecimalFormatSymbols dfs = null;
    double num = getArg0().execute(xctxt).num();
    String patternStr = getArg1().execute(xctxt).str();
    // TODO: what should be the behavior here??
    if (patternStr.indexOf(0x00A4) > 0)
        // currency sign not allowed
        ss.error(XSLTErrorResources.ER_CURRENCY_SIGN_ILLEGAL);
    // decimal-format declared in the stylesheet!(xsl:decimal-format
    try {
        Expression arg2Expr = getArg2();
        if (null != arg2Expr) {
            String dfName = arg2Expr.execute(xctxt).str();
            QName qname = new QName(dfName, xctxt.getNamespaceContext());
            dfs = ss.getDecimalFormatComposed(qname);
            if (null == dfs) {
                warn(xctxt, XSLTErrorResources.WG_NO_DECIMALFORMAT_DECLARATION, //"not found!!!
                new Object[] { dfName });
            //formatter = new java.text.DecimalFormat(patternStr);
            } else {
                //formatter = new java.text.DecimalFormat(patternStr, dfs);
                formatter = new java.text.DecimalFormat();
                formatter.setDecimalFormatSymbols(dfs);
                formatter.applyLocalizedPattern(patternStr);
            }
        }
        //else
        if (null == formatter) {
            // look for a possible default decimal-format
            dfs = ss.getDecimalFormatComposed(new QName(""));
            if (dfs != null) {
                formatter = new java.text.DecimalFormat();
                formatter.setDecimalFormatSymbols(dfs);
                formatter.applyLocalizedPattern(patternStr);
            } else {
                dfs = new java.text.DecimalFormatSymbols(java.util.Locale.US);
                dfs.setInfinity(Constants.ATTRVAL_INFINITY);
                dfs.setNaN(Constants.ATTRVAL_NAN);
                formatter = new java.text.DecimalFormat();
                formatter.setDecimalFormatSymbols(dfs);
                if (null != patternStr)
                    formatter.applyLocalizedPattern(patternStr);
            }
        }
        return new XString(formatter.format(num));
    } catch (Exception iae) {
        templElem.error(XSLTErrorResources.ER_MALFORMED_FORMAT_STRING, new Object[] { patternStr });
        return XString.EMPTYSTRING;
    //throw new XSLProcessorException(iae);
    }
}
Also used : Expression(org.apache.xpath.Expression) QName(org.apache.xml.utils.QName) XString(org.apache.xpath.objects.XString) XObject(org.apache.xpath.objects.XObject) XPathContext(org.apache.xpath.XPathContext) XString(org.apache.xpath.objects.XString) TransformerException(javax.xml.transform.TransformerException) WrongNumberArgsException(org.apache.xpath.functions.WrongNumberArgsException)

Example 15 with Expression

use of org.apache.xpath.Expression in project j2objc by google.

the class RedundentExprEliminator method findAndEliminateRedundant.

/**
   * Look through the vector from start point, looking for redundant occurances.
   * When one or more are found, create a psuedo variable declaration, insert 
   * it into the stylesheet, and replace the occurance with a reference to 
   * the psuedo variable.  When a redundent variable is found, it's slot in 
   * the vector will be replaced by null.
   * 
   * @param start The position to start looking in the vector.
   * @param firstOccuranceIndex The position of firstOccuranceOwner.
   * @param firstOccuranceOwner The owner of the expression we are looking for.
   * @param psuedoVarRecipient Where to put the psuedo variables.
   * 
   * @return The number of expression occurances that were modified.
   */
protected int findAndEliminateRedundant(int start, int firstOccuranceIndex, ExpressionOwner firstOccuranceOwner, ElemTemplateElement psuedoVarRecipient, Vector paths) throws org.w3c.dom.DOMException {
    MultistepExprHolder head = null;
    MultistepExprHolder tail = null;
    int numPathsFound = 0;
    int n = paths.size();
    Expression expr1 = firstOccuranceOwner.getExpression();
    if (DEBUG)
        assertIsLocPathIterator(expr1, firstOccuranceOwner);
    boolean isGlobal = (paths == m_absPaths);
    LocPathIterator lpi = (LocPathIterator) expr1;
    int stepCount = countSteps(lpi);
    for (int j = start; j < n; j++) {
        ExpressionOwner owner2 = (ExpressionOwner) paths.elementAt(j);
        if (null != owner2) {
            Expression expr2 = owner2.getExpression();
            boolean isEqual = expr2.deepEquals(lpi);
            if (isEqual) {
                LocPathIterator lpi2 = (LocPathIterator) expr2;
                if (null == head) {
                    head = new MultistepExprHolder(firstOccuranceOwner, stepCount, null);
                    tail = head;
                    numPathsFound++;
                }
                tail.m_next = new MultistepExprHolder(owner2, stepCount, null);
                tail = tail.m_next;
                // Null out the occurance, so we don't have to test it again.
                paths.setElementAt(null, j);
                // foundFirst = true;
                numPathsFound++;
            }
        }
    }
    // Change all globals in xsl:templates, etc, to global vars no matter what.
    if ((0 == numPathsFound) && isGlobal) {
        head = new MultistepExprHolder(firstOccuranceOwner, stepCount, null);
        numPathsFound++;
    }
    if (null != head) {
        ElemTemplateElement root = isGlobal ? psuedoVarRecipient : findCommonAncestor(head);
        LocPathIterator sharedIter = (LocPathIterator) head.m_exprOwner.getExpression();
        ElemVariable var = createPseudoVarDecl(root, sharedIter, isGlobal);
        if (DIAGNOSE_MULTISTEPLIST)
            System.err.println("Created var: " + var.getName() + (isGlobal ? "(Global)" : ""));
        QName uniquePseudoVarName = var.getName();
        while (null != head) {
            ExpressionOwner owner = head.m_exprOwner;
            if (DIAGNOSE_MULTISTEPLIST)
                diagnoseLineNumber(owner.getExpression());
            changeToVarRef(uniquePseudoVarName, owner, paths, root);
            head = head.m_next;
        }
        // Replace the first occurance with the variable's XPath, so  
        // that further reduction may take place if needed.
        paths.setElementAt(var.getSelect(), firstOccuranceIndex);
    }
    return numPathsFound;
}
Also used : Expression(org.apache.xpath.Expression) QName(org.apache.xml.utils.QName) LocPathIterator(org.apache.xpath.axes.LocPathIterator) ExpressionOwner(org.apache.xpath.ExpressionOwner)

Aggregations

Expression (org.apache.xpath.Expression)28 XObject (org.apache.xpath.objects.XObject)8 QName (org.apache.xml.utils.QName)6 TransformerException (javax.xml.transform.TransformerException)4 ExpressionOwner (org.apache.xpath.ExpressionOwner)4 XPathContext (org.apache.xpath.XPathContext)4 LocPathIterator (org.apache.xpath.axes.LocPathIterator)4 XNodeSet (org.apache.xpath.objects.XNodeSet)4 StepPattern (org.apache.xpath.patterns.StepPattern)4 Vector (java.util.Vector)2 DTM (org.apache.xml.dtm.DTM)2 DTMIterator (org.apache.xml.dtm.DTMIterator)2 DTMManager (org.apache.xml.dtm.DTMManager)2 SerializationHandler (org.apache.xml.serializer.SerializationHandler)2 XMLString (org.apache.xml.utils.XMLString)2 ExtensionsProvider (org.apache.xpath.ExtensionsProvider)2 NodeSetDTM (org.apache.xpath.NodeSetDTM)2 XPath (org.apache.xpath.XPath)2 WrongNumberArgsException (org.apache.xpath.functions.WrongNumberArgsException)2 XNull (org.apache.xpath.objects.XNull)2