Example usage for org.apache.commons.jxpath JXPathContext newContext

List of usage examples for org.apache.commons.jxpath JXPathContext newContext

Introduction

In this page you can find the example usage for org.apache.commons.jxpath JXPathContext newContext.

Prototype

public static JXPathContext newContext(Object contextBean) 

Source Link

Document

Creates a new JXPathContext with the specified object as the root node.

Usage

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  va 2s  . co  m
    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;
    }//w  w  w .ja va  2 s .  c om
    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;
}

From source file:pt.webdetails.cdf.dd.render.components.ComponentManager.java

public void parseCdaDefinitions(JSON json) throws Exception {
    cdaSettings = json;//from w  w  w.  j a  va  2s  . co  m
    final JXPathContext doc = JXPathContext.newContext(json);
    Iterator<Pointer> pointers = doc.iteratePointers("*");
    while (pointers.hasNext()) {
        Pointer pointer = pointers.next();
        CdaDatasource ds = new CdaDatasource(pointer);
        componentPool.put(ds.getName(), ds);
    }
}

From source file:pt.webdetails.cdf.dd.util.JsonUtils.java

public static JXPathContext toJXPathContext(JSONObject json) throws JSONException {
    Map<String, Object> hashMap = new HashMap<String, Object>();
    Iterator<String> it = json.keys();
    while (it.hasNext()) {
        String key = it.next();/*from ww w  .  j a v a 2s. com*/
        hashMap.put(key, processValue(json.get(key)));
    }
    return JXPathContext.newContext(hashMap);
}

From source file:pt.webdetails.cdf.dd.ws.ChartList.java

@SuppressWarnings("rawtypes")
public String toJSON() {

    StringBuilder builder = new StringBuilder("");
    builder.append("[");

    for (String fileName : this.fileNames) {

        if (!WebServiceCommons.isUnitTest())
            fileName = Utils.joinPath(PentahoSystem.getApplicationContext().getSolutionPath(""), fileName);

        JSON json = JsonUtils.getFileAsJson(fileName);
        if (json == null)
            return WebServiceCommons.JSON_FILE_DOES_NOT_EXIST;

        String dashboardName = getDashboardName(fileName);
        Iterator it = JXPathContext.newContext(json)
                .iterate("components/rows[parent='CHARTS' or type='Componentsxaction']");

        while (it.hasNext()) {
            buildOutputJSON(builder, getIdAndTitleFromJSON((JSONObject) it.next()), dashboardName);
        }/*w  w  w  .  ja  va  2  s. com*/

    }

    builder.append("]");
    return builder.toString();
}

From source file:pt.webdetails.cdf.dd.ws.ChartScript.java

private String getComponentScript(String componentId, String newHtmlObject, String fileName) {

    if (!WebServiceCommons.isUnitTest())
        fileName = Utils.joinPath(PentahoSystem.getApplicationContext().getSolutionPath(""), fileName);

    JSON json = JsonUtils.getFileAsJson(fileName);
    if (json == null) {
        return WebServiceCommons.JSON_FILE_DOES_NOT_EXIST;
    }/* w w  w  . jav  a2  s  .co  m*/

    try {
        return prepareScript(newHtmlObject, new RenderComponents().renderComponent(
                JXPathContext.newContext(json), "mydashboard", "render_mydashboard_" + componentId));

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:se.cambio.cds.gdl.editor.view.panels.DescriptionPanel.java

public DescriptionPanel(GDLEditor gdlEditor) {
    _controller = gdlEditor;/*from   w  w w.  j  a va2 s . c  o m*/
    _descriptionContext = JXPathContext.newContext(_controller.getResourceDescription());
    _conceptContext = JXPathContext.newContext(_controller.getConceptTerm());
    init();
}

From source file:se.cambio.cds.gdl.editor.view.panels.TerminologyPanel.java

public void refresh() {
    getMainPanel().removeAll();//from   w ww  .j  a  v a2s  . c o  m
    terminologyScrollPanel = null;
    terminologyTable = null;
    _context = JXPathContext.newContext(_controller.getCurrentTermsMap());
    getMainPanel().add(getTerminologyScrollPanel(), BorderLayout.CENTER);
    getMainPanel().add(getAddDeleteButtonPanel(), BorderLayout.WEST);
    TerminologyTableModel ttm = getTerminologyTable().getTerminologyTableModel();
    Map<String, Term> termMap = _controller.getCurrentTermsMap();
    List<String> gtCodes = new ArrayList<String>(termMap.keySet());
    Collections.sort(gtCodes);
    for (String gtCode : gtCodes) {
        Term term = termMap.get(gtCode);
        Vector<String> v = new Vector<String>();
        v.add(term.getId());
        v.add(term.getText());
        v.add(term.getDescription());
        ttm.addRow(v);
    }
    getMainPanel().revalidate();
    getMainPanel().repaint();
}

From source file:se.cambio.cds.gdl.parser.CreateModelClassTest.java

private void check(Object expected, String path) throws Exception {
    JXPathContext context = JXPathContext.newContext(ret);
    assertEquals(expected, context.getValue(path));
}

From source file:se.cambio.cds.gdl.parser.ExpressionTestBase.java

void check(Object expected, String path) throws Exception {
    JXPathContext context = JXPathContext.newContext(item);
    Object obj = context.getValue(path);
    assertEquals(expected, obj);/*from   w w  w .java2  s.c  o m*/
}