List of usage examples for org.apache.commons.jxpath JXPathContext createPath
public abstract Pointer createPath(String xpath);
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 .java 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;//w w w. j av a 2 s .c om } 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 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 {// w ww . j av a 2s. c o 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 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;// w ww . jav a 2 s . c o m 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.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. *///from w w w . j av a 2 s. com 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()); } }
From source file:org.apache.cocoon.forms.binding.SimpleRepeaterJXPathBinding.java
public void doSave(Widget frmModel, JXPathContext jctx) throws BindingException { // Find the repeater Repeater repeater = (Repeater) selectWidget(frmModel, this.repeaterId); if (repeater.getSize() == 0 && this.deleteIfEmpty) { // Repeater is empty : erase all jctx.removeAll(this.repeaterPath); } else {//from w w w.java2 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); } } }
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 {/*from w ww .ja va 2s . co 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.woody.binding.ContextJXPathBinding.java
/** * Actively performs the binding from the Woody-form to the ObjectModel * wrapped in a jxpath context./*from w w w.j a v a 2s . c om*/ */ 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 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 w ww . j ava2 s . c om // 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 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); }/*from ww w .j a va2 s. 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); } }