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

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

Introduction

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

Prototype

public abstract Pointer getPointer(String xpath);

Source Link

Document

Traverses the xpath and returns a Pointer.

Usage

From source file:org.apache.cocoon.woody.binding.RepeaterJXPathBinding.java

/**
 * Uses the mapped unique-id of each row to detect if rows have been
 * updated, inserted or removed.  Depending on what happened the appropriate
 * child-bindings are alowed to visit the narrowed contexts.
 *//* ww w .j av  a2 s  . c o  m*/
public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
    // Find the repeater
    Repeater repeater = (Repeater) frmModel.getWidget(this.repeaterId);
    // and his context
    JXPathContext repeaterContext = jxpc.getRelativeContext(jxpc.getPointer(this.repeaterPath));

    // create set of updatedRowIds
    Set updatedRowIds = new HashSet();
    //create list of rows to insert at end
    List rowsToInsert = new ArrayList();

    // iterate rows in the form model...
    int formRowCount = repeater.getSize();
    for (int i = 0; i < formRowCount; i++) {
        Repeater.RepeaterRow thisRow = repeater.getRow(i);

        // Get the key values
        List rowIdValues = getUniqueRowValues(thisRow);

        if (isAnyListElementNotNull(rowIdValues)) {
            // iterate nodes to find match
            Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath);
            boolean found = false;
            while (rowPointers.hasNext()) {
                Pointer jxp = (Pointer) rowPointers.next();
                JXPathContext rowContext = repeaterContext.getRelativeContext(jxp);
                List matchIds = getMatchIds(rowContext);
                if (ListUtils.isEqualList(rowIdValues, matchIds)) {
                    // match! --> bind to children
                    this.rowBinding.saveFormToModel(thisRow, rowContext);
                    //        --> store rowIdValue in list of updatedRowIds
                    updatedRowIds.add(rowIdValues);
                    found = true;
                    break;
                }
            }
            if (!found) {
                // this is a new row
                rowsToInsert.add(thisRow);
                // also add it to the updated row id's so that this row doesn't get deleted
                updatedRowIds.add(rowIdValues);
            }
        } else {
            // if all rowIdValues == null --> this is a new row
            rowsToInsert.add(thisRow);
        }
    }
    // Iterate again nodes for deletion
    Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath);
    List rowsToDelete = new ArrayList();
    while (rowPointers.hasNext()) {
        Pointer jxp = (Pointer) rowPointers.next();
        JXPathContext rowContext = repeaterContext.getRelativeContext((Pointer) jxp.clone());
        List matchIds = getMatchIds(rowContext);
        // check if matchPath was in list of updates, if not --> bind for delete
        if (!isListInSet(updatedRowIds, matchIds)) {
            rowsToDelete.add(rowContext);
        }
    }
    if (rowsToDelete.size() > 0) {
        if (this.deleteRowBinding != null) {
            // run backwards through the list, so that we don't get into
            // trouble by shifting indexes
            for (int i = rowsToDelete.size() - 1; i >= 0; i--) {
                this.deleteRowBinding.saveFormToModel(frmModel, rowsToDelete.get(i));
            }
        } else {
            if (getLogger().isWarnEnabled()) {
                getLogger().warn("RepeaterBinding has detected rows to delete, "
                        + "but misses the <on-delete-row> binding to do it.");
            }
        }
    }
    // count how many we have now
    int indexCount = 1;
    rowPointers = repeaterContext.iteratePointers(this.rowPathForInsert);
    while (rowPointers.hasNext()) {
        rowPointers.next();
        indexCount++;
    }
    // end with rows to insert (to make sure they don't get deleted!)
    if (rowsToInsert.size() > 0) {
        if (this.insertRowBinding != null) {
            Iterator rowIterator = rowsToInsert.iterator();
            //register the factory!
            while (rowIterator.hasNext()) {
                Repeater.RepeaterRow thisRow = (Repeater.RepeaterRow) rowIterator.next();
                // Perform the insert row binding.
                this.insertRowBinding.saveFormToModel(repeater, repeaterContext);
                // -->  create the path to let the context be created
                Pointer newRowContextPointer = repeaterContext
                        .createPath(this.rowPathForInsert + "[" + indexCount + "]");
                JXPathContext newRowContext = repeaterContext.getRelativeContext(newRowContextPointer);
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("inserted row at " + newRowContextPointer.asPath());
                }
                //    + rebind to children for update
                this.rowBinding.saveFormToModel(thisRow, newRowContext);
                getLogger().debug("bound new row");
                indexCount++;
            }
        } else {
            if (getLogger().isWarnEnabled()) {
                getLogger().warn("RepeaterBinding has detected rows to insert, but misses "
                        + "the <on-insert-row> binding to do it.");
            }
        }
    }
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("done saving rows " + toString());
    }
}

