Example usage for org.apache.commons.jxpath JXPathContext iteratePointers

List of usage examples for org.apache.commons.jxpath JXPathContext iteratePointers

Introduction

In this page you can find the example usage for org.apache.commons.jxpath JXPathContext iteratePointers.

Prototype

public abstract Iterator iteratePointers(String xpath);

Source Link

Document

Traverses the xpath and returns an Iterator of Pointers.

Usage

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  ww .  j  av a 2 s .co 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());
    }
}

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 ww  w . j a va2s.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 {//  w  ww .ja va  2  s  .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.datatype.FlowJXPathSelectionList.java

public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
    JXPathContext ctx = null;
    Iterator iter = null;/*from w ww  . j ava2  s.c  o  m*/
    if (model == null) {
        Object flowData = FlowHelper.getContextObject(ContextHelper.getObjectModel(this.context));
        if (flowData == null) {
            throw new SAXException("No flow data to produce selection list");
        }

        // Move to the list location
        ctx = JXPathContext.newContext(flowData);

        // Iterate on all elements of the list
        iter = ctx.iteratePointers(this.listPath);
    } else {
        // Move to the list location
        ctx = JXPathContext.newContext(model);

        // Iterate on all elements of the list
        iter = ctx.iteratePointers(".");
    }

    // Start the selection-list
    contentHandler.startElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL,
            FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL, XMLUtils.EMPTY_ATTRIBUTES);
    if (this.nullable) {
        final AttributesImpl voidAttrs = new AttributesImpl();
        voidAttrs.addCDATAAttribute("value", "");
        contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, voidAttrs);

        if (this.nullText != null) {
            contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL,
                    FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);

            if (this.nullTextIsI18nKey) {
                if ((this.i18nCatalog != null) && (this.i18nCatalog.trim().length() > 0)) {
                    new I18nMessage(this.nullText, this.i18nCatalog).toSAX(contentHandler);
                } else {
                    new I18nMessage(this.nullText).toSAX(contentHandler);
                }
            } else {
                contentHandler.characters(this.nullText.toCharArray(), 0, this.nullText.length());
            }

            contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL,
                    FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL);
        }

        contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
    }

    while (iter.hasNext()) {
        String stringValue = "";
        Object label = null;

        // Get a context on the current item
        Pointer ptr = (Pointer) iter.next();
        if (ptr.getValue() != null) {
            JXPathContext itemCtx = ctx.getRelativeContext(ptr);

            // Get the value as a string
            Object value = itemCtx.getValue(this.valuePath);

            // List may contain null value, and (per contract with convertors),
            // convertors are not invoked on nulls.
            if (value != null) {
                stringValue = this.datatype.convertToString(value, locale);
            }

            // Get the label (can be ommitted)
            itemCtx.setLenient(true);
            label = itemCtx.getValue(this.labelPath);
            if (label == null) {
                label = stringValue;
            }
        }

        // Output this item
        AttributesImpl itemAttrs = new AttributesImpl();
        itemAttrs.addCDATAAttribute("value", stringValue);
        contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, itemAttrs);
        if (label != null) {
            contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL,
                    FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);
            if (label instanceof XMLizable) {
                ((XMLizable) label).toSAX(contentHandler);
            } else if (this.labelIsI18nKey) {
                String stringLabel = label.toString();

                if ((this.i18nCatalog != null) && (this.i18nCatalog.trim().length() > 0)) {
                    new I18nMessage(stringLabel, this.i18nCatalog).toSAX(contentHandler);
                } else {
                    new I18nMessage(stringLabel).toSAX(contentHandler);
                }
            } else {
                String stringLabel = label.toString();
                contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length());
            }
            contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL,
                    FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL);
        }
        contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
    }

    // End the selection-list
    contentHandler.endElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL,
            FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL);
}

From source file:org.apache.cocoon.woody.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  .  j  a  v a2  s.  c o m*/
 */
