+ manifestFile, e ); final DocumentContainer documentContainer = new DocumentContainer( xmlURL ); final Object instrumentationRunner; try instrumentationRunner = JXPathContext.newContext( documentContainer ) .getValue( "manifest//instrumentation/@android:name", String.class );
public Object get(Object object, String xpath, Class target) throws IllegalArgumentException { JXPathContext context = JXPathContext.newContext(object); context.setLenient(true); Object value = context.getValue(xpath); return value; }
private static JXPathContext initialiseContext(NamespaceSupport ns, Document doc) { JXPathContext context = JXPathContext.newContext(doc); addNamespaces(ns, context); context.setLenient(true); return context; }
public void set(Object object, String xpath, Object value, Class target) throws IllegalAttributeException { if (object instanceof FeatureType) { throw new IllegalAttributeException(null, "feature type is immutable"); } 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); } context.setValue(xpath, value); assert value == context.getValue(xpath); } }
/** * Create a new context * * @param contextBean * the context bean (=root of the xpath expression) */ JXPathContextImpl(Object contextBean) { this.context = JXPathContext.newContext(contextBean); this.context.setFunctions(new ClassFunctions(EMFFunctions.class, "ecore")); }
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; } }
@Override public boolean filter(Event event) { JXPathContext context = JXPathContext.newContext(event); Pointer pointer = context.getPointer(expression); return (Boolean)pointer.getValue(); }
protected boolean stream(ElementHandler handler) { // create an xpath context from the root element // TODO: cache the context, should work just the same // JXPathIntrospector.registerDynamicClass(ElementHandlerImpl.class, // ElementHandlerPropertyHandler.class); JXPathIntrospector.registerDynamicClass(NodeImpl.class, NodePropertyHandler.class); // ElementHandler rootHandler = // ((DocumentHandler) handlers.firstElement()).getDocumentElementHandler(); Node root = ((DocumentHandler) handlers.firstElement()).getParseNode(); JXPathContext jxpContext = JXPathContextFactory.newInstance().newContext(null, root); jxpContext.setLenient(true); Iterator itr = jxpContext.iterate(xpath); while (itr.hasNext()) { Object obj = itr.next(); if (handler.getParseNode().equals(obj)) { return true; } } return false; } }
JXPathContext context(Object object) { JXPathContext context = JXPathContextFactory.newInstance().newContext(null, object); context.setLenient(true); return context; } }
/** * @param ns namespaces * @param xpathString xpath to search on * @param doc xml to search * @return the (single) value matching the xpath in the xml supplied */ public static String getSingleXPathValue( NamespaceSupport ns, String xpathString, Document doc) { String id = null; JXPathContext context = initialiseContext(ns, doc); try { Object ob = context.getValue(xpathString); id = (String) ob; } catch (RuntimeException e) { throw new RuntimeException("Error reading xpath " + xpathString, e); } return id; }
/** 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); }
/** * {@inheritDoc} This implementation interprets the passed in key as an XPATH * expression. */ @Override public <T> List<QueryResult<T>> query(final T root, final String key, final NodeHandler<T> handler) { if (StringUtils.isEmpty(key)) { final QueryResult<T> result = createResult(root); return Collections.singletonList(result); } final JXPathContext context = createContext(root, handler); List<?> results = context.selectNodes(key); if (results == null) { results = Collections.emptyList(); } return convertResults(results); }
/** * Creates a new JXPathContext with the specified object as the root node. * @param contextBean Object * @return JXPathContext */ public static JXPathContext newContext(Object contextBean) { return getContextFactory().newContext(null, contextBean); }
private static void addNamespaces(NamespaceSupport ns, JXPathContext context) { Enumeration<String> prefixes = ns.getPrefixes(); while (prefixes.hasMoreElements()) { String prefix = prefixes.nextElement(); String uri = ns.getURI(prefix); context.registerNamespace(prefix, uri); } }
/** * Creates a new {@code JXPathContext} based on the passed in arguments. * * @param root the root node * @param handler the node handler * @param <T> the type of the nodes to be handled * @return the newly created context */ public <T> JXPathContext createContext(final T root, final NodeHandler<T> handler) { final JXPathContext context = JXPathContext.newContext(ConfigurationNodePointerFactory .wrapNode(root, handler)); context.setLenient(true); return context; } }
/** * Create a new context * * @param contextBean * the context bean (=root of the xpath expression) */ JXPathContextImpl(Object contextBean) { this.context = JXPathContext.newContext(contextBean); this.context.setFunctions(new ClassFunctions(EMFFunctions.class, "ecore")); }
JXPathContext context(Object object) { JXPathContext context = JXPathContextFactory.newInstance().newContext(null, object); context.setLenient(true); return context; } }
private static List<String> getXPathValues(String xpathString, JXPathContext context) { List values = null; try { values = context.selectNodes(xpathString); } catch (RuntimeException e) { throw new RuntimeException("Error reading xpath " + xpathString, e); } List<String> ls = null; if (values == null) { ls = new ArrayList<String>(); } else { ls = new ArrayList<String>(values.size()); for (int i = 0; i < values.size(); i++) { Object value = values.get(i); String unwrappedValue = ""; if (value instanceof org.jdom2.Attribute) { unwrappedValue = ((org.jdom2.Attribute) value).getValue(); } else if (value instanceof org.jdom2.Element) { unwrappedValue = ((org.jdom2.Element) value).getValue(); } ls.add(unwrappedValue); } } return ls; } }
/** * Creates a new JXPathContext with the specified bean as the root node and * the specified parent context. Variables defined in a parent context can * be referenced in XPaths passed to the child context. * @param parentContext parent context * @param contextBean Object * @return JXPathContext */ public static JXPathContext newContext( JXPathContext parentContext, Object contextBean) { return getContextFactory().newContext(parentContext, contextBean); }
JXPathContext context( Object object ) { JXPathContext context = JXPathContextFactory.newInstance().newContext( null, object ); context.setLenient( true ); return context; }