From source file:org.apache.cocoon.woody.binding.SimpleRepeaterJXPathBinding.java

public void doLoad(Widget frmModel, JXPathContext jctx) throws BindingException {
    // Find the repeater and clear it
    Repeater repeater = (Repeater) frmModel.getWidget(this.repeaterId);

    if (this.clearOnLoad) {
        repeater.removeRows();/* w  w w  .  jav  a2 s  .c  o  m*/
    }

    // Move to repeater context
    Pointer ptr = jctx.getPointer(this.repeaterPath);
    if (ptr.getNode() != null) {
        // There are some nodes to load from

        JXPathContext repeaterContext = jctx.getRelativeContext(ptr);
        // build a jxpath iterator for pointers
        Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath);

        //iterate through it
        int rowNum = 0;
        while (rowPointers.hasNext()) {
            // Get a row. It is created if needed (depends on clearOnLoad)
            Repeater.RepeaterRow thisRow;
            if (repeater.getSize() > rowNum) {
                thisRow = repeater.getRow(rowNum);
            } else {
                thisRow = repeater.addRow();
            }
            rowNum++;

            // make a jxpath sub context on the iterated element
            Pointer jxp = (Pointer) rowPointers.next();
            JXPathContext rowContext = repeaterContext.getRelativeContext(jxp);

            this.rowBinding.loadFormFromModel(thisRow, rowContext);
        }
    }
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("done loading rows " + toString());
    }
}

From source file:org.apache.cocoon.woody.binding.StructJXPathBinding.java

/**
 * Narrows the scope on the form-model to the member widget-field, and
 * narrows the scope on the object-model to the member xpath-context
 * before continuing the binding over the child-bindings.
 */// ww w.j  a  v  a  2 s .c  om
public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Struct structWidget = (Struct) getWidget(frmModel, this.widgetId);
    JXPathContext subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
    super.doLoad(structWidget, subContext);
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("done loading " + toString());
    }
}

From source file:org.apache.cocoon.woody.binding.StructJXPathBinding.java

/**
 * Narrows the scope on the form-model to the member widget-field, and
 * narrows the scope on the object-model to the member xpath-context
 * before continuing the binding over the child-bindings.
 *///  ww w  .j  av  a2s.com
public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Struct structWidget = (Struct) frmModel.getWidget(this.widgetId);
    JXPathContext subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
    super.doSave(structWidget, subContext);
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("done saving " + toString());
    }
}

From source file:org.apache.cocoon.woody.binding.TempRepeaterJXPathBinding.java

public void doLoad(Widget frmModel, JXPathContext jctx) throws BindingException {
    // (There should be a general widget type checker for all the bindings to use,
    // coupled with a general informative exception class to throw if the widget is
    // of the wrong type or null.)
    Repeater repeater = (Repeater) frmModel.getWidget(this.repeaterId);
    if (repeater == null) {
        String fullId = frmModel.getFullyQualifiedId();
        if (fullId == null || fullId.length() == 0) {
            fullId = "";
        } else {//from w w w  .j ava 2 s  .  co  m
            fullId = fullId + ".";
        }
        throw new RuntimeException("TempRepeaterJXPathBinding: Repeater \"" + fullId + this.repeaterId
                + "\" does not exist (" + frmModel.getLocation() + ")");
    }

    // Start by clearing the repeater, if necessary.
    if (this.clearOnLoad) {
        repeater.removeRows();
    }

    // Find the location of the repeater data.
    Pointer repeaterPointer = jctx.getPointer(this.repeaterPath);

    // Check if there is data present.
    //
    // (Otherwise, should we check the leniency config option
    // to decide whether to be silent or throw an exception?) 
    if (repeaterPointer != null) {

        // Narrow to repeater context.
        JXPathContext repeaterContext = jctx.getRelativeContext(repeaterPointer);

        // Build a jxpath iterator for the repeater row pointers.
        Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath);

        // Iterate through the rows of data.
        int rowNum = 0;
        while (rowPointers.hasNext()) {

            // Get or create a row widget.
            Repeater.RepeaterRow thisRow;
            if (repeater.getSize() > rowNum) {
                thisRow = repeater.getRow(rowNum);
            } else {
                thisRow = repeater.addRow();
            }
            rowNum++;

            // Narrow to the row context.
            Pointer rowPointer = (Pointer) rowPointers.next();
            JXPathContext rowContext = repeaterContext.getRelativeContext(rowPointer);

            // If virtual rows are requested, place a deep clone of the row data
            // into a temporary node, and narrow the context to this virtual row.
            //
            // (A clone of the data is used to prevent modifying the source document.
            // Otherwise, the appendChild method would remove the data from the source
            // document.  Is this protection worth the penalty of a deep clone?)
            //
            // (This implementation of virtual rows currently only supports DOM
            // bindings, but could easily be extended to support other bindings.)

            if (virtualRows == true) {
                Node repeaterNode = (Node) repeaterPointer.getNode();
                Node virtualNode = repeaterNode.getOwnerDocument().createElementNS(null, "virtual");
                Node clone = ((Node) rowPointer.getNode()).cloneNode(true);
                virtualNode.appendChild(clone);
                rowContext = JXPathContext.newContext(repeaterContext, virtualNode);
            }

            // Finally, perform the load row binding.
            this.rowBinding.loadFormFromModel(thisRow, rowContext);
        }
    }

    if (getLogger().isDebugEnabled())
        getLogger().debug("done loading rows " + toString());
}

