You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ug...@apache.org on 2003/12/11 00:34:11 UTC
cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype FlowJXPathSelectionList.java
ugo 2003/12/10 15:34:11
Modified: src/blocks/woody/test/org/apache/cocoon/woody/datatype
FlowJXPathSelectionListTestCase.java
src/blocks/woody/java/org/apache/cocoon/woody/datatype
FlowJXPathSelectionList.java
Added: src/blocks/woody/test/org/apache/cocoon/woody/datatype
FlowJXPathSelectionListTestCaseWithNull.dest.xml
Log:
Flow-JXPath selection lists also now allow empty value (which is
translated to the null object).
Revision Changes Path
1.2 +33 -2 cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.java
Index: FlowJXPathSelectionListTestCase.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FlowJXPathSelectionListTestCase.java 23 Oct 2003 22:08:13 -0000 1.1
+++ FlowJXPathSelectionListTestCase.java 10 Dec 2003 23:34:11 -0000 1.2
@@ -151,7 +151,38 @@
assertEqual("Test if generated list matches expected",
expected, dest.getDocument());
}
-
+
+ /**
+ * Test the generateSaxFragment method with a list containing a null value.
+ */
+ public void testGenerateSaxFragmentWithNull() throws Exception {
+ List beans = new ArrayList(2);
+ beans.add(null);
+ beans.add(new TestBean("1", "One"));
+ beans.add(new TestBean("2", "Two"));
+ Map flowContextObject = new HashMap();
+ flowContextObject.put("beans", beans);
+ Request request = new MockRequest();
+ request.setAttribute(FlowHelper.CONTEXT_OBJECT, flowContextObject);
+ Map objectModel = new HashMap();
+ objectModel.put(ObjectModelHelper.REQUEST_OBJECT, request);
+ Map contextObjectModel = new HashMap();
+ contextObjectModel.put(ContextHelper.CONTEXT_OBJECT_MODEL, objectModel);
+ Context context = new DefaultContext(contextObjectModel);
+ Source sampleSource = new ResourceSource("resource://org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.source.xml");
+ Document sample = parser.parse(sampleSource.getInputStream());
+ Element datatypeElement = (Element) sample.getElementsByTagNameNS(Constants.WD_NS, "datatype").item(0);
+ Datatype datatype = datatypeManager.createDatatype(datatypeElement, false);
+ FlowJXPathSelectionList list = new FlowJXPathSelectionList
+ (context, "beans", "key", "value", datatype);
+ DOMBuilder dest = new DOMBuilder();
+ list.generateSaxFragment(dest, Locale.ENGLISH);
+ Source expectedSource = new ResourceSource("resource://org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCaseWithNull.dest.xml");
+ Document expected = parser.parse(expectedSource.getInputStream());
+ assertEqual("Test if generated list matches expected",
+ expected, dest.getDocument());
+ }
+
/**
* Check is the source document is equal to the one produced by the method under test.
* @param message A message to print in case of failure.
1.1 cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCaseWithNull.dest.xml
Index: FlowJXPathSelectionListTestCaseWithNull.dest.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<wi:selection-list xmlns:wi="http://apache.org/cocoon/woody/instance/1.0"><wi:item value=""/><wi:item value="1"><wi:label>One</wi:label></wi:item><wi:item value="2"><wi:label>Two</wi:label></wi:item></wi:selection-list>
1.4 +21 -16 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/FlowJXPathSelectionList.java
Index: FlowJXPathSelectionList.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/FlowJXPathSelectionList.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- FlowJXPathSelectionList.java 15 Nov 2003 04:21:28 -0000 1.3
+++ FlowJXPathSelectionList.java 10 Dec 2003 23:34:11 -0000 1.4
@@ -136,27 +136,32 @@
contentHandler.startElement(Constants.WI_NS, SELECTION_LIST_EL, Constants.WI_PREFIX_COLON + SELECTION_LIST_EL, Constants.EMPTY_ATTRS);
while(iter.hasNext()) {
-
+
+ String stringValue = "";
+ String stringLabel = null;
// Get a context on the current item
Pointer ptr = (Pointer)iter.next();
- JXPathContext itemCtx = ctx.getRelativeContext(ptr);
-
- // Get the value as a string
- Object value = itemCtx.getValue(this.valuePath);
- String stringValue = this.datatype.convertToString(value, locale);
-
- // Get the label (can be ommitted)
- itemCtx.setLenient(true);
- Object label = itemCtx.getValue(this.labelPath);
- String stringLabel = (label == null) ? stringValue : label.toString();
-
+ if (ptr.getValue() != null) {
+ JXPathContext itemCtx = ctx.getRelativeContext(ptr);
+
+ // Get the value as a string
+ Object value = itemCtx.getValue(this.valuePath);
+ stringValue = this.datatype.convertToString(value, locale);
+
+ // Get the label (can be ommitted)
+ itemCtx.setLenient(true);
+ Object label = itemCtx.getValue(this.labelPath);
+ stringLabel = (label == null) ? stringValue : label.toString();
+ }
// 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);
- contentHandler.startElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL, Constants.EMPTY_ATTRS);
- contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length());
- contentHandler.endElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL);
+ if (stringLabel != null) {
+ contentHandler.startElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL, Constants.EMPTY_ATTRS);
+ 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);
}