use of org.apache.xml.utils.XMLString in project j2objc by google.
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.utils.XMLString in project j2objc by google.
the class FuncSubstringAfter 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 {
XMLString s1 = m_arg0.execute(xctxt).xstr();
XMLString s2 = m_arg1.execute(xctxt).xstr();
int index = s1.indexOf(s2);
return (-1 == index) ? XString.EMPTYSTRING : (XString) s1.substring(index + s2.length());
}
use of org.apache.xml.utils.XMLString in project j2objc by google.
the class FuncKey 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 {
// TransformerImpl transformer = (TransformerImpl)xctxt;
TransformerImpl transformer = (TransformerImpl) xctxt.getOwnerObject();
XNodeSet nodes = null;
int context = xctxt.getCurrentNode();
DTM dtm = xctxt.getDTM(context);
int docContext = dtm.getDocumentRoot(context);
if (DTM.NULL == docContext) {
// path.error(context, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC); //"context does not have an owner document!");
}
String xkeyname = getArg0().execute(xctxt).str();
QName keyname = new QName(xkeyname, xctxt.getNamespaceContext());
XObject arg = getArg1().execute(xctxt);
boolean argIsNodeSetDTM = (XObject.CLASS_NODESET == arg.getType());
KeyManager kmgr = transformer.getKeyManager();
// Don't bother with nodeset logic if the thing is only one node.
if (argIsNodeSetDTM) {
XNodeSet ns = (XNodeSet) arg;
ns.setShouldCacheNodes(true);
int len = ns.getLength();
if (len <= 1)
argIsNodeSetDTM = false;
}
if (argIsNodeSetDTM) {
Hashtable usedrefs = null;
DTMIterator ni = arg.iter();
int pos;
UnionPathIterator upi = new UnionPathIterator();
upi.exprSetParent(this);
while (DTM.NULL != (pos = ni.nextNode())) {
dtm = xctxt.getDTM(pos);
XMLString ref = dtm.getStringValue(pos);
if (null == ref)
continue;
if (null == usedrefs)
usedrefs = new Hashtable();
if (usedrefs.get(ref) != null) {
// We already have 'em.
continue;
} else {
// ISTRUE being used as a dummy value.
usedrefs.put(ref, ISTRUE);
}
XNodeSet nl = kmgr.getNodeSetDTMByKey(xctxt, docContext, keyname, ref, xctxt.getNamespaceContext());
nl.setRoot(xctxt.getCurrentNode(), xctxt);
// try
// {
upi.addIterator(nl);
// }
// catch(CloneNotSupportedException cnse)
// {
// // will never happen.
// }
//mnodeset.addNodesInDocOrder(nl, xctxt); needed??
}
int current = xctxt.getCurrentNode();
upi.setRoot(current, xctxt);
nodes = new XNodeSet(upi);
} else {
XMLString ref = arg.xstr();
nodes = kmgr.getNodeSetDTMByKey(xctxt, docContext, keyname, ref, xctxt.getNamespaceContext());
nodes.setRoot(xctxt.getCurrentNode(), xctxt);
}
return nodes;
}
use of org.apache.xml.utils.XMLString in project j2objc by google.
the class ClonerToResultTree method cloneToResultTree.
// /**
// * Clone an element with or without children.
// * TODO: Fix or figure out node clone failure!
// * the error condition is severe enough to halt processing.
// *
// * @param node The node to clone
// * @param shouldCloneAttributes Flag indicating whether to
// * clone children attributes
// *
// * @throws TransformerException
// */
// public void cloneToResultTree(int node, boolean shouldCloneAttributes)
// throws TransformerException
// {
//
// try
// {
// XPathContext xctxt = m_transformer.getXPathContext();
// DTM dtm = xctxt.getDTM(node);
//
// int type = dtm.getNodeType(node);
// switch (type)
// {
// case DTM.TEXT_NODE :
// dtm.dispatchCharactersEvents(node, m_rth, false);
// break;
// case DTM.DOCUMENT_FRAGMENT_NODE :
// case DTM.DOCUMENT_NODE :
//
// // Can't clone a document, but refrain from throwing an error
// // so that copy-of will work
// break;
// case DTM.ELEMENT_NODE :
// {
// Attributes atts;
//
// if (shouldCloneAttributes)
// {
// m_rth.addAttributes(node);
// m_rth.processNSDecls(node, type, dtm);
// }
//
// String ns = dtm.getNamespaceURI(node);
// String localName = dtm.getLocalName(node);
//
// m_rth.startElement(ns, localName, dtm.getNodeNameX(node), null);
// }
// break;
// case DTM.CDATA_SECTION_NODE :
// m_rth.startCDATA();
// dtm.dispatchCharactersEvents(node, m_rth, false);
// m_rth.endCDATA();
// break;
// case DTM.ATTRIBUTE_NODE :
// m_rth.addAttribute(node);
// break;
// case DTM.COMMENT_NODE :
// XMLString xstr = dtm.getStringValue (node);
// xstr.dispatchAsComment(m_rth);
// break;
// case DTM.ENTITY_REFERENCE_NODE :
// m_rth.entityReference(dtm.getNodeNameX(node));
// break;
// case DTM.PROCESSING_INSTRUCTION_NODE :
// {
// // %REVIEW% Is the node name the same as the "target"?
// m_rth.processingInstruction(dtm.getNodeNameX(node),
// dtm.getNodeValue(node));
// }
// break;
// default :
// //"Can not create item in result tree: "+node.getNodeName());
// m_transformer.getMsgMgr().error(null,
// XSLTErrorResources.ER_CANT_CREATE_ITEM,
// new Object[]{ dtm.getNodeName(node) });
// }
// }
// catch(org.xml.sax.SAXException se)
// {
// throw new TransformerException(se);
// }
// } // end cloneToResultTree function
/**
* Clone an element with or without children.
* TODO: Fix or figure out node clone failure!
* the error condition is severe enough to halt processing.
*
* @param node The node to clone
* @param shouldCloneAttributes Flag indicating whether to
* clone children attributes
*
* @throws TransformerException
*/
public static void cloneToResultTree(int node, int nodeType, DTM dtm, SerializationHandler rth, boolean shouldCloneAttributes) throws TransformerException {
try {
switch(nodeType) {
case DTM.TEXT_NODE:
dtm.dispatchCharactersEvents(node, rth, false);
break;
case DTM.DOCUMENT_FRAGMENT_NODE:
case DTM.DOCUMENT_NODE:
// so that copy-of will work
break;
case DTM.ELEMENT_NODE:
{
// Note: SAX apparently expects "no namespace" to be
// represented as "" rather than null.
String ns = dtm.getNamespaceURI(node);
if (ns == null)
ns = "";
String localName = dtm.getLocalName(node);
// rth.startElement(ns, localName, dtm.getNodeNameX(node), null);
// don't call a real SAX startElement (as commented out above),
// call a SAX-like startElement, to be able to add attributes after this call
rth.startElement(ns, localName, dtm.getNodeNameX(node));
// xsl:attribute directive.)
if (shouldCloneAttributes) {
SerializerUtils.addAttributes(rth, node);
SerializerUtils.processNSDecls(rth, node, nodeType, dtm);
}
}
break;
case DTM.CDATA_SECTION_NODE:
rth.startCDATA();
dtm.dispatchCharactersEvents(node, rth, false);
rth.endCDATA();
break;
case DTM.ATTRIBUTE_NODE:
SerializerUtils.addAttribute(rth, node);
break;
case DTM.NAMESPACE_NODE:
// %REVIEW% Normally, these should have been handled with element.
// It's possible that someone may write a stylesheet that tries to
// clone them explicitly. If so, we need the equivalent of
// rth.addAttribute().
SerializerUtils.processNSDecls(rth, node, DTM.NAMESPACE_NODE, dtm);
break;
case DTM.COMMENT_NODE:
XMLString xstr = dtm.getStringValue(node);
xstr.dispatchAsComment(rth);
break;
case DTM.ENTITY_REFERENCE_NODE:
rth.entityReference(dtm.getNodeNameX(node));
break;
case DTM.PROCESSING_INSTRUCTION_NODE:
{
// %REVIEW% Is the node name the same as the "target"?
rth.processingInstruction(dtm.getNodeNameX(node), dtm.getNodeValue(node));
}
break;
default:
//"Can not create item in result tree: "+node.getNodeName());
throw new TransformerException("Can't clone node: " + dtm.getNodeName(node));
}
} catch (org.xml.sax.SAXException se) {
throw new TransformerException(se);
}
}
use of org.apache.xml.utils.XMLString in project j2objc by google.
the class KeyTable method getRefsTable.
/**
* @return lazy initialized refs table associating evaluation of key function
* with a XNodeSet
*/
private Hashtable getRefsTable() {
if (m_refsTable == null) {
// initial capacity set to a prime number to improve hash performance
m_refsTable = new Hashtable(89);
KeyIterator ki = (KeyIterator) (m_keyNodes).getContainedIter();
XPathContext xctxt = ki.getXPathContext();
Vector keyDecls = getKeyDeclarations();
int nKeyDecls = keyDecls.size();
int currentNode;
m_keyNodes.reset();
while (DTM.NULL != (currentNode = m_keyNodes.nextNode())) {
try {
for (int keyDeclIdx = 0; keyDeclIdx < nKeyDecls; keyDeclIdx++) {
KeyDeclaration keyDeclaration = (KeyDeclaration) keyDecls.elementAt(keyDeclIdx);
XObject xuse = keyDeclaration.getUse().execute(xctxt, currentNode, ki.getPrefixResolver());
if (xuse.getType() != xuse.CLASS_NODESET) {
XMLString exprResult = xuse.xstr();
addValueInRefsTable(xctxt, exprResult, currentNode);
} else {
DTMIterator i = ((XNodeSet) xuse).iterRaw();
int currentNodeInUseClause;
while (DTM.NULL != (currentNodeInUseClause = i.nextNode())) {
DTM dtm = xctxt.getDTM(currentNodeInUseClause);
XMLString exprResult = dtm.getStringValue(currentNodeInUseClause);
addValueInRefsTable(xctxt, exprResult, currentNode);
}
}
}
} catch (TransformerException te) {
throw new WrappedRuntimeException(te);
}
}
}
return m_refsTable;
}
Aggregations