List of usage examples for org.apache.commons.jxpath JXPathContext createPath
public abstract Pointer createPath(String xpath);
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. *//*from www.j a v a2 s . c o m*/ 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 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 {//from w w w . j a v a2s .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.woody.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) frmModel.getWidget(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 w w. j a va2 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(); virtualNode = rowNode.getOwnerDocument().createElementNS(null, "virtual"); rowContext = JXPathContext.newContext(repeaterContext, virtualNode); } // 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.firesoa.common.jxpath.JXPathTestCase.java
protected void assertXPathCreatePath(JXPathContext ctx, String xpath, Object expectedValue, String expectedPath) {//from w ww . ja v a2s .co m Pointer pointer = ctx.createPath(xpath); assertEquals("Creating path <" + xpath + ">", expectedPath, pointer.asPath()); assertEquals("Creating path (pointer value) <" + xpath + ">", expectedValue, pointer.getValue()); assertEquals("Creating path (context value) <" + xpath + ">", expectedValue, ctx.getValue(pointer.asPath())); }
From source file:org.onecmdb.core.utils.xpath.commands.CreateCommand.java
@Override public void transfer(OutputStream out) { this.context.put("create", true); JXPathContext xPathContext = getXPathContext(); xPathContext.setLenient(true);//from w ww . j av a 2 s . c o m Object o = xPathContext.getValue(getPath()); if (o != null) { throw new IllegalArgumentException("Path '" + getPath() + "' exists."); } setupTX(); Pointer p = xPathContext.createPath(getPath()); JXPathContext relContext = getRelativeContext(p); for (String outputAttribute : getInputAttributeNameAsArray()) { /* Iterator<Pointer> outputAttrPointersIter = context.iteratePointers(outputAttribute); while(outputAttrPointersIter.hasNext()) { Pointer vPointer = outputAttrPointersIter.next(); Object values = getValues(outputAttribute); //NodePointer nP = (NodePointer)vPointer; //nP.getImmediateValuePointer().setValue(values); vPointer.setValue(values); } */ Object values = getValues(outputAttribute); relContext.setValue(outputAttribute, values); } processTX(); }
From source file:org.openvpms.component.business.service.archetype.AbstractIMObjectFactory.java
/** * Creates a node in the context, populating any default value. * * @param context the jxpath context//from w ww.jav a 2 s. c o m * @param node the node to create * @throws ArchetypeServiceException if the create fails */ private void create(JXPathContext context, NodeDescriptor node) { if (log.isDebugEnabled()) { log.debug("Attempting to create path " + node.getPath() + " for node " + node.getName()); } context.getVariables().declareVariable("node", node); context.createPath(node.getPath()); String expression = node.getDefaultValue(); if (!StringUtils.isEmpty(expression)) { if (log.isDebugEnabled()) { log.debug("evaluating default value expression for node " + node.getName() + " path " + node.getPath() + " and expression " + expression); } Object value = context.getValue(expression); IMObject object = (IMObject) context.getContextBean(); if (node.isCollection()) { if (value != null) { if (Collection.class.isAssignableFrom(value.getClass())) { for (Object v : (Collection) value) { node.addChildToCollection(object, v); } } else { node.addChildToCollection(object, value); } } } else { node.setValue(object, value); } } }