From source file:org.apache.cocoon.woody.binding.TempRepeaterJXPathBinding.java

public void doSave(Widget frmModel, JXPathContext jctx) throws BindingException {
    // (See comment in doLoad about type checking and throwing a meaningful exception.)
    Repeater repeater = (Repeater) frmModel.getWidget(this.repeaterId);

    // Perform shortcut binding if the repeater is empty
    // and the deleteIfEmpty config option is selected.
    if (repeater.getSize() == 0 && this.deleteIfEmpty) {
        // Delete all of the old data for this repeater.
        jctx.removeAll(this.repeaterPath);

        // Otherwise perform the normal save binding.
    } else {/*from ww w  .  ja v  a2 s .com*/

        // Narrow to the repeater context, creating the path if it did not exist.
        JXPathContext repeaterContext = jctx.getRelativeContext(jctx.createPath(this.repeaterPath));

        // Start by deleting all of the old row data.
        repeaterContext.removeAll(this.rowPath);

        // Verify that repeater is not empty and has an insert row binding.
        if (repeater.getSize() > 0) {
            if (this.insertRowBinding != null) {

                //register the factory!
                //this.insertRowBinding.saveFormToModel(repeater, repeaterContext);

                // Iterate through the repeater rows.
                for (int i = 0; i < repeater.getSize(); i++) {

                    // Narrow to the repeater row context.
                    Pointer rowPointer = repeaterContext.getPointer(this.rowPathInsert);
                    JXPathContext rowContext = repeaterContext.getRelativeContext(rowPointer);

                    // Variables used for virtual rows.
                    // They are initialized here just to keep the compiler happy. 
                    Node rowNode = null;
                    Node virtualNode = null;

                    // If virtual rows are requested, create a temporary node and
                    // narrow the context to this initially empty new virtual row.
                    if (virtualRows == true) {
                        rowNode = (Node) rowContext.getContextBean();
                        virtualNode = rowNode.getOwnerDocument().createElementNS(null, "virtual");
                        rowContext = JXPathContext.newContext(repeaterContext, virtualNode);
                    }

                    // Perform the insert row binding
                    this.insertRowBinding.saveFormToModel(repeater, rowContext);

                    // Perform the save row binding.
                    this.rowBinding.saveFormToModel(repeater.getRow(i), rowContext);

                    // If virtual rows are requested, finish by appending the
                    // children of the virtual row to the real context node.
                    if (virtualRows == true) {
                        NodeList list = virtualNode.getChildNodes();
                        int count = list.getLength();
                        for (int j = 0; j < count; j++) {
                            // The list shrinks when a child is appended to the context
                            // node, so we always reference the first child in the list.
                            rowNode.appendChild(list.item(0));
                        }
                    }
                    getLogger().debug("bound new row");
                }
            } else {
                getLogger().warn("TempRepeaterBinding has detected rows to insert, "
                        + "but misses the <on-insert-row> binding to do it.");
            }
        }
    }
}

From source file:org.apache.cocoon.woody.binding.UnionJXPathBinding.java

/**
 * Narrows the scope on the form-model to the member widget-field, and
 * narrows the scope on the object-model to the member xpath-context
 * before continuing the binding over the child-bindings.
 *//*from w  w  w  . j  a v  a  2 s  .c  o  m*/