public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
    // Find the repeater
    Repeater repeater = (Repeater) frmModel.getWidget(this.repeaterId);
    repeater.removeRows();
    int initialSize = repeater.getSize();

    // build a jxpath iterator for pointers
    JXPathContext repeaterContext = jxpc.getRelativeContext(jxpc.getPointer(this.repeaterPath));
    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
        Iterator iter = this.uniqueRowBinding.iterator();
        while (iter.hasNext()) {
            ((UniqueFieldJXPathBinding) iter.next()).loadFormFromModel(thisRow, rowContext);
        }
        this.rowBinding.loadFormFromModel(thisRow, rowContext);
    }
    if (getLogger().isDebugEnabled())
        getLogger().debug("done loading rows " + toString());
}

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.
 *///  w ww  . ja  v  a 2s.  c om
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 doLoad(Widget frmModel, JXPathContext jctx) throws BindingException {
    // Find the repeater and clear it
    Repeater repeater = (Repeater) frmModel.getWidget(this.repeaterId);

    if (this.clearOnLoad) {
        repeater.removeRows();//ww w  . j a v a 2  s . 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.woody.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) frmModel.getWidget(this.repeaterId);
    if (repeater == null) {
        String fullId = frmModel.getFullyQualifiedId();
        if (fullId == null || fullId.length() == 0) {
            fullId = "";
        } else {/*from w  w w.j  a 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.removeRows();
    }

    // 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 clone = ((Node) rowPointer.getNode()).cloneNode(true);
                virtualNode.appendChild(clone);
                rowContext = JXPathContext.newContext(repeaterContext, virtualNode);
            }

            // 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.woody.datatype.FlowJXPathSelectionList.java

public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
    JXPathContext ctx = null;
    Iterator iter = null;//w  w  w. j a  va  2  s.co  m
    if (model == null) {
        Object flowData = FlowHelper.getContextObject(ContextHelper.getObjectModel(this.context));
        if (flowData == null) {
            throw new SAXException("No flow data to produce selection list");
        }

        // Move to the list location
        ctx = JXPathContext.newContext(flowData);

        // Iterate on all elements of the list
        iter = ctx.iteratePointers(this.listPath);
    } else {
        // Move to the list location
        ctx = JXPathContext.newContext(model);

        // Iterate on all elements of the list
        iter = ctx.iteratePointers(".");
    }

    // Start the selection-list
    contentHandler.startElement(Constants.WI_NS, SELECTION_LIST_EL,
            Constants.WI_PREFIX_COLON + SELECTION_LIST_EL, Constants.EMPTY_ATTRS);

    while (iter.hasNext()) {
        String stringValue = "";
        Object label = null;

        // Get a context on the current item
        Pointer ptr = (Pointer) iter.next();
        if (ptr.getValue() != null) {
            JXPathContext itemCtx = ctx.getRelativeContext(ptr);

            // Get the value as a string
            Object value = itemCtx.getValue(this.valuePath);

            // List may contain null value, and (per contract with convertors),
            // convertors are not invoked on nulls.
            if (value != null) {
                stringValue = this.datatype.convertToString(value, locale);
            }

            // Get the label (can be ommitted)
            itemCtx.setLenient(true);
            label = itemCtx.getValue(this.labelPath);
            if (label == null) {
                label = stringValue;
            }
        }

        // Output this item
        AttributesImpl itemAttrs = new AttributesImpl();
        itemAttrs.addCDATAAttribute("value", stringValue);
        contentHandler.startElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL, itemAttrs);
        if (label != null) {
            contentHandler.startElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL,
                    Constants.EMPTY_ATTRS);
            if (label instanceof XMLizable) {
                ((XMLizable) label).toSAX(contentHandler);
            } else {
                String stringLabel = label.toString();
                contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length());
            }
            contentHandler.endElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL);
        }
        contentHandler.endElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL);
    }

    // End the selection-list
    contentHandler.endElement(Constants.WI_NS, SELECTION_LIST_EL,
            Constants.WI_PREFIX_COLON + SELECTION_LIST_EL);
}

From source file:org.apache.ctakes.jdl.data.loader.XmlLoader.java

/**
 * @param jdlConnection//from  w  w  w. ja  v a2  s  . c o  m
 *            the jdlConnection to manage
 */
@Override
public final void dataInsert(final JdlConnection jdlConnection) {
    String sql = getSqlInsert(loader);
    Number ncommit = loader.getCommit();
    int r = 0;
    try {
        Iterator<?> iterator = context.iteratePointers(loader.getXroot());
        while (iterator.hasNext()) {
            r++;
            NodePointer pointer = (NodePointer) iterator.next();
            Node node = (Node) pointer.getNode();
            JXPathContext context = JXPathContext.newContext(DomUtil.nodeToDocument(node));
            try {
                int c = 0;
                PreparedStatement preparedStatement = jdlConnection.getOpenConnection().prepareStatement(sql);
                if (ncommit == null) {
                    jdlConnection.setAutoCommit(true);
                } else {
                    jdlConnection.setAutoCommit(false);
                }
                for (Column column : loader.getColumn()) {
                    c++;
                    Object value = column.getConstant();
                    if (value == null) {
                        if (column.getSeq() != null) {
                            value = r + column.getSeq().intValue();
                        } else if (column.getXpath() != null) {
                            value = this.context.getValue(column.getXpath());
                        } else {
                            value = context.getPointer(column.getXleaf()).getValue();
                        }
                    }
                    preparedStatement.setObject(c, value);
                }
                executeBatch(preparedStatement);
                if (!jdlConnection.isAutoCommit() && (r % ncommit.intValue() == 0)) {
                    jdlConnection.commitConnection();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        if (!jdlConnection.isAutoCommit()) {
            jdlConnection.commitConnection();
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}