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.forms.binding.ContextJXPathBinding.java

/**
 * Actively performs the binding from the CocoonForm to the ObjectModel
 * wrapped in a jxpath context.//from w  w w  . j  a va  2 s.c o  m
 */
public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
    if (this.factory != null) {
        jxpc.setFactory(this.factory);
    }
    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.forms.binding.EnhancedRepeaterJXPathBinding.java

public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Repeater repeater = (Repeater) selectWidget(frmModel, super.getId());
    if (!(repeater instanceof EnhancedRepeater)) {
        super.doSave(frmModel, jxpc);
        return;//from   ww  w. jav  a  2  s  . co m
    }

    EnhancedRepeater rep = (EnhancedRepeater) repeater;
    rep.doPageSave();
    Pointer ptr = jxpc.getPointer(super.getRepeaterPath());
    JXPathContext repeaterContext = jxpc.getRelativeContext(ptr);
    RepeaterJXPathCollection collection = rep.getCollection();
    // iterate updated rows. note: we don't iterate over the whole context
    for (Iterator iter = collection.getUpdatedRows().iterator(); iter.hasNext();) {
        RepeaterItem item = (RepeaterItem) iter.next();
        Repeater.RepeaterRow thisRow = item.getRow();
        // Get the identity
        List identity = getIdentity(thisRow);
        if (hasNonNullElements(identity)) {
            // iterate nodes to find match
            Iterator rowPointers = repeaterContext.iteratePointers(getRowPath());
            while (rowPointers.hasNext()) {
                Pointer jxp = (Pointer) rowPointers.next();
                JXPathContext rowContext = repeaterContext.getRelativeContext(jxp);
                List contextIdentity = getIdentity(rowContext);
                if (ListUtils.isEqualList(identity, contextIdentity)) {
                    getRowBinding().saveFormToModel(thisRow, rowContext);
                    break;
                }
            }
        } else {
            getRowBinding().saveFormToModel(thisRow, item.getContext().getContextPointer());
        }
    }

    for (Iterator iter = collection.getDeletedRows().iterator(); iter.hasNext();) {
        RepeaterItem item = (RepeaterItem) iter.next();
        jxpc.removePath(item.getContext().createPath(".").asPath());
    }

    // insert rows
    int indexCount = collection.getOriginalCollectionSize() - collection.getDeletedRows().size();
    for (Iterator iter = collection.getInsertedRows().iterator(); iter.hasNext();) {
        indexCount++;
        RepeaterItem item = (RepeaterItem) iter.next();

        // Perform the insert row binding.
        if (getInsertRowBinding() != null) {
            getInsertRowBinding().saveFormToModel(item.getRow(), repeaterContext);
        }
        // -->  create the path to let the context be created
        Pointer newRowContextPointer = repeaterContext
                .createPath(super.getInsertRowPath() + "[" + indexCount + "]");
        JXPathContext newRowContext = repeaterContext.getRelativeContext(newRowContextPointer);
        //    + rebind to children for update
        super.getRowBinding().saveFormToModel(item.getRow(), newRowContext);
    }
}

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

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

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

        Map objectModel = ContextHelper.getObjectModel(this.avalonContext);

        try {/*  w  ww. j a v a 2s  .c o m*/
            JXPathContext newCtx = pointer.getNode() == null ? null : jctx.getRelativeContext(pointer);

            JavaScriptHelper.callFunction(this.loadScript, widget,
                    new Object[] { widget, pointer, newCtx, this.childBindings }, 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.forms.binding.MultiValueJXPathBinding.java

public void doLoad(Widget frmModel, JXPathContext jctx) throws BindingException {
    Widget widget = selectWidget(frmModel, 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.");
    }//  www  .j  ava 2 s.  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) {
                    ConversionResult conversionResult = convertor.convertFromString((String) value,
                            convertorLocale, null);
                    if (conversionResult.isSuccessful())
                        value = conversionResult.getResult();
                    else
                        value = 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.forms.binding.RepeaterJXPathAdapter.java

public RepeaterItem getItem(int i) {
    if (i < 0)
        return null;
    if (i >= jxCollection.getOriginalCollectionSize())
        return null;
    if (this.sortedItems == null) {
        JXPathContext storageContext = this.jxCollection.getStorageContext();
        Pointer pointer = storageContext.getPointer(binding.getRowPath() + "[" + (i + 1) + "]");
        JXPathContext rowContext = storageContext.getRelativeContext(pointer);
        RepeaterItem item = new RepeaterItem(new Integer(i + 1));
        item.setContext(rowContext);//from ww  w. j  a  va2  s.  c  o  m
        return item;
    } else {
        return (RepeaterItem) sortedItems.get(i);
    }
}

From source file:org.apache.cocoon.forms.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.// w  ww  .  jav  a  2s  . c o m
 */
public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
    // Find the repeater
    Repeater repeater = (Repeater) selectWidget(frmModel, this.repeaterId);
    if (repeater == null) {
        throw new BindingException("The repeater with the ID [" + this.repeaterId
                + "] referenced in the binding does not exist in the form definition.");
    }
    repeater.clear();

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

        int initialSize = repeater.getSize();

        // build a jxpath iterator for pointers
        JXPathContext repeaterContext = jxpc.getRelativeContext(ptr);
        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
            if (this.identityBinding != null) {
                this.identityBinding.loadFormFromModel(thisRow, rowContext);
            }
            this.rowBinding.loadFormFromModel(thisRow, rowContext);
        }
    }
    if (getLogger().isDebugEnabled())
        getLogger().debug("done loading rows " + toString());
}

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

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

    if (this.clearOnLoad) {
        repeater.clear();//ww  w. ja 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.forms.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) selectWidget(frmModel, this.repeaterId);
    if (repeater == null) {
        String fullId = frmModel.getRequestParameterName();
        if (fullId == null || fullId.length() == 0) {
            fullId = "";
        } else {// w  ww . j a  v a 2s.c o 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.clear();
    }

    // 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 node = (Node) rowPointer.getNode();
                Node clone = node.cloneNode(true);
                Node fakeDocElement = node.getOwnerDocument().getDocumentElement().cloneNode(false);
                virtualNode.appendChild(clone);
                fakeDocElement.appendChild(virtualNode);
                rowContext = JXPathContext.newContext(repeaterContext, fakeDocElement);
                rowContext = rowContext.getRelativeContext(rowContext.getPointer("virtual"));
            }

            // 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.forms.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) selectWidget(frmModel, 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 {/*  w  ww. j a v  a2  s  . c o m*/

        // 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();
                        Document document = rowNode.getOwnerDocument();
                        virtualNode = document.createElementNS(null, "virtual");
                        Node fakeDocElement = document.getDocumentElement().cloneNode(false);
                        fakeDocElement.appendChild(virtualNode);
                        rowContext = JXPathContext.newContext(repeaterContext, fakeDocElement);
                        rowContext = rowContext.getRelativeContext(rowContext.getPointer("virtual"));
                    }

                    // 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.forms.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 ww . jav a  2  s  . co  m
public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Widget widget = selectWidget(frmModel, 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());
    }
}