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; } }
/** * Finds all nodes that match the specified XPath. * * @param xpath the xpath to be evaluated * @return a list of found objects */ public List selectNodes(String xpath) { ArrayList list = new ArrayList(); Iterator iterator = iteratePointers(xpath); while (iterator.hasNext()) { Pointer pointer = (Pointer) iterator.next(); list.add(pointer.getNode()); } return list; }
/** * TODO document that method. * * @param xpath * @param root * @param clazz * @return TODO * @throws ValidatorException */ protected static List<XPathResult> evaluateXPathWithClass( String xpath, Object root, Class clazz ) throws ValidatorException { JXPathContext ctx = JXPathContext.newContext( root ); Iterator iter = ctx.iteratePointers( xpath ); List<XPathResult> results = new ArrayList<XPathResult>(); while ( iter.hasNext() ) { Pointer p = ( Pointer ) iter.next(); try { Constructor constructor = clazz.getConstructor( new Class[]{Pointer.class, JXPathContext.class} ); results.add( ( XPathResult ) constructor.newInstance( new Object[]{p, ctx} ) ); } catch ( Exception e ) { throw new ValidatorException( "Error creating XPath Result class" ); } } return results; }
/** * Query a collection JXPath and return a pointer. * * @return pointer to node */ // TODO: This does not belong here! public Iterator<?> xpathIterator(Collection<?> collection, String query) { JXPathContext pathContext = JXPathContext.newContext(collection); Iterator<?> result = null; try { result = pathContext.iteratePointers(query); } catch (JXPathNotFoundException e) { logger.log(Level.WARNING, "JXPath exception: {0}", e.getMessage()); } return result; }
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; } }
ite = context.iteratePointers(requestXpath); } catch (JXPathException e) {
Iterator<Pointer> iterator = context.iteratePointers(contentLocationXPath);
iter = ctx.iteratePointers(this.listPath); } else { iter = ctx.iteratePointers(".");
Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath); boolean found = false; while (rowPointers.hasNext()) { Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath); List rowsToDelete = new ArrayList(); while (rowPointers.hasNext()) { rowPointers = repeaterContext.iteratePointers(this.rowPathForInsert); while (rowPointers.hasNext()) { rowPointers.next();
Iterator rowPointers = multiValueContext.iteratePointers(this.rowPath); int cnt = 0; while( rowPointers.hasNext() )
@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()); }
Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath);
Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath);
LOG.debug("fillCollectionSet: getting resources for XML group {} using XPATH {}", group.getName(), group.getResourceXpath()); Date timestamp = getTimeStamp(context, group); Iterator<Pointer> itr = context.iteratePointers(group.getResourceXpath()); while (itr.hasNext()) { JXPathContext relativeContext = context.getRelativeContext(itr.next());
LOG.debug("fillCollectionSet: getting resources for XML group {} using XPATH {}", group.getName(), group.getResourceXpath()); Date timestamp = getTimeStamp(context, group); Iterator<Pointer> itr = context.iteratePointers(group.getResourceXpath()); while (itr.hasNext()) { JXPathContext relativeContext = context.getRelativeContext(itr.next());
int r = 0; try { Iterator<?> iterator = context.iteratePointers(loader.getXroot()); while (iterator.hasNext()) { r++;
Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath);
/** * 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)); } }