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 com.threerings.antidote.field.BaseField;
034    import com.threerings.antidote.field.OptionalField;
035    import com.threerings.antidote.field.RequiredField;
036    import com.threerings.jpkg.debian.PackageInfo;
037    
038    import static com.threerings.antidote.MutabilityHelper.areMutablesSet;
039    import static com.threerings.antidote.MutabilityHelper.requiresValidation;
040    
041    /**
042     * The <dpkg> task <package> <info> field. Used to keep track of all package meta-information.
043     */
044    public class Info extends BaseField
045    {
046        // from Field
047        public String getFieldName ()
048        {
049            return "info";
050        }
051    
052        /**
053         * Ant adder field: Set the package name.
054         */
055        public void addName (Name name)
056        {
057            _name.setField(name);
058        }
059    
060        /**
061         * Ant adder field: Set the package version.
062         */
063        public void addVersion (Version version)
064        {
065            _version.setField(version);
066        }
067    
068        /**
069         * Ant adder field: Set the package architecture.
070         */
071        public void addArch (Arch arch)
072        {
073            _arch.setField(arch);
074        }
075    
076        /**
077         * Ant adder field: Set the package description.
078         */
079        public void addDescription (Description description)
080        {
081            _description.setField(description);
082        }
083    
084        /**
085         * Ant adder field: Set the package maintainer.
086         */
087        public void addMaintainer (Maintainer maintainer)
088        {
089            _maintainer.setField(maintainer);
090        }
091    
092        /**
093         * Ant adder field: Set the package priority.
094         */
095        public void addPriority (Priority priority)
096        {
097            _priority.setField(priority);
098        }
099    
100        /**
101         * Ant adder field: Set the package section.
102         */
103        public void addSection (Section section)
104        {
105            _section.setField(section);
106        }
107    
108        /**
109         * Returns the user data converted into a {@link PackageInfo} object. Cannot be called before validate().
110         */
111        public PackageInfo getPackageInfo ()
112        {
113            requiresValidation(_packageInfo);
114            return _packageInfo;
115        }
116    
117        /**
118         * Returns the user data for the package name. Cannot be called before validate().
119         */
120        public String getPackageNameAsString ()
121        {
122            return _name.getField().getPackageName().getFieldValue();
123        }
124    
125        /**
126         * Returns the user data for the package version. Cannot be called before validate().
127         */
128        public String getVersionAsString ()
129        {
130            return _version.getField().getPackageVersion().getFieldValue();
131        }
132    
133        @Override // from BaseComponent
134        protected void validateField ()
135        {
136            // validate the required fields
137            switch (validateChildFields(_name, _version, _arch, _description, _maintainer)) {
138                case ALL_INVALID:
139                case SOME_INVALID:
140                    return;
141    
142                case ALL_VALID:
143                    break;
144            }
145    
146            // check the optional fields
147            switch (areMutablesSet(_section, _priority)) {
148                // if the optional fields are not set, create the simple PackageInfo object and finish.
149                case ALL_UNSET:
150                    _packageInfo = new PackageInfo(
151                        _name.getField().getPackageName(),
152                        _version.getField().getPackageVersion(),
153                        _arch.getField().getPackageArchitecture(),
154                        _maintainer.getField().getPackageMaintainer(),
155                        _description.getField().getPackageDescription());
156                    return;
157    
158                case SOME_UNSET:
159                    reportUnsetDependentFields(_priority, _priority, _section);
160                    reportUnsetDependentFields(_section, _section, _priority);
161                    return;
162    
163                // if all the optional fields are set, continue to validate those fields.
164                case ALL_SET:
165                    break;
166            }
167    
168            // validate the optional fields
169            switch (validateChildFields(_section, _priority)) {
170                case ALL_INVALID:
171                case SOME_INVALID:
172                    return;
173    
174                case ALL_VALID:
175                    // construct a fully populated PackageInfo object with the optional fields included.
176                    _packageInfo = new PackageInfo(
177                        _name.getField().getPackageName(),
178                        _version.getField().getPackageVersion(),
179                        _arch.getField().getPackageArchitecture(),
180                        _maintainer.getField().getPackageMaintainer(),
181                        _description.getField().getPackageDescription(),
182                        _section.getField().getPackageSection(),
183                        _priority.getField().getPackagePriority());
184                    return;
185            }
186        }
187    
188        /** Ant adder/setter fields. */
189        private final RequiredField<Name> _name = new RequiredField<Name>(Name.class, this);
190        private final RequiredField<Version> _version = new RequiredField<Version>(Version.class, this);
191        private final RequiredField<Arch> _arch = new RequiredField<Arch>(Arch.class, this);
192        private final RequiredField<Description> _description = new RequiredField<Description>(Description.class, this);
193        private final RequiredField<Maintainer> _maintainer = new RequiredField<Maintainer>(Maintainer.class, this);
194        private final OptionalField<Priority> _priority = new OptionalField<Priority>(Priority.class, this);
195        private final OptionalField<Section> _section = new OptionalField<Section>(Section.class, this);
196    
197        /** The PackageInfo object representing the user supplied data. */
198        private PackageInfo _packageInfo;
199    }