use of org.apache.xml.utils.XMLString in project robovm by robovm.
the class FuncSubstring 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();
double start = m_arg1.execute(xctxt).num();
int lenOfS1 = s1.length();
XMLString substr;
if (lenOfS1 <= 0)
return XString.EMPTYSTRING;
else {
int startIndex;
if (Double.isNaN(start)) {
// Double.MIN_VALUE doesn't work with math below
// so just use a big number and hope I never get caught.
start = -1000000;
startIndex = 0;
} else {
start = Math.round(start);
startIndex = (start > 0) ? (int) start - 1 : 0;
}
if (null != m_arg2) {
double len = m_arg2.num(xctxt);
int end = (int) (Math.round(len) + start) - 1;
// Normalize end index.
if (end < 0)
end = 0;
else if (end > lenOfS1)
end = lenOfS1;
if (startIndex > lenOfS1)
startIndex = lenOfS1;
substr = s1.substring(startIndex, end);
} else {
if (startIndex > lenOfS1)
startIndex = lenOfS1;
substr = s1.substring(startIndex);
}
}
// cast semi-safe
return (XString) substr;
}
use of org.apache.xml.utils.XMLString in project robovm by robovm.
the class DOM2DTM method getStringValue.
/**
* Get the string-value of a node as a String object
* (see http://www.w3.org/TR/xpath#data-model
* for the definition of a node's string-value).
*
* @param nodeHandle The node ID.
*
* @return A string object that represents the string-value of the given node.
*/
public XMLString getStringValue(int nodeHandle) {
int type = getNodeType(nodeHandle);
Node node = getNode(nodeHandle);
// directly.
if (DTM.ELEMENT_NODE == type || DTM.DOCUMENT_NODE == type || DTM.DOCUMENT_FRAGMENT_NODE == type) {
FastStringBuffer buf = StringBufferPool.get();
String s;
try {
getNodeData(node, buf);
s = (buf.length() > 0) ? buf.toString() : "";
} finally {
StringBufferPool.free(buf);
}
return m_xstrf.newstr(s);
} else if (TEXT_NODE == type || CDATA_SECTION_NODE == type) {
// If this is a DTM text node, it may be made of multiple DOM text
// nodes -- including navigating into Entity References. DOM2DTM
// records the first node in the sequence and requires that we
// pick up the others when we retrieve the DTM node's value.
//
// %REVIEW% DOM Level 3 is expected to add a "whole text"
// retrieval method which performs this function for us.
FastStringBuffer buf = StringBufferPool.get();
while (node != null) {
buf.append(node.getNodeValue());
node = logicalNextDOMTextNode(node);
}
String s = (buf.length() > 0) ? buf.toString() : "";
StringBufferPool.free(buf);
return m_xstrf.newstr(s);
} else
return m_xstrf.newstr(node.getNodeValue());
}
Aggregations