org.apache.commons.jxpath.Pointer Java Examples
The following examples show how to use
org.apache.commons.jxpath.Pointer.
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: BasicTypeConverterTest.java From commons-jxpath with Apache License 2.0 | 6 votes |
public void testPointerToString() { assertConversion(new Pointer() { public Object getValue() { return "value"; } public Object getNode() { return null; } public void setValue(Object value) { } public Object getRootNode() { return null; } public String asPath() { return null; } public Object clone() { return null; } public int compareTo(Object o) { return 0; } }, String.class, "value"); }
Example #2
Source File: UnionContext.java From commons-jxpath with Apache License 2.0 | 6 votes |
public boolean setPosition(int position) { if (!prepared) { prepared = true; BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet(); ArrayList pointers = new ArrayList(); for (int i = 0; i < contexts.length; i++) { EvalContext ctx = contexts[i]; while (ctx.nextSet()) { while (ctx.nextNode()) { NodePointer ptr = ctx.getCurrentNodePointer(); if (!pointers.contains(ptr)) { pointers.add(ptr); } } } } sortPointers(pointers); for (Iterator it = pointers.iterator(); it.hasNext();) { nodeSet.add((Pointer) it.next()); } } return super.setPosition(position); }
Example #3
Source File: ChildContext.java From commons-jxpath with Apache License 2.0 | 6 votes |
/** * This method is called on the last context on the path when only * one value is needed. Note that this will return the whole property, * even if it is a collection. It will not extract the first element * of the collection. For example, "books" will return the collection * of books rather than the first book from that collection. * @return Pointer */ public Pointer getSingleNodePointer() { if (position == 0) { while (nextSet()) { prepare(); if (iterator == null) { return null; } // See if there is a property there, singular or collection NodePointer pointer = iterator.getNodePointer(); if (pointer != null) { return pointer; } } return null; } return getCurrentNodePointer(); }
Example #4
Source File: MetaService.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
private void remove(String path, JXPathContext context) throws SpagoBIException { if (path.matches(".*\\]$")) { int i = path.lastIndexOf("["); Pointer obj = context.getPointer(path); logger.debug("REMOVE > " + path + " > " + obj.getNode()); Pointer coll = context.getPointer(path.substring(0, i)); if (obj.getNode() instanceof SimpleBusinessColumnImpl) { ((SimpleBusinessColumnImpl) obj.getNode()).setIdentifier(false); } if (obj.getNode() instanceof BusinessRelationshipImpl) { ((BusinessRelationshipImpl) obj.getNode()).removeRelationship(); return; } ((List<?>) coll.getNode()).remove(obj.getNode()); } else { // TODO // throw new SpagoBIException("TODO operation \"remove\" not supported for single element"); } }
Example #5
Source File: EvalContext.java From commons-jxpath with Apache License 2.0 | 6 votes |
/** * Returns the list of all Pointers in this context for all positions * of the parent contexts. If there was an ongoing iteration over * this context, the method should not be called. * @return NodeSet */ public NodeSet getNodeSet() { if (position != 0) { throw new JXPathException( "Simultaneous operations: " + "should not request pointer list while " + "iterating over an EvalContext"); } BasicNodeSet set = new BasicNodeSet(); while (nextSet()) { while (nextNode()) { set.add((Pointer) getCurrentNodePointer().clone()); } } return set; }
Example #6
Source File: InfoSetUtil.java From commons-jxpath with Apache License 2.0 | 6 votes |
/** * Converts the supplied object to String. * @param object to convert * @return String value */ public static String stringValue(Object object) { if (object instanceof String) { return (String) object; } if (object instanceof Number) { double d = ((Number) object).doubleValue(); long l = ((Number) object).longValue(); return d == l ? String.valueOf(l) : String.valueOf(d); } if (object instanceof Boolean) { return ((Boolean) object).booleanValue() ? "true" : "false"; } if (object == null) { return ""; } if (object instanceof NodePointer) { return stringValue(((NodePointer) object).getValue()); } if (object instanceof EvalContext) { EvalContext ctx = (EvalContext) object; Pointer ptr = ctx.getSingleNodePointer(); return ptr == null ? "" : stringValue(ptr); } return String.valueOf(object); }
Example #7
Source File: ModelPropertyFactory.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
@Override public boolean createObject(JXPathContext context, Pointer pointer, Object parent, String name, int index) { if ((parent instanceof BusinessTableImpl) && "relationships".equals(name)) { BusinessTableImpl bt = ((BusinessTableImpl) parent); BusinessRelationship br = BusinessModelFactory.eINSTANCE.createBusinessRelationship(); bt.getModel().getRelationships().add(br); return true; } else if (parent instanceof ModelPropertyImpl) { ModelPropertyImpl mp = ((ModelPropertyImpl) parent); mp.setPropertyType(ModelFactory.eINSTANCE.createModelPropertyType()); return true; } else if ((parent instanceof BusinessColumn)) { BusinessColumnImpl bc = ((BusinessColumnImpl) parent); bc.getProperties(); bc.getPropertyTypes(); return true; } else if ((parent instanceof ModelPropertyMapEntryImpl)) { ModelPropertyMapEntryImpl map = ((ModelPropertyMapEntryImpl) parent); map.setKey(name); return true; } return false; }
Example #8
Source File: MixedModelTest.java From commons-jxpath with Apache License 2.0 | 6 votes |
public void testIteratePointersArray() { Map map = new HashMap(); map.put("foo", new String[] { "a", "b", "c" }); JXPathContext context = JXPathContext.newContext(map); Iterator it = context.iteratePointers("foo"); List actual = new ArrayList(); while (it.hasNext()) { Pointer ptr = (Pointer) it.next(); actual.add(context.getValue(ptr.asPath())); } assertEquals( "Iterating pointers <" + "foo" + ">", list("a", "b", "c"), actual); }
Example #9
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 #10
Source File: TestDOMFactory.java From commons-jxpath with Apache License 2.0 | 6 votes |
/** * Return <b>false</b> if this factory cannot create the requested object. */ public boolean createObject( JXPathContext context, Pointer pointer, Object parent, String name, int index) { if (name.equals("location") || name.equals("address") || name.equals("street")) { addDOMElement((Node) parent, index, name, null); return true; } if (name.startsWith("price:")) { String namespaceURI = context.getNamespaceURI("price"); addDOMElement((Node) parent, index, name, namespaceURI); return true; } return false; }
Example #11
Source File: InfoSetUtil.java From commons-jxpath with Apache License 2.0 | 6 votes |
/** * Converts the supplied object to Number. * @param object to convert * @return Number result */ public static Number number(Object object) { if (object instanceof Number) { return (Number) object; } if (object instanceof Boolean) { return ((Boolean) object).booleanValue() ? ONE : ZERO; } if (object instanceof String) { try { return new Double((String) object); } catch (NumberFormatException ex) { return NOT_A_NUMBER; } } if (object instanceof EvalContext) { EvalContext ctx = (EvalContext) object; Pointer ptr = ctx.getSingleNodePointer(); return ptr == null ? NOT_A_NUMBER : number(ptr); } if (object instanceof NodePointer) { return number(((NodePointer) object).getValue()); } return number(stringValue(object)); }
Example #12
Source File: VariableFactory.java From commons-jxpath with Apache License 2.0 | 6 votes |
/** */ public boolean createObject( JXPathContext context, Pointer pointer, Object parent, String name, int index) { if (name.equals("testArray")) { ((TestBean[]) parent)[index] = new TestBean(); return true; } else if (name.equals("stringArray")) { ((String[]) parent)[index] = ""; return true; } else if (name.equals("array")) { ((String[]) parent)[index] = ""; return true; } return false; }
Example #13
Source File: SimplePathInterpreterTest.java From commons-jxpath with Apache License 2.0 | 6 votes |
/** * Since we need to test the internal Signature of a pointer, * we will get a signature which will contain a single character * per pointer in the chain, representing that pointer's type. */ private String pointerSignature(Pointer pointer) { if (pointer == null) { return ""; } char type = '?'; if (pointer instanceof NullPointer) { type = 'N'; } else if (pointer instanceof NullPropertyPointer) { type = 'n'; } else if (pointer instanceof NullElementPointer) { type = 'E'; } else if (pointer instanceof VariablePointer) { type = 'V'; } else if (pointer instanceof CollectionPointer) { type = 'C'; } else if (pointer instanceof BeanPointer) { type = 'B'; } else if (pointer instanceof BeanPropertyPointer) { type = 'b'; } else if (pointer instanceof DynamicPointer) { type = 'D'; } else if (pointer instanceof DynamicPropertyPointer) { type = 'd'; } else if (pointer instanceof DOMNodePointer) { type = 'M'; } else { System.err.println("UNKNOWN TYPE: " + pointer.getClass()); } NodePointer parent = ((NodePointer) pointer).getImmediateParentPointer(); return pointerSignature(parent) + type; }
Example #14
Source File: SimplePathInterpreterTest.java From commons-jxpath with Apache License 2.0 | 6 votes |
private void assertValueAndPointer( String path, Object expectedValue, String expectedPath, String expectedSignature, String expectedValueSignature) { Object value = context.getValue(path); assertEquals("Checking value: " + path, expectedValue, value); Pointer pointer = context.getPointer(path); assertEquals("Checking pointer: " + path, expectedPath, pointer.toString()); assertEquals("Checking signature: " + path, expectedSignature, pointerSignature(pointer)); Pointer vPointer = ((NodePointer) pointer).getValuePointer(); assertEquals("Checking value pointer signature: " + path, expectedValueSignature, pointerSignature(vPointer)); }
Example #15
Source File: TestJDOMFactory.java From commons-jxpath with Apache License 2.0 | 6 votes |
/** * Create a new instance and put it in the collection on the parent object. * Return <b>false</b> if this factory cannot create the requested object. */ public boolean createObject( JXPathContext context, Pointer pointer, Object parent, String name, int index) { if (name.equals("location") || name.equals("address") || name.equals("street")) { addJDOMElement((Element) parent, index, name, null); return true; } if (name.startsWith("price:")) { String namespaceURI = context.getNamespaceURI("price"); addJDOMElement((Element) parent, index, name, namespaceURI); return true; } return false; }
Example #16
Source File: BeanModelTestCase.java From commons-jxpath with Apache License 2.0 | 6 votes |
public void testIterateAndSet() { JXPathContext context = JXPathContext.newContext(createContextBean()); Iterator it = context.iteratePointers("beans/int"); int i = 5; while (it.hasNext()) { NodePointer pointer = (NodePointer) it.next(); pointer.setValue(new Integer(i++)); } it = context.iteratePointers("beans/int"); List actual = new ArrayList(); while (it.hasNext()) { actual.add(((Pointer) it.next()).getValue()); } assertEquals( "Iterating <" + "beans/int" + ">", list(new Integer(5), new Integer(6)), actual); }
Example #17
Source File: SimplePathInterpreterTest.java From commons-jxpath with Apache License 2.0 | 6 votes |
private void assertNullPointer(String path, String expectedPath, String expectedSignature) { Pointer pointer = context.getPointer(path); assertNotNull("Null path exists: " + path, pointer); assertEquals("Null path as path: " + path, expectedPath, pointer.asPath()); assertEquals("Checking Signature: " + path, expectedSignature, pointerSignature(pointer)); Pointer vPointer = ((NodePointer) pointer).getValuePointer(); assertTrue("Null path is null: " + path, !((NodePointer) vPointer).isActual()); assertEquals("Checking value pointer signature: " + path, expectedSignature + "N", pointerSignature(vPointer)); }
Example #18
Source File: TestMixedModelFactory.java From commons-jxpath with Apache License 2.0 | 5 votes |
/** * Create a new instance and put it in the collection on the parent object. * Return <b>false</b> if this factory cannot create the requested object. */ public boolean createObject( JXPathContext context, Pointer pointer, Object parent, String name, int index) { if (name.equals("nestedBean")) { ((TestBean) parent).setNestedBean(new NestedTestBean("newName")); return true; } else if (name.equals("beans")) { TestBean bean = (TestBean) parent; if (bean.getBeans() == null || index >= bean.getBeans().length) { bean.setBeans(new NestedTestBean[index + 1]); } bean.getBeans()[index] = new NestedTestBean("newName"); return true; } else if (name.equals("map")) { ((TestBean) parent).setMap(new HashMap()); return true; } else if (name.equals("TestKey5")) { TestBean tb = new TestBean(); tb.setNestedBean(null); tb.setBeans(null); ((Map) parent).put(name, tb); return true; } else if (name.equals("matrix")) { int[][] matrix = new int[2][]; matrix[0] = new int[1]; // matrix[1] = new int[2]; ((TestMixedModelBean) parent).setMatrix(matrix); return true; } return false; }
Example #19
Source File: JXPath172Test.java From commons-jxpath with Apache License 2.0 | 5 votes |
public void testIssue172_propertyExistAndIsNotNull() { final JXPathContext context = getContext("ciao", false); final Object bRet = context.selectSingleNode("value"); assertNotNull("null!!", bRet); assertEquals("Is " + bRet.getClass(), "ciao", bRet); final Pointer pointer = context.getPointer("value"); assertNotNull(pointer); assertEquals(BeanPropertyPointer.class, pointer.getClass()); assertEquals("ciao", pointer.getValue()); }
Example #20
Source File: JXPath172DynamicTest.java From commons-jxpath with Apache License 2.0 | 5 votes |
public void testIssue172_nestedpropertyDoesNotExist_NotLenient() { final JXPathContext context = getContext(null, false); final Object bRet = context.selectSingleNode("value.unexisting"); assertNull(bRet); final Pointer pointer = context.getPointer("value.unexisting"); assertEquals(DynamicPropertyPointer.class, pointer.getClass()); assertNull(pointer.getValue()); }
Example #21
Source File: MetaService.java From Knowage-Server with GNU Affero General Public License v3.0 | 5 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) private void addValue(String obj, String topath, JXPathContext context) throws SpagoBIException { logger.debug("ADD > " + obj + " > " + topath); if (topath.endsWith("]")) { int i = topath.lastIndexOf("["); Pointer toColl = context.getPointer(topath.substring(0, i)); if (toColl.getNode() instanceof List) { if (!((List) toColl.getNode()).contains(obj)) { ((List) toColl.getNode()).add(obj); } } } else { replace(obj, topath, context); } }
Example #22
Source File: ExceptionHandlerTest.java From commons-jxpath with Apache License 2.0 | 5 votes |
public void setUp() throws Exception { context = JXPathContext.newContext(this); context.setExceptionHandler(new ExceptionHandler() { public void handle(Throwable t, Pointer ptr) { if (t instanceof Error) { throw (Error) t; } if (t instanceof RuntimeException) { throw (RuntimeException) t; } throw new RuntimeException(t); } }); }
Example #23
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 #24
Source File: PathExistsMessageFilter.java From suro with Apache License 2.0 | 5 votes |
@Override public boolean apply(Object input) { JXPathContext jxpath = JXPathContext.newContext(input); // We should allow non-existing path, and let predicate handle it. jxpath.setLenient(true); Pointer pointer = jxpath.getPointer(xpath); return pointer != null && !(pointer instanceof NullPointer) && pointer.getValue() != null; }
Example #25
Source File: DOMNodePointer.java From commons-jxpath with Apache License 2.0 | 5 votes |
/** * Locates a node by ID. * @param context starting context * @param id to find * @return Pointer */ public Pointer getPointerByID(JXPathContext context, String id) { Document document = node.getNodeType() == Node.DOCUMENT_NODE ? (Document) node : node.getOwnerDocument(); Element element = document.getElementById(id); return element == null ? (Pointer) new NullPointer(getLocale(), id) : new DOMNodePointer(element, getLocale(), id); }
Example #26
Source File: TestDynamicPropertyFactory.java From commons-jxpath with Apache License 2.0 | 5 votes |
/** * Create a new instance and put it in the collection on the parent object. * Return <b>false</b> if this factory cannot create the requested object. */ public boolean createObject( JXPathContext context, Pointer pointer, Object parent, String name, int index) { if (name.equals("map")) { ((TestBean) parent).setMap(new HashMap()); return true; } else if (name.equals("TestKey1")) { ((Map) parent).put(name, ""); return true; } else if (name.equals("TestKey2")) { ((Map) parent).put(name, new NestedTestBean("newName")); return true; } else if (name.equals("TestKey3")) { Vector v = new Vector(); for (int i = 0; i <= index; i++) { v.add(null); } ((Map) parent).put(name, v); return true; } else if (name.equals("TestKey4")) { ((Map) parent).put(name, new Object[] { new TestBean()}); return true; } return false; }
Example #27
Source File: BeanModelTestCase.java From commons-jxpath with Apache License 2.0 | 5 votes |
/** * Test contributed by Kate Dvortsova */ public void testIteratePointerSetValue() { JXPathContext context = JXPathContext.newContext(createContextBean()); assertXPathValue(context, "/beans[1]/name", "Name 1"); assertXPathValue(context, "/beans[2]/name", "Name 2"); // Test setting via context context.setValue("/beans[2]/name", "Name 2 set"); assertXPathValue(context, "/beans[2]/name", "Name 2 set"); // Restore original value context.setValue("/beans[2]/name", "Name 2"); assertXPathValue(context, "/beans[2]/name", "Name 2"); int iterCount = 0; Iterator iter = context.iteratePointers("/beans/name"); while (iter.hasNext()) { iterCount++; Pointer pointer = (Pointer) iter.next(); String s = (String) pointer.getValue(); s = s + "suffix"; pointer.setValue(s); assertEquals("pointer.getValue", s, pointer.getValue()); // fails right here, the value isn't getting set in the bean. assertEquals( "context.getValue", s, context.getValue(pointer.asPath())); } assertEquals("Iteration count", 2, iterCount); assertXPathValue(context, "/beans[1]/name", "Name 1suffix"); assertXPathValue(context, "/beans[2]/name", "Name 2suffix"); }
Example #28
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 #29
Source File: NamespaceResolver.java From commons-jxpath with Apache License 2.0 | 5 votes |
/** * Get the namespace context pointer. * @return Pointer */ public Pointer getNamespaceContextPointer() { if (pointer == null && parent != null) { return parent.getNamespaceContextPointer(); } return pointer; }
Example #30
Source File: TestBeanFactory.java From commons-jxpath with Apache License 2.0 | 5 votes |
/** * Return <b>false</b> if this factory cannot create the requested object. */ public boolean createObject( JXPathContext context, Pointer pointer, Object parent, String name, int index) { if (name.equals("nestedBean")) { ((TestBean) parent).setNestedBean(new NestedTestBean("newName")); return true; } else if (name.equals("beans")) { TestBean bean = (TestBean) parent; if (bean.getBeans() == null || index >= bean.getBeans().length) { bean.setBeans(new NestedTestBean[index + 1]); } bean.getBeans()[index] = new NestedTestBean("newName"); return true; } else if (name.equals("integers")) { // This will implicitly expand the collection ((TestBean) parent).setIntegers(index, 0); return true; } return false; }