harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r468163 - in /incubator/harmony/enhanced/tools/trunk/msgstool: ./ src/ src/org/ src/org/apache/ src/org/apache/harmony/ src/org/apache/harmony/tools/ src/org/apache/harmony/tools/msgstool/
Date Thu, 26 Oct 2006 21:58:00 GMT
Author: tellison
Date: Thu Oct 26 14:57:59 2006
New Revision: 468163

URL: http://svn.apache.org/viewvc?view=rev&rev=468163
Log:
Apply HARMONY-1041 ([classlib]Generation tool for internationalization classes)


Added:
    incubator/harmony/enhanced/tools/trunk/msgstool/
    incubator/harmony/enhanced/tools/trunk/msgstool/README   (with props)
    incubator/harmony/enhanced/tools/trunk/msgstool/build.xml   (with props)
    incubator/harmony/enhanced/tools/trunk/msgstool/src/
    incubator/harmony/enhanced/tools/trunk/msgstool/src/org/
    incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/
    incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/
    incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/
    incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/
    incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/Main.java
  (with props)
    incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/Messages.tpl
    incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/MsgClassGenerator.java
  (with props)
    incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/messages.properties
  (with props)
    incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/modules.properties
  (with props)

Added: incubator/harmony/enhanced/tools/trunk/msgstool/README
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/tools/trunk/msgstool/README?view=auto&rev=468163
==============================================================================
--- incubator/harmony/enhanced/tools/trunk/msgstool/README (added)
+++ incubator/harmony/enhanced/tools/trunk/msgstool/README Thu Oct 26 14:57:59 2006
@@ -0,0 +1,57 @@
+DESCRIPTION
+
+This msgstool helps to generate Messages.java and MsgUtil.java classes source files
+for modules listed in the property file modules.properties. Also initial empty 
+messages.property with Apache copyright in the heading is copied.
+
+CONTENTS
+
+Tool consist of the source files, template files, properties files and this
+README file:
+
+./org/apache/harmony/tools/msgstool/README
+./org/apache/harmony/tools/msgstool/Main.java 
+./org/apache/harmony/tools/msgstool/MsgClassGenerator.java 
+./org/apache/harmony/tools/msgstool/Messages.tpl
+./org/apache/harmony/tools/msgstool/messages.properties
+./org/apache/harmony/tools/msgstool/modules.properties
+
+USING
+
+To see a usage of the msgstool you need to:
+1. compile source files with Ant utility (call ant)
+2. go to ./bin directory execute the following command from a command line:
+
+java -jar msgstool.jar [options]
+
+where [options]:
+
+    -help                   Print help message
+
+    -d <dst path>           Destination path where to copy
+                            generated files that will be
+                            created if necessary.If this option is not specified
+                            current user dir is used.
+
+    -m <modules>            Property file, where 
+                            the list of modules is specified.
+                            If this option is not specified
+                            default ./org/apache/harmony/tools/msgstool/modules.properties
+                            file is used.
+
+After msgstool execution for each module from the list defined in the modules.properties

+file there will be generated next source files:
+<dst path>/modules/<module>/src/main/java/[common/]org/apache/harmony/<module>/internal/nls/Messages.java
+<dst path>/modules/<module>/src/main/java/[common/]org/apache/harmony/<module>/internal.nls/messages.properties
+
+COMMENTS
+
+1. "<dst path>/modules/<module>/src/main/java/[common/]" path corresponds to
the java src files
+path structure used for modules in Apache Harmony project.
+If "common" subdirerctory exists - java src files generated relative to this folder.
+2. Generated Messages.java source file replace existing one, while 
+messages.properties doesn't to avoid deletion of the externilized messages.
+
+
+    
+

