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: RbCheckerUtility.java,v 1.6 2008/07/03 08:15:42 moishi Exp $ |
19 | |
20 | package org.ktc.rbutils.rb.check; |
21 | |
22 | import java.io.File; |
23 | import java.io.PrintWriter; |
24 | import java.util.Collection; |
25 | import java.util.List; |
26 | |
27 | import org.apache.commons.cli.Option; |
28 | import org.apache.commons.cli.Options; |
29 | import org.ktc.rbutils.api.RbUtilsException; |
30 | import org.ktc.rbutils.api.audit.MainProcessor; |
31 | import org.ktc.rbutils.rb.AbstractUtility; |
32 | import org.ktc.rbutils.rb.CommonProps; |
33 | import org.ktc.rbutils.rb.TaskType; |
34 | |
35 | /** |
36 | * Command line utility for ResourceBundle check. |
37 | * @since RbUtils 0.5.0 |
38 | * @version $Revision: 1.6 $ |
39 | * @author redfish |
40 | */ |
41 | public class RbCheckerUtility extends AbstractUtility implements CheckerUtility { |
42 | // TODO need a refactoring with RbGeneratorUtility |
43 | // A lot of thing can be done in AbstractUtility as it is done with Ant Task |
44 | |
45 | /** The options to the command line. */ |
46 | private static final Options CHECKER_OPTS; |
47 | |
48 | /** The fc option (for filechecker class name). */ |
49 | protected static final String OPTION_FC = "c"; |
50 | |
51 | /** Error mesage when more than on target generation directory is specified. */ |
52 | public static final String ERROR_MSG_FC_NBR = "Must specify one " |
53 | + "and only one filechecker class name"; |
54 | |
55 | static { |
56 | CHECKER_OPTS = getCommonOptions(); |
57 | final Option filecheckerOption = new Option(OPTION_FC, "filechecker", true, |
58 | "the file checker class name to be used."); |
59 | CHECKER_OPTS.addOption(filecheckerOption); |
60 | } |
61 | |
62 | /** |
63 | * Number of files that have been checked by the utility. |
64 | * @since RbUtils 0.11 |
65 | */ |
66 | private int processedFilesCount; |
67 | |
68 | /** |
69 | * Creates a new instance of RbCheckerUtility. |
70 | * @param args the arguments to be passed to the utility. |
71 | */ |
72 | protected RbCheckerUtility(final String[] args) { |
73 | taskName = CommonProps.RBCHECKER_TASKNAME; |
74 | internalArgs = args; |
75 | options = clone(CHECKER_OPTS); |
76 | } |
77 | |
78 | /** |
79 | * {@inheritDoc} |
80 | */ |
81 | public String getFileChecker() throws RbUtilsException { |
82 | final String fcClassName = getUniqueOptionValue(OPTION_FC, ERROR_MSG_FC_NBR, false); |
83 | return fcClassName; |
84 | } |
85 | |
86 | /** |
87 | * Launches the utility. Do not display anything. Calling method should catch parsed exceptions |
88 | * and display messages to user. |
89 | * @return the number of errors that occur during the check (this does not count arguments parse |
90 | * error). |
91 | * @throws Exception if the arguments to be passed to the utility are not valid. |
92 | */ |
93 | protected int process() throws Exception { |
94 | // TODO should be public and in the Utility interface |
95 | |
96 | final MainProcessor processor = buildProcessor(); |
97 | final int errorsNumber = processor.process(); |
98 | processedFilesCount = processor.getProcessedFilesCount(); |
99 | return errorsNumber; |
100 | } |
101 | |
102 | /** |
103 | * Build the utility processor by parsing the user arguments. |
104 | * @return the built processor. |
105 | * @throws Exception if the arguments to be passed to the utility are not valid. |
106 | * @since RbUtils 0.12 |
107 | */ |
108 | protected MainProcessor buildProcessor() throws Exception { |
109 | // TODO in the super class + used by RbGeneratorUtility |
110 | // this should help to better unit test the code |
111 | |
112 | parseParameters(); |
113 | final File rootDir = getRootDir(); |
114 | final String filechecker = getFileChecker(); |
115 | final List files = getFilesToBeProcessed(); |
116 | final Collection loggers = getLoggers(); |
117 | |
118 | final MainChecker checker = new MainChecker(rootDir); |
119 | checker.addLoggers(loggers); |
120 | checker.setFileCheckerClassname(filechecker); |
121 | |
122 | checker.setExtension(getExtension()); |
123 | |
124 | if (!files.isEmpty()) { |
125 | checker.addFiles(files); |
126 | checker.setFilesAlreadyAdded(true); |
127 | } |
128 | |
129 | return checker; |
130 | } |
131 | |
132 | /** |
133 | * Launches the utility. Acts like the main method but does not exit. |
134 | * @param args the arguments to be passed to the utility. |
135 | * @param pw the Writer to be used to print messages. |
136 | * @return the number of error detected during generation. |
137 | */ |
138 | protected static int mainNoExit(final String[] args, final PrintWriter pw) { |
139 | // RFE add outputstream where log error output |
140 | pw.println(CommonProps.getRunningTask(TaskType.RBCHECKER)); |
141 | int errorsNumber = -1; |
142 | |
143 | RbCheckerUtility util = null; |
144 | try { |
145 | util = new RbCheckerUtility(args); |
146 | errorsNumber = util.process(); |
147 | // RBUTILS-028: Display the number of processed files |
148 | pw.println("Processed Files: " + util.processedFilesCount); |
149 | pw.println("Errors: " + errorsNumber); |
150 | } |
151 | catch (final Exception exception) { |
152 | util.parse(exception, pw); |
153 | } |
154 | finally { |
155 | // TODO see how the defaultLogger flush to be sure it is done sometimes |
156 | pw.flush(); |
157 | } |
158 | |
159 | return errorsNumber; |
160 | } |
161 | } |