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

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

Introduction

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

Prototype

public abstract JXPathContext getRelativeContext(Pointer pointer);

Source Link

Document

Returns a JXPathContext that is relative to the current JXPathContext.

Usage

From source file:org.apache.cocoon.woody.binding.AggregateJXPathBinding.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.
 *//*w  ww  .  jav a  2s  .c  om*/
public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
    AggregateField aggregate = (AggregateField) frmModel.getWidget(this.widgetId);
    JXPathContext subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
    super.doSave(aggregate, subContext);
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("Done saving " + toString());
    }
}

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

/**
 * Actively performs the binding from the Woody-form to the ObjectModel
 * wrapped in a jxpath context./*www.  jav  a2  s .c o m*/
 */
public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Pointer ptr = jxpc.getPointer(this.xpath);
    if (ptr.getNode() == null) {
        jxpc.createPath(this.xpath);
        // Need to recreate the pointer after creating the path
        ptr = jxpc.getPointer(this.xpath);
    }
    JXPathContext subContext = jxpc.getRelativeContext(ptr);
    super.doSave(frmModel, subContext);
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("done saving " + toString());
    }
}

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

public void doLoad(Widget frmModel, JXPathContext jctx) {
    if (this.loadScript != null) {
        Widget widget = frmModel.getWidget(this.id);

        // Move to widget context
        Pointer pointer = jctx.getPointer(this.path);

        // FIXME: remove this ugly hack and get the request from the
        // Avalon context once binding builder are real components
        Map objectModel = CocoonComponentManager.getCurrentEnvironment().getObjectModel();

        try {// ww w.j a  va2  s . c o  m
            Map values = new HashMap(3);
            values.put("widget", widget);
            values.put("jxpathPointer", pointer);
            if (pointer.getNode() != null) {
                values.put("jxpathContext", jctx.getRelativeContext(pointer));
            }

            JavaScriptHelper.execScript(this.loadScript, values, objectModel);

        } catch (RuntimeException re) {
            // rethrow
            throw re;
        } catch (Exception e) {
            throw new CascadingRuntimeException("Error invoking JavaScript event handler", e);
        }
    } else {
        if (this.getLogger().isInfoEnabled()) {
            this.getLogger().info(
                    "[Javascript Binding] - loadForm: No javascript code avaliable. Widget id=" + this.getId());
        }
    }
}

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

public void doSave(Widget frmModel, JXPathContext jctx) throws BindingException {
    if (this.saveScript != null) {
        Widget widget = frmModel.getWidget(this.id);

        // Move to widget context and create the path if needed
        Pointer pointer = jctx.createPath(this.path);
        JXPathContext widgetCtx = jctx.getRelativeContext(pointer);
        try {//from  www .  j  a  v a  2s  .c  o m
            // FIXME: remove this ugly hack and get the request from the Avalon context once
            // binding builder are real components
            Map objectModel = CocoonComponentManager.getCurrentEnvironment().getObjectModel();

            Map values = new HashMap();
            values.put("widget", widget);
            values.put("jxpathContext", widgetCtx);
            values.put("jxpathPointer", pointer);

            JavaScriptHelper.execScript(this.saveScript, values, objectModel);

        } catch (RuntimeException re) {
            // rethrow
            throw re;
        } catch (Exception e) {
            throw new CascadingRuntimeException("Error invoking JavaScript event handler", e);
        }
    } else {
        if (this.getLogger().isInfoEnabled()) {
            this.getLogger()
                    .info("[Javascript Binding] - saveForm: No javascript code avaliable. <wb:javascript id="
                            + this.getId() + ">");
        }
    }
}

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

