List of usage examples for org.apache.commons.jxpath JXPathContext getRelativeContext
public abstract JXPathContext getRelativeContext(Pointer pointer);
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 ww . j a v a 2 s.c om */ 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 om */ 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 w w w .java2 s . c o 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 {/*from www. j ava 2 s . 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.JavaScriptJXPathBinding.java
public void doSave(Widget frmModel, JXPathContext jctx) throws BindingException { if (this.saveScript != null) { Widget widget = selectWidget(frmModel, 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 2 s . co m*/ Map objectModel = ContextHelper.getObjectModel(this.avalonContext); JavaScriptHelper.callFunction(this.saveScript, widget, new Object[] { widget, pointer, widgetCtx, 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] - saveForm: No code avaliable on the javascript binding with 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 ww w . ja va 2s.com*/ // 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.MultiValueJXPathBinding.java
public void doSave(Widget frmModel, JXPathContext jctx) throws BindingException { Widget widget = selectWidget(frmModel, this.multiValueId); Object[] values = (Object[]) widget.getValue(); JXPathContext multiValueContext = jctx.getRelativeContext(jctx.createPath(this.multiValuePath)); // Delete all that is already present // Unfortunately the following statement doesn't work (it doesn't removes all elements from the // list because of a bug in JXPath I wasn't able to locate). //multiValueContext.removeAll(this.rowPath); // TODO: This is a workaround until the bug in commons-jxpath is found, fixed and released Iterator rowPointers = multiValueContext.iteratePointers(this.rowPath); int cnt = 0;// ww w . jav a 2 s.com while (rowPointers.hasNext()) { cnt++; rowPointers.next(); } while (cnt >= 1) { String thePath = this.rowPath + "[" + cnt + "]"; multiValueContext.removePath(thePath); cnt--; } 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); } 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.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 w w w.ja v a2s . com*/ 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./* www .j a va2 s. c om*/ */ 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.RepeaterJXPathBinding.java
/** * Uses the mapped identity of each row to detect if rows have been * updated, inserted or removed. Depending on what happened the appropriate * child-bindings are allowed to visit the narrowed contexts. *///w w w. ja va 2s .c o m public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException { // Find the repeater Repeater repeater = (Repeater) selectWidget(frmModel, this.repeaterId); // and his context, creating the path if needed JXPathContext repeaterContext = jxpc.getRelativeContext(jxpc.createPath(this.repeaterPath)); // create set of updatedRowIds Set updatedRows = 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 identity List identity = getIdentity(thisRow); if (hasNonNullElements(identity)) { // 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 contextIdentity = getIdentity(rowContext); if (ListUtils.isEqualList(identity, contextIdentity)) { // match! --> bind to children this.rowBinding.saveFormToModel(thisRow, rowContext); // --> store rowIdValue in list of updatedRowIds updatedRows.add(identity); 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 updatedRows.add(identity); } } else { // if there is no value to determine the identity --> 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 contextIdentity = getIdentity(rowContext); // check if the identity of the rowContext is in the updated rows // if not --> bind for delete if (!isIdentityInUpdatedRows(updatedRows, contextIdentity)) { rowsToDelete.add(rowContext); } } if (rowsToDelete.size() > 0) { // 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--) { if (this.deleteRowBinding != null) { this.deleteRowBinding.saveFormToModel(frmModel, rowsToDelete.get(i)); } else { // Simply remove the corresponding path ((JXPathContext) rowsToDelete.get(i)).removePath("."); } } } // 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) { Iterator rowIterator = rowsToInsert.iterator(); //register the factory! while (rowIterator.hasNext()) { Repeater.RepeaterRow thisRow = (Repeater.RepeaterRow) rowIterator.next(); // Perform the insert row binding. if (this.insertRowBinding != null) { 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()); } }