use of org.apache.xpath.Expression in project robovm by robovm.
the class Compiler method compile.
/**
* Execute the XPath object from a given opcode position.
* @param opPos The current position in the xpath.m_opMap array.
* @return The result of the XPath.
*
* @throws TransformerException if there is a syntax or other error.
* @xsl.usage advanced
*/
public Expression compile(int opPos) throws TransformerException {
int op = getOp(opPos);
Expression expr = null;
// System.out.println(getPatternString()+"op: "+op);
switch(op) {
case OpCodes.OP_XPATH:
expr = compile(opPos + 2);
break;
case OpCodes.OP_OR:
expr = or(opPos);
break;
case OpCodes.OP_AND:
expr = and(opPos);
break;
case OpCodes.OP_NOTEQUALS:
expr = notequals(opPos);
break;
case OpCodes.OP_EQUALS:
expr = equals(opPos);
break;
case OpCodes.OP_LTE:
expr = lte(opPos);
break;
case OpCodes.OP_LT:
expr = lt(opPos);
break;
case OpCodes.OP_GTE:
expr = gte(opPos);
break;
case OpCodes.OP_GT:
expr = gt(opPos);
break;
case OpCodes.OP_PLUS:
expr = plus(opPos);
break;
case OpCodes.OP_MINUS:
expr = minus(opPos);
break;
case OpCodes.OP_MULT:
expr = mult(opPos);
break;
case OpCodes.OP_DIV:
expr = div(opPos);
break;
case OpCodes.OP_MOD:
expr = mod(opPos);
break;
// expr = quo(opPos); break;
case OpCodes.OP_NEG:
expr = neg(opPos);
break;
case OpCodes.OP_STRING:
expr = string(opPos);
break;
case OpCodes.OP_BOOL:
expr = bool(opPos);
break;
case OpCodes.OP_NUMBER:
expr = number(opPos);
break;
case OpCodes.OP_UNION:
expr = union(opPos);
break;
case OpCodes.OP_LITERAL:
expr = literal(opPos);
break;
case OpCodes.OP_VARIABLE:
expr = variable(opPos);
break;
case OpCodes.OP_GROUP:
expr = group(opPos);
break;
case OpCodes.OP_NUMBERLIT:
expr = numberlit(opPos);
break;
case OpCodes.OP_ARGUMENT:
expr = arg(opPos);
break;
case OpCodes.OP_EXTFUNCTION:
expr = compileExtension(opPos);
break;
case OpCodes.OP_FUNCTION:
expr = compileFunction(opPos);
break;
case OpCodes.OP_LOCATIONPATH:
expr = locationPath(opPos);
break;
case OpCodes.OP_PREDICATE:
expr = null;
// should never hit this here.
break;
case OpCodes.OP_MATCHPATTERN:
expr = matchPattern(opPos + 2);
break;
case OpCodes.OP_LOCATIONPATHPATTERN:
expr = locationPathPattern(opPos);
break;
case OpCodes.OP_QUO:
error(XPATHErrorResources.ER_UNKNOWN_OPCODE, //"ERROR! Unknown op code: "+m_opMap[opPos]);
new Object[] { "quo" });
break;
default:
error(XPATHErrorResources.ER_UNKNOWN_OPCODE, //"ERROR! Unknown op code: "+m_opMap[opPos]);
new Object[] { Integer.toString(getOp(opPos)) });
}
return expr;
}
use of org.apache.xpath.Expression in project robovm by robovm.
the class FuncDocument 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 {
int context = xctxt.getCurrentNode();
DTM dtm = xctxt.getDTM(context);
int docContext = dtm.getDocumentRoot(context);
XObject arg = (XObject) this.getArg0().execute(xctxt);
String base = "";
Expression arg1Expr = this.getArg1();
if (null != arg1Expr) {
// The URI reference may be relative. The base URI (see [3.2 Base URI])
// of the node in the second argument node-set that is first in document
// order is used as the base URI for resolving the
// relative URI into an absolute URI.
XObject arg2 = arg1Expr.execute(xctxt);
if (XObject.CLASS_NODESET == arg2.getType()) {
int baseNode = arg2.iter().nextNode();
if (baseNode == DTM.NULL) {
// See http://www.w3.org/1999/11/REC-xslt-19991116-errata#E14.
// If the second argument is an empty nodeset, this is an error.
// The processor can recover by returning an empty nodeset.
warn(xctxt, XSLTErrorResources.WG_EMPTY_SECOND_ARG, null);
XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
return nodes;
} else {
DTM baseDTM = xctxt.getDTM(baseNode);
base = baseDTM.getDocumentBaseURI();
}
// %REVIEW% This doesn't seem to be a problem with the conformance
// suite, but maybe it's just not doing a good test?
// int baseDoc = baseDTM.getDocument();
//
// if (baseDoc == DTM.NULL /* || baseDoc instanceof Stylesheet -->What to do?? */)
// {
//
// // base = ((Stylesheet)baseDoc).getBaseIdentifier();
// base = xctxt.getNamespaceContext().getBaseIdentifier();
// }
// else
// base = xctxt.getSourceTreeManager().findURIFromDoc(baseDoc);
} else {
//Can not convert other type to a node-set!;
arg2.iter();
}
} else {
// If the second argument is omitted, then it defaults to
// the node in the stylesheet that contains the expression that
// includes the call to the document function. Note that a
// zero-length URI reference is a reference to the document
// relative to which the URI reference is being resolved; thus
// document("") refers to the root node of the stylesheet;
// the tree representation of the stylesheet is exactly
// the same as if the XML document containing the stylesheet
// was the initial source document.
assertion(null != xctxt.getNamespaceContext(), "Namespace context can not be null!");
base = xctxt.getNamespaceContext().getBaseIdentifier();
}
XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
NodeSetDTM mnl = nodes.mutableNodeset();
DTMIterator iterator = (XObject.CLASS_NODESET == arg.getType()) ? arg.iter() : null;
int pos = DTM.NULL;
while ((null == iterator) || (DTM.NULL != (pos = iterator.nextNode()))) {
XMLString ref = (null != iterator) ? xctxt.getDTM(pos).getStringValue(pos) : arg.xstr();
// member.
if (null == arg1Expr && DTM.NULL != pos) {
DTM baseDTM = xctxt.getDTM(pos);
base = baseDTM.getDocumentBaseURI();
}
if (null == ref)
continue;
if (DTM.NULL == docContext) {
//"context does not have an owner document!");
error(xctxt, XSLTErrorResources.ER_NO_CONTEXT_OWNERDOC, null);
}
// From http://www.ics.uci.edu/pub/ietf/uri/rfc1630.txt
// A partial form can be distinguished from an absolute form in that the
// latter must have a colon and that colon must occur before any slash
// characters. Systems not requiring partial forms should not use any
// unencoded slashes in their naming schemes. If they do, absolute URIs
// will still work, but confusion may result.
int indexOfColon = ref.indexOf(':');
int indexOfSlash = ref.indexOf('/');
if ((indexOfColon != -1) && (indexOfSlash != -1) && (indexOfColon < indexOfSlash)) {
// The url (or filename, for that matter) is absolute.
base = null;
}
int newDoc = getDoc(xctxt, context, ref.toString(), base);
// nodes.mutableNodeset().addNode(newDoc);
if (DTM.NULL != newDoc) {
// TODO: mnl.addNodeInDocOrder(newDoc, true, xctxt); ??
if (!mnl.contains(newDoc)) {
mnl.addElement(newDoc);
}
}
if (null == iterator || newDoc == DTM.NULL)
break;
}
return nodes;
}
use of org.apache.xpath.Expression in project robovm by robovm.
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);
}
}
use of org.apache.xpath.Expression in project robovm by robovm.
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;
}
use of org.apache.xpath.Expression in project robovm by robovm.
the class FuncExtFunction method fixupVariables.
/**
* This function is used to fixup variables from QNames to stack frame
* indexes at stylesheet build time.
* @param vars List of QNames that correspond to variables. This list
* should be searched backwards for the first qualified name that
* corresponds to the variable reference qname. The position of the
* QName in the vector from the start of the vector will be its position
* in the stack frame (but variables above the globalsTop value will need
* to be offset to the current stack frame).
* NEEDSDOC @param globalsSize
*/
public void fixupVariables(java.util.Vector vars, int globalsSize) {
if (null != m_argVec) {
int nArgs = m_argVec.size();
for (int i = 0; i < nArgs; i++) {
Expression arg = (Expression) m_argVec.elementAt(i);
arg.fixupVariables(vars, globalsSize);
}
}
}
Aggregations