use of org.apache.xpath.objects.XObject in project robovm by robovm.
the class Equals method bool.
/**
* Execute a binary operation by calling execute on each of the operands,
* and then calling the operate method on the derived class.
*
*
* @param xctxt The runtime execution context.
*
* @return The XObject result of the operation.
*
* @throws javax.xml.transform.TransformerException
*/
public boolean bool(XPathContext xctxt) throws javax.xml.transform.TransformerException {
XObject left = m_left.execute(xctxt, true);
XObject right = m_right.execute(xctxt, true);
boolean result = left.equals(right) ? true : false;
left.detach();
right.detach();
return result;
}
use of org.apache.xpath.objects.XObject in project robovm by robovm.
the class Variable 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 {
org.apache.xml.utils.PrefixResolver xprefixResolver = xctxt.getNamespaceContext();
XObject result;
// XObject result = xctxt.getVariable(m_qname);
if (m_fixUpWasCalled) {
if (m_isGlobal)
result = xctxt.getVarStack().getGlobalVariable(xctxt, m_index, destructiveOK);
else
result = xctxt.getVarStack().getLocalVariable(xctxt, m_index, destructiveOK);
} else {
result = xctxt.getVarStack().getVariableOrParam(xctxt, m_qname);
}
if (null == result) {
// This should now never happen...
warn(xctxt, XPATHErrorResources.WG_ILLEGAL_VARIABLE_REFERENCE, //"VariableReference given for variable out "+
new Object[] { m_qname.getLocalPart() });
// (new RuntimeException()).printStackTrace();
// error(xctxt, XPATHErrorResources.ER_COULDNOT_GET_VAR_NAMED,
// new Object[]{ m_qname.getLocalPart() }); //"Could not get variable named "+varName);
result = new XNodeSet(xctxt.getDTMManager());
}
return result;
// }
// else
// {
// // Hack city... big time. This is needed to evaluate xpaths from extensions,
// // pending some bright light going off in my head. Some sort of callback?
// synchronized(this)
// {
// org.apache.xalan.templates.ElemVariable vvar= getElemVariable();
// if(null != vvar)
// {
// m_index = vvar.getIndex();
// m_isGlobal = vvar.getIsTopLevel();
// m_fixUpWasCalled = true;
// return execute(xctxt);
// }
// }
// throw new javax.xml.transform.TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VAR_NOT_RESOLVABLE, new Object[]{m_qname.toString()})); //"Variable not resolvable: "+m_qname);
// }
}
use of org.apache.xpath.objects.XObject in project robovm by robovm.
the class ElemApplyTemplates method transformSelectedNodes.
/**
* Perform a query if needed, and call transformNode for each child.
*
* @param transformer non-null reference to the the current transform-time state.
*
* @throws TransformerException Thrown in a variety of circumstances.
* @xsl.usage advanced
*/
public void transformSelectedNodes(TransformerImpl transformer) throws TransformerException {
final XPathContext xctxt = transformer.getXPathContext();
final int sourceNode = xctxt.getCurrentNode();
DTMIterator sourceNodes = m_selectExpression.asIterator(xctxt, sourceNode);
VariableStack vars = xctxt.getVarStack();
int nParams = getParamElemCount();
int thisframe = vars.getStackFrame();
boolean pushContextNodeListFlag = false;
try {
xctxt.pushCurrentNode(DTM.NULL);
xctxt.pushCurrentExpressionNode(DTM.NULL);
xctxt.pushSAXLocatorNull();
transformer.pushElemTemplateElement(null);
final Vector keys = (m_sortElems == null) ? null : transformer.processSortKeys(this, sourceNode);
// Sort if we need to.
if (null != keys)
sourceNodes = sortNodes(xctxt, keys, sourceNodes);
final SerializationHandler rth = transformer.getSerializationHandler();
// ContentHandler chandler = rth.getContentHandler();
final StylesheetRoot sroot = transformer.getStylesheet();
final TemplateList tl = sroot.getTemplateListComposed();
final boolean quiet = transformer.getQuietConflictWarnings();
// Should be able to get this from the iterator but there must be a bug.
DTM dtm = xctxt.getDTM(sourceNode);
int argsFrame = -1;
if (nParams > 0) {
// This code will create a section on the stack that is all the
// evaluated arguments. These will be copied into the real params
// section of each called template.
argsFrame = vars.link(nParams);
vars.setStackFrame(thisframe);
for (int i = 0; i < nParams; i++) {
ElemWithParam ewp = m_paramElems[i];
XObject obj = ewp.getValue(transformer, sourceNode);
vars.setLocalVariable(i, obj, argsFrame);
}
vars.setStackFrame(argsFrame);
}
xctxt.pushContextNodeList(sourceNodes);
pushContextNodeListFlag = true;
IntStack currentNodes = xctxt.getCurrentNodeStack();
IntStack currentExpressionNodes = xctxt.getCurrentExpressionNodeStack();
// pushParams(transformer, xctxt);
int child;
while (DTM.NULL != (child = sourceNodes.nextNode())) {
currentNodes.setTop(child);
currentExpressionNodes.setTop(child);
if (xctxt.getDTM(child) != dtm) {
dtm = xctxt.getDTM(child);
}
final int exNodeType = dtm.getExpandedTypeID(child);
final int nodeType = dtm.getNodeType(child);
final QName mode = transformer.getMode();
ElemTemplate template = tl.getTemplateFast(xctxt, child, exNodeType, mode, -1, quiet, dtm);
// See http://www.w3.org/TR/xslt#built-in-rule.
if (null == template) {
switch(nodeType) {
case DTM.DOCUMENT_FRAGMENT_NODE:
case DTM.ELEMENT_NODE:
template = sroot.getDefaultRule();
// %OPT% direct faster?
break;
case DTM.ATTRIBUTE_NODE:
case DTM.CDATA_SECTION_NODE:
case DTM.TEXT_NODE:
// if(rth.m_elemIsPending || rth.m_docPending)
// rth.flushPending(true);
transformer.pushPairCurrentMatched(sroot.getDefaultTextRule(), child);
transformer.setCurrentElement(sroot.getDefaultTextRule());
// dtm.dispatchCharactersEvents(child, chandler, false);
dtm.dispatchCharactersEvents(child, rth, false);
transformer.popCurrentMatched();
continue;
case DTM.DOCUMENT_NODE:
template = sroot.getDefaultRootRule();
break;
default:
// No default rules for processing instructions and the like.
continue;
}
} else {
transformer.setCurrentElement(template);
}
transformer.pushPairCurrentMatched(template, child);
// See comment with unlink, below
int currentFrameBottom;
if (template.m_frameSize > 0) {
xctxt.pushRTFContext();
// See comment with unlink, below
currentFrameBottom = vars.getStackFrame();
vars.link(template.m_frameSize);
// xsl:params might not be nulled.
if (/* nParams > 0 && */
template.m_inArgsSize > 0) {
int paramIndex = 0;
for (ElemTemplateElement elem = template.getFirstChildElem(); null != elem; elem = elem.getNextSiblingElem()) {
if (Constants.ELEMNAME_PARAMVARIABLE == elem.getXSLToken()) {
ElemParam ep = (ElemParam) elem;
int i;
for (i = 0; i < nParams; i++) {
ElemWithParam ewp = m_paramElems[i];
if (ewp.m_qnameID == ep.m_qnameID) {
XObject obj = vars.getLocalVariable(i, argsFrame);
vars.setLocalVariable(paramIndex, obj);
break;
}
}
if (i == nParams)
vars.setLocalVariable(paramIndex, null);
} else
break;
paramIndex++;
}
}
} else
currentFrameBottom = 0;
// each of them.
for (ElemTemplateElement t = template.m_firstChild; t != null; t = t.m_nextSibling) {
xctxt.setSAXLocator(t);
try {
transformer.pushElemTemplateElement(t);
t.execute(transformer);
} finally {
transformer.popElemTemplateElement();
}
}
if (template.m_frameSize > 0) {
// See Frank Weiss bug around 03/19/2002 (no Bugzilla report yet).
// While unlink will restore to the proper place, the real position
// may have been changed for xsl:with-param, so that variables
// can be accessed.
// of right now.
// More:
// When we entered this function, the current
// frame buffer (cfb) index in the variable stack may
// have been manually set. If we just call
// unlink(), however, it will restore the cfb to the
// previous link index from the link stack, rather than
// the manually set cfb. So,
// the only safe solution is to restore it back
// to the same position it was on entry, since we're
// really not working in a stack context here. (Bug4218)
vars.unlink(currentFrameBottom);
xctxt.popRTFContext();
}
transformer.popCurrentMatched();
}
// end while (DTM.NULL != (child = sourceNodes.nextNode()))
} catch (SAXException se) {
transformer.getErrorListener().fatalError(new TransformerException(se));
} finally {
// Unlink to the original stack frame
if (nParams > 0)
vars.unlink(thisframe);
xctxt.popSAXLocator();
if (pushContextNodeListFlag)
xctxt.popContextNodeList();
transformer.popElemTemplateElement();
xctxt.popCurrentExpressionNode();
xctxt.popCurrentNode();
sourceNodes.detach();
}
}
use of org.apache.xpath.objects.XObject in project robovm by robovm.
the class ElemCopyOf method execute.
/**
* The xsl:copy-of element can be used to insert a result tree
* fragment into the result tree, without first converting it to
* a string as xsl:value-of does (see [7.6.1 Generating Text with
* xsl:value-of]).
*
* @param transformer non-null reference to the the current transform-time state.
*
* @throws TransformerException
*/
public void execute(TransformerImpl transformer) throws TransformerException {
try {
XPathContext xctxt = transformer.getXPathContext();
int sourceNode = xctxt.getCurrentNode();
XObject value = m_selectExpression.execute(xctxt, sourceNode, this);
SerializationHandler handler = transformer.getSerializationHandler();
if (null != value) {
int type = value.getType();
String s;
switch(type) {
case XObject.CLASS_BOOLEAN:
case XObject.CLASS_NUMBER:
case XObject.CLASS_STRING:
s = value.str();
handler.characters(s.toCharArray(), 0, s.length());
break;
case XObject.CLASS_NODESET:
// System.out.println(value);
DTMIterator nl = value.iter();
// Copy the tree.
DTMTreeWalker tw = new TreeWalker2Result(transformer, handler);
int pos;
while (DTM.NULL != (pos = nl.nextNode())) {
DTM dtm = xctxt.getDTMManager().getDTM(pos);
short t = dtm.getNodeType(pos);
// generated, so we need to only walk the child nodes.
if (t == DTM.DOCUMENT_NODE) {
for (int child = dtm.getFirstChild(pos); child != DTM.NULL; child = dtm.getNextSibling(child)) {
tw.traverse(child);
}
} else if (t == DTM.ATTRIBUTE_NODE) {
SerializerUtils.addAttribute(handler, pos);
} else {
tw.traverse(pos);
}
}
// nl.detach();
break;
case XObject.CLASS_RTREEFRAG:
SerializerUtils.outputResultTreeFragment(handler, value, transformer.getXPathContext());
break;
default:
s = value.str();
handler.characters(s.toCharArray(), 0, s.length());
break;
}
}
// I don't think we want this. -sb
// if (transformer.getDebug())
// transformer.getTraceManager().fireSelectedEvent(sourceNode, this,
// "endSelect", m_selectExpression, value);
} catch (org.xml.sax.SAXException se) {
throw new TransformerException(se);
}
}
use of org.apache.xpath.objects.XObject 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;
}
Aggregations