public void doLoad(Widget frmModel, JXPathContext jctx) throws BindingException {
    Widget widget = frmModel.getWidget(this.multiValueId);
    if (widget == null) {
        throw new BindingException("The widget with the ID [" + this.multiValueId
                + "] referenced in the binding does not exist in the form definition.");
    }//w w  w . j  a v a  2s.  c o  m

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

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

        LinkedList list = new LinkedList();

        while (rowPointers.hasNext()) {
            Object value = rowPointers.next();

            if (value != null && convertor != null) {
                if (value instanceof String) {
                    value = convertor.convertFromString((String) value, convertorLocale, null);
                } else {
                    getLogger().warn("Convertor ignored on backend-value which isn't of type String.");
                }
            }

            list.add(value);
        }

        widget.setValue(list.toArray());
    }

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

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

public void doSave(Widget frmModel, JXPathContext jctx) throws BindingException {
    Widget widget = frmModel.getWidget(this.multiValueId);
    Object[] values = (Object[]) widget.getValue();

    JXPathContext multiValueContext = jctx.getRelativeContext(jctx.createPath(this.multiValuePath));
    // Delete all that is already present
    multiValueContext.removeAll(this.rowPath);

    boolean update = false;

    if (values != null) {
        // first update the values
        for (int i = 0; i < values.length; i++) {
            String path = this.rowPath + '[' + (i + 1) + ']';
            Pointer rowPtr = multiValueContext.createPath(path);

            Object value = values[i];
            if (value != null && convertor != null) {
                value = convertor.convertToString(value, convertorLocale, null);
            }/*  ww w . ja  v a 2s .  c  o m*/

            rowPtr.setValue(value);
        }

        // now perform any other bindings that need to be performed when the value is updated
        this.updateBinding.saveFormToModel(frmModel, multiValueContext);

        update = true;
    }

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

}

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

/**
 * Binds the unique-id of the repeated rows, and narrows the context on
 * objectModelContext and Repeater to the repeated rows before handing
 * over to the actual binding-children.//from  w w  w  .  j a va  2s.  c om
 */
public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
    // Find the repeater
    Repeater repeater = (Repeater) frmModel.getWidget(this.repeaterId);
    repeater.removeRows();
    int initialSize = repeater.getSize();

    // build a jxpath iterator for pointers
    JXPathContext repeaterContext = jxpc.getRelativeContext(jxpc.getPointer(this.repeaterPath));
    Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath);
    //iterate through it
    while (rowPointers.hasNext()) {
        // create a new row, take that as the frmModelSubContext
        Repeater.RepeaterRow thisRow;
        if (initialSize > 0) {
            thisRow = repeater.getRow(--initialSize);
        } else {
            thisRow = repeater.addRow();
        }
        // make a jxpath ObjectModelSubcontext on the iterated element
        Pointer jxp = (Pointer) rowPointers.next();
        JXPathContext rowContext = repeaterContext.getRelativeContext(jxp);
        // hand it over to children
        Iterator iter = this.uniqueRowBinding.iterator();
        while (iter.hasNext()) {
            ((UniqueFieldJXPathBinding) iter.next()).loadFormFromModel(thisRow, rowContext);
        }
        this.rowBinding.loadFormFromModel(thisRow, rowContext);
    }
    if (getLogger().isDebugEnabled())
        getLogger().debug("done loading rows " + toString());
}

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.
 *//*w  w  w  .  j  a  v  a 2  s. c  om*/
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();/*ww w .  j a  v  a  2 s . c om*/
    }

    // 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.SimpleRepeaterJXPathBinding.java

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

    if (repeater.getSize() == 0 && this.deleteIfEmpty) {
        // Repeater is empty : erase all
        jctx.removeAll(this.repeaterPath);
    } else {//ww  w.  ja  v  a 2 s .c  o m
        // Repeater is not empty
        // Move to repeater context and create the path if needed
        JXPathContext repeaterContext = jctx.getRelativeContext(jctx.createPath(this.repeaterPath));

        // Delete all that is already present
        repeaterContext.removeAll(this.rowPath);

        for (int i = 0; i < repeater.getSize(); i++) {
            Pointer rowPtr = repeaterContext.createPath(this.rowPath + '[' + (i + 1) + ']');
            JXPathContext rowContext = repeaterContext.getRelativeContext(rowPtr);
            this.rowBinding.saveFormToModel(repeater.getRow(i), rowContext);
        }
    }
}