001    /*
002    // $Id: //open/util/resgen/src/org/eigenbase/resgen/AbstractJavaGenerator.java#3 $
003    // Package org.eigenbase.resgen is an i18n resource generator.
004    // Copyright (C) 2005-2005 The Eigenbase Project
005    // Copyright (C) 2005-2005 Disruptive Tech
006    // Copyright (C) 2005-2005 LucidEra, Inc.
007    // Portions Copyright (C) 2001-2005 Kana Software, Inc. and others.
008    //
009    // This library is free software; you can redistribute it and/or modify it
010    // under the terms of the GNU Lesser General Public License as published by the
011    // Free Software Foundation; either version 2 of the License, or (at your
012    // option) any later version approved by The Eigenbase Project.
013    //
014    // This library is distributed in the hope that it will be useful, 
015    // but WITHOUT ANY WARRANTY; without even the implied warranty of
016    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017    // GNU Lesser General Public License for more details.
018    // 
019    // You should have received a copy of the GNU Lesser General Public License
020    // along with this library; if not, write to the Free Software
021    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022    */
023    package org.eigenbase.resgen;
024    
025    import java.io.PrintWriter;
026    import java.io.File;
027    
028    /**
029     * Abstract base for all generators which generate Java code.
030     *
031     * @author jhyde
032     * @since 19 September, 2005
033     * @version $Id: //open/util/resgen/src/org/eigenbase/resgen/AbstractJavaGenerator.java#3 $
034     */
035    abstract class AbstractJavaGenerator extends AbstractGenerator
036    {
037        protected final String className;
038        protected final ResourceDef.ResourceBundle resourceBundle;
039        protected final String baseClassName;
040    
041        private static final String JAVA_STRING = "String";
042        private static final String JAVA_NUMBER = "Number";
043        private static final String JAVA_DATE_TIME = "java.util.Date";
044        private static final String[] JAVA_TYPE_NAMES =
045            {JAVA_STRING, JAVA_NUMBER, JAVA_DATE_TIME, JAVA_DATE_TIME};
046    
047        AbstractJavaGenerator(
048            File srcFile,
049            File file,
050            String className,
051            ResourceDef.ResourceBundle resourceBundle,
052            String baseClassName)
053        {
054            super(srcFile, file);
055            this.className = className;
056            this.baseClassName = baseClassName;
057            this.resourceBundle = resourceBundle;
058        }
059    
060        /**
061         * Returns the type of error which is to be thrown by this resource.
062         * Result is null if this is not an error.
063         */
064        protected String getErrorClass(
065                ResourceDef.Exception exception) {
066            if (exception.className != null) {
067                return exception.className;
068            } else if (resourceBundle.exceptionClassName != null) {
069                return resourceBundle.exceptionClassName;
070            } else {
071                return "java.lang.RuntimeException";
072            }
073        }
074    
075        protected String getPackageName()
076        {
077            int lastDot = className.lastIndexOf('.');
078            if (lastDot < 0) {
079                return null;
080            } else {
081                return className.substring(0,lastDot);
082            }
083        }
084    
085        protected String[] getArgTypes(String message) {
086            return ResourceDefinition.getArgTypes(message, JAVA_TYPE_NAMES);
087        }
088    
089        protected void generateHeader(PrintWriter pw) {
090            generateDoNotModifyHeader(pw);
091            String packageName = getPackageName();
092            if (packageName != null) {
093                pw.println("package " + packageName + ";");
094            }
095            pw.println("import java.io.IOException;");
096            pw.println("import java.util.Locale;");
097            pw.println("import java.util.ResourceBundle;");
098            pw.println("import org.eigenbase.resgen.*;");
099            pw.println();
100            generateGeneratedByBlock(pw);
101        }
102    
103        protected void generateFooter(PrintWriter pw, String className) {
104            pw.println("// End " + className + ".java");
105        }
106    
107        protected String getClassName()
108        {
109            return className;
110        }
111    
112        protected String getBaseClassName()
113        {
114            return baseClassName;
115        }
116    }
117    
118    // End AbstractJavaGenerator.java