List of usage examples for org.apache.commons.jxpath JXPathContext getValue
public abstract Object getValue(String xpath);
From source file:org.solmix.datax.xmlfile.XmlFileDataService.java
@Override protected DSResponse executeFetch(DSRequest req) throws DSCallException { OperationInfo oi = req.getOperationInfo(); Object dataUrl = oi.getProperty("dataUrl"); Object recordXPath = oi.getProperty("recordXPath"); DSResponseImpl res = new DSResponseImpl(this, req); Object value = getDataFromFile(dataUrl); if (recordXPath != null) { JXPathContext context = JXPathContext.newContext(value); res.setRawData(context.getValue(recordXPath.toString())); } else {/*from w w w . ja v a2 s .c o m*/ res.setRawData(value); } return res; }
From source file:org.structr.common.geo.AbstractGeoCodingProvider.java
protected <T> T extract(Map source, String path, Class<T> type) { JXPathContext context = JXPathContext.newContext(source); T value = (T) context.getValue(path); return value; }
From source file:org.toobsframework.pres.chart.ChartUtil.java
public static String evaluateTextLabel(IRequest componentRequest, TextLabel textLabel, Map params) { if (textLabel == null || textLabel.getPath() == null) return null; if (textLabel.getIsStatic()) { return ParameterUtil.resolveParam(componentRequest, textLabel.getPath(), params, textLabel.getDefault())[0]; }/*from w ww . j a v a2 s . c o m*/ JXPathContext context = JXPathContext.newContext(params); String labelText = (String) context.getValue(textLabel.getPath()); if (labelText == null & textLabel.getDefault() != null) { labelText = textLabel.getDefault(); } return labelText; }
From source file:org.toobsframework.pres.doit.DoItRunner.java
private void runAction(IRequest request, String doItName, Action thisAction, Map<String, Object> params, Map<String, Object> responseParams, boolean lastAction) throws Exception { String actionType = thisAction.getActionType(); Object retObj = null;//from w w w . j a v a2 s .c o m if (actionType.equalsIgnoreCase("objectAction")) { //Fix the input params using the param mapping for //this configuration. if (thisAction.getParameters() != null) { // Cant do this for now cause of the array problem //ParameterUtil.mapParameters(thisAction.getParameters().getParameter(), params, params, doItName); ParameterUtil.mapDoItParameters(request, thisAction.getParameters().getParameter(), params, params, true); } try { if (thisAction.isExtended()) { retObj = this.getDataProvider().dispatchActionEx(request, thisAction.getAction(), ((String[]) ParameterUtil.resolveParam(request, thisAction.getServiceProvider(), params))[0], ((String[]) ParameterUtil.resolveParam(request, thisAction.getInputObjectType(), params))[0], thisAction.getReturnObjectType(), ((String[]) ParameterUtil.resolveParam(request, thisAction.getGuidParam(), params))[0], thisAction.getPermissionContext(), thisAction.getIndexParam(), thisAction.getNamespace(), params, responseParams); } else { retObj = this.getDataProvider().dispatchAction(thisAction.getAction(), ((String[]) ParameterUtil.resolveParam(request, thisAction.getServiceProvider(), params))[0], ((String[]) ParameterUtil.resolveParam(request, thisAction.getInputObjectType(), params))[0], thisAction.getReturnObjectType(), ((String[]) ParameterUtil.resolveParam(request, thisAction.getGuidParam(), params))[0], thisAction.getPermissionContext(), thisAction.getIndexParam(), thisAction.getNamespace(), params, responseParams); } /* TODO: Remove this later Iterator iter = responseParams.keySet().iterator(); while (iter.hasNext()) { Object key = iter.next(); params.put((String)key, responseParams.get(key)); } */ } catch (Exception e) { /* TODO Check to see if making responseParams work as error forward params * cause this sucks balls if (e.getCause() instanceof ValidationException) { responseParams.put("ErrorForwardParams", params.get("ErrorForwardParams")); } */ throw e; } } else if (actionType.equalsIgnoreCase("cookieAction")) { String cookieName = ((String[]) ParameterUtil.resolveParam(request, params.get("cookieName"), params))[0]; String cookieValue = ((String[]) ParameterUtil.resolveParam(request, params.get("cookieValue"), params))[0]; int maxAge = -1; try { maxAge = Integer.parseInt( ((String[]) ParameterUtil.resolveParam(request, params.get("maxAge"), params))[0]); } catch (Exception e) { } Cookie doitCookie = new Cookie(cookieName, cookieValue); doitCookie.setMaxAge(maxAge); componentRequestManager.get().getHttpResponse().addCookie(doitCookie); } else if (actionType.equalsIgnoreCase("sessionAction")) { Map<String, Object> sessionMap = new HashMap<String, Object>(); if (thisAction.getParameters() != null) { ParameterUtil.mapDoItParameters(request, thisAction.getParameters().getParameter(), params, sessionMap, true); } HttpSession session = componentRequestManager.get().getHttpRequest().getSession(); Iterator<Map.Entry<String, Object>> iter = sessionMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<String, Object> entry = iter.next(); session.setAttribute(entry.getKey(), entry.getValue()); } } else if (actionType.equalsIgnoreCase("indexAction") && lastAction) { if (this.getIndexBuilder() != null) { indexBuilder.buildIndexes(thisAction.getServiceProvider()); } } else { //TODO -- Add the ability to run scripts defined in config here. } //HashMap responseParams = new HashMap(); //Add the output params into the request for //this configuration. if (thisAction.getOutputParameters() != null && retObj != null) { JXPathContext context = null; if ("delete".equalsIgnoreCase(thisAction.getAction())) { context = JXPathContext.newContext(responseParams); responseParams.put("deleted", String.valueOf(((Boolean) retObj).booleanValue())); } else { context = JXPathContext.newContext(retObj); } Parameter[] paramMap = thisAction.getOutputParameters().getParameter(); for (int j = 0; j < paramMap.length; j++) { Parameter thisParam = paramMap[j]; String[] paramPath = ParameterUtil.resolveParam(request, thisParam.getPath(), params); String[] paramName = ParameterUtil.resolveParam(request, thisParam.getName(), params); Object value = null; for (int i = 0; i < paramName.length; i++) { if (thisParam.getIsStatic()) { value = thisParam.getPath(); } else { try { value = context.getValue(paramPath[i]); } catch (org.apache.commons.jxpath.JXPathException e) { if (!thisParam.getIgnoreNull()) { log.warn("Problem evaluating jxpath: " + paramName[i] + " value: " + paramPath[i] + " action: " + thisAction.getServiceProvider(), e); } continue; } if (value != null && value.getClass().isArray()) { value = ((String[]) value)[0]; } } responseParams.put(paramName[i], value); } } } //Add if (thisAction.getReturnAttributeName() != null && retObj != null) { JXPathContext context = JXPathContext.newContext(retObj); responseParams.put(thisAction.getReturnAttributeName(), context.getValue("./valueObject/guid")); } Iterator<String> iter = responseParams.keySet().iterator(); while (iter.hasNext()) { String key = iter.next(); params.put(key, responseParams.get(key)); } }
From source file:org.toobsframework.pres.util.ParameterUtil.java
public static void mapParameters(IRequest request, String callingContext, Parameter[] paramMap, Map inParams, Map outParams, String scopeId, List<IDataProviderObject> objects) throws ParameterException { if (paramMap != null) { JXPathContext context = JXPathContext.newContext(inParams); for (int j = 0; j < paramMap.length; j++) { Parameter thisParam = paramMap[j]; Object value = null;// ww w .j a v a2 s . com String thisPath = null; String thisName = null; try { if (thisParam.getScope() != null && !thisParam.getScope().equalsIgnoreCase("all") && !thisParam.getScope().equalsIgnoreCase(scopeId)) { continue; } if (!thisParam.getOverwriteExisting() && inParams.get(thisParam.getName()) != null) { continue; } thisName = resolveParam(request, thisParam.getName(), inParams)[0]; thisPath = resolveParam(request, thisParam.getPath(), inParams)[0]; boolean condition = true; if (thisParam.getCondition() != null) { Object condObj = context.getValue(thisParam.getCondition()); if (log.isDebugEnabled()) { log.debug("Condition Object: " + condObj); } if (condObj != null && condObj instanceof Boolean) { condition = (Boolean) condObj; } } if (condition) { if (thisParam.getIsStatic()) { value = thisPath; } else if (thisParam.getIsObject()) { if ((objects == null) || (objects != null && thisParam.getObjectIndex() >= objects.size())) { continue; } JXPathContext objContext = JXPathContext .newContext(objects.get(thisParam.getObjectIndex())); if (thisParam.getIsList()) { Iterator iter = objContext.iterate(thisPath); value = new ArrayList(); while (iter.hasNext()) { ((ArrayList) value).add(iter.next()); } if (((ArrayList) value).size() == 0 && thisParam.getDefault() != null) { ((ArrayList) value).add(thisParam.getDefault()); } } else { value = objContext.getValue(thisPath); } } else if (thisParam.getIsList()) { Object newList = inParams.get(thisName); if (newList == null) newList = outParams.get(thisName); if (newList != null && !(newList instanceof ArrayList)) { newList = new ArrayList(); ((ArrayList) newList).add(value); } if (newList == null) newList = new ArrayList(); value = context.getValue(thisPath); if (value != null && value.getClass().isArray()) { Object[] valueArray = (Object[]) value; if (valueArray.length > 1) { for (int i = 0; i < valueArray.length; i++) { if (valueArray[i] != null && ((String) valueArray[i]).length() > 0) ((ArrayList) newList).add(valueArray[i]); } value = null; } else { value = valueArray[0]; } } if (value != null && !"".equals(value)) ((ArrayList) newList).add(value); value = newList; } else { value = context.getValue(thisPath); if (value != null && value.getClass().isArray()) { Object[] valueArray = (Object[]) value; if (valueArray.length > 1) { value = valueArray; } else { value = valueArray[0]; } } else if (value == null && thisParam.getSessionPath() != null) { value = context.getValue(thisParam.getSessionPath()); } } if (value != null && value.getClass().isArray() && thisParam.getIsList()) { outParams.put(thisName, value); } else if (value != null && value.getClass().isArray()) { outParams.put(thisName, ((String[]) value)[0]); } else if (value != null && value instanceof ArrayList && ((ArrayList) value).size() > 0) { outParams.put(thisName, value); } else if (value != null && value instanceof String) { outParams.put(thisName, (String) value); } else if (value != null && !(value instanceof ArrayList) && String.valueOf(value).length() > 0) { outParams.put(thisName, String.valueOf(value)); } else if (thisParam.getDefault() != null) { String[] defVal = resolveParam(request, thisParam.getDefault(), inParams); if (defVal != null) { outParams.put(thisName, defVal[0]); } } else if (!thisParam.getIgnoreNull()) { throw new ParameterException(callingContext, thisName, thisPath); } else if (log.isDebugEnabled()) { log.debug("Param " + thisName + " evaluated to null"); } } } catch (Exception e) { log.error("mapParameters - exception [name:" + thisName + " path:" + thisPath + " value:" + value + "]"); throw new ParameterException(callingContext, thisName, thisPath); } } } }
From source file:org.toobsframework.pres.util.ParameterUtil.java
public static void mapOutputParameters(IRequest request, Parameter[] paramMap, Map paramsIn, String scopeId, List<IDataProviderObject> objects) { for (int j = 0; j < paramMap.length; j++) { Parameter thisParam = paramMap[j]; if (thisParam.getScope() != null && !thisParam.getScope().equalsIgnoreCase("all") && !thisParam.getScope().equalsIgnoreCase(scopeId)) { continue; }// w w w . j a v a 2s .c o m if (!thisParam.getOverwriteExisting() && paramsIn.get(thisParam.getName()) != null) { continue; } if (thisParam.getObjectIndex() >= objects.size()) { continue; } JXPathContext context = null; Object value = null; String paramName = resolveParam(request, thisParam.getName(), paramsIn)[0]; try { String thisPath = resolveParam(request, thisParam.getPath(), paramsIn)[0]; if (thisParam.getIsStatic()) { value = thisPath; } else { if (thisParam.getIsList()) { value = new ArrayList(); if (thisParam.getObjectIndex() == -1) { for (int i = 0; i < objects.size(); i++) { context = JXPathContext.newContext(objects.get(i)); ((ArrayList) value).add(context.getValue(thisPath)); } } else { context = JXPathContext.newContext(objects.get(thisParam.getObjectIndex())); Iterator iter = context.iterate(thisPath); while (iter.hasNext()) { ((ArrayList) value).add(iter.next()); } } if (((ArrayList) value).size() == 0) { if (thisParam.getDefault() != null) { try { ((ArrayList) value).add(Integer.parseInt(thisParam.getDefault())); } catch (NumberFormatException nfe) { ((ArrayList) value).add(thisParam.getDefault()); } } else { value = null; } } } else { context = JXPathContext.newContext(objects.get(thisParam.getObjectIndex())); value = context.getValue(thisPath); } } if (value != null && List.class.isAssignableFrom(value.getClass()) && ((List) value).size() == 0 && thisParam.getDefault() != null) { ((List) value).add(thisPath); } paramsIn.put(paramName, value); } catch (JXPathException e) { if (thisParam.getDefault() != null) { String[] def = resolveParam(request, thisParam.getDefault(), paramsIn); if (def != null && def.length > 0) { paramsIn.put(paramName, def[0]); } } else if (!thisParam.getIgnoreNull()) { log.error("JXPathException for parameter " + paramName + " in scope " + scopeId); // TODO This should be a BaseException throw e; } } } }
From source file:org.toobsframework.pres.util.ParameterUtil.java
public static void mapDoItParameters(IRequest request, Parameter[] paramMap, Map paramsIn, Map paramsOut, boolean useJXPathContext) { JXPathContext context = null; if (useJXPathContext) context = JXPathContext.newContext(paramsIn); for (int j = 0; j < paramMap.length; j++) { Parameter thisParam = paramMap[j]; Object value = null;//from w ww . j a v a 2s . c o m if (thisParam.getIsStatic()) { String[] valueAry = new String[1]; valueAry[0] = resolveParam(request, thisParam.getPath(), paramsIn)[0]; value = valueAry; } else { value = context.getValue(resolveParam(request, thisParam.getPath(), paramsIn)[0]); if (value != null && value.getClass().isArray() && ((Object[]) value).length == 1) { value = ((Object[]) value)[0]; } else if (value == null && thisParam.getDefault() != null) { value = thisParam.getDefault(); } } paramsOut.put(resolveParam(request, thisParam.getName(), paramsIn)[0], value); } }
From source file:org.xchain.framework.jxpath.JXPathContextTest.java
@Test public void testRelativeContext() throws Exception { Node root = new Node("root"); Node child = new Node("child"); root.getChild().add(child);/*from w w w.j ava2 s . co m*/ JXPathContext rootContext = JXPathContext.newContext(root); JXPathContext childContext = rootContext.getRelativeContext(rootContext.getPointer("/child")); String rootName = (String) childContext.getValue("/name"); assertEquals("The root node has the wrong name.", "root", rootName); String childName = (String) childContext.getValue("name"); assertEquals("The context node has the wrong name.", "child", childName); }
From source file:org.xchain.framework.util.ComponentUtil.java
/** * Perform dependency injection on the given component. *//*from w w w.j a v a 2 s . c om*/ public static void doInjection(Object component, ComponentAnalysis analysis, JXPathContext context) throws IllegalAccessException, InvocationTargetException { for (InjectionAnalysis injection : analysis.getInjections()) { Map<String, String> originalPrefixMapping = pushPrefixMap(context, injection.getPrefixMappings()); try { if (injection.getField() != null) { injection.getField().set(component, context.getValue(injection.getSelect())); } else if (injection.getMethod() != null) { injection.getMethod().invoke(component, context.getValue(injection.getSelect())); } } catch (Exception ex) { if (!injection.isNullable()) { // Unable to inject value on a non-nullable field. String failurePoint = null; if (injection.getField() != null) failurePoint = "field '" + injection.getField().getName() + "'"; else if (injection.getMethod() != null) failurePoint = "method '" + injection.getMethod().getName() + "'"; String failurePath = " from path '" + injection.getSelect() + "'"; if (context instanceof ScopedJXPathContextImpl) { ScopedJXPathContextImpl impl = (ScopedJXPathContextImpl) context; failurePath += " at scope '" + impl.getScope() + "'."; } throw new DependencyInjectionException( "Unable to inject value into " + failurePoint + failurePath, ex); } } popPrefixMap(context, originalPrefixMapping); } }
From source file:org.xchain.javassist.CommandEngineeringTest.java
@Test public void testExtendingExecute() throws Exception { Command command = getExtendingCommand("org.xchain.javassist.SimpleCommand"); // create a context. JXPathContext context = JXPathContext.newContext(new Object()); // execute the command command.execute(context);//from ww w.j a v a 2s . com // assert that the namespace was defined while the command executed. assertEquals("The namespace returned was not correct.", (Object) "http://www.xchain.org/test", context.getValue("$namespace")); // assert that the namespace is no longer defined on the context. assertEquals("The namespace was still defined on the context.", (String) null, context.getNamespaceURI("test")); }