Author: tim Date: Thu Oct 7 08:49:56 2004 New Revision: 53992 Modified: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/transformation/EffectWidgetReplacingPipe.java Log: Add part of the choose/when functionality to the cforms template transformer, and change widget id lookups to widget path lookups.
Modified: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/transformation/EffectWidgetReplacingPipe.java ============================================================================== --- cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/transformation/EffectWidgetReplacingPipe.java (original) +++ cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/transformation/EffectWidgetReplacingPipe.java Thu Oct 7 08:49:56 2004 @@ -19,6 +19,7 @@ import org.apache.cocoon.forms.Constants; import org.apache.cocoon.forms.formmodel.AggregateField; import org.apache.cocoon.forms.formmodel.ContainerWidget; +import org.apache.cocoon.forms.formmodel.DataWidget; import org.apache.cocoon.forms.formmodel.Repeater; import org.apache.cocoon.forms.formmodel.Struct; import org.apache.cocoon.forms.formmodel.Union; @@ -80,6 +81,7 @@ private static final String AGGREGATE_WIDGET = "aggregate-widget"; private static final String STRUCT = "struct"; private static final String STYLING_EL = "styling"; + private static final String CHOOSE = "choose"; private static final String UNION = "union"; private static final String VALIDATION_ERROR = "validation-error"; private static final String WIDGET_LABEL = "widget-label"; @@ -87,7 +89,8 @@ protected Widget contextWidget; protected LinkedList contextWidgets; - protected String widgetId; + protected LinkedList chooseWidgets; + protected String widgetPath; protected Widget widget; protected Map classes; @@ -101,6 +104,8 @@ private final RepeaterWidgetHandler repeaterWidgetHandler = new RepeaterWidgetHandler(); private final AggregateWidgetHandler aggregateWidgetHandler = new AggregateWidgetHandler(); private final StructHandler structHandler = new StructHandler(); + private final ChooseHandler chooseHandler = new ChooseHandler(); + private final ChoosePassThruHandler choosePassThruHandler = new ChoosePassThruHandler(); private final UnionHandler unionHandler = new UnionHandler(); private final UnionPassThruHandler unionPassThruHandler = new UnionPassThruHandler(); private final NewHandler newHandler = new NewHandler(); @@ -136,6 +141,7 @@ templates.put(REPEATER_SIZE, repeaterSizeHandler); templates.put(REPEATER_WIDGET_LABEL, repeaterWidgetLabelHandler); templates.put(STRUCT, structHandler); + templates.put(CHOOSE, chooseHandler); templates.put(UNION, unionHandler); templates.put(NEW, newHandler); templates.put(CLASS, classHandler); @@ -156,6 +162,7 @@ // Initialize widget related variables contextWidgets = new LinkedList(); + chooseWidgets = new LinkedList(); classes = new HashMap(); } @@ -167,22 +174,30 @@ return widgetId; } - protected Widget getWidget(String widgetId) throws SAXException { - Widget widget = ((ContainerWidget)contextWidget).getChild(widgetId); + protected String getWidgetPath(Attributes attributes) throws SAXException { + String widgetPath = attributes.getValue("path"); + if (widgetPath == null || widgetPath.equals("")) { + throwSAXException("Missing required widget \"path\" attribute."); + } + return widgetPath; + } + + protected Widget getWidget(String widgetPath) throws SAXException { + Widget widget = ((ContainerWidget)contextWidget).lookupWidget(widgetPath); if (widget == null) { if (contextWidget.getRequestParameterName().equals("")) { - throwSAXException("Widget with id \"" + widgetId + "\" does not exist in the form container."); + throwSAXException("No widget exists at the path \"" + widgetPath + "\", relative to the form container."); } else { - throwSAXException("Widget with id \"" + widgetId + "\" does not exist in the container \"" + - contextWidget.getRequestParameterName() + "\""); + throwSAXException("No widget exists at the path \"" + widgetPath + "\", relative to the container \"" + + contextWidget.getRequestParameterName() + "\"."); } } return widget; } protected void getRepeaterWidget(String handler) throws SAXException { - widgetId = getWidgetId(input.attrs); - widget = getWidget(widgetId); + widgetPath = getWidgetId(input.attrs); + widget = getWidget(widgetPath); if (!(widget instanceof Repeater)) { throwWrongWidgetType("RepeaterWidgetLabelHandler", input.loc, "repeater"); } @@ -350,8 +365,8 @@ public Handler process() throws SAXException { switch (event) { case EVENT_START_ELEMENT: - widgetId = getWidgetId(input.attrs); - Widget widget = getWidget(widgetId); + widgetPath = getWidgetId(input.attrs); + Widget widget = getWidget(widgetPath); widget.generateLabel(getContentHandler()); widget = null; return this; @@ -370,8 +385,8 @@ public Handler process() throws SAXException { switch (event) { case EVENT_START_ELEMENT: - widgetId = getWidgetId(input.attrs); - widget = getWidget(widgetId); + widgetPath = getWidgetId(input.attrs); + widget = getWidget(widgetPath); gotStylingElement = false; out.bufferInit(); return this; @@ -423,10 +438,10 @@ switch(event) { case EVENT_START_ELEMENT: getRepeaterWidget("RepeaterWidgetLabelHandler"); - String widgetId = input.attrs.getValue("widget-id"); - if (widgetId == null || widgetId.equals("")) + String widgetPath = input.attrs.getValue("widget-id"); + if (widgetPath == null || widgetPath.equals("")) throwSAXException("Element repeater-widget-label missing required widget-id attribute."); - ((Repeater)widget).generateWidgetLabel(widgetId, getContentHandler()); + ((Repeater)widget).generateWidgetLabel(widgetPath, getContentHandler()); widget = null; return this; case EVENT_ELEMENT: @@ -476,8 +491,8 @@ public Handler process() throws SAXException { switch(event) { case EVENT_START_ELEMENT: - widgetId = getWidgetId(input.attrs); - widget = getWidget(widgetId); + widgetPath = getWidgetId(input.attrs); + widget = getWidget(widgetPath); if (!(widget instanceof AggregateField)) { throwWrongWidgetType("AggregateWidgetHandler", input.loc, "aggregate"); } @@ -500,8 +515,8 @@ public Handler process() throws SAXException { switch(event) { case EVENT_START_ELEMENT: - widgetId = getWidgetId(input.attrs); - widget = getWidget(widgetId); + widgetPath = getWidgetId(input.attrs); + widget = getWidget(widgetPath); if (!(widget instanceof Struct)) { throwWrongWidgetType("StructHandler", input.loc, "struct"); } @@ -524,12 +539,89 @@ } } + protected class ChooseHandler extends Handler { + public Handler process() throws SAXException { + switch(event) { + case EVENT_START_ELEMENT: + widgetPath = getWidgetPath(input.attrs); + widget = getWidget(widgetPath); + // TODO: Should instead check for datatype convertable to String. + if (!(widget instanceof DataWidget)) { + throwWrongWidgetType("ChooseHandler", input.loc, "DataWidget"); + } + contextWidgets.addFirst(contextWidget); + // Choose does not change the context widget like Union does: + // contextWidget = widget; + chooseWidgets.addFirst(widget); + out.element(Constants.INSTANCE_PREFIX, Constants.INSTANCE_NS, "choose"); + out.startElement(); + return this; + case EVENT_ELEMENT: + if (Constants.TEMPLATE_NS.equals(input.uri)) { + if ("when".equals(input.loc)) { + String testValue = input.attrs.getValue("value"); + if (testValue == null) throwSAXException("Element \"when\" missing required \"value\" attribute."); + String value = (String)((Widget)chooseWidgets.get(0)).getValue(); + if (testValue.equals(value)) { + return nestedHandler; + } else { + return nullHandler; + } + } else if (FORM_TEMPLATE_EL.equals(input.loc)) { + throwSAXException("Element \"form-template\" must not be nested."); + } else { + throwSAXException("Unrecognized template: " + input.loc); + } + } else { + return choosePassThruHandler; + } + case EVENT_END_ELEMENT: + out.endElement(); + chooseWidgets.removeFirst(); + contextWidget = (Widget)contextWidgets.removeFirst(); + return this; + default: + out.copy(); + return this; + } + } + } + + protected class ChoosePassThruHandler extends Handler { + public Handler process() throws SAXException { + switch(event) { + case EVENT_ELEMENT: + if (Constants.TEMPLATE_NS.equals(input.uri)) { + if ("when".equals(input.loc)) { + String testValue = input.attrs.getValue("value"); + if (testValue == null) throwSAXException("Element \"when\" missing required \"value\" attribute."); + String value = (String)((Widget)chooseWidgets.get(0)).getValue(); + if (testValue.equals(value)) { + return nestedHandler; + } else { + return nullHandler; + } + } else if (FORM_TEMPLATE_EL.equals(input.loc)) { + throwSAXException("Element \"form-template\" must not be nested."); + } else { + throwSAXException("Unrecognized template: " + input.loc); + } + } else { + return this; + } + default: + out.copy(); + return this; + } + } + } + protected class UnionHandler extends Handler { public Handler process() throws SAXException { switch(event) { case EVENT_START_ELEMENT: - widgetId = getWidgetId(input.attrs); - widget = getWidget(widgetId); + widgetPath = getWidgetId(input.attrs); + widget = getWidget(widgetPath); if (!(widget instanceof Union)) { throwWrongWidgetType("UnionHandler", input.loc, "union"); } @@ -598,10 +690,10 @@ public Handler process() throws SAXException { switch (event) { case EVENT_START_ELEMENT: - widgetId = getWidgetId(input.attrs); - SaxBuffer classBuffer = (SaxBuffer)classes.get(widgetId); + widgetPath = getWidgetId(input.attrs); + SaxBuffer classBuffer = (SaxBuffer)classes.get(widgetPath); if (classBuffer == null) { - throwSAXException("New: Class \"" + widgetId + "\" does not exist."); + throwSAXException("New: Class \"" + widgetPath + "\" does not exist."); } handlers.addFirst(handler); handler = nestedHandler; @@ -623,13 +715,13 @@ public Handler process() throws SAXException { switch (event) { case EVENT_START_ELEMENT: - widgetId = getWidgetId(input.attrs); + widgetPath = getWidgetId(input.attrs); out.bufferInit(); return this; case EVENT_ELEMENT: return bufferHandler; case EVENT_END_ELEMENT: - classes.put(widgetId, out.getBuffer()); + classes.put(widgetPath, out.getBuffer()); out.bufferFini(); return this; default: @@ -716,8 +808,8 @@ public Handler process() throws SAXException { switch (event) { case EVENT_START_ELEMENT: - widgetId = getWidgetId(input.attrs); - widget = getWidget(widgetId); + widgetPath = getWidgetId(input.attrs); + widget = getWidget(widgetPath); out.bufferInit(); return this; @@ -812,7 +904,7 @@ super.recycle(); this.contextWidget = null; this.widget = null; - this.widgetId = null; + this.widgetPath = null; this.pipeContext = null; this.namespacePrefix = null; }