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
020package prantl.ant.eclipse;
021
022import java.util.HashSet;
023
024import org.apache.tools.ant.BuildException;
025
026/**
027 * Configures the component preferences file
028 * <tt>.settings/org.eclipse.core.resources.prefs</tt> on the high level using
029 * attributes for the typical constellations of variable values.
030 * 
031 * @since Ant-Eclipse 1.0
032 * @author Ferdinand Prantl &lt;prantl@users.sourceforge.net&gt;
033 */
034public class OrgEclipseCoreResourcesPreferencesElement extends PreferencesElement {
035
036    private static final String ELEMENT = "resources";
037
038    private static final String ENCODING_ATTRIBUTE = "encoding";
039
040    private static final String ENCODING_NAME = "encoding/<project>";
041
042    private static final HashSet ENCODING_VALUES = new HashSet();
043
044    /**
045     * Returns the name of the package these preferences belong to.
046     * 
047     * @return The name of the package these preferences belong to.
048     */
049    static final String getPackageName() {
050        return "org.eclipse.core.resources";
051    }
052
053    /**
054     * Creates a new instance of the element for the file with preferences for
055     * org.eclipse.core.resources.
056     * 
057     * @param parent
058     *        The parent settings element of this preferences one.
059     * @since Ant-Eclipse 1.0
060     */
061    public OrgEclipseCoreResourcesPreferencesElement(SettingsElement parent) {
062        super(parent);
063        internalSetName(getPackageName());
064        ENCODING_VALUES.add("ISO-8859-1");
065        ENCODING_VALUES.add("US-ASCII");
066        ENCODING_VALUES.add("UTF-16");
067        ENCODING_VALUES.add("UTF-16BE");
068        ENCODING_VALUES.add("UTF-16LE");
069        ENCODING_VALUES.add("UTF-8");
070    }
071
072    /**
073     * Returns the source file encoding for the project (default is inherited from the
074     * workspace settings and not set here in the file).
075     * 
076     * @return The source file encoding for the project (default is inherited from the
077     *         workspace settings and not set here in the file)
078     */
079    public String getEncoding() {
080        VariableElement variable = getVariable(ENCODING_NAME);
081        return variable == null ? null : variable.getValue();
082    }
083
084    /**
085     * Sets the version of the Eclipse preferences. The default value should be left and
086     * not set explicitely.
087     * 
088     * @param value
089     *        A valid encoding for the project.
090     * @since Ant-Eclipse 1.0
091     */
092    public void setEncoding(String value) {
093        value = value.toUpperCase();
094        if (!ENCODING_VALUES.contains(value))
095            throw new BuildException("The attribute \"" + ENCODING_ATTRIBUTE
096                    + "\" (variable \"" + ENCODING_NAME + "\") has an invalid value \""
097                    + value + "\". Valid values are " + getValidEncodingValues() + ".");
098        internalCreateVariable(ENCODING_NAME, value);
099    }
100
101    /**
102     * Returns allowed values for the variable encoding/<project>.
103     * 
104     * @return A new string with allowed values for the variable line.separator.
105     * @since Ant-Eclipse 1.0
106     */
107    String getValidEncodingValues() {
108        return getValidValues(ENCODING_VALUES);
109    }
110
111    /**
112     * Performs the validation of the element at the time when the whole build file was
113     * parsed checking the content of the element and possibly adding mandatory variables
114     * with default settings.
115     * 
116     * @since Ant-Eclipse 1.0
117     */
118    public void validate() {
119        if (!hasVariable(ENCODING_NAME))
120            throw new BuildException("The attribute \"" + ENCODING_ATTRIBUTE
121                    + "\" (variable \"" + ENCODING_NAME
122                    + "\") was missing in the element \"" + ELEMENT + "\".");
123        super.validate();
124    }
125
126}