/** return the xpath for a particular object. * * @param target object to return xpath for. must be in object tree (or a similar object equivalent under equals()) * @return xpath for that object, or null * */ public final String getXPathFor(Object target) { // add variable we're looking for. JXPathContext cxt = this.accessJXPathContext(); cxt.getVariables().declareVariable("target",target); Pointer p = cxt.getPointer("//*[fn:matchTarget()]"); // tidyup cxt.getVariables().undeclareVariable("target"); // sanity check.. return p != null && p.getNode() != null ? p.asPath() : null; }
public Object getBaseValue() { if (!actual) { throw new JXPathException("Undefined variable: " + name); } return variables.getVariable(name.toString()); }
/** * Assimilate the Variables instance associated with the specified context. * @param context JXPathContext to search */ protected void findVariables(JXPathContext context) { valuePointer = null; JXPathContext varCtx = context; while (varCtx != null) { variables = varCtx.getVariables(); if (variables.isDeclaredVariable(name.toString())) { actual = true; break; } varCtx = varCtx.getParentContext(); variables = null; } }
public void setValue(Object value) { if (!actual) { throw new JXPathException("Cannot set undefined variable: " + name); } valuePointer = null; if (index != WHOLE_COLLECTION) { Object collection = getBaseValue(); ValueUtils.setValue(collection, index, value); } else { variables.declareVariable(name.toString(), value); } }
ObjectTree nameLocal = getNameMap(true, false); vars.declareVariable(XPATH_NAME_GLOBAL_VAR, nameGlobal); vars.declareVariable(XPATH_NAME_LOCAL_VAR, nameLocal);
public void remove() { if (actual) { if (index == WHOLE_COLLECTION) { variables.undeclareVariable(name.toString()); } else { if (index < 0) { throw new JXPathInvalidAccessException( "Index is less than 1: " + asPath()); } Object collection = getBaseValue(); if (collection != null && index < getLength()) { collection = ValueUtils.remove(collection, index); variables.declareVariable(name.toString(), collection); } } } }
ObjectTree nameLocal = getNameMap(true, false); vars.declareVariable(XPATH_NAME_GLOBAL_VAR, nameGlobal); vars.declareVariable(XPATH_NAME_LOCAL_VAR, nameLocal);
/** 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); }
public NodePointer createNodePointer(QName name, Object object, Locale locale) { if (object instanceof VariableContextWrapper) { JXPathContext varCtx = ((VariableContextWrapper) object).getContext(); while (varCtx != null) { Variables vars = varCtx.getVariables(); if (vars.isDeclaredVariable(name.toString())) { return new VariablePointer(vars, name); } varCtx = varCtx.getParentContext(); } // The variable is not declared, but we will create // a pointer anyway in case the user wants to set, rather // than get, the value of the variable. return new VariablePointer(name); } return null; }
/** * Create a collection. * @param context JXPathContext * @param index collection index * @return Object */ private Object createCollection(JXPathContext context, int index) { createPath(context); Object collection = getBaseValue(); if (collection == null) { throw new JXPathAbstractFactoryException( "Factory did not assign a collection to variable '" + name + "' for path: " + asPath()); } if (index == WHOLE_COLLECTION) { index = 0; } else if (index < 0) { throw new JXPathInvalidAccessException("Index is less than 1: " + asPath()); } if (index >= getLength()) { collection = ValueUtils.expandCollection(collection, index + 1); variables.declareVariable(name.toString(), collection); } return collection; }