001    // Copyright 2005-2006 Ferdinand Prantl <prantl@users.sourceforge.net>
002    // Copyright 2001-2004 The Apache Software Foundation
003    // All rights reserved.
004    //
005    // Licensed under the Apache License, Version 2.0 (the "License");
006    // you may not use this file except in compliance with the License.
007    // 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    // See http://ant-eclipse.sourceforge.net for the most recent version
018    // and more information.
019    
020    package prantl.ant.eclipse;
021    
022    import org.apache.tools.ant.BuildException;
023    
024    /**
025     * Configures name and value of a variable in a preferences file under the directory
026     * <tt>.settings</tt>. Both attributes <tt>name</tt> and <tt>value</tt> are
027     * mandatory.
028     * 
029     * @since Ant-Eclipse 1.0
030     * @author Ferdinand Prantl &lt;prantl@users.sourceforge.net&gt;
031     */
032    public class VariableElement {
033    
034        private PreferencesElement preferences = null;
035    
036        private String name = null;
037    
038        private String value = null;
039    
040        /**
041         * Creates a new instance of the variable element.
042         * 
043         * @param parent
044         *        The parent preferences element of this variable one.
045         * @since Ant-Eclipse 1.0
046         */
047        public VariableElement(PreferencesElement parent) {
048            preferences = parent;
049        }
050    
051        /**
052         * Returns the name of the configuration variable. The name must not be <tt>null</tt>,
053         * it is a mandatory attribute.
054         * 
055         * @return The name of the configuration variable or <tt>null</tt> if having not
056         *         been set.
057         */
058        public String getName() {
059            return name;
060        }
061    
062        /**
063         * Returns the value of the configuration variable. The value must not be
064         * <tt>null</tt>, it is a mandatory attribute.
065         * 
066         * @return The value of the configuration variable or <tt>null</tt> if having not
067         *         been set.
068         */
069        public String getValue() {
070            return value;
071        }
072    
073        /**
074         * Sets the name of the configuration variable.
075         * 
076         * @param name
077         *        A name of the configuration variable.
078         * @since Ant-Eclipse 1.0
079         */
080        public void setName(String name) {
081            preferences.validateVariableName(name);
082            this.name = name;
083        }
084    
085        /**
086         * Sets the value of the configuration variable.
087         * 
088         * @param value
089         *        A value of the configuration variable.
090         * @since Ant-Eclipse 1.0
091         */
092        public void setValue(String value) {
093            this.value = value;
094        }
095    
096        /**
097         * Performs the validation of the element at the time when the whole build file was
098         * parsed checking the content of the element and possibly adding mandatory variables
099         * with default settings.
100         * 
101         * @since Ant-Eclipse 1.0
102         */
103        public void validate() {
104            if (name == null)
105                throw new BuildException(
106                        "The mandatory attribute \"name\" was missing in an element \"variable\".");
107            if (value == null)
108                throw new BuildException(
109                        "The mandatory attribute \"value\" was missing in an element \"variable\".");
110        }
111    
112    }