public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Widget widget = frmModel.getWidget(this.widgetId);
    JXPathContext subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
    if (!(widget instanceof Union))
        throw new RuntimeException(
                "Binding: Expected Union widget, but received class: \"" + widget.getClass().getName() + "\".");
    Union unionWidget = (Union) widget;
    Binding[] subBindings = getChildBindings();
    if (subBindings != null) {
        int size = subBindings.length;
        for (int i = 0; i < size; i++) {
            subBindings[i].loadFormFromModel(unionWidget, subContext);
        }
    }
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("done loading " + toString());
    }
}

From source file:org.apache.cocoon.woody.binding.UnionJXPathBinding.java

/**
 * Narrows the scope on the form-model to the member widget-field, and
 * narrows the scope on the object-model to the member xpath-context
 * before continuing the binding over the child-bindings.
 *//*from   ww w .ja va  2 s.  c  o m*/
public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Union unionWidget = (Union) frmModel.getWidget(this.widgetId);
    JXPathContext subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
    Binding[] subBindings = getChildBindings();
    if (subBindings != null) {
        int size = subBindings.length;
        for (int i = 0; i < size; i++) {
            subBindings[i].saveFormToModel(unionWidget, subContext);
        }
    }
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("done saving " + toString());
    }
}

From source file:org.apache.cocoon.woody.binding.ValueJXPathBinding.java

/**
 * Actively performs the binding from the Woody-form to the ObjectModel
 * wrapped in a jxpath context/* www. ja v a  2s . c o m*/
 */
public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Widget widget = frmModel.getWidget(this.fieldId);
    Object value = widget.getValue();
    if (value != null && convertor != null) {
        value = convertor.convertToString(value, convertorLocale, null);
    }

    Object oldValue = jxpc.getValue(this.xpath);
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("value= " + value + "-- oldvalue=" + oldValue);
    }

    boolean update = false;

    if ((value == null && oldValue != null) || value != null && !value.equals(oldValue)) {
        // first update the value itself
        jxpc.createPathAndSetValue(this.xpath, value);

        // now perform any other bindings that need to be performed when the value is updated
        JXPathContext subContext = null;
        try {
            subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
        } catch (JXPathException e) {
            // if the value has been set to null and the underlying model is a bean, then
            // JXPath will not be able to create a relative context
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("(Ignorable) problem binding field " + widget.getFullyQualifiedId(), e);
            }
        }
        if (subContext != null) {
            this.updateBinding.saveFormToModel(frmModel, subContext);
        }

        update = true;
    }

    if (getLogger().isDebugEnabled()) {
        getLogger().debug("done saving " + toString() + " -- value= " + value + " -- on-update == " + update);
    }
}

From source file:org.apache.ctakes.jdl.data.loader.XmlLoader.java

/**
 * @param jdlConnection//from www  .  j  a v a  2 s  .com
 *            the jdlConnection to manage
 */
@Override
public final void dataInsert(final JdlConnection jdlConnection) {
    String sql = getSqlInsert(loader);
    Number ncommit = loader.getCommit();
    int r = 0;
    try {
        Iterator<?> iterator = context.iteratePointers(loader.getXroot());
        while (iterator.hasNext()) {
            r++;
            NodePointer pointer = (NodePointer) iterator.next();
            Node node = (Node) pointer.getNode();
            JXPathContext context = JXPathContext.newContext(DomUtil.nodeToDocument(node));
            try {
                int c = 0;
                PreparedStatement preparedStatement = jdlConnection.getOpenConnection().prepareStatement(sql);
                if (ncommit == null) {
                    jdlConnection.setAutoCommit(true);
                } else {
                    jdlConnection.setAutoCommit(false);
                }
                for (Column column : loader.getColumn()) {
                    c++;
                    Object value = column.getConstant();
                    if (value == null) {
                        if (column.getSeq() != null) {
                            value = r + column.getSeq().intValue();
                        } else if (column.getXpath() != null) {
                            value = this.context.getValue(column.getXpath());
                        } else {
                            value = context.getPointer(column.getXleaf()).getValue();
                        }
                    }
                    preparedStatement.setObject(c, value);
                }
                executeBatch(preparedStatement);
                if (!jdlConnection.isAutoCommit() && (r % ncommit.intValue() == 0)) {
                    jdlConnection.commitConnection();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        if (!jdlConnection.isAutoCommit()) {
            jdlConnection.commitConnection();
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}