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 fess by codelibs.
the class FessXpathTransformer method storeData.
@Override
protected void storeData(final ResponseData responseData, final ResultData resultData) {
final DOMParser parser = getDomParser();
try (final BufferedInputStream bis = new BufferedInputStream(responseData.getResponseBody())) {
final byte[] bomBytes = new byte[UTF8_BOM_SIZE];
bis.mark(UTF8_BOM_SIZE);
final int size = bis.read(bomBytes);
if (size < 3 || !isUtf8BomBytes(bomBytes)) {
bis.reset();
}
final InputSource is = new InputSource(bis);
if (responseData.getCharSet() != null) {
is.setEncoding(responseData.getCharSet());
}
parser.parse(is);
} catch (final Exception e) {
throw new CrawlingAccessException("Could not parse " + responseData.getUrl(), e);
}
final Document document = parser.getDocument();
if (!fessConfig.isCrawlerIgnoreMetaRobots()) {
processMetaRobots(responseData, resultData, document);
}
final Map<String, Object> dataMap = new LinkedHashMap<>();
for (final Map.Entry<String, String> entry : fieldRuleMap.entrySet()) {
final String path = entry.getValue();
try {
final XObject xObj = getXPathAPI().eval(document, path);
final int type = xObj.getType();
switch(type) {
case XObject.CLASS_BOOLEAN:
final boolean b = xObj.bool();
putResultDataBody(dataMap, entry.getKey(), Boolean.toString(b));
break;
case XObject.CLASS_NUMBER:
final double d = xObj.num();
putResultDataBody(dataMap, entry.getKey(), Double.toString(d));
break;
case XObject.CLASS_STRING:
final String str = xObj.str();
putResultDataBody(dataMap, entry.getKey(), str);
break;
case XObject.CLASS_NULL:
case XObject.CLASS_UNKNOWN:
case XObject.CLASS_NODESET:
case XObject.CLASS_RTREEFRAG:
case XObject.CLASS_UNRESOLVEDVARIABLE:
default:
final Node value = getXPathAPI().selectSingleNode(document, entry.getValue());
putResultDataBody(dataMap, entry.getKey(), value != null ? value.getTextContent() : null);
break;
}
} catch (final TransformerException e) {
logger.warn("Could not parse a value of " + entry.getKey() + ":" + entry.getValue());
}
}
putAdditionalData(dataMap, responseData, document);
try {
resultData.setData(SerializeUtil.fromObjectToBinary(dataMap));
} catch (final Exception e) {
throw new CrawlingAccessException("Could not serialize object: " + responseData.getUrl(), e);
}
resultData.setEncoding(charsetName);
}
use of org.apache.xpath.objects.XObject in project j2objc by google.
the class PredicatedNodeTest method acceptNode.
//=============== NodeFilter Implementation ===============
/**
* Test whether a specified node is visible in the logical view of a
* TreeWalker or NodeIterator. This function will be called by the
* implementation of TreeWalker and NodeIterator; it is not intended to
* be called directly from user code.
* @param n The node to check to see if it passes the filter or not.
* @return a constant to determine whether the node is accepted,
* rejected, or skipped, as defined above .
*/
public short acceptNode(int n) {
XPathContext xctxt = m_lpi.getXPathContext();
try {
xctxt.pushCurrentNode(n);
XObject score = execute(xctxt, n);
// System.out.println("\n::acceptNode - score: "+score.num()+"::");
if (score != NodeTest.SCORE_NONE) {
if (getPredicateCount() > 0) {
countProximityPosition(0);
if (!executePredicates(n, xctxt))
return DTMIterator.FILTER_SKIP;
}
return DTMIterator.FILTER_ACCEPT;
}
} catch (javax.xml.transform.TransformerException se) {
// TODO: Fix this.
throw new RuntimeException(se.getMessage());
} finally {
xctxt.popCurrentNode();
}
return DTMIterator.FILTER_SKIP;
}
use of org.apache.xpath.objects.XObject in project j2objc by google.
the class FuncSystemProperty 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 {
String fullName = m_arg0.execute(xctxt).str();
int indexOfNSSep = fullName.indexOf(':');
String result = null;
String propName = "";
// List of properties where the name of the
// property argument is to be looked for.
Properties xsltInfo = new Properties();
loadPropertyFile(XSLT_PROPERTIES, xsltInfo);
if (indexOfNSSep > 0) {
String prefix = (indexOfNSSep >= 0) ? fullName.substring(0, indexOfNSSep) : "";
String namespace;
namespace = xctxt.getNamespaceContext().getNamespaceForPrefix(prefix);
propName = (indexOfNSSep < 0) ? fullName : fullName.substring(indexOfNSSep + 1);
if (namespace.startsWith("http://www.w3.org/XSL/Transform") || namespace.equals("http://www.w3.org/1999/XSL/Transform")) {
result = xsltInfo.getProperty(propName);
if (null == result) {
warn(xctxt, XPATHErrorResources.WG_PROPERTY_NOT_SUPPORTED, //"XSL Property not supported: "+fullName);
new Object[] { fullName });
return XString.EMPTYSTRING;
}
} else {
warn(xctxt, XPATHErrorResources.WG_DONT_DO_ANYTHING_WITH_NS, new Object[] { namespace, //"Don't currently do anything with namespace "+namespace+" in property: "+fullName);
fullName });
try {
//if secure procession is enabled only handle required properties do not not map any valid system property
if (!xctxt.isSecureProcessing()) {
result = System.getProperty(propName);
} else {
warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION, //"SecurityException when trying to access XSL system property: "+fullName);
new Object[] { fullName });
}
if (null == result) {
return XString.EMPTYSTRING;
}
} catch (SecurityException se) {
warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION, //"SecurityException when trying to access XSL system property: "+fullName);
new Object[] { fullName });
return XString.EMPTYSTRING;
}
}
} else {
try {
//if secure procession is enabled only handle required properties do not not map any valid system property
if (!xctxt.isSecureProcessing()) {
result = System.getProperty(fullName);
} else {
warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION, //"SecurityException when trying to access XSL system property: "+fullName);
new Object[] { fullName });
}
if (null == result) {
return XString.EMPTYSTRING;
}
} catch (SecurityException se) {
warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION, //"SecurityException when trying to access XSL system property: "+fullName);
new Object[] { fullName });
return XString.EMPTYSTRING;
}
}
if (propName.equals("version") && result.length() > 0) {
try {
// Needs to return the version number of the spec we conform to.
return new XString("1.0");
} catch (Exception ex) {
return new XString(result);
}
} else
return new XString(result);
}
use of org.apache.xpath.objects.XObject in project j2objc by google.
the class JAXPVariableStack method getVariableOrParam.
public XObject getVariableOrParam(XPathContext xctxt, QName qname) throws TransformerException, IllegalArgumentException {
if (qname == null) {
//JAXP 1.3 spec says that if variable name is null then
// we need to through IllegalArgumentException
String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "Variable qname" });
throw new IllegalArgumentException(fmsg);
}
javax.xml.namespace.QName name = new javax.xml.namespace.QName(qname.getNamespace(), qname.getLocalPart());
Object varValue = resolver.resolveVariable(name);
if (varValue == null) {
String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_RESOLVE_VARIABLE_RETURNS_NULL, new Object[] { name.toString() });
throw new TransformerException(fmsg);
}
return XObject.create(varValue, xctxt);
}
Aggregations