001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.woody.binding;
018:
019: import org.apache.cocoon.woody.formmodel.Widget;
020: import org.apache.cocoon.woody.datatype.convertor.Convertor;
021: import org.apache.commons.jxpath.JXPathContext;
022:
023: import java.util.Locale;
024:
025: /**
026: * UniqueFieldJXPathBinding provides an implementation of a {@link Binding}
027: * that that allows the specification of a uniquefields defined inside a repeater.
028: * <p>
029: * NOTES: <ol>
030: * <li>This Binding uses the provided widget-id of a defined field in the repeater.</li>
031: * </ol>
032: *
033: * @version CVS $Id: UniqueFieldJXPathBinding.java 433543 2006-08-22 06:22:54Z crossley $
034: */
035: public class UniqueFieldJXPathBinding extends JXPathBindingBase {
036:
037: /**
038: * The xpath expression to the objectModel property
039: */
040: private final String xpath;
041:
042: /**
043: * The id of the Woody form-widget
044: */
045: private final String fieldId;
046:
047: /**
048: * Optional convertor to convert values to and from strings when setting or reading
049: * the from the model. Especially used in combination with XML models where everything
050: * are strings.
051: */
052: private final Convertor convertor;
053:
054: /**
055: * The locale to pass to the {@link #convertor}.
056: */
057: private final Locale convertorLocale;
058:
059: /**
060: * Constructs UniqueFieldJXPathBinding.
061: *
062: * @param convertor may be null
063: */
064: public UniqueFieldJXPathBinding(
065: JXPathBindingBuilderBase.CommonAttributes commonAtts,
066: String widgetId, String xpath, Convertor convertor,
067: Locale convertorLocale) {
068: super (commonAtts);
069: this .fieldId = widgetId;
070: this .xpath = xpath;
071: this .convertor = convertor;
072: this .convertorLocale = convertorLocale;
073: }
074:
075: /**
076: * Actively performs the binding from the ObjectModel wrapped in a jxpath
077: * context to the Woody-form-widget specified in this object.
078: */
079: public void doLoad(Widget frmModel, JXPathContext jxpc)
080: throws BindingException {
081: Widget widget = frmModel.getWidget(this .fieldId);
082: if (widget == null) {
083: throw new BindingException(
084: "The widget with the ID ["
085: + this .fieldId
086: + "] referenced in the binding does not exist in the form definition.");
087: }
088:
089: Object value = jxpc.getValue(this .xpath);
090: if (value != null && convertor != null) {
091: if (value instanceof String) {
092: value = convertor.convertFromString((String) value,
093: convertorLocale, null);
094: } else {
095: getLogger()
096: .warn(
097: "Convertor ignored on backend-value which isn't of type String.");
098: }
099: }
100:
101: widget.setValue(value);
102: if (getLogger().isDebugEnabled()) {
103: getLogger().debug(
104: "Done loading " + toString() + " -- value= "
105: + value);
106: }
107: }
108:
109: /**
110: * Actively performs the binding from the Woody-form to the ObjectModel
111: * wrapped in a jxpath context
112: */
113: public void doSave(Widget frmModel, JXPathContext jxpc)
114: throws BindingException {
115: // Do nothing
116: }
117:
118: public String toString() {
119: return "UniqueFieldJXPathBinding [widget=" + this .fieldId
120: + ", xpath=" + this .xpath + "]";
121: }
122:
123: /*public void enableLogging(Logger logger) {
124: super.enableLogging(logger);
125: }*/
126: /**
127: * @return Returns the convertor.
128: */
129: public Convertor getConvertor() {
130: return convertor;
131: }
132:
133: /**
134: * @return Returns the convertorLocale.
135: */
136: public Locale getConvertorLocale() {
137: return convertorLocale;
138: }
139:
140: /**
141: * @return Returns the fieldId.
142: */
143: public String getFieldId() {
144: return fieldId;
145: }
146:
147: /**
148: * @return Returns the xpath.
149: */
150: public String getXpath() {
151: return xpath;
152: }
153: }
|