List of usage examples for org.apache.commons.jxpath JXPathContext getValue
public abstract Object getValue(String xpath, Class requiredType);
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 ww w . j a va 2 s . c om*/ 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.xchain.framework.jxpath.JXPathContextTest.java
@Test public void testBindMethodIgnore() throws Exception { JXPathContext context = JXPathContext.newContext(new Object()); context.getVariables().declareVariable("sub-foo", new SubFoo()); context.getVariables().declareVariable("bar", new SubBar()); // This will throw a JXPathException, due to an ambiguous method lookup, unless // GenericsWisePackageFunctions is used. String result = (String) context.getValue("take($sub-foo, $bar)", String.class); assertEquals("SubFoo", result); }
From source file:org.xchain.framework.util.AttributesUtil.java
/** * Evaluates an attribute value template using the provided JXPathContext object. *//*from ww w. ja va 2s. com*/ public static String evaluateAttributeValueTemplate(JXPathContext context, String attributeValueTemplate) throws SAXException, Exception { List<String> parsedAvt = parseAttributeValueTemplate(attributeValueTemplate); // if there is just one part, then return the string. if (parsedAvt.size() == 1) { return parsedAvt.get(0); } // build a string buffer and start building the result in it. StringBuilder sb = new StringBuilder(); for (int i = 0; i < parsedAvt.size(); i += 2) { sb.append(parsedAvt.get(i)); if ((i + 1) < parsedAvt.size()) { sb.append((String) context.getValue(parsedAvt.get(i + 1), String.class)); } } return sb.toString(); }
From source file:org.xchain.namespaces.jsl.AbstractTemplateCommand.java
/** * Uses the specified nameXPath and namespaceXPath to create a dynamic qName. *//* ww w . jav a 2 s. c o m*/ protected QName dynamicQName(JXPathContext context, String nameXPath, String namespaceXPath, boolean includeDefaultPrefix) throws SAXException { String name = (String) context.getValue(nameXPath, String.class); if (name == null) { throw new SAXException("QNames cannot have null names."); } String namespace = null; if (namespaceXPath != null) { namespace = (String) context.getValue(namespaceXPath, String.class); if (namespace == null) { throw new SAXException("Namespace uris cannot be null."); } } String[] parts = name.split(":", 2); String prefixPart = parts.length == 2 ? parts[0] : ""; String localPart = parts.length == 2 ? parts[1] : parts[0]; // if the prefix is not "", then it must be defined in the context. String prefixPartNamespace = (includeDefaultPrefix || !"".equals(prefixPart)) ? context.getNamespaceURI(prefixPart) : ""; if (!"".equals(prefixPart) && prefixPartNamespace == null) { throw new SAXException("The prefix '" + prefixPart + "' is not defined in the context."); } // ASSERT: if the prefix is not "", then it is defined in the context. // if the namespace is null, then the prefix defines the namespace. if (namespace == null) { return new QName(prefixPartNamespace != null ? prefixPartNamespace : "", localPart, prefixPart); } // ASSERT: the namespace was specified. // if the prefix is "", then we can lookup the proper namespace. if ("".equals(prefixPart)) { if (!namespace.equals(prefixPartNamespace)) { String namespacePrefix = ((ScopedJXPathContextImpl) context).getPrefix(namespace); if (namespacePrefix == null) { throw new SAXException("The namespace '" + namespace + "' is not bound to a prefix."); } if (!includeDefaultPrefix && "".equals(namespacePrefix)) { throw new SAXException("The namespace '" + namespace + "' cannot be used for the attribute '" + name + "', because it maps to the default namespace."); } return new QName(namespace, localPart, namespacePrefix); } else { return new QName(namespace, localPart, prefixPart); } } // ASSERT: the prefix is defined and the namespace is defined, if they do not match, we must fail. if (!namespace.equals(prefixPartNamespace)) { throw new SAXException("The prefix '" + prefixPart + "' is bound to '" + prefixPartNamespace + "', but the namespace '" + namespace + "' is required."); } // ASSERT: the namespace and prefix will work together. return new QName(namespace, localPart, prefixPart); }
From source file:pt.webdetails.cdf.dd.render.cda.CompoundComponent.java
public void renderInto(Element compound) { JXPathContext context = JXPathContext.newContext(definition); compound.setAttribute("id", (String) context.getValue("value", String.class)); }
From source file:pt.webdetails.cdf.dd.render.CdaRenderer.java
public String render() throws Exception { ByteArrayOutputStream result = new ByteArrayOutputStream(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document cdaFile = builder.newDocument(); Element root = cdaFile.createElement("CDADescriptor"); cdaFile.appendChild(root);/*from w w w . j a v a 2 s.c o m*/ Element connections = cdaFile.createElement("DataSources"); root.appendChild(connections); Iterator<Pointer> pointers = doc.iteratePointers(CDA_ELEMENTS_JXPATH); while (pointers.hasNext()) { Pointer pointer = pointers.next(); JXPathContext context = JXPathContext.newContext(pointer.getNode()); String connectionId = (String) context.getValue("properties/.[name='name']/value", String.class); Element conn; try { conn = exportConnection(cdaFile, context, connectionId); connections.appendChild(conn); } catch (Exception e) { // things without connections end up here. All is fine, // we just need to make sure exportDataAccess doesn't try to generate a connection link connectionId = null; } Element dataAccess = exportDataAccess(cdaFile, context, connectionId); if (dataAccess != null) { root.appendChild(dataAccess); } } TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(cdaFile); StreamResult res = new StreamResult(new OutputStreamWriter(result, CharsetHelper.getEncoding())); transformer.setOutputProperty(OutputKeys.ENCODING, CharsetHelper.getEncoding()); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "Query"); transformer.transform(source, res); return result.toString(); }
From source file:pt.webdetails.cdf.dd.render.CdaRenderer.java
private Element exportConnection(Document doc, JXPathContext context, String id) throws Exception { JXPathContext cda = JsonUtils.toJXPathContext(cdaDefinitions); String type = ((String) context.getValue("type", String.class)).replaceAll("Components(.*)", "$1"); String conntype = ((String) context.getValue("meta_conntype", String.class)); if (conntype.isEmpty() || conntype.equals("null")) { throw new Exception("No connection here!"); }/*from w w w. ja v a2 s. c om*/ JXPathContext conn = JXPathContext.newContext(cda.getValue(type + "/definition/connection")); Element connection = doc.createElement("Connection"); connection.setAttribute("id", id); connection.setAttribute("type", conntype); Iterator<Pointer> params = conn.iteratePointers("*"); while (params.hasNext()) { Pointer pointer = params.next(); //JSONObject param = (JSONObject) pointer.getNode(); String paramName = pointer.asPath().replaceAll(".*name='(.*?)'.*", "$1"); String placement = ((String) conn.getValue(pointer.asPath() + "/placement", String.class)) .toLowerCase(); if (placement.equals("attrib")) { if (paramName.equals("id")) { continue; } else { String value = (String) context.getValue("properties/.[name='" + paramName + "']/value", String.class); connection.setAttribute(paramName, value); } } else if (paramName.equals("variables")) { Variables vars = new Variables(); vars.setContext(context); renderProperty(vars, context, paramName, connection); } else if (paramName.matches("olap4j.*")) { renderProperty(new Olap4jProperties(paramName), context, paramName, connection); } else if (paramName.equals("dataFile")) { renderProperty(new DataFile(), context, paramName, connection); } else if (paramName.equals("property") && isValidJsonArray(context, paramName)) { Object array = context.getValue("properties/.[name='" + paramName + "']/value"); JSONArray jsonArray = array instanceof String ? new JSONArray(array.toString()) : new JSONArray(array); for (int i = 0; i < jsonArray.length(); i++) { JSONArray json = (JSONArray) jsonArray.get(i); String name = json.getString(0); // property name String value = json.getString(1); // property value Element child = doc.createElement(Utils.toFirstUpperCase(paramName)); child.setAttribute("name", name); child.appendChild(doc.createTextNode(value)); connection.appendChild(child); } } else { String value = (String) context.getValue("properties/.[name='" + paramName + "']/value", String.class); Element child = doc.createElement(Utils.toFirstUpperCase(paramName)); child.appendChild(doc.createTextNode(value)); connection.appendChild(child); } } return connection; }
From source file:pt.webdetails.cdf.dd.render.CdaRenderer.java
private Element exportDataAccess(Document doc, JXPathContext context, String connectionId) throws JSONException { String tagName = "DataAccess"; // Boolean compound = false; JXPathContext cda = JsonUtils.toJXPathContext(cdaDefinitions); String type = ((String) context.getValue("type", String.class)).replaceAll("Components(.*)", "$1"); String daType = ((String) context.getValue("meta_datype", String.class)); if (type.equals("join") || type.equals("union")) { tagName = "CompoundDataAccess"; // compound = true; }/*from w w w . j a v a2 s . c o m*/ String name = (String) context.getValue("properties/.[name='name']/value", String.class); JXPathContext conn = JXPathContext.newContext(cda.getValue(type + "/definition/dataaccess")); Element dataAccess = doc.createElement(tagName); dataAccess.setAttribute("id", name); dataAccess.setAttribute("type", daType); if (connectionId != null && !connectionId.equals("")) { dataAccess.setAttribute("connection", connectionId); } Element elName = doc.createElement("Name"); elName.appendChild(doc.createTextNode(name)); dataAccess.appendChild(elName); @SuppressWarnings("unchecked") Iterator<Pointer> params = conn.iteratePointers("*"); Pointer pointer; String paramName; while (params.hasNext()) { pointer = params.next(); paramName = pointer.asPath().replaceAll(".*name='(.*?)'.*", "$1"); if (((String) conn.getValue(pointer.asPath() + "/placement", String.class)).toLowerCase() .equals("attrib")) { if (paramName.equals("id") || paramName.equals("connection") || paramName.equals("cacheDuration")) { continue; } else { dataAccess.setAttribute(paramName, (String) context .getValue("properties/.[name='" + paramName + "']/value", String.class)); } } else if (paramName.equals("parameters")) { renderProperty(new Parameters(), context, paramName, dataAccess); } else if (paramName.equals("output")) { renderProperty(new Output(context), context, paramName, dataAccess); } else if (paramName.equals("variables")) { renderProperty(new Variables(context), context, paramName, dataAccess); } else if (paramName.equals("outputMode")) { // Skip over outputMode, it's handled by output. continue; } else if (paramName.equals("cacheKeys")) { // Skip over cacheKeys, it's handled by cache. continue; } else if (paramName.equals("cache")) { renderProperty(new Cache(context), context, paramName, dataAccess); } else if (paramName.equals("columns")) { Element cols = dataAccess.getOwnerDocument().createElement("Columns"); renderProperty(new Columns(), context, "cdacolumns", cols); renderProperty(new CalculatedColumns(), context, "cdacalculatedcolumns", cols); dataAccess.appendChild(cols); } else if (paramName.equals("top") || paramName.equals("bottom") || paramName.equals("left") || paramName.equals("right")) { Element compoundElem = dataAccess.getOwnerDocument() .createElement(Utils.toFirstUpperCase(paramName)); renderProperty(new CompoundComponent(), context, paramName, compoundElem); dataAccess.appendChild(compoundElem); if (paramName.equals("left")) { renderProperty(new Keys(), context, "leftkeys", compoundElem); } else if (paramName.equals("right")) { renderProperty(new Keys(), context, "rightkeys", compoundElem); } } else { String value = (String) context.getValue("properties/.[name='" + paramName + "']/value", String.class); if (paramName.equals("query")) { value = value.trim(); } Element child = doc.createElement(Utils.toFirstUpperCase(paramName)); child.appendChild(doc.createTextNode(value)); dataAccess.appendChild(child); } } return dataAccess; }