1 | /* |
2 | * Copyright 2005-2006 The RbUtils Project |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | * |
16 | */ |
17 | |
18 | // $Id: RbGeneratorUtility.java,v 1.7 2008/07/02 11:30:39 moishi Exp $ |
19 | |
20 | package org.ktc.rbutils.rb.generation; |
21 | |
22 | import java.io.File; |
23 | import java.io.FileNotFoundException; |
24 | import java.io.PrintWriter; |
25 | import java.util.Collection; |
26 | import java.util.List; |
27 | |
28 | import org.apache.commons.cli.Option; |
29 | import org.apache.commons.cli.Options; |
30 | import org.ktc.rbutils.api.RbUtilsException; |
31 | import org.ktc.rbutils.api.audit.MainProcessor; |
32 | import org.ktc.rbutils.rb.AbstractUtility; |
33 | import org.ktc.rbutils.rb.CommonProps; |
34 | import org.ktc.rbutils.rb.TaskType; |
35 | |
36 | /** |
37 | * Command line utility for ResourceBundle generation. |
38 | * @since RbUtils 0.8.1 |
39 | * @version $Revision: 1.7 $ |
40 | * @author ktcguru |
41 | */ |
42 | public class RbGeneratorUtility extends AbstractUtility implements GeneratorUtility { |
43 | // TODO need a refactoring with RbGeneratorUtility |
44 | // A lot of thing can be done in AbstractUtility as it is done with Ant Task |
45 | |
46 | /** The options to the command line. */ |
47 | private static final Options GENERATOR_OPTS; |
48 | |
49 | /** The g option (for target generation directory). */ |
50 | protected static final String OPTION_GEN = "g"; |
51 | |
52 | /** Error mesage when more than on target generation directory is specified. */ |
53 | public static final String ERROR_MSG_GEN_NBR = "Must specify one " |
54 | + "and only one target generation directory"; |
55 | |
56 | static { |
57 | GENERATOR_OPTS = getCommonOptions(); |
58 | final Option genDirOption = new Option(OPTION_GEN, "gendir", true, REQUIRED_HEADER |
59 | + "the directory where the java resourcebundle source files will be generated."); |
60 | genDirOption.setRequired(true); |
61 | GENERATOR_OPTS.addOption(genDirOption); |
62 | } |
63 | |
64 | /** |
65 | * Number of files that have been checked by the utility. |
66 | * @since RbUtils 0.11 |
67 | */ |
68 | // TODO variable duplication with RbCheckerUtility, should be move to super class as protected |
69 | private int processedFilesCount; |
70 | |
71 | /** |
72 | * Creates a new instance of RbGeneratorUtility. |
73 | * @param args the arguments to be passed to the utility. |
74 | */ |
75 | protected RbGeneratorUtility(final String[] args) { |
76 | taskName = CommonProps.RBGENERATOR_TASKNAME; |
77 | internalArgs = args; |
78 | options = clone(GENERATOR_OPTS); |
79 | } |
80 | |
81 | /** |
82 | * {@inheritDoc} |
83 | */ |
84 | public File getGenDir() throws RbUtilsException, FileNotFoundException { |
85 | final File uniqueDir = getUniqueDir(OPTION_GEN, ERROR_MSG_GEN_NBR); |
86 | return uniqueDir; |
87 | } |
88 | |
89 | /** |
90 | * Launches the utility. Do not display anything. Call method should catch parse exceptions and |
91 | * display messages to user. |
92 | * @return the number of errors that occur during the generation (this does not count arguments |
93 | * parse error). |
94 | * @throws Exception the arguments to be passed to the utility. |
95 | */ |
96 | protected int process() throws Exception { |
97 | parseParameters(); |
98 | final File rootDir = getRootDir(); |
99 | final File genDir = getGenDir(); |
100 | final List files = getFilesToBeProcessed(); |
101 | final Collection loggers = getLoggers(); |
102 | final String extension = getExtension(); |
103 | |
104 | final MainProcessor generator = new MainGenerator(rootDir, genDir); |
105 | generator.addLoggers(loggers); |
106 | generator.setExtension(extension); |
107 | |
108 | if (!files.isEmpty()) { |
109 | generator.addFiles(files); |
110 | generator.setFilesAlreadyAdded(true); |
111 | } |
112 | |
113 | final int errorsNumber = generator.process(); |
114 | processedFilesCount = generator.getProcessedFilesCount(); |
115 | return errorsNumber; |
116 | } |
117 | |
118 | /** |
119 | * Launches the utility. Acts like the main method but does not exit. |
120 | * @param args the arguments to be passed to the utility. |
121 | * @param pw the Writer to be used to print messages. |
122 | * @return the number of error detected during generation. |
123 | */ |
124 | protected static int mainNoExit(final String[] args, final PrintWriter pw) { |
125 | // RFE add outputstream where log error output |
126 | pw.println(CommonProps.getRunningTask(TaskType.RBGENERATOR)); |
127 | int errorsNumber = -1; |
128 | |
129 | RbGeneratorUtility util = null; |
130 | try { |
131 | util = new RbGeneratorUtility(args); |
132 | errorsNumber = util.process(); |
133 | // RBUTILS-028: Display the number of processed files |
134 | pw.println("Processed Files: " + util.processedFilesCount); |
135 | pw.println("Errors: " + errorsNumber); |
136 | } |
137 | catch (final Exception exception) { |
138 | util.parse(exception, pw); |
139 | } |
140 | finally { |
141 | // TODO see how the defaultLogger flush to be sure it is done sometimes |
142 | pw.flush(); |
143 | } |
144 | |
145 | return errorsNumber; |
146 | } |
147 | |
148 | } |