Java Code Examples for org.apache.commons.jxpath.JXPathContext#newContext()
The following examples show how to use
org.apache.commons.jxpath.JXPathContext#newContext() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EPLUtilities.java From jetstream-esper with GNU General Public License v2.0 | 6 votes |
public static Object getAttribute(Object object, String key) { try { Object event = object; if (object instanceof String) { ObjectMapper mapper = new ObjectMapper(); event = mapper.readValue(object.toString(), HashMap.class); } if (event != null) { JXPathContext context = JXPathContext.newContext(event); context.setLenient(true); return context.getValue(key); } } catch (Exception e) { // NOPMD return null; } return null; }
Example 2
Source File: BeanModelTestCase.java From commons-jxpath with Apache License 2.0 | 6 votes |
public void testIteratePropertyArrayWithHasNext() { JXPathContext context = JXPathContext.newContext(createContextBean()); Iterator it = context.iteratePointers("/integers"); List actual = new ArrayList(); while (it.hasNext()) { actual.add(((Pointer) it.next()).asPath()); } assertEquals( "Iterating 'hasNext'/'next'<" + "/integers" + ">", list( "/integers[1]", "/integers[2]", "/integers[3]", "/integers[4]"), actual); }
Example 3
Source File: AssertionUtils.java From cougar with Apache License 2.0 | 6 votes |
private static void doJsonSorting(JSONObject doc, String x) throws XPathExpressionException, IOException, JSONException { JXPathContext ctx = JXPathContext.newContext(doc); String parentX = x.substring(0,x.lastIndexOf("/")); if ("".equals(parentX)) { parentX = "/"; } String childName = x.substring(x.lastIndexOf("/")+1); Iterator it = ctx.iterate(parentX); while (it.hasNext()) { JSONObject p = (JSONObject) it.next(); JSONArray n = p.getJSONArray(childName); List allKids = new ArrayList<>(n.length()); for (int j=0; j<n.length(); j++) { allKids.add(n.get(j)); } Collections.sort(allKids, new Comparator<Object>() { @Override public int compare(Object o1, Object o2) { return o1.toString().compareTo(o2.toString()); } }); JSONArray newArray = new JSONArray(allKids); p.put(childName,newArray); } }
Example 4
Source File: ContainerModelTest.java From commons-jxpath with Apache License 2.0 | 6 votes |
public void testContainerMapWithCollection() { ListContainer container = new ListContainer(); List list = (List) container.getValue(); Map map = new HashMap(); map.put("container", container); JXPathContext context = JXPathContext.newContext(map); assertXPathValueAndPointer(context, "/container", list, "/.[@name='container']"); assertXPathValueAndPointer(context, "/container[1]", list.get(0), "/.[@name='container'][1]"); assertXPathValueAndPointer(context, "/container[2]", list.get(1), "/.[@name='container'][2]"); assertXPathSetValue(context, "/container[1]", "baz"); assertEquals("Checking setValue(index)", "baz", list.get(0)); }
Example 5
Source File: RecursiveAxesTest.java From commons-jxpath with Apache License 2.0 | 5 votes |
protected void setUp() throws Exception { bean = new RecursiveBean("zero"); RecursiveBean bean1 = new RecursiveBean("one"); RecursiveBean bean2 = new RecursiveBean("two"); RecursiveBean bean3 = new RecursiveBean("three"); bean.setFirst(bean1); bean1.setFirst(bean2); bean2.setFirst(bean1); bean2.setSecond(bean3); context = JXPathContext.newContext(null, bean); }
Example 6
Source File: ExternalXMLNamespaceTest.java From commons-jxpath with Apache License 2.0 | 5 votes |
protected JXPathContext createContext(String model) { JXPathContext context = JXPathContext .newContext(createDocumentContainer(model)); context.registerNamespace("A", "foo"); context.registerNamespace("B", "bar"); return context; }
Example 7
Source File: MixedModelTest.java From commons-jxpath with Apache License 2.0 | 5 votes |
public void testIteratePointersArrayElementWithVariable() { Map map = new HashMap(); map.put("foo", new String[] { "a", "b", "c" }); JXPathContext context = JXPathContext.newContext(map); context.getVariables().declareVariable("x", new Integer(2)); Iterator it = context.iteratePointers("foo[$x]"); List actual = new ArrayList(); while (it.hasNext()) { Pointer ptr = (Pointer) it.next(); actual.add(context.getValue(ptr.asPath())); } assertEquals("Iterating pointers <" + "foo" + ">", list("b"), actual); }
Example 8
Source File: XPathContextFactory.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * 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; }
Example 9
Source File: BeanModelTest.java From commons-jxpath with Apache License 2.0 | 5 votes |
public void testIndexedProperty() { JXPathContext context = JXPathContext.newContext(null, new TestIndexedPropertyBean()); assertXPathValueAndPointer( context, "indexed[1]", new Integer(0), "/indexed[1]"); }
Example 10
Source File: JXPath172Test.java From commons-jxpath with Apache License 2.0 | 5 votes |
/** * Helper, returns a {@link JXPathContext} filled with {@link TestBean172} * whose {@link TestBean172#getValue()} method returns the passed * <code>val</code> value. * * @param val * @return A {@link JXPathContext}, never <code>null</code>. */ private JXPathContext getContext(final String val, boolean lenient) { final TestBean172 b = new TestBean172(); b.setValue(val); final Object target = b; final JXPathContext context = JXPathContext.newContext(null, target); context.setLenient(lenient); return context; }
Example 11
Source File: JXPath118Test.java From commons-jxpath with Apache License 2.0 | 5 votes |
public void testJXPATH118IssueWithAsPath() throws Exception { Object contextBean = new SomeChildClass(); JXPathContext context = JXPathContext.newContext(contextBean); Iterator iteratePointers = context.iteratePointers("//*"); Assert.assertEquals("/bar", ((Pointer) iteratePointers.next()).asPath()); Assert.assertEquals("/baz", ((Pointer) iteratePointers.next()).asPath()); Assert.assertEquals("/foo", ((Pointer) iteratePointers.next()).asPath()); }
Example 12
Source File: VariableTest.java From commons-jxpath with Apache License 2.0 | 5 votes |
public void setUp() { if (context == null) { context = JXPathContext.newContext(new TestMixedModelBean()); context.setFactory(new VariableFactory()); Variables vars = context.getVariables(); vars.declareVariable("a", new Double(1)); vars.declareVariable("b", new Double(1)); vars.declareVariable("c", null); vars.declareVariable("d", new String[] { "a", "b" }); vars.declareVariable("integer", new Integer(1)); vars.declareVariable("nan", new Double(Double.NaN)); vars.declareVariable("x", null); } }
Example 13
Source File: ContainerModelTest.java From commons-jxpath with Apache License 2.0 | 5 votes |
public void testContainerRootWithCollection() { ArrayContainer container = new ArrayContainer(); String[] array = (String[]) container.getValue(); JXPathContext context = JXPathContext.newContext(container); context.getVariables().declareVariable("list", container); assertXPathValueAndPointer(context, "/", array, "/"); assertXPathValueAndPointer(context, "/.[1]", "foo", "/.[1]"); assertXPathValueAndPointer(context, "/.[2]", "bar", "/.[2]"); assertXPathSetValue(context, "/.[1]", "baz"); assertEquals("Checking setValue(index)", "baz", array[0]); }
Example 14
Source File: JXPath149Test.java From commons-jxpath with Apache License 2.0 | 5 votes |
public void testComplexOperationWithVariables() { JXPathContext context = JXPathContext.newContext(null); context.getVariables().declareVariable("a", Integer.valueOf(0)); context.getVariables().declareVariable("b", Integer.valueOf(0)); context.getVariables().declareVariable("c", Integer.valueOf(1)); assertXPathValue(context, "$a + $b <= $c", Boolean.TRUE); }
Example 15
Source File: ObjectGraphNavigatorJXPathImpl.java From oval with Eclipse Public License 2.0 | 5 votes |
@Override public ObjectGraphNavigationResult navigateTo(final Object root, final String xpath) throws InvalidConfigurationException { Assert.argumentNotNull("root", root); Assert.argumentNotNull("xpath", xpath); try { final JXPathContext ctx = JXPathContext.newContext(root); ctx.setLenient(true); // do not throw an exception if object graph is incomplete, e.g. contains null-values Pointer pointer = ctx.getPointer(xpath); // no match found or invalid xpath if (pointer instanceof NullPropertyPointer || pointer instanceof NullPointer) return null; if (pointer instanceof BeanPointer) { final Pointer parent = ((BeanPointer) pointer).getImmediateParentPointer(); if (parent instanceof PropertyPointer) { pointer = parent; } } if (pointer instanceof PropertyPointer) { final PropertyPointer pp = (PropertyPointer) pointer; final Class<?> beanClass = pp.getBean().getClass(); AccessibleObject accessor = ReflectionUtils.getField(beanClass, pp.getPropertyName()); if (accessor == null) { accessor = ReflectionUtils.getGetter(beanClass, pp.getPropertyName()); } return new ObjectGraphNavigationResult(root, xpath, pp.getBean(), accessor, pointer.getValue()); } throw new InvalidConfigurationException("Don't know how to handle pointer [" + pointer + "] of type [" + pointer.getClass().getName() + "] for xpath [" + xpath + "]"); } catch (final JXPathNotFoundException ex) { // thrown if the xpath is invalid throw new InvalidConfigurationException(ex); } }
Example 16
Source File: XMLSpaceTest.java From commons-jxpath with Apache License 2.0 | 4 votes |
protected JXPathContext createContext(String model) { JXPathContext context = JXPathContext .newContext(createDocumentContainer(model)); return context; }
Example 17
Source File: CloudMLQueryUtil.java From cloudml with GNU Lesser General Public License v3.0 | 4 votes |
public static Object cloudmlQuery(String jxpath, Object context){ JXPathContext jxpathcontext = JXPathContext.newContext(context); return jxpathcontext.getValue(jxpath); }
Example 18
Source File: XMLSpaceTest.java From commons-jxpath with Apache License 2.0 | 4 votes |
protected void doTest(String id, String model, String expectedValue) { JXPathContext context = JXPathContext .newContext(createDocumentContainer(model)); assertEquals(context.getValue("test/text[@id='" + id + "']"), expectedValue); }
Example 19
Source File: XMLUpperCaseElementsTest.java From commons-jxpath with Apache License 2.0 | 4 votes |
protected void doTest(String id, String model, String expectedValue) { JXPathContext context = JXPathContext.newContext(createDocumentContainer(model)); assertEquals(context.getValue("test/text[@id='" + id + "']"), expectedValue); }
Example 20
Source File: MixedModelTest.java From commons-jxpath with Apache License 2.0 | 3 votes |
public void testNull() { assertXPathPointerLenient(context, "$null", "$null"); assertXPathPointerLenient(context, "$null[3]", "$null[3]"); assertXPathPointerLenient( context, "$testnull/nothing", "$testnull/nothing"); assertXPathPointerLenient( context, "$testnull/nothing[2]", "$testnull/nothing[2]"); assertXPathPointerLenient(context, "beans[8]/int", "/beans[8]/int"); assertXPathValueIterator( context, "$testnull/nothing[1]", list(null)); JXPathContext ctx = JXPathContext.newContext(new TestNull()); assertXPathValue(ctx, "nothing", null); assertXPathValue(ctx, "child/nothing", null); assertXPathValue(ctx, "array[2]", null); assertXPathValueLenient(ctx, "nothing/something", null); assertXPathValueLenient(ctx, "array[2]/something", null); }