001    /*
002     * Jpkg - Java library and tools for operating system package creation.
003     *
004     * Copyright (c) 2007 Three Rings Design, Inc.
005     * All rights reserved.
006     *
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions
009     * are met:
010     * 1. Redistributions of source code must retain the above copyright
011     *    notice, this list of conditions and the following disclaimer.
012     * 2. Redistributions in binary form must reproduce the above copyright
013     *    notice, this list of conditions and the following disclaimer in the
014     *    documentation and/or other materials provided with the distribution.
015     * 3. Neither the name of the copyright owner nor the names of contributors
016     *    may be used to endorse or promote products derived from this software
017     *    without specific prior written permission.
018     *
019     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
021     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022     * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
023     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
024     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
025     * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
026     * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
027     * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
029     * POSSIBILITY OF SUCH DAMAGE.
030     */
031    package com.threerings.antidote.field.text;
032    
033    import com.threerings.antidote.field.BaseField;
034    import com.threerings.antidote.field.Field;
035    
036    import static com.threerings.antidote.MutabilityHelper.objectIsNotSet;
037    
038    /**
039     * An Ant {@link Field} which allows text input in the form <field>Text here</field>.
040     */
041    public abstract class TextField extends BaseField
042    {
043        /**
044         * Ant adder field: grabs the text between any two <field></field> elements.
045         */
046        public void addText (String text)
047        {
048            // perform all Ant property substitutions, e.g. ${field} -> value
049            _text = getProject().replaceProperties(text);
050        }
051    
052        /**
053         * Returns the text supplied to the field.
054         */
055        protected String getText ()
056        {
057            return _text;
058        }
059    
060        /**
061         * Provide concrete classes a method to validate that the text field was set.
062         * A violation will be appended to the field if the text is not set.
063         * @return the {@link TextStatus} status of the text.
064         */
065        protected TextStatus validateTextWasSet ()
066        {
067            if (objectIsNotSet(_text)) {
068                appendViolation(new UnsetTextFieldViolation(this));
069                return TextStatus.INVALID_TEXT;
070            }
071            return TextStatus.VALID_TEXT;
072        }
073    
074        /**
075         * Provide concrete classes a method to validate that the text field was set and not blank.
076         * The text data will first have any newline or tab characters removed before it is checked
077         * e.g. a field filled with only tabs and newlines will be considered empty.
078         * A violation will be appended to the field if the text is not set and blank.
079         * @return the {@link TextStatus} status of the text.
080         */
081        protected TextStatus validateTextNotEmpty ()
082        {
083            if (validateTextWasSet() == TextStatus.INVALID_TEXT) {
084                return TextStatus.INVALID_TEXT;
085            }
086            // scrub new lines etc. and then trim meaning if the string is only empty chracters
087            // it will now have a 0 length.
088            final String scrubbed = scrubString(_text).trim();
089    
090            if (scrubbed.length() <= 0) {
091                appendViolation(new EmptyTextFieldViolation(this));
092                return TextStatus.INVALID_TEXT;
093            }
094            return TextStatus.VALID_TEXT;
095        }
096    
097        /**
098         * Provide concrete classes a convenience method to scrub whitespace from the text field.
099         */
100        protected void scrubTextWhitespace ()
101        {
102            _text = scrubString(_text);
103        }
104    
105        /**
106         * Scrub the supplied String of all characters which may have been placed into the build file
107         * for the text field which is assumed to be just a single line.
108         */
109        private String scrubString (String text)
110        {
111            return text.replaceAll("[\t\n\r\f]", "");
112        }
113    
114        private String _text;
115    }