Example usage for org.apache.commons.jxpath Pointer getNode

List of usage examples for org.apache.commons.jxpath Pointer getNode

Introduction

In this page you can find the example usage for org.apache.commons.jxpath Pointer getNode.

Prototype

Object getNode();

Source Link

Document

Returns the raw value of the object, property or collection element this pointer represents.

Usage

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

/**
 * Actively performs the binding from the ObjectModel wrapped in a jxpath
 * context to the CocoonForm.//from   w w  w. j a  v a2 s.  c  o m
 */
public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Pointer ptr = jxpc.getPointer(this.xpath);
    if (ptr.getNode() != null) {
        JXPathContext subContext = jxpc.getRelativeContext(ptr);
        super.doLoad(frmModel, subContext);
        if (getLogger().isDebugEnabled())
            getLogger().debug("done loading " + toString());
    } else {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("non-existent path: skipping " + toString());
        }
    }
}

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  www .j ava2  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.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 {/*from   w  ww .j a  v a  2s. c  om*/
            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.");
    }//from   www  .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) {
                    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.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 ww.  jav  a  2s.co 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();//from www .  ja  v  a  2s.  co  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.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 {/*from w ww.ja v  a2s . 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.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.formmodel.WidgetTestHelper.java

public static void assertXPathExists(String message, String xpath, Document doc) {
    JXPathContext ctx = JXPathContext.newContext(doc);
    ctx.setLenient(true);/*from  w w w  .j  a  v  a  2s.co  m*/
    Pointer pointer = ctx.getPointer(xpath);
    Assert.assertNotNull(message, pointer.getNode());
}

From source file:org.apache.cocoon.forms.formmodel.WidgetTestHelper.java

public static void assertXPathNotExists(String message, String xpath, Document doc) {
    JXPathContext ctx = JXPathContext.newContext(doc);
    ctx.setLenient(true);//from w ww .  j a  va  2  s .c  o m
    Pointer pointer = ctx.getPointer(xpath);
    Assert.assertNull(message, pointer.getNode());
}

From source file:org.apache.cocoon.generation.JXTemplateGenerator.java

private Object getNode(JXTExpression expr, JexlContext jexlContext, JXPathContext jxpathContext,
        Boolean lenient) throws Exception {
    try {/*from   w w w.ja va  2  s.c o  m*/
        Object compiled = expr.compiledExpression;
        if (compiled instanceof CompiledExpression) {
            CompiledExpression e = (CompiledExpression) compiled;
            boolean oldLenient = jxpathContext.isLenient();
            if (lenient != null)
                jxpathContext.setLenient(lenient.booleanValue());
            try {
                Iterator iter = e.iteratePointers(jxpathContext);
                if (iter.hasNext()) {
                    Pointer first = (Pointer) iter.next();
                    if (iter.hasNext()) {
                        List result = new LinkedList();
                        result.add(first.getNode());
                        boolean dom = (first.getNode() instanceof Node);
                        while (iter.hasNext()) {
                            Object obj = ((Pointer) iter.next()).getNode();
                            dom = dom && (obj instanceof Node);
                            result.add(obj);
                        }
                        Object[] arr;
                        if (dom) {
                            arr = new Node[result.size()];
                        } else {
                            arr = new Object[result.size()];
                        }
                        result.toArray(arr);
                        return arr;
                    }
                    return first.getNode();
                }
                return null;
            } finally {
                jxpathContext.setLenient(oldLenient);
            }
        } else if (compiled instanceof Expression) {
            Expression e = (Expression) compiled;
            return e.evaluate(jexlContext);
        }
        return expr.raw;
    } catch (InvocationTargetException e) {
        Throwable t = e.getTargetException();
        if (t instanceof Exception) {
            throw (Exception) t;
        }
        throw (Error) t;
    }
}