sylvain 2003/09/08 08:33:27
Modified: src/blocks/woody/java/org/apache/cocoon/woody/binding
ContextJXPathBinding.java ValueJXPathBinding.java
Added: src/blocks/woody/java/org/apache/cocoon/woody/binding
SimpleRepeaterJXPathBinding.java
SimpleRepeaterJXPathBindingBuilder.java
Log:
- allow creation of non existent paths (i.e. new XML elements)
- new simple binding for repeaters
Revision Changes Path
1.3 +19 -5
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/ContextJXPathBinding.java
Index: ContextJXPathBinding.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/ContextJXPathBinding.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ContextJXPathBinding.java 24 Jul 2003 12:39:17 -0000 1.2
+++ ContextJXPathBinding.java 8 Sep 2003 15:33:27 -0000 1.3
@@ -52,6 +52,7 @@
import org.apache.cocoon.woody.formmodel.Widget;
import org.apache.commons.jxpath.JXPathContext;
+import org.apache.commons.jxpath.Pointer;
/**
* ContextJXPathBinding provides an implementation of a [EMAIL PROTECTED]
Binding}
@@ -78,10 +79,17 @@
* context to the Woody-form.
*/
public void loadFormFromModel(Widget frmModel, JXPathContext jxpc) {
- JXPathContext subContext =
jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
- super.loadFormFromModel(frmModel, subContext);
- if (getLogger().isDebugEnabled())
- getLogger().debug("done loading " + toString());
+ Pointer ptr = jxpc.getPointer(this.xpath);
+ if (ptr.getNode() != null) {
+ JXPathContext subContext = jxpc.getRelativeContext(ptr);
+ super.loadFormFromModel(frmModel, subContext);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug("done loading " + toString());
+ } else {
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("non-existent path: skipping " +
toString());
+ }
+ }
}
/**
@@ -89,7 +97,13 @@
* wrapped in a jxpath context.
*/
public void saveFormToModel(Widget frmModel, JXPathContext jxpc) throws
BindingException {
- JXPathContext subContext =
jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
+ 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.saveFormToModel(frmModel, subContext);
if (getLogger().isDebugEnabled())
getLogger().debug("done saving " + toString());
1.2 +1 -1
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/ValueJXPathBinding.java
Index: ValueJXPathBinding.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/ValueJXPathBinding.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ValueJXPathBinding.java 12 Aug 2003 12:54:45 -0000 1.1
+++ ValueJXPathBinding.java 8 Sep 2003 15:33:27 -0000 1.2
@@ -152,7 +152,7 @@
if ((value == null && oldValue != null) || value != null &&
!value.equals(oldValue)) {
// first update the value itself
- jxpc.setValue(this.xpath, value);
+ jxpc.createPathAndSetValue(this.xpath, value);
// now perform any other bindings that need to be
performed when the value is updated
JXPathContext subContext = null;
1.1
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/SimpleRepeaterJXPathBinding.java
Index: SimpleRepeaterJXPathBinding.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.woody.binding;
import java.util.Iterator;
import org.apache.avalon.framework.logger.Logger;
import org.apache.cocoon.woody.formmodel.Repeater;
import org.apache.cocoon.woody.formmodel.Widget;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;
/**
* Simple binding for repeaters: on save, first deletes the target data
* before recreating it from scratch.
* <p>
* For a smarter binding that avoids deletion and recreation, consider
* [EMAIL PROTECTED] org.apache.cocoon.woody.binding.RepeaterJXPathBinding}
*
* @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
*/
public class SimpleRepeaterJXPathBinding extends JXPathBindingBase {
private final String repeaterId;
private final String repeaterPath;
private final String rowPath;
private final JXPathBindingBase rowBinding;
public SimpleRepeaterJXPathBinding(
String repeaterId, String repeaterPath, String rowPath,
JXPathBindingBase rowBinding) {
this.repeaterId = repeaterId;
this.repeaterPath = repeaterPath;
this.rowPath = rowPath;
this.rowBinding = rowBinding;
}
public void loadFormFromModel(Widget frmModel, JXPathContext jctx) {
// Find the repeater and clear it
Repeater repeater = (Repeater) frmModel.getWidget(this.repeaterId);
repeater.removeRows();
// 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
while (rowPointers.hasNext()) {
// create a new row, take that as the frmModelSubContext
Repeater.RepeaterRow thisRow = repeater.addRow();
// make a jxpath ObjectModelSubcontext 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());
}
public void saveFormToModel(Widget frmModel, JXPathContext jctx) throws
BindingException {
// Find the repeater
Repeater repeater = (Repeater) frmModel.getWidget(this.repeaterId);
// Move to repeater context and create the path if needed
// FIXME: should avoid creating the path if repeater is empty
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++) {
String path = this.rowPath + '[' + (i+1) + ']';
Pointer rowPtr = repeaterContext.createPath(path);
JXPathContext rowContext =
repeaterContext.getRelativeContext(rowPtr);
this.rowBinding.saveFormToModel(repeater.getRow(i), rowContext);
}
}
public String toString() {
return "SimpleRepeaterJXPathBinding [widget=" + this.repeaterId + ",
xpath=" + this.repeaterPath + "]";
}
public void enableLogging(Logger logger) {
super.enableLogging(logger);
this.rowBinding.enableLogging(logger);
}
}
1.1
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/SimpleRepeaterJXPathBindingBuilder.java
Index: SimpleRepeaterJXPathBindingBuilder.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.woody.binding;
import org.apache.cocoon.woody.util.DomHelper;
import org.w3c.dom.Element;
/**
* A simple repeater binding that will replace (i.e. delete then re-add all)
its
* content.
* <pre>
* <wb:simple-repeater
* id="contacts"
* parent-path="contacts">
* <<em>... child bindings ...</em>
* </wb:simple-repeater>
* </pre>
*
* @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
*/
public class SimpleRepeaterJXPathBindingBuilder
extends JXpathBindingBuilderBase {
public JXPathBindingBase buildBinding(
Element bindingElem,
JXPathBindingManager.Assistant assistant) throws BindingException {
try {
String repeaterId = DomHelper.getAttribute(bindingElem, "id");
String parentPath = DomHelper.getAttribute(bindingElem,
"parent-path");
String rowPath = DomHelper.getAttribute(bindingElem, "row-path");
JXPathBindingBase[] childBindings =
assistant.makeChildBindings(bindingElem);
return new SimpleRepeaterJXPathBinding(repeaterId, parentPath,
rowPath,
new ComposedJXPathBindingBase(childBindings));
} catch (BindingException e) {
throw e;
} catch (Exception e) {
throw new BindingException("Error building repeater binding
defined at " + DomHelper.getLocation(bindingElem), e);
}
}
}