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 java.util.HashMap;
023    
024    import org.apache.tools.ant.BuildException;
025    
026    /**
027     * Configures the component preferences file
028     * <tt>.settings/org.eclipse.core.runtime.prefs</tt> on the high level using attributes
029     * 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     */
034    public class OrgEclipseCoreRuntimePreferencesElement extends PreferencesElement {
035    
036        private static final String ELEMENT = "runtime";
037    
038        private static final String LINESEPARATOR_ATTRIBUTE = "lineseparator";
039    
040        private static final String LINESEPARATOR_NAME = "line.separator";
041    
042        private static final HashMap LINESEPARATOR_VALUES = new HashMap();
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.runtime";
051        }
052    
053        /**
054         * Creates a new instance of the element for the file with preferences for
055         * org.eclipse.core.runtime.
056         * 
057         * @param parent
058         *        The parent settings element of this preferences one.
059         * @since Ant-Eclipse 1.0
060         */
061        public OrgEclipseCoreRuntimePreferencesElement(SettingsElement parent) {
062            super(parent);
063            internalSetName(getPackageName());
064            LINESEPARATOR_VALUES.put("unix", "\\n");
065            LINESEPARATOR_VALUES.put("macintosh", "\\r");
066            LINESEPARATOR_VALUES.put("windows", "\\r\\n");
067        }
068    
069        /**
070         * Returns the line separator (default is inherited from the workspace settings and
071         * not set here in the file).
072         * 
073         * @return The line separator (default is inherited from the workspace settings and
074         *         not set here in the file).
075         */
076        public String getLineSeparator() {
077            VariableElement variable = getVariable(LINESEPARATOR_NAME);
078            return variable == null ? null : variable.getValue();
079        }
080    
081        /**
082         * Sets the version of the Eclipse preferences. The default value should be left and
083         * not set explicitely.
084         * 
085         * @param value
086         *        A valid line separator.
087         * @since Ant-Eclipse 1.0
088         */
089        public void setLineSeparator(String value) {
090            String lineSeparator = (String) LINESEPARATOR_VALUES.get(value.toLowerCase());
091            if (lineSeparator == null)
092                throw new BuildException("The attribute \"" + LINESEPARATOR_ATTRIBUTE
093                        + "\" (variable \"" + LINESEPARATOR_NAME
094                        + "\")  has an invalid value \"" + value + "\". Valid values are "
095                        + getValidLineSeparatorValues() + ".");
096            internalCreateVariable(LINESEPARATOR_NAME, value);
097        }
098    
099        /**
100         * Returns allowed values for the variable line.separator.
101         * 
102         * @return A new string with allowed values for the variable line.separator.
103         * @since Ant-Eclipse 1.0
104         */
105        String getValidLineSeparatorValues() {
106            return getValidValues(LINESEPARATOR_VALUES.keySet());
107        }
108    
109        /**
110         * Performs the validation of the element at the time when the whole build file was
111         * parsed checking the content of the element and possibly adding mandatory variables
112         * with default settings.
113         * 
114         * @since Ant-Eclipse 1.0
115         */
116        public void validate() {
117            if (!hasVariable(LINESEPARATOR_NAME))
118                throw new BuildException("The attribute \"" + LINESEPARATOR_ATTRIBUTE
119                        + "\" (variable \"" + LINESEPARATOR_NAME
120                        + "\") was missing in the element \"" + ELEMENT + "\".");
121            super.validate();
122        }
123    
124    }