List of usage examples for org.apache.commons.jxpath JXPathContext getValue
public abstract Object getValue(String xpath);
From source file:org.openvpms.component.system.service.jxpath.JXPathTestCase.java
/** * Verifies that {@link ObjectFunctions} can be added to the function * library and their instance and static methods invoked. *//*from www. j a v a 2 s . com*/ @Test public void testObjectFunctions() { List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); JXPathContext ctx = JXPathHelper.newContext(list); FunctionLibrary lib = new FunctionLibrary(); lib.addFunctions(new ObjectFunctions(new TestFunctions(), "test")); ctx.setFunctions(lib); // test instance methods assertEquals("a", ctx.getValue("test:getValue(., 0)")); assertEquals("c", ctx.getValue("test:getValue(., 2)")); assertEquals("foo", ctx.getValue("test:getValue()")); // test static methods assertEquals("Jimmy", ctx.getValue("test:getContacts()")); }
From source file:org.openvpms.macro.impl.MacroFunctionsTestCase.java
/** * Tests the single argument version:/* w w w. ja v a 2s .c o m*/ * <pre> * macro:eval('macroname') * </pre> * This evaluates the macro against the context object. */ @Test public void testSingleArgEval() { Party customer = TestHelper.createCustomer(false); JXPathContext ctx = createContext(customer); assertEquals("Customer", ctx.getValue("macro:eval('displayName')")); }
From source file:org.openvpms.macro.impl.MacroFunctionsTestCase.java
/** * Tests the two argument version:/* w w w . j a va2s . c om*/ * <pre> * macro:eval('macroname', somecontext) * </pre> * This evaluates the macro against the specified context object. */ @Test public void testTwoArgEval() { Party customer = TestHelper.createCustomer(false); JXPathContext ctx = createContext(customer); assertEquals("Customer", ctx.getValue("macro:eval('displayName', .)")); ctx = createContext(new Object()); ctx.getVariables().declareVariable("customer", customer); assertEquals("Customer", ctx.getValue("macro:eval('displayName', $customer)")); }
From source file:org.openvpms.macro.impl.MacroFunctionsTestCase.java
/** * Tests that numeric prefixes are expanded as the $number variable. *//*from www .ja v a2s .co m*/ @Test public void testNumericPrefix() { Object dummy = new Object(); JXPathContext ctx = createContext(dummy); // verify that when no prefix is specified, the number doesn't evaluate // to anything Object text1 = ctx.getValue("macro:eval('numbertest')"); assertEquals("input number: ", text1); Object text2 = ctx.getValue("macro:eval('99numbertest')"); assertEquals("input number: 99", text2); }
From source file:org.openvpms.report.jasper.IMObjectDataSource.java
/** * Returns a data source for the given jxpath expression. * * @param expression the expression. Must return an {@code Iterable} or {@code Iterator} returning {@code IMObjects} * @return the data source//from www . j av a 2s.com * @throws JRException for any error */ @Override @SuppressWarnings("unchecked") public JRRewindableDataSource getExpressionDataSource(String expression) throws JRException { JXPathContext context = JXPathHelper.newContext(object, getFunctions()); Object value = context.getValue(expression); Iterable<IMObject> iterable; if (value instanceof Iterable) { iterable = (Iterable<IMObject>) value; } else { throw new JRException("Unsupported value type=" + ((value != null) ? value.getClass() : null) + " returned by expression=" + expression); } return new IMObjectCollectionDataSource(iterable, fields, getArchetypeService(), getLookupService(), getDocumentHandlers(), getFunctions()); }
From source file:org.openvpms.web.component.im.doc.FileNameFormatter.java
/** * Formats a file name using the jxpath expression returned by {@link DocumentTemplate#getFileNameExpression()}. * <p/>/*from www. j ava 2 s . c om*/ * Any extension is removed from the original file name. * * @param name the original file name. The base name of this is passed to the expression in the {@code $file} * variable * @param object the context object. If this an act, any related customer, patient, or supplier will be passed as * the variables $customer, $patient, and $supplier respectively. May be {@code null} * @param template the document template * @return the formatted name, or the base name of {@code name} if the template doesn't specify a format or * generation fails */ public String format(String name, IMObject object, DocumentTemplate template) { String result; String file = FilenameUtils.getBaseName(name); String expression = template.getFileNameExpression(); if (!StringUtils.isEmpty(expression)) { JXPathContext context = JXPathHelper.newContext(object != null ? object : new Object()); FileNameVariables variables = new FileNameVariables(archetypeService, lookups); context.setVariables(variables); Party patient = null; Party customer = null; Party supplier = null; if (object instanceof Act) { Act act = (Act) object; ActBean bean = new ActBean(act); if (bean.hasNode("patient")) { patient = (Party) bean.getNodeParticipant("patient"); } if (bean.hasNode("customer")) { customer = (Party) bean.getNodeParticipant("customer"); } else if (patient != null) { PatientRules rules = ServiceHelper.getBean(PatientRules.class); customer = rules.getOwner(patient, act.getActivityStartTime(), false); } if (bean.hasNode("supplier")) { supplier = (Party) bean.getNodeParticipant("supplier"); } } variables.declareVariable("customer", customer); variables.declareVariable("patient", patient); variables.declareVariable("supplier", supplier); variables.declareVariable("file", file); try { Object value = context.getValue(expression); result = (value != null) ? clean(value.toString()) : file; } catch (Throwable exception) { log.error("Failed to evaluate expression: " + expression, exception); result = file; } } else { result = file; } return result; }
From source file:org.openvpms.web.workspace.patient.history.AbstractPatientHistoryTableModel.java
/** * Returns the act detail as a string.// w ww . j a va2 s . co m * If a jxpath expression is registered, this will be evaluated, otherwise the act description will be used. * * @param act the act * @return the text. May be {@code null} */ protected String getText(Act act) { String text = null; String shortName = act.getArchetypeId().getShortName(); String expr = getExpression(shortName); if (!StringUtils.isEmpty(expr)) { try { JXPathContext context = JXPathHelper.newContext(act, functions); Object value = context.getValue(expr); if (value != null) { text = value.toString(); } } catch (Throwable exception) { log.error(exception, exception); text = exception.getMessage(); } } else { text = act.getDescription(); } return text; }
From source file:org.openvpms.web.workspace.workflow.scheduling.SchedulingHelper.java
/** * Evaluates an xpath expression against the supplied event. * <p/>/*www .jav a2s . com*/ * This adds a "waiting" time attribute to the event prior to evaluation as determined by {@link #getWaitingTime}. * <p/> * NOTE: any string sequence containing the characters '\\n' will be treated * as new lines. * * @param expression the expression * @param event the event * @return the evaluate result. May be {@code null} */ public static String evaluate(String expression, PropertySet event) { String text; String waiting = getWaitingTime(event); if (waiting != null) { waiting = Messages.format("scheduleview.expression.waiting", waiting); } else { waiting = ""; // makes it easier to use in expressions } event.set("waiting", waiting); JXPathContext context = JXPathHelper.newContext(event); // hack to replace all instances of '\\n' with new lines to // enable new lines to be included in the text // Can't use <br> as all xml is escaped expression = expression.replace("\'\\n\'", "\'\n\'"); try { Object value = context.getValue(expression); text = (value != null) ? value.toString() : null; } catch (Throwable exception) { text = "Expression Error"; } return text; }
From source file:org.paxle.tools.ieporter.cm.impl.ConfigurationIEPorterTest.java
public void testExportConfiguration() throws ParserConfigurationException { final Dictionary<String, Object> props = new Hashtable<String, Object>(); props.put("myProperty.Integer", Integer.valueOf(1)); props.put("myProperty.intArray", new int[] { 1, 2, 3, 4 }); props.put("myProperty.String", "test"); props.put("myProperty.StringArray", new String[] { "test1", "test2", "test3" }); final Configuration config = mock(Configuration.class); checking(new Expectations() { {/*from www .j av a 2 s .com*/ atLeast(1).of(config).getPid(); will(returnValue("testPid")); atLeast(1).of(config).getProperties(); will(returnValue(props)); never(config); } }); Map<String, Document> configs = this.ieporter.exportConfiguration(config); assertNotNull(configs); assertEquals(1, configs.size()); assertTrue(configs.containsKey("testPid")); Document doc = configs.get("testPid"); assertNotNull(doc); JXPathContext objContext = JXPathContext.newContext(doc); assertEquals("testPid", objContext.getValue("//service.pid")); assertEquals(props.size(), ((Double) objContext.getValue("count(//property)")).intValue()); assertEquals(props.get("myProperty.Integer").toString(), objContext.getValue("//property[@key='myProperty.Integer']/value")); assertEquals(Array.getLength(props.get("myProperty.intArray")), ((Double) objContext.getValue("count(//property[@key='myProperty.intArray']/values/value)")) .intValue()); assertEquals(props.get("myProperty.String").toString(), objContext.getValue("//property[@key='myProperty.String']/value")); assertEquals(Array.getLength(props.get("myProperty.StringArray")), ((Double) objContext.getValue("count(//property[@key='myProperty.StringArray']/values/value)")) .intValue()); }
From source file:org.romaframework.core.schema.SchemaHelper.java
/** * Returns the object of field in expression. * // w w w . j av a 2 s. c o m * @param iStartingObject * Starting object to navigate * @param iExpression * Path of field * @return */ public static Object getFieldObject(Object iStartingObject, String iExpression) { int lastSep = iExpression.lastIndexOf('.'); if (lastSep == -1) { return iStartingObject; } String exp = iExpression.substring(0, lastSep); exp = exp.replace('.', '/'); JXPathContext context = JXPathContext.newContext(iStartingObject); return context.getValue(exp); }