001/*
002// $Id: //open/util/resgen/src/org/eigenbase/resgen/ResourceGen.java#7 $
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
024package org.eigenbase.resgen;
025import org.eigenbase.xom.DOMWrapper;
026
027import java.io.File;
028import java.io.IOException;
029
030/**
031 * <code>ResourceGen</code> parses an XML file containing error messages, and
032 * generates .java file to access the errors. Usage:<blockquote>
033 *
034 * <pre>ResourceGen xmlFile</pre>
035 *
036 * </blockquote>For example,<blockquote>
037 *
038 * <pre>java org.eigenbase.resgen.ResourceGen MyResource_en.xml</pre>
039 *
040 * </blockquote></p>
041 *
042 * <p>This will create class <code>MyResource</code>, with a
043 * function corresponding to each error message in
044 * <code>MyResource_en.xml</code>.</p>
045 *
046 * <p>See also the ANT Task, {@link ResourceGenTask}.</p>
047 *
048 * @author jhyde
049 * @since 3 December, 2001
050 * @version $Id: //open/util/resgen/src/org/eigenbase/resgen/ResourceGen.java#7 $
051 */
052public class ResourceGen
053{
054
055    public static void main(String [] args) throws IOException
056    {
057        ResourceGenTask rootArgs = parse(args);
058        new ResourceGen().run(rootArgs);
059    }
060
061    static ResourceGenTask parse(String[] args)
062    {
063        ResourceGenTask rootArgs = new ResourceGenTask();
064        for (int i = 0; i < args.length; i++) {
065            String arg = args[i];
066            if (arg.equals("-mode") && i + 1 < args.length) {
067                rootArgs.setMode(args[++i]);
068            } else if (arg.equals("-srcdir") && i + 1 < args.length) {
069                rootArgs.setSrcdir(new File(args[++i]));
070            } else if (arg.equals("-destdir") && i + 1 < args.length) {
071                rootArgs.setDestdir(new File(args[++i]));
072            } else if (arg.equals("-resdir") && i + 1 < args.length) {
073                rootArgs.setResdir(new File(args[++i]));
074            } else if (arg.equals("-locales") && i + 1 < args.length) {
075                rootArgs.setLocales(args[++i]);
076            } else if (arg.equals("-style") && i + 1 < args.length) {
077                rootArgs.setStyle(args[++i]);
078            } else if (arg.equals("-force")) {
079                rootArgs.setForce(true);
080            } else if (arg.equals("-commentstyle")) {
081                rootArgs.setCommentStyle(args[++i]);
082            } else {
083                ResourceGenTask.Include resourceArgs =
084                        new ResourceGenTask.Include();
085                rootArgs.addInclude(resourceArgs);
086                resourceArgs.setName(arg);
087            }
088        }
089        if (rootArgs.getIncludes().length == 0) {
090            throw new java.lang.Error("No input file specified.");
091        }
092        if (rootArgs.getDestdir() == null) {
093            rootArgs.setDestdir(rootArgs.getSrcdir());
094        }
095        return rootArgs;
096    }
097
098    void run(ResourceGenTask rootArgs) throws IOException {
099        rootArgs.validate();
100        final ResourceGenTask.Include[] includes = rootArgs.getIncludes();
101        for (int i = 0; i < includes.length; i++) {
102            includes[i].process(this);
103        }
104    }
105
106    /**
107     * Prints a message to the output stream.
108     */
109    void comment(String message)
110    {
111        System.out.println(message);
112    }
113
114    /**
115     * Returns the name of the resource with the first letter capitalized,
116     * suitable for use in method names. For example, "MyErrorMessage".
117     */
118    static String getResourceInitcap(ResourceDef.Resource resource)
119    {
120        String name = resource.name;
121        if (name.equals(name.toUpperCase())) {
122            return "_" + name;
123        } else {
124            return name.substring(0,1).toUpperCase() + name.substring(1);
125        }
126    }
127
128    /**
129     * Returns any comment relating to the message.
130     */
131    static String getComment(ResourceDef.Resource resource)
132    {
133        DOMWrapper[] children = resource.getDef().getChildren();
134        for (int i = 0; i < children.length; i++) {
135            DOMWrapper child = children[i];
136            if (child.getType() == DOMWrapper.COMMENT) {
137                return child.getText(); // first comment only
138            }
139        }
140        return null; // no comment
141    }
142
143    FileTask createXmlTask(
144            ResourceGenTask.Include include, String fileName, String className,
145            String baseClassName, boolean outputJava, String cppClassName,
146            String cppBaseClassName, boolean outputCpp)
147    {
148        return new XmlFileTask(
149            include, fileName, className, baseClassName,
150            outputJava, cppClassName, cppBaseClassName,
151            outputCpp);
152    }
153
154    FileTask createPropertiesTask(
155            ResourceGenTask.Include include, String fileName) {
156        return new PropertiesFileTask(include, fileName);
157    }
158
159}
160
161// End ResourceGen.java