/** * Creates a new JXPathContext. * * @param object the context object * @return a new JXPathContext */ private JXPathContext createContext(Object object) { ListFunctions listFunctions = new ListFunctions(getArchetypeService(), getLookupService()); FunctionLibrary library = new FunctionLibrary(); library.addFunctions(new ObjectFunctions(listFunctions, "list")); library.addFunctions(new ExpressionFunctions("expr")); return JXPathHelper.newContext(object, library); }
/** * Adds functions for the specified function object and namespace. * * @param functionObject the function object * @param namespace the namespace */ private void addFunctions(Object functionObject, String namespace) { if (functionObject instanceof Functions) { // namespace is ignored functions.addFunctions((Functions) functionObject); } else { functions.addFunctions(new ObjectFunctions(functionObject, namespace)); } }
/** * Create a new context for the specified object that has access to the supplied functions. * * @param object the context bean * @param functions the functions * @return JXPathContext the context object */ public static JXPathContext newContext(Object object, Functions functions) { JXPathContext context = JXPathContext.newContext(object); FunctionLibrary lib = new FunctionLibrary(); lib.addFunctions(context.getFunctions()); lib.addFunctions(functions); context.setFunctions(lib); context.setLenient(true); return context; }
static { System.setProperty( JXPathContextFactory.FACTORY_NAME_PROPERTY, OpenVPMSContextFactoryReferenceImpl.class.getName()); // set the extended type converter TypeUtils.setTypeConverter(new OpenVPMSTypeConverter()); // now just add the default functions functions.addFunctions(JXPathContext.newContext(new Object()).getFunctions()); }
/** * Adds functions for the specified class name and namespace. * * @param className the class name * @param namespace the namespace */ private void addFunctions(String className, String namespace) { try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); Class clazz = loader.loadClass(className); functions.addFunctions(new ClassFunctions(clazz, namespace)); } catch (Exception exception) { throw new JXPathHelperException( JXPathHelperException.ErrorCode.InvalidClassSpecified, new Object[] {className}, exception); } }