List of usage examples for org.apache.commons.jxpath JXPathContext newContext
public static JXPathContext newContext(Object contextBean)
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.sweble.wikitext.example.XPath.java
static String query(EngProcessedPage cp, String query) { Iterator<?> results = null; try {//from w w w .ja v a 2 s.c om JXPathContext context = JXPathContext.newContext(cp.getPage()); results = context.iterate(query); } catch (Throwable t) { System.err.println("An error occurred when executing XPath query."); t.printStackTrace(); } if (results != null) { if (!results.hasNext()) { System.err.println("XPath result empty!"); } else { List<Object> r = new ArrayList<Object>(); while (results.hasNext()) r.add(results.next()); System.err.println("Found " + r.size() + " matching nodes."); StringBuilder b = new StringBuilder(); int i = 1; for (Object o : r) { WtNode n = (WtNode) o; b.append('('); b.append(query); b.append(")["); b.append(i); b.append("]:\n\"\"\""); b.append(WtRtDataPrinter.print(n)); b.append("\"\"\"\n\n"); ++i; } return b.toString(); } } return ""; }
From source file:org.sweble.wikitext.parser.XPathTest.java
@Test public void testFrance() throws IOException, ParseException { String title = "raw-France"; WtNode ast = parse(title);//from w ww .j av a2s.c o m JXPathContext context = JXPathContext.newContext(ast); StringBuilder b = new StringBuilder(); doQuery(context, b, "/WtParagraph[3]"); doQuery(context, b, "(//WtSection[@level=3])[1]"); doQuery(context, b, "//WtTemplate[contains(name//WtText[@content],\"Infobox Country\")]//WtTemplateArgument[contains(name//WtText[@content],\"capital\")]/value"); String actual = FileUtils.lineEndToUnix(b.toString()); String expected = null; try { expected = load(PATH + "/ast/" + title + ".ast"); } catch (IOException e) { } Assert.assertEquals(expected, actual); }
From source file:org.sweble.wikitext.parser.XPathTest.java
@Test public void testGermany() throws IOException, ParseException { String title = "raw-Germany"; WtNode ast = parse(title);//from www . j a va 2 s. c o m JXPathContext context = JXPathContext.newContext(ast); StringBuilder b = new StringBuilder(); doQuery(context, b, "//WtTemplate[contains(name//WtText[@content],\"Infobox country\")]//WtTemplateArgument[contains(name//WtText[@content],\"capital\")]/value"); String actual = FileUtils.lineEndToUnix(b.toString()); String expected = null; try { expected = load(PATH + "/ast/" + title + ".ast"); } catch (IOException e) { } Assert.assertEquals(expected, actual); }
From source file:org.talend.esb.sam.common.filter.impl.JxPathFilter.java
@Override public boolean filter(Event event) { JXPathContext context = JXPathContext.newContext(event); Pointer pointer = context.getPointer(expression); return (Boolean) pointer.getValue(); }
From source file:org.talend.mdm.webapp.browserecords.server.util.DynamicLabelUtil.java
private static String getFKInfo(String key, String foreignkey, List<String> fkInfos) { try {/*from w w w. j ava 2s. co m*/ if (key == null || key.trim().length() == 0) return null; List<String> ids = new ArrayList<String>(); if (!key.matches("^\\[(.*?)\\]$")) { //$NON-NLS-1$ ids.add(key); } else { Pattern p = Pattern.compile("\\[(.*?)\\]"); //$NON-NLS-1$ Matcher m = p.matcher(key); while (m.find()) { ids.add(m.group(1)); } } // Collections.reverse(ids); String concept = Util.getForeignPathFromPath(foreignkey); concept = concept.split("/")[0]; //$NON-NLS-1$ Configuration config = Configuration.getConfiguration(); String dataClusterPK = config.getCluster(); WSItemPK wsItem = new WSItemPK(new WSDataClusterPK(dataClusterPK), concept, (String[]) ids.toArray(new String[ids.size()])); WSItem item = Util.getPort().getItem(new WSGetItem(wsItem)); if (item != null) { String content = item.getContent(); Node node = Util.parse(content).getDocumentElement(); if (fkInfos.size() > 0) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < fkInfos.size(); i++) { String info = fkInfos.get(i); JXPathContext jxpContext = JXPathContext.newContext(node); jxpContext.setLenient(true); info = info.replaceFirst(concept + "/", ""); //$NON-NLS-1$ //$NON-NLS-2$ String fkinfo = (String) jxpContext.getValue(info, String.class); if (fkinfo != null && fkinfo.length() != 0) { sb.append(fkinfo); } if (i < fkInfos.size() - 1 && fkInfos.size() > 1) { sb.append("-"); //$NON-NLS-1$ } } return sb.toString(); } else { return key; } } } catch (Exception e) { LOG.error(e.getMessage(), e); return key; } return null; }
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]; }/* w w w . ja 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 www . ja 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;// www .ja v a 2 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; }//from w w w .ja va2 s. com 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; } } } }