Propchange: incubator/harmony/enhanced/tools/trunk/msgstool/README
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/tools/trunk/msgstool/build.xml
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/tools/trunk/msgstool/build.xml?view=auto&rev=468163
==============================================================================
--- incubator/harmony/enhanced/tools/trunk/msgstool/build.xml (added)
+++ incubator/harmony/enhanced/tools/trunk/msgstool/build.xml Thu Oct 26 14:57:59 2006
@@ -0,0 +1,57 @@
+<?xml version="1.0"?>
+
+<project name="msgstool" default="jar" basedir=".">
+
+    <target name="prepare.build">
+        <property file="${basedir}/build.properties" />
+
+        <property name="java.compile.debug" value="false" />
+        <property name="java.compile.optimize" value="true" />
+        <property name="java.compile.deprecation" value="false" />
+        <property name="java.compile.verbose" value="false" />
+
+        <property name="dir.build" location="${basedir}/bin" />
+        <property name="dir.build.classes" location="${dir.build}/classes" />
+
+        <path id="id.classpath">
+            <pathelement path="${dir.build.classes}"/>
+        </path>
+
+        <tstamp />
+        <mkdir dir="${dir.build}" />
+        <mkdir dir="${dir.build.classes}" />
+    </target>
+
+    <target name="build" depends="prepare.build">
+
+        <javac srcdir="${basedir}" 
+               destdir="${dir.build.classes}" 
+               deprecation="${java.compile.deprecation}" 
+               debug="${java.compile.debug}" 
+               optimize="${java.compile.optimize}" 
+               verbose="${java.compile.verbose}">
+
+            <classpath refid="id.classpath" />
+
+            <include name="src/**" />
+        </javac>
+    
+        <copy todir="${dir.build.classes}">
+            <fileset dir="${basedir}/src" casesensitive="yes">
+                <exclude name="**/*.java"/>
+            </fileset>
+        </copy>
+
+    </target>
+
+    <target name="jar" depends="build">
+
+        <jar destfile="${dir.build}/msgstool.jar" basedir="${dir.build.classes}">
+            <manifest>
+                <attribute name="Main-Class" value="org.apache.harmony.tools.msgstool.Main"
/>
+            </manifest>
+        </jar>
+
+    </target>
+
+</project>

