001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.form.translator;
016    
017    import java.util.Locale;
018    
019    import org.apache.hivemind.util.PropertyUtils;
020    import org.apache.tapestry.form.IFormComponent;
021    import org.apache.tapestry.form.ValidationMessages;
022    
023    /**
024     * A trivial {@link Translator} implementation. By default, empty text submissions are interpretted
025     * as null.
026     * 
027     * @author Paul Ferraro
028     * @since 4.0
029     */
030    public class StringTranslator extends AbstractTranslator
031    {
032    
033        private String _empty = null;
034    
035        public StringTranslator()
036        {
037        }
038    
039        // Needed until HIVEMIND-134 fix is available
040    
041        public StringTranslator(String initializer)
042        {
043            PropertyUtils.configureProperties(this, initializer);
044        }
045    
046        /**
047         * @see org.apache.tapestry.form.translator.AbstractTranslator#parseText(org.apache.tapestry.form.IFormComponent,
048         *      ValidationMessages, java.lang.String)
049         */
050        protected Object parseText(IFormComponent field, ValidationMessages messages, String text)
051        {
052            return text;
053        }
054    
055        /**
056         * @see org.apache.tapestry.form.translator.AbstractTranslator#formatObject(org.apache.tapestry.form.IFormComponent,
057         *      Locale, java.lang.Object)
058         */
059        protected String formatObject(IFormComponent field, Locale locale, Object object)
060        {
061            return object.toString();
062        }
063    
064        public Object getValueForEmptyInput()
065        {
066            return _empty;
067        }
068    
069        public void setEmpty(String empty)
070        {
071            _empty = empty;
072        }
073    
074        public String getEmpty()
075        {
076            return _empty;
077        }
078    }