VariableReferenceExpression implements Expression { @Override public String getValue(EvaluationContext context) { String propertyName = pathExpression; String propertyPath = null; int delimiterIndex = -1; for (String delimiter : PROPERTY_NAME_DELIMITERS) { delimiterIndex = pathExpression.indexOf(delimiter); if (delimiterIndex != -1) { propertyName = pathExpression.substring(0, delimiterIndex); propertyPath = pathExpression.substring(delimiterIndex); break; } } Object variableValue = getProperty(variable, propertyName); if (log.isDebugEnabled()) { log.debug("propertyName: " + propertyName); log.debug("propertyPath: " + propertyPath); log.debug("variableValue: " + variableValue); } if (propertyPath != null) { if (variableValue.getClass().equals(String.class)) { if (propertyPath.startsWith("/")) { String variableString = String.valueOf(variableValue); if (variableString.trim().startsWith("{")) { variableValue = getJsonPointerValue(variableString, propertyPath); } else if (variableString.trim().startsWith("<")) { variableValue = getXPathValue(variableString, propertyPath); } else { log.warn("Got a path query expression but content seems not to be JSON nor XML..."); variableValue = null; } } } else if (variableValue.getClass().isArray()) { if (propertyPath.matches("\\[(\\d+)\\]")) { Matcher m = ARRAY_INDEX_PATTERN.matcher(propertyPath); if (m.matches()) { String arrayIndex = m.group(1); Object[] variableValues = (Object[])variableValue; try { variableValue = variableValues[Integer.parseInt(arrayIndex)]; } catch (ArrayIndexOutOfBoundsException ae) { log.warn("Expression asked for " + arrayIndex + " but array is smaller (" + variableValues.length + "). Returning null."); variableValue = null; } } } } else if (Map.class.isAssignableFrom(variableValue.getClass())) { if (propertyPath.matches("\\[(\\w+)\\]")) { Matcher m = MAP_INDEX_PATTERN.matcher(propertyPath); if (m.matches()) { String mapKey = m.group(1); Map variableValues = (Map)variableValue; variableValue = variableValues.get(mapKey); } } } } return String.valueOf(variableValue); } VariableReferenceExpression(Object variable, String pathExpression); Object getVariable(); void setVariable(Object variable); String getPathExpression(); void setPathExpression(String pathExpression); @Override String getValue(EvaluationContext context);  }
@Test public void testStringValue() { EvaluableRequest request = new EvaluableRequest("hello world", null); VariableReferenceExpression exp = new VariableReferenceExpression(request, "body"); String result = exp.getValue(new EvaluationContext()); assertEquals("hello world", result); }
@Test public void testJSONPointerValue() { String jsonString = "{\n" + " \"library\": \"My Personal Library\",\n" + " \"books\": [\n" + " { \"title\":\"Title 1\", \"author\":\"Jane Doe\" },\n" + " { \"title\":\"Title 2\", \"author\":\"John Doe\" }\n" + " ]\n" + "}"; EvaluableRequest request = new EvaluableRequest(jsonString, null); VariableReferenceExpression exp = new VariableReferenceExpression(request, "body/books/1/author"); String result = exp.getValue(new EvaluationContext()); assertEquals("John Doe", result); }
@Test public void testXPathValue() { String xmlString = "<library>\n" + " <name>My Personal Library</name>\n" + " <books>\n" + " <book><title>Title 1</title><author>Jane Doe</author></book>\n" + " <book><title>Title 2</title><author>John Doe</author></book>\n" + " </books>\n" + "</library>"; EvaluableRequest request = new EvaluableRequest(xmlString, null); VariableReferenceExpression exp = new VariableReferenceExpression(request, "body/library/books/book[1]/author"); String result = exp.getValue(new EvaluationContext()); assertEquals("Jane Doe", result); }
@Test public void testXPathWithNamespaceValue() { String xmlString = "<ns:library xmlns:ns=\"https: " <ns:name>My Personal Library</ns:name>\n" + " <ns:books>\n" + " <ns:book><ns:title>Title 1</ns:title><ns:author>Jane Doe</ns:author></ns:book>\n" + " <ns:book><ns:title>Title 2</ns:title><ns:author>John Doe</ns:author></ns:book>\n" + " </ns:books>\n" + "</ns:library>"; EvaluableRequest request = new EvaluableRequest(xmlString, null); VariableReferenceExpression exp = new VariableReferenceExpression(request, "body String result = exp.getValue(new EvaluationContext()); assertEquals("My Personal Library", result); }