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.jpkg.ant.dpkg.info;
032    
033    import org.apache.tools.ant.Project;
034    
035    import com.threerings.antidote.EnumHelper;
036    import com.threerings.antidote.field.text.SingleLineTextField;
037    import com.threerings.antidote.property.BooleanProperty;
038    import com.threerings.jpkg.debian.DebianArchitectures;
039    import com.threerings.jpkg.debian.PackageArchitecture;
040    
041    import static com.threerings.antidote.MutabilityHelper.objectIsSet;
042    import static com.threerings.antidote.MutabilityHelper.requiresValidation;
043    
044    
045    /**
046     * Stores the <info> <arch> field, which is the package architecture.
047     * @see PackageArchitecture
048     */
049    public class Arch extends SingleLineTextField
050    {
051        // from Field
052        public String getFieldName ()
053        {
054            return "arch";
055        }
056    
057        /**
058         * Ant setter field: strict. If set to false, unknown architectures will be permitted.
059         * Defaults to true.
060         */
061        public void setStrict (String value)
062        {
063            _strict.setValue(value);
064        }
065    
066        /**
067         * Returns the user data converted into a {@link PackageArchitecture}. Cannot be called before validate().
068         */
069        public PackageArchitecture getPackageArchitecture ()
070        {
071            requiresValidation(_packageArchitecture);
072            return _packageArchitecture;
073        }
074    
075        @Override // from SingleLineTextField
076        protected void validateTextField ()
077        {
078            // strict defaults to true
079            switch (validateProperties(_strict)) {
080                case SOME_INVALID:
081                case ALL_INVALID:
082                    return;
083    
084                case ALL_VALID:
085                    break;
086            }
087    
088            // if the user supplied architecture name is known, use it.
089            final DebianArchitectures arch = EnumHelper.parseEnum(getText(), DebianArchitectures.class);
090            if (objectIsSet(arch)) {
091                _packageArchitecture = new PackageArchitecture(arch);
092                return;
093            }
094    
095            // if the user supplied name is not known, and we are in strict mode, append a violation,
096            // and end. if not in strict mode, log a warning and proceed.
097            if (_strict.getValue()) {
098                appendViolation(new UnknownArchitectureViolation(this, getText()));
099                return;
100    
101            } else {
102                log("The supplied architecture name \'" + getText() + "\' for the <" + getFieldName() +
103                    "> field is unknown. Strict was set to false so the data will be trusted. See " +
104                    "DebianArchitectures or dpkg-architecture for a list of known architectures.", Project.MSG_WARN);
105                _packageArchitecture = new PackageArchitecture(getText());
106                return;
107            }
108        }
109    
110        /** Ant adder/setter fields. */
111        private final BooleanProperty _strict = new BooleanProperty("strict", this, true);
112    
113        /** The PackageArchitecture object representing the user supplied data. */
114        private PackageArchitecture _packageArchitecture;
115    }