001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.infra.build.ant;
038:
039: import java.io.File;
040: import java.io.FileInputStream;
041: import java.io.IOException;
042: import java.util.Locale;
043: import java.util.Properties;
044: import org.apache.tools.ant.BuildException;
045: import org.apache.tools.ant.Task;
046: import org.netbeans.installer.infra.build.ant.utils.Utils;
047:
048: /**
049: * This class is an ant task which is capable of loading localized properties data.
050: *
051: * @author Kirill Sorokin
052: */
053: public class LoadLocales extends Task {
054: /////////////////////////////////////////////////////////////////////////////////
055: // Instance
056: /**
057: * The basename of the .properties file.
058: */
059: private String basename;
060:
061: /**
062: * Name of the property whose value should be set to the list of found locales.
063: */
064: private String localesList;
065:
066: // setters //////////////////////////////////////////////////////////////////////
067: /**
068: * Setter for the 'basename' property.
069: *
070: * @param basename New value for the 'basename' property.
071: */
072: public void setBasename(final String basename) {
073: this .basename = basename;
074: final File basenameFile = new File(basename);
075: if (!(basenameFile.equals(basenameFile.getAbsoluteFile()))) {
076: this .basename = new File(getProject().getBaseDir(),
077: basename).getPath();
078: }
079: }
080:
081: /**
082: * Setter for the 'localesList' property.
083: *
084: * @param localesList New value for the 'localesList' property.
085: */
086: public void setList(final String localesList) {
087: this .localesList = localesList;
088: }
089:
090: // execution ////////////////////////////////////////////////////////////////////
091: /**
092: * Executes the task. The properties are loaded from the resource file with the
093: * given base name and the corresponding project properties are set.
094: *
095: * @throws org.apache.tools.ant.BuildException if an I/O error occurs.
096: */
097: public void execute() throws BuildException {
098: Utils.setProject(getProject());
099:
100: String locales = ""; // NOI18N
101:
102: try {
103: // handle the default locale
104: File file = new File(basename + ".properties"); // NOI18N
105:
106: Properties properties = new Properties();
107:
108: properties.load(new FileInputStream(file));
109: for (Object key : properties.keySet()) {
110: getProject().setProperty(key + ".default", // NOI18N
111: properties.get(key).toString());
112: }
113:
114: for (Locale locale : Locale.getAvailableLocales()) {
115: file = new File(basename + "_" + locale + ".properties"); // NOI18N
116: if (file.exists()) {
117: locales += " " + locale; // NOI18N
118: properties = new Properties();
119: properties.load(new FileInputStream(file));
120:
121: for (Object key : properties.keySet()) {
122: getProject().setProperty(
123: "" + key + "." + locale, // NOI18N
124: Utils.toAscii(properties.get(key)
125: .toString()));
126: }
127: }
128: }
129: } catch (IOException e) {
130: throw new BuildException(e);
131: }
132:
133: getProject().setProperty(localesList, locales.trim());
134: }
135: }
|