Propchange: incubator/harmony/enhanced/tools/trunk/msgstool/build.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/Main.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/Main.java?view=auto&rev=468163
==============================================================================
--- incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/Main.java
(added)
+++ incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/Main.java
Thu Oct 26 14:57:59 2006
@@ -0,0 +1,84 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.tools.msgstool;
+
+import java.io.File;
+
+
+/**
+ * This is the entry point for the msgtool tool.
+ */
+public final class Main {
+    public static final String HELP_MSG = "-help";
+    
+    /**
+     * Prints the usage information.
+     */
+    public static void printUsage() {
+        System.out.println("Usage: " + MsgClassGenerator.class.getName()
+                + " [options]");
+        System.out.println();
+        System.out.println("[options]");
+        System.out.println();
+        System.out.println("    -help                   Print help message");
+        System.out.println();
+        System.out.println("    -d <dst path>           Destination path where to copy");
+        System.out.println("                            generated files that will be");
+        System.out.println("                            created if necessary.");
+        System.out.println("                            If this option is not specified");
+        System.out.println("                            current user dir is used.");
+        System.out.println();
+        System.out.println("    -m <modules>            Property file, where ");
+        System.out.println("                            the list of modules is specified.");
+        System.out.println("                            If this option is not specified");
+        System.out.println("                            default modules.properties file is
used.");
+        System.out.println();
+    }
+
+    public static void main(String[] args) {
+        File propFile = null;
+        File dstDir = null;
+        
+        int i = 0;
+        while (i < args.length) {
+            if (args[i].equals("-help")) {
+                printUsage();
+                return;
+            } 
+            
+            if (args[i].equals("-d")) {
+                i++;
+                dstDir = new File(args[i]);
+            } else if (args[i].equals("-m")) {
+                i++;
+                propFile = new File(args[i]);
+            }
+            i++;
+        }
+
+
+        /* Invoke the messages classes generator */
+        MsgClassGenerator.run(dstDir, propFile);
+    }
+
+    /**
+     * Default constructor.
+     */
+    public Main() {
+        super();
+    }
+
+}

Propchange: incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/Main.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/Messages.tpl
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/Messages.tpl?view=auto&rev=468163
==============================================================================
--- incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/Messages.tpl
(added)
+++ incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/Messages.tpl
Thu Oct 26 14:57:59 2006
@@ -0,0 +1,241 @@
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * THE FILE HAS BEEN AUTOGENERATED BY MSGTOOL TOOL.
+ * All changes made to this file manually will be overwritten 
+ * if this tool runs again. Better make changes in the template file.
+ */
+
+package org.apache.harmony.<module>.internal.nls;
+
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+import org.apache.harmony.kernel.vm.VM;
+
+/**
+ * This class retrieves strings from a resource bundle and returns them,
+ * formatting them with MessageFormat when required.
+ * <p>
+ * It is used by the system classes to provide national language support, by
+ * looking up messages in the <code>
+ *    org.apache.harmony.<module>.internal.nls.messages
+ * </code>
+ * resource bundle. Note that if this file is not available, or an invalid key
+ * is looked up, or resource bundle support is not available, the key itself
+ * will be returned as the associated message. This means that the <em>KEY</em>
+ * should a reasonable human-readable (english) string.
+ * 
+ */
+public class Messages {
+
+    // ResourceBundle holding the system messages.
+    static private ResourceBundle bundle = null;
+
+    /**
+     * Retrieves a message which has no arguments.
+     * 
+     * @param msg
+     *            String the key to look up.
+     * @return String the message for that key in the system message bundle.
+     */
+    static public String getString(String msg) {
+        if (bundle == null)
+            return msg;
+        try {
+            return bundle.getString(msg);
+        } catch (MissingResourceException e) {
+            return "Missing message: " + msg; //$NON-NLS-1$
+        }
+    }
+
+    /**
+     * Retrieves a message which takes 1 argument.
+     * 
+     * @param msg
+     *            String the key to look up.
+     * @param arg
+     *            Object the object to insert in the formatted output.
+     * @return String the message for that key in the system message bundle.
+     */
+    static public String getString(String msg, Object arg) {
+        return getString(msg, new Object[] { arg });
+    }
+
+    /**
+     * Retrieves a message which takes 1 integer argument.
+     * 
+     * @param msg
+     *            String the key to look up.
+     * @param arg
+     *            int the integer to insert in the formatted output.
+     * @return String the message for that key in the system message bundle.
+     */
+    static public String getString(String msg, int arg) {
+        return getString(msg, new Object[] { Integer.toString(arg) });
+    }
+
+    /**
+     * Retrieves a message which takes 1 character argument.
+     * 
+     * @param msg
+     *            String the key to look up.
+     * @param arg
+     *            char the character to insert in the formatted output.
+     * @return String the message for that key in the system message bundle.
+     */
+    static public String getString(String msg, char arg) {
+        return getString(msg, new Object[] { String.valueOf(arg) });
+    }
+
+    /**
+     * Retrieves a message which takes 2 arguments.
+     * 
+     * @param msg
+     *            String the key to look up.
+     * @param arg1
+     *            Object an object to insert in the formatted output.
+     * @param arg2
+     *            Object another object to insert in the formatted output.
+     * @return String the message for that key in the system message bundle.
+     */
+    static public String getString(String msg, Object arg1, Object arg2) {
+        return getString(msg, new Object[] { arg1, arg2 });
+    }
+
+    /**
+     * Retrieves a message which takes several arguments.
+     * 
+     * @param msg
+     *            String the key to look up.
+     * @param args
+     *            Object[] the objects to insert in the formatted output.
+     * @return String the message for that key in the system message bundle.
+     */
+    static public String getString(String msg, Object[] args) {
+        String format = msg;
+
+        if (bundle != null) {
+            try {
+                format = bundle.getString(msg);
+            } catch (MissingResourceException e) {
+            }
+        }
+
+        return format(format, args);
+    }
+    
+    /**
+     * Generates a formatted text string given a source string containing
+     * "argument markers" of the form "{argNum}" where each argNum must be in
+     * the range 0..9. The result is generated by inserting the toString of each
+     * argument into the position indicated in the string.
+     * <p>
+     * To insert the "{" character into the output, use a single backslash
+     * character to escape it (i.e. "\{"). The "}" character does not need to be
+     * escaped.
+     * 
+     * @param format
+     *            String the format to use when printing.
+     * @param args
+     *            Object[] the arguments to use.
+     * @return String the formatted message.
+     */
+    public static String format(String format, Object[] args) {
+        StringBuilder answer = new StringBuilder(format.length()
+                + (args.length * 20));
+        String[] argStrings = new String[args.length];
+        for (int i = 0; i < args.length; ++i) {
+            if (args[i] == null)
+                argStrings[i] = "<null>";	//$NON-NLS-1$
+            else
+                argStrings[i] = args[i].toString();
+        }
+        int lastI = 0;
+        for (int i = format.indexOf('{', 0); i >= 0; i = format.indexOf('{',
+                lastI)) {
+            if (i != 0 && format.charAt(i - 1) == '\\') {
+                // It's escaped, just print and loop.
+                if (i != 1)
+                    answer.append(format.substring(lastI, i - 1));
+                answer.append('{');
+                lastI = i + 1;
+            } else {
+                // It's a format character.
+                if (i > format.length() - 3) {
+                    // Bad format, just print and loop.
+                    answer.append(format.substring(lastI, format.length()));
+                    lastI = format.length();
+                } else {
+                    int argnum = (byte) Character.digit(format.charAt(i + 1),
+                            10);
+                    if (argnum < 0 || format.charAt(i + 2) != '}') {
+                        // Bad format, just print and loop.
+						answer.append(format.substring(lastI, i + 1));
+						lastI = i + 1;
+                    } else {
+                        // Got a good one!
+                        answer.append(format.substring(lastI, i));
+                        if (argnum >= argStrings.length)
+                            answer.append("<missing argument>");	//$NON-NLS-1$
+                        else
+                            answer.append(argStrings[argnum]);
+						lastI = i + 3;
+                    }
+                }
+            }
+        }
+        if (lastI < format.length())
+            answer.append(format.substring(lastI, format.length()));
+        return answer.toString();
+    }
+
+    /**
+     * Changes the locale of the messages.
+     * 
+     * @param locale
+     *            Locale the locale to change to.
+     */
+    static public ResourceBundle setLocale(final Locale locale,
+            final String resource) {
+        try {
+            final ClassLoader loader = VM.bootCallerClassLoader();
+            return (ResourceBundle) AccessController
+                    .doPrivileged(new PrivilegedAction<Object>() {
+                        public Object run() {
+                            return ResourceBundle.getBundle(resource, locale,
+                                    loader != null ? loader : ClassLoader.getSystemClassLoader());
+                        }
+                    });
+        } catch (MissingResourceException e) {
+        }
+        return null;
+    }
+
+    static {
+        // Attempt to load the messages.
+        try {
+            bundle = setLocale(Locale.getDefault(),
+                    "org.apache.harmony.<module>.internal.nls.messages"); //$NON-NLS-1$
+        } catch (Throwable e) {
+            e.printStackTrace();
+        }
+    }
+}

Added: incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/MsgClassGenerator.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/MsgClassGenerator.java?view=auto&rev=468163
==============================================================================
--- incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/MsgClassGenerator.java
(added)
+++ incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/MsgClassGenerator.java
Thu Oct 26 14:57:59 2006
@@ -0,0 +1,367 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.tools.msgstool;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.FilenameFilter;
+import java.io.InputStream;
+import java.util.Enumeration;
+import java.util.Properties;
+
+/**
+ * MsgClassGenerator class helps to generate nesessary source files and
+ * properties files to support messages internationalization task. See also
+ * msgstool README for details.
+ */
+public class MsgClassGenerator {
+
+    /* Word for replacement in templates */
+    private static final String MODULE_PATTERN_NAME = "<module>";
+
+    /* Messages.java file name without extension */
+    private static final String MSG_CLASS_NAME = "Messages";
+
+    /* messages.properties file name */
+    private static final String MSGS_PROPERTIES_NAME = "messages.properties";
+
+    /* Default modules.properties file name */
+    private static final String MODULES_PROPERTIES_NAME = "modules.properties";
+
+    /* Pattern files extension */
+    private static final String PATTERN_EXT_NAME = ".tpl";
+
+    /* Source files extension */
+    private static final String JAVA_EXT_NAME = ".java";
+
+    /* user.dir property value with file separator */
+    private static final String userDir = System.getProperty("user.dir")
+            + File.separatorChar;
+
+    /* entry path in the msgstool.jar file - common for all nesessary entries */
+    private static final String ENTRY_PATH = MsgClassGenerator.class
+            .getPackage().getName().replace('.', '/') + '/';
+
+    /* messages.properties File object */
+    private static final File MSGS_PROPERTIES_FILE = getResourceFromJar(
+            ENTRY_PATH, MSGS_PROPERTIES_NAME);
+
+    /* Messages template File object */
+    private static final File MESSAGES_TEMPLATE_FILE = getResourceFromJar(
+            ENTRY_PATH, MSG_CLASS_NAME + PATTERN_EXT_NAME);
+
+    /* Destination dir to store generation results */
+    private File dstDir = null;
+
+    /**
+     * Default constructor.
+     */
+    public MsgClassGenerator() {
+        super();
+    }
+
+    /**
+     * Generates files for internationalization task to the specified directory
+     * for the modules listed in specified property file.
+     * 
+     * @param dir -
+     *            destination folder
+     * @param props -
+     *            file with properties list that is the list of modules names
+     *            that are to be processed.
+     * @throws Exception
+     */
+    public void generate(File dir, File props) {
+        File modProps;
+        if (props == null) {
+            modProps = getResourceFromJar(ENTRY_PATH,
+                    MODULES_PROPERTIES_NAME);
+            modProps.deleteOnExit();
+        } else {
+            modProps = props;
+        }
+
+        dstDir = (dir != null) ? dir : new File(userDir);
+        Properties prs = null;
+        FileInputStream fis = null;
+        int fails = 0; // number of fails
+
+        try {
+            fis = new FileInputStream(modProps);
+        } catch (FileNotFoundException e) {
+            logMessage("Generation error: Specified file " + modProps.getPath()
+                    + " doesn't exist!");
+            return;
+        }
+        prs = new Properties();
+        try {
+            prs.load(fis);
+            fis.close();
+        } catch (Exception e) {
+            logMessage("\nGeneration error:" + e.getMessage());
+        }
+
+        logMessage("\nGeneration started:\n");
+        logMessage("Modules generation status: ");
+
+        Enumeration en = prs.keys();
+        while (en.hasMoreElements()) {
+            String modName = prs.getProperty((String) en.nextElement()).trim();
+            logMessage(modName + " : ");
+            try {
+                createSourceFromTemplate(modName, MESSAGES_TEMPLATE_FILE,
+                        MSG_CLASS_NAME);
+                createMsgsProperties(modName);
+            } catch (Exception e) {
+                logMessage("error: " + e.getMessage());
+                fails++;
+                continue;
+            }
+
+            logMessage("completed");
+        }
+
+        if (fails == 0) {
+            logMessage("\nGeneration successfully finished!");
+        } else {
+            logMessage("\nGeneration finished with " + fails + " fails!");
+            logMessage("See output information for details!");
+        }
+    }
+
+    /**
+     * Writes text to destination file from the source file with replacing
+     * specified word in source file to the moduleName value.
+     * 
+     * @param srcFile -
+     *            source file
+     * @param dstFile -
+     *            destination file
+     * @param moduleName -
+     *            string to replace
+     * @throws FileNotFoundException
+     *             if src template or dst source file doesn't exist.
+     * @throws IOException
+     *             if an I/O error occurred.
+     */
+    private void templateReplace(File srcFile, File dstFile, String moduleName)
+            throws Exception {
+        BufferedReader br = new BufferedReader(new FileReader(srcFile));
+        BufferedWriter bw = new BufferedWriter(new FileWriter(dstFile));
+
+        String s;
+        while ((s = br.readLine()) != null) {
+            if (s.indexOf(MODULE_PATTERN_NAME) != -1) {
+                s = s.replaceAll(MODULE_PATTERN_NAME, moduleName);
+            }
+            bw.write(s);
+            bw.newLine();
+        }
+
+        bw.close();
+        br.close();
+
+    }
+
+    /**
+     * Creates java source file from the template file with specified
+     * templateName and place it in the "<code>dstDir</code>\modules\<code>moduleName</code>\src\main\java\org\apache\
+     * harmony\<code>moduleName</code>\internal\nls" folder.
+     * 
+     * @param moduleName
+     *            name of the module
+     * @param srcFile
+     *            template File objet
+     * @param templateName
+     *            name of the template file
+     * @throws FileNotFoundException
+     *             if src template or dst source file doesn't exist.
+     * @throws IOException
+     *             if an I/O error occurred.
+     */
+    private void createSourceFromTemplate(String moduleName, File srcFile,
+            String templateName) throws Exception {
+        /* Open destination file */
+        File dstFile = new File(getDstDir(moduleName), templateName + JAVA_EXT_NAME);
+        dstFile.createNewFile();
+        logMessage("\t" + dstFile.getAbsolutePath());
+        templateReplace(srcFile, dstFile, getPackageFromModule(moduleName));
+
+    }
+
+    /**
+     * Copies messages.properties file to the "<code>dstDir</code>\modules\<code>moduleName</code>\src\main\java\org\apache\
+     * harmony\<code>moduleName</code>\internal\nls" folder. If another
+     * messages.file already exists nothing is done.
+     * 
+     * @param moduleName
+     *            name of the module
+     * @throws FileNotFoundException
+     *             if src or dst messages.properties file doesn't exist.
+     * @throws IOException
+     *             if an I/O error occurred.
+     */
+    private void createMsgsProperties(String moduleName) throws Exception {
+        
+        File dstFile = new File(getDstDir(moduleName), MSGS_PROPERTIES_NAME);
+
+        if (dstFile.exists()) {
+            /* Existing messages.properties file isn't to be overridden */
+            return;
+
+        }
+
+        /* Open pattern file */
+        FileInputStream fis = new FileInputStream(MSGS_PROPERTIES_FILE);
+        FileOutputStream fos = new FileOutputStream(dstFile);
+
+        byte[] data = new byte[fis.available()];
+        while (fis.read(data) > 0) {
+            fos.write(data);
+            data = new byte[fis.available()];
+        }
+
+        fis.close();
+        fos.close();
+
+        logMessage("\t" + dstFile.getAbsolutePath());
+    }
+
+    /**
+     * Returns destination dir for output. Directory is created if it wasn't
+     * exist. 
+     * 
+     * @param moduleName - module name
+     * @return File object that is the dir for output.
+     */
+    private File getDstDir(String moduleName){
+        // Path to the source code dir
+        File srcDir = new File(dstDir, "modules/" + moduleName
+                + "/src/main/java");
+        
+        // Some modules have platform dependent code, thus source code dir
+        // splitted into the common dir ("common") and platform dependent dirs. 
+        if (srcDir.exists() && srcDir.isDirectory()){
+            String names[] = srcDir.list(new FilenameCommonFilter());
+            /* If there is a "common" subdir - choose it */
+            if (names.length > 0){
+                srcDir = new File(srcDir, names[0]);
+            }
+        }
+        
+        // Path to the generated source files from the source dir 
+        String path = "org/apache/harmony/" + getPackageFromModule(moduleName)
+                + "/internal/nls";
+        
+        File dir = new File(srcDir, path);
+        dir.mkdirs();
+        
+        return dir;
+    }
+    /**
+     * Convenient way to run generation for the specified destination directory
+     * and properties file with the list of modules which are to be processed.
+     * 
+     * @param dir -
+     *            destination dir
+     * @param props -
+     *            properties file
+     * @throws Exception
+     */
+    public static void run(File dir, File props) {
+        new MsgClassGenerator().generate(dir, props);
+    }
+
+    /**
+     * Returns File object from the msgstool.jar JAR file according to the entry
+     * name specified.
+     * 
+     * @param entryName -
+     *            entry name
+     * @param fileName - resource file name
+     * @return new File object from the JAR file corresponding to the given
+     *         entry name
+     */
+    private static File getResourceFromJar(String entryPath, String fileName) {
+        try {
+            InputStream jis = MsgClassGenerator.class.getClassLoader()
+                    .getResourceAsStream(entryPath + fileName);
+            if (jis == null){
+                logMessage("Error: there is no " + entryPath + fileName
+                        + " in the resources!");
+                return null;
+            }
+            File resFile = new File(fileName);
+            resFile.createNewFile();
+            resFile.deleteOnExit();
+
+            FileOutputStream fos = new FileOutputStream(resFile);
+
+            byte[] data = new byte[jis.available()];
+            int i;
+            while ((i = jis.read(data)) > 0) {
+                fos.write(data, 0 , i);
+                data = new byte[jis.available()];
+            }
+
+            jis.close();
+            fos.close();
+
+            return resFile;
+
+        } catch (Exception e) {
+            logMessage("Couldn't extract resource!");
+            e.printStackTrace();
+            return null;
+        }
+
+    }
+    
+    /**
+     * Returns package name from the given module name.
+     * Some modules have package names differing from their names, 
+     * these names are to be formatted.
+     * E.g. 'nio_char' or 'x-net' would have package names o.a.h.niochar. 
+     * and o.a.h.xnet respectively. 
+     * @param modName name of the module.
+     */
+    private String getPackageFromModule(String modName){
+        String pkgName = modName;
+        return pkgName.replaceAll("[_,-]", "");
+    }
+    
+    public static void logMessage(String msg) {
+        System.out.println(msg);
+    }
+    
+    private class FilenameCommonFilter implements FilenameFilter{
+
+        public boolean accept(File dir, String name) {
+            if (name.equals("common")){
+                File commonDir = new File(dir, name);
+                return commonDir.isDirectory();
+            }
+            return false;
+        }
+        
+    }
+}

Propchange: incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/MsgClassGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/messages.properties
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/messages.properties?view=auto&rev=468163
==============================================================================
--- incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/messages.properties
(added)
+++ incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/messages.properties
Thu Oct 26 14:57:59 2006
@@ -0,0 +1,16 @@
+# Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
+#  
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at
+#  
+#      http://www.apache.org/licenses/LICENSE-2.0
+#  
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+# 
+
+# messages for EN locale
\ No newline at end of file

Propchange: incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/messages.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/modules.properties
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/modules.properties?view=auto&rev=468163
==============================================================================
--- incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/modules.properties
(added)
+++ incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/modules.properties
Thu Oct 26 14:57:59 2006
@@ -0,0 +1,29 @@
+# Default set of modules
+module.00=luni
+module.01=security
+module.03=auth
+module.04=awt
+module.05=nio
+module.06=prefs
+module.07=regex
+module.08=rmi
+module.09=security
+module.0A=sql
+module.0B=swing
+module.0C=text
+module.0D=tools
+module.0E=math
+module.OF=misc
+module.10=annotation
+module.11=applet
+module.12=archive
+module.13=beans
+module.14=crypto
+module.15=jndi
+module.16=logging
+module.17=accessibility
+module.18=instrument
+module.19=sound
+module.1A=x-net
+module.1B=nio_char
+

Propchange: incubator/harmony/enhanced/tools/trunk/msgstool/src/org/apache/harmony/tools/msgstool/modules.properties
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message