use of org.apache.xml.dtm.DTMIterator in project robovm by robovm.
the class NotEqualComparator method compare.
/**
* Tell if one object is less than the other.
*
* @param obj2 Object to compare this nodeset to
* @param comparator Comparator to use
*
* @return See the comments below for each object type comparison
*
* @throws javax.xml.transform.TransformerException
*/
public boolean compare(XObject obj2, Comparator comparator) throws javax.xml.transform.TransformerException {
boolean result = false;
int type = obj2.getType();
if (XObject.CLASS_NODESET == type) {
// %OPT% This should be XMLString based instead of string based...
// From http://www.w3.org/TR/xpath:
// If both objects to be compared are node-sets, then the comparison
// will be true if and only if there is a node in the first node-set
// and a node in the second node-set such that the result of performing
// the comparison on the string-values of the two nodes is true.
// Note this little gem from the draft:
// NOTE: If $x is bound to a node-set, then $x="foo"
// does not mean the same as not($x!="foo"): the former
// is true if and only if some node in $x has the string-value
// foo; the latter is true if and only if all nodes in $x have
// the string-value foo.
DTMIterator list1 = iterRaw();
DTMIterator list2 = ((XNodeSet) obj2).iterRaw();
int node1;
java.util.Vector node2Strings = null;
while (DTM.NULL != (node1 = list1.nextNode())) {
XMLString s1 = getStringFromNode(node1);
if (null == node2Strings) {
int node2;
while (DTM.NULL != (node2 = list2.nextNode())) {
XMLString s2 = getStringFromNode(node2);
if (comparator.compareStrings(s1, s2)) {
result = true;
break;
}
if (null == node2Strings)
node2Strings = new java.util.Vector();
node2Strings.addElement(s2);
}
} else {
int n = node2Strings.size();
for (int i = 0; i < n; i++) {
if (comparator.compareStrings(s1, (XMLString) node2Strings.elementAt(i))) {
result = true;
break;
}
}
}
}
list1.reset();
list2.reset();
} else if (XObject.CLASS_BOOLEAN == type) {
// From http://www.w3.org/TR/xpath:
// If one object to be compared is a node-set and the other is a boolean,
// then the comparison will be true if and only if the result of
// performing the comparison on the boolean and on the result of
// converting the node-set to a boolean using the boolean function
// is true.
double num1 = bool() ? 1.0 : 0.0;
double num2 = obj2.num();
result = comparator.compareNumbers(num1, num2);
} else if (XObject.CLASS_NUMBER == type) {
// From http://www.w3.org/TR/xpath:
// If one object to be compared is a node-set and the other is a number,
// then the comparison will be true if and only if there is a
// node in the node-set such that the result of performing the
// comparison on the number to be compared and on the result of
// converting the string-value of that node to a number using
// the number function is true.
DTMIterator list1 = iterRaw();
double num2 = obj2.num();
int node;
while (DTM.NULL != (node = list1.nextNode())) {
double num1 = getNumberFromNode(node);
if (comparator.compareNumbers(num1, num2)) {
result = true;
break;
}
}
list1.reset();
} else if (XObject.CLASS_RTREEFRAG == type) {
XMLString s2 = obj2.xstr();
DTMIterator list1 = iterRaw();
int node;
while (DTM.NULL != (node = list1.nextNode())) {
XMLString s1 = getStringFromNode(node);
if (comparator.compareStrings(s1, s2)) {
result = true;
break;
}
}
list1.reset();
} else if (XObject.CLASS_STRING == type) {
// From http://www.w3.org/TR/xpath:
// If one object to be compared is a node-set and the other is a
// string, then the comparison will be true if and only if there
// is a node in the node-set such that the result of performing
// the comparison on the string-value of the node and the other
// string is true.
XMLString s2 = obj2.xstr();
DTMIterator list1 = iterRaw();
int node;
while (DTM.NULL != (node = list1.nextNode())) {
XMLString s1 = getStringFromNode(node);
if (comparator.compareStrings(s1, s2)) {
result = true;
break;
}
}
list1.reset();
} else {
result = comparator.compareNumbers(this.num(), obj2.num());
}
return result;
}
use of org.apache.xml.dtm.DTMIterator in project j2objc by google.
the class XPathContext method createDTMIterator.
/**
* Create a new <code>DTMIterator</code> that holds exactly one node.
*
* @param node The node handle that the DTMIterator will iterate to.
*
* @return The newly created <code>DTMIterator</code>.
*/
public DTMIterator createDTMIterator(int node) {
// DescendantIterator iter = new DescendantIterator();
DTMIterator iter = new org.apache.xpath.axes.OneStepIteratorForward(Axis.SELF);
iter.setRoot(node, this);
return iter;
// return m_dtmManager.createDTMIterator(node);
}
use of org.apache.xml.dtm.DTMIterator in project j2objc by google.
the class ElemForEach 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);
try {
final Vector keys = (m_sortElems == null) ? null : transformer.processSortKeys(this, sourceNode);
// Sort if we need to.
if (null != keys)
sourceNodes = sortNodes(xctxt, keys, sourceNodes);
xctxt.pushCurrentNode(DTM.NULL);
IntStack currentNodes = xctxt.getCurrentNodeStack();
xctxt.pushCurrentExpressionNode(DTM.NULL);
IntStack currentExpressionNodes = xctxt.getCurrentExpressionNodeStack();
xctxt.pushSAXLocatorNull();
xctxt.pushContextNodeList(sourceNodes);
transformer.pushElemTemplateElement(null);
// pushParams(transformer, xctxt);
// Should be able to get this from the iterator but there must be a bug.
DTM dtm = xctxt.getDTM(sourceNode);
int docID = sourceNode & DTMManager.IDENT_DTM_DEFAULT;
int child;
while (DTM.NULL != (child = sourceNodes.nextNode())) {
currentNodes.setTop(child);
currentExpressionNodes.setTop(child);
if ((child & DTMManager.IDENT_DTM_DEFAULT) != docID) {
dtm = xctxt.getDTM(child);
docID = child & DTMManager.IDENT_DTM_DEFAULT;
}
//final int exNodeType = dtm.getExpandedTypeID(child);
final int nodeType = dtm.getNodeType(child);
// each of them.
for (ElemTemplateElement t = this.m_firstChild; t != null; t = t.m_nextSibling) {
xctxt.setSAXLocator(t);
transformer.setCurrentElement(t);
t.execute(transformer);
}
// FuncDocument and here.
if (m_doc_cache_off) {
if (DEBUG)
System.out.println("JJK***** CACHE RELEASE *****\n" + "\tdtm=" + dtm.getDocumentBaseURI());
// NOTE: This will work because this is _NOT_ a shared DTM, and thus has
// only a single Document node. If it could ever be an RTF or other
// shared DTM, this would require substantial rework.
xctxt.getSourceTreeManager().removeDocumentFromCache(dtm.getDocument());
xctxt.release(dtm, false);
}
}
} finally {
xctxt.popSAXLocator();
xctxt.popContextNodeList();
transformer.popElemTemplateElement();
xctxt.popCurrentExpressionNode();
xctxt.popCurrentNode();
sourceNodes.detach();
}
}
use of org.apache.xml.dtm.DTMIterator in project j2objc by google.
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.xml.dtm.DTMIterator in project j2objc by google.
the class XObjectFactory method create.
/**
* Create the right XObject based on the type of the object passed.
* This function <emph>can</emph> make an XObject that exposes DOM Nodes, NodeLists, and
* NodeIterators to the XSLT stylesheet as node-sets.
*
* @param val The java object which this object will wrap.
* @param xctxt The XPath context.
*
* @return the right XObject based on the type of the object passed.
*/
public static XObject create(Object val, XPathContext xctxt) {
XObject result;
if (val instanceof XObject) {
result = (XObject) val;
} else if (val instanceof String) {
result = new XString((String) val);
} else if (val instanceof Boolean) {
result = new XBoolean((Boolean) val);
} else if (val instanceof Number) {
result = new XNumber(((Number) val));
} else if (val instanceof DTM) {
DTM dtm = (DTM) val;
try {
int dtmRoot = dtm.getDocument();
DTMAxisIterator iter = dtm.getAxisIterator(Axis.SELF);
iter.setStartNode(dtmRoot);
DTMIterator iterator = new OneStepIterator(iter, Axis.SELF);
iterator.setRoot(dtmRoot, xctxt);
result = new XNodeSet(iterator);
} catch (Exception ex) {
throw new org.apache.xml.utils.WrappedRuntimeException(ex);
}
} else if (val instanceof DTMAxisIterator) {
DTMAxisIterator iter = (DTMAxisIterator) val;
try {
DTMIterator iterator = new OneStepIterator(iter, Axis.SELF);
iterator.setRoot(iter.getStartNode(), xctxt);
result = new XNodeSet(iterator);
} catch (Exception ex) {
throw new org.apache.xml.utils.WrappedRuntimeException(ex);
}
} else if (val instanceof DTMIterator) {
result = new XNodeSet((DTMIterator) val);
} else // might also implement a Node!
if (val instanceof org.w3c.dom.Node) {
result = new XNodeSetForDOM((org.w3c.dom.Node) val, xctxt);
} else // also implement NodeList.
if (val instanceof org.w3c.dom.NodeList) {
result = new XNodeSetForDOM((org.w3c.dom.NodeList) val, xctxt);
} else if (val instanceof org.w3c.dom.traversal.NodeIterator) {
result = new XNodeSetForDOM((org.w3c.dom.traversal.NodeIterator) val, xctxt);
} else {
result = new XObject(val);
}
return result;
}
Aggregations