use of org.apache.commons.jxpath.JXPathContext in project galley by Commonjava.
the class JXPathContextAncestryTest method basicJXPathTest.
@Test
@Ignore
public void basicJXPathTest() throws Exception {
final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("jxpath/simple.pom.xml");
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
final JXPathContext ctx = JXPathContext.newContext(document);
document.getDocumentElement().removeAttribute("xmlns");
final String projectGroupIdPath = "ancestor::project/groupId";
// NOT what's failing...just populating the node set to traverse in order to feed the ancestor:: axis test.
final List<?> nodes = ctx.selectNodes("/project/dependencies/dependency");
for (final Object object : nodes) {
final Node node = (Node) object;
dump(node);
final Stack<Node> revPath = new Stack<Node>();
Node parent = node;
while (parent != null) {
revPath.push(parent);
parent = parent.getParentNode();
}
JXPathContext nodeCtx = null;
while (!revPath.isEmpty()) {
final Node part = revPath.pop();
if (nodeCtx == null) {
nodeCtx = JXPathContext.newContext(part);
} else {
nodeCtx = JXPathContext.newContext(nodeCtx, part);
}
}
System.out.println("Path derived from context: '" + nodeCtx.getNamespaceContextPointer().asPath() + "'");
// brute-force approach...try to force population of the parent pointers by painstakingly constructing contexts for all intermediate nodes.
System.out.println("Selecting groupId for declaring project using path-derived context...");
System.out.println(nodeCtx.getValue(projectGroupIdPath));
// Naive approach...this has all the context info it needs to get parent contexts up to and including the document!
System.out.println("Selecting groupId for declaring project using non-derived context...");
System.out.println(JXPathContext.newContext(node).getValue(projectGroupIdPath));
}
}
use of org.apache.commons.jxpath.JXPathContext in project opennms by OpenNMS.
the class JsonCollectorSolarisZonesIT method testXpath.
/**
* Test to verify XPath content.
*
* @throws Exception the exception
*/
@Test
@SuppressWarnings("unchecked")
public void testXpath() throws Exception {
JSONObject json = MockDocumentBuilder.getJSONDocument();
JXPathContext context = JXPathContext.newContext(json);
Iterator<Pointer> itr = context.iteratePointers("/zones/zone");
while (itr.hasNext()) {
Pointer resPtr = itr.next();
JXPathContext relativeContext = context.getRelativeContext(resPtr);
String resourceName = (String) relativeContext.getValue("@name");
Assert.assertNotNull(resourceName);
String value = (String) relativeContext.getValue("parameter[@key='nproc']/@value");
Assert.assertNotNull(Integer.valueOf(value));
}
}
use of org.apache.commons.jxpath.JXPathContext in project vorto by eclipse.
the class AbstractDataMapper method map.
public MappedData map(DataInput input, MappingContext mappingContext) {
JXPathContext context = newContext(input.getValue());
context.setFunctions(converterLibrary);
InfomodelData normalized = new InfomodelData();
final Infomodel deviceInfoModel = specification.getInfoModel();
for (ModelProperty fbProperty : deviceInfoModel.getFunctionblocks()) {
if (mappingContext.isIncluded(fbProperty.getName())) {
FunctionblockData mappedFb = mapFunctionBlock(fbProperty, context);
if (mappedFb != null) {
normalized.withFunctionblock(mappedFb);
}
}
}
return this.doMap(normalized, mappingContext);
}
use of org.apache.commons.jxpath.JXPathContext in project vorto by eclipse.
the class Jxpath method eval.
public static Object eval(String exp, Object value) {
JXPathContext context = newContext(value);
context.setFunctions(converterLibrary);
return context.getValue(exp.replaceAll("\\.", "/"));
}
use of org.apache.commons.jxpath.JXPathContext in project vorto by eclipse.
the class Jxpath method newContext.
private static JXPathContext newContext(Object ctxObject) {
JXPathContext context = JXPathContext.newContext(ctxObject);
TypeUtils.setTypeConverter(new MyTypeConverter());
context.setLenient(false);
return context;
}
Aggregations