public Object get(Object object, String xpath, Class target) { JXPathContext context = JXPathContext.newContext(object); Enumeration declaredPrefixes = namespaces.getDeclaredPrefixes(); while (declaredPrefixes.hasMoreElements()) { String prefix = (String) declaredPrefixes.nextElement(); String uri = namespaces.getURI(prefix); context.registerNamespace(prefix, uri); } Iterator it = context.iteratePointers(xpath); List results = new ArrayList<Object>(); while (it.hasNext()) { Pointer pointer = (Pointer) it.next(); if (pointer instanceof AttributeNodePointer) { results.add(((AttributeNodePointer) pointer).getImmediateAttribute()); } else { results.add(pointer.getValue()); } } if (results.size() == 0) { throw new IllegalArgumentException("x-path gives no results."); } else if (results.size() == 1) { return results.get(0); } else { return results; } }
private Object getXpathResultObject(Object obj) { if (obj instanceof Pointer) { obj = ((Pointer) obj).getValue(); } return obj; }
private Object getXpathResultObject(Object obj) { if (obj instanceof Pointer) { obj = ((Pointer) obj).getValue(); } return obj; }
public Object next() { Object o = iterator.next(); return o instanceof Pointer ? ((Pointer) o).getValue() : o; }
public synchronized List getValues() { if (values == null) { values = new ArrayList(); for (int i = 0; i < pointers.size(); i++) { Pointer pointer = (Pointer) pointers.get(i); values.add(pointer.getValue()); } values = Collections.unmodifiableList(values); } return values; }
private String getFileName(JXPathContext context, String xpath) throws Exception { Pointer pointer = context.getPointer(xpath); Object value = pointer.getValue(); return value.toString(); }
/** * Get the current node. * * @param context * the xpath context * @return the current node, null if no current node */ public static Object current(ExpressionContext context) { Pointer pointer = context.getContextNodePointer(); if (pointer == null) { return null; } return pointer.getValue(); }
/** * Get the current node. * * @param context * the xpath context * @return the current node, null if no current node */ public static Object current(ExpressionContext context) { Pointer pointer = context.getContextNodePointer(); if (pointer == null) { return null; } return pointer.getValue(); }
protected Runtime findRuntime(int windowId) { createAllRuntimes(); Pointer pointer = xpathPointer(runtimesList.values(), String.format("/.[htmlFramePath='%s' and windowID='%d']", currentFramePath, windowId)); if (pointer != null) { return (Runtime) pointer.getValue(); } return null; }
protected RuntimeInfo findRuntime(int windowId) { createAllRuntimes(); return (RuntimeInfo) xpathPointer(runtimesList.values(), String.format("/.[htmlFramePath='%s' and windowID='%d']", currentFramePath, windowId)).getValue(); }
/** access the name of the class of the context node - useful for finding nodes by class * e.g. //*[functions:type() = 'org.astrogrid.Blob'] * */ public static String type(ExpressionContext ctxt) { Pointer ptr = ctxt.getContextNodePointer(); if (ptr == null) { return "null"; } Object candidate = ctxt.getContextNodePointer().getValue(); if (candidate == null) { return "null"; } return candidate.getClass().getName(); }
@Override public boolean filter(Event event) { JXPathContext context = JXPathContext.newContext(event); Pointer pointer = context.getPointer(expression); return (Boolean)pointer.getValue(); }
private ChildrenResultList visitIterator(Context context, Iterator<?> it) { if (it == null) { return null; } ChildrenResultList list = null; int i = 0; while (it.hasNext()) { Object value = it.next(); if (value instanceof Pointer) { value = ((Pointer) value).getValue(); } if (value != null) { list = addAll(list, visit(context, it, i + "", i, value)); i++; } } return list; }
/** returns true if context node is equal to variable target. * i.e. $target.equals(.) * @param ctxt * @return */ public static boolean matchTarget(ExpressionContext ctxt) { Object candidate = ctxt.getContextNodePointer().getValue(); Object target = ctxt.getJXPathContext().getVariables().getVariable("target"); return target.equals(candidate); }
/** * Get the id of the current node * * @param obj * @return */ public static String findId(ExpressionContext context) { Pointer pointer = context.getContextNodePointer(); if (pointer == null) { return null; } Object obj = pointer.getValue(); return Context.getCurrentContext().findConstId(obj, true, true); } }
/** * Get the id of the current node * * @param obj * @return */ public static String findId(ExpressionContext context) { Pointer pointer = context.getContextNodePointer(); if (pointer == null) { return null; } Object obj = pointer.getValue(); return Context.getCurrentContext().findConstId(obj, true, true); } }
public void setActiveWindow(String title) { Integer windowId = (Integer) xpathPointer(windows.values(), "/.[title='" + title + "']/windowID").getValue(); if (windowId == null) { throw new NoSuchWindowException("No such window: " + title); } setActiveWindowId(windowId); }
@Override public Object currentValue(Object[] originalArguments) { if (updatedArgumentIndex >= originalArguments.length) { throw new VariationRuntimeException("Failed to apply variaion \"" + getVariationID() + "\". Number of argument to modify is [" + updatedArgumentIndex + "] but arguments length is " + originalArguments.length); } JXPathContext context = JXPathContext.newContext(originalArguments[updatedArgumentIndex]); Pointer pointer = compiledExpression.createPath(context); return pointer.getValue(); }
@Override public Object currentValue(Object[] originalArguments) { if (updatedArgumentIndex >= originalArguments.length) { throw new VariationRuntimeException("Failed to apply variaion \"" + getVariationID() + "\". Number of argument to modify is [" + updatedArgumentIndex + "] but arguments length is " + originalArguments.length); } JXPathContext context = JXPathContext.newContext(originalArguments[updatedArgumentIndex]); Pointer pointer = compiledExpression.createPath(context); return pointer.getValue(); }
@Test @SuppressWarnings("unchecked") public void testXpath() throws Exception { JSONObject json = MockDocumentBuilder.getJSONDocument(); JXPathContext context = JXPathContext.newContext(json); Iterator<Pointer> itr = context.iteratePointers("/elements[4]/it"); Assert.assertTrue(itr.hasNext()); Assert.assertEquals(itr.next().getValue(), "works"); Assert.assertFalse(itr.hasNext()); }