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: MainChecker.java,v 1.5 2007/07/08 01:14:44 moishi Exp $ |
19 | |
20 | package org.ktc.rbutils.rb.check; |
21 | |
22 | import java.io.File; |
23 | import java.io.FileNotFoundException; |
24 | import java.util.Iterator; |
25 | |
26 | import org.ktc.rbutils.api.audit.AbstractMainProcessor; |
27 | import org.ktc.rbutils.api.file.FileTools; |
28 | import org.ktc.rbutils.api.file.ValidateFile; |
29 | import org.ktc.rbutils.api.reflect.ReflectUtils; |
30 | import org.ktc.rbutils.rb.generation.ConsoleLogger; |
31 | |
32 | /** |
33 | * Checks a set of Java RessourceBundle files using a set of Properties files. |
34 | * @since RbUtils 0.1.0 |
35 | * @version $Revision: 1.5 $ |
36 | * @author redfish |
37 | */ |
38 | public class MainChecker extends AbstractMainProcessor { |
39 | // TODO Javadoc - improves class documentation. |
40 | |
41 | /** Name of the class to be used as file checker. */ |
42 | private String fileCheckerClassname; |
43 | |
44 | /** Name of the default class to be used as file checker. */ |
45 | private static final String DEFAULT_FILECHECKER_CLASSNAME = FileChecker.class.getName(); |
46 | |
47 | /** |
48 | * Constructs a checker with recursive check in directories and default valid extension for |
49 | * properties files. |
50 | * @param root the root directory for the RB check. |
51 | * @throws org.apache.commons.lang.NullArgumentException if <code>root</code> is |
52 | * <code>null</code>. |
53 | * @throws FileNotFoundException if <code>root</code> does not exist. |
54 | * @throws org.ktc.rbutils.api.file.NotDirectoryException if <code>root</code> is not a |
55 | * directory. |
56 | */ |
57 | public MainChecker(final File root) throws FileNotFoundException { |
58 | this(root, DEFAULT_PROPERTIES_EXTENSION); |
59 | } |
60 | |
61 | /** |
62 | * Constructs a checker with recursive check in directories. |
63 | * @param root the root directory for the RB check. |
64 | * @param srcRBextension valid extension for properties files. |
65 | * @throws org.apache.commons.lang.NullArgumentException if <code>root</code> is |
66 | * <code>null</code>. |
67 | * @throws FileNotFoundException if <code>root</code> does not exist. |
68 | * @throws org.ktc.rbutils.api.file.NotDirectoryException if <code>root</code> is not a |
69 | * directory. |
70 | */ |
71 | public MainChecker(final File root, final String srcRBextension) |
72 | throws FileNotFoundException |
73 | { |
74 | this(root, srcRBextension, DEFAULT_RECURSIVE_CHECK); |
75 | } |
76 | |
77 | /** |
78 | * Constructs a checker. |
79 | * @param root the root directory for the RB check. |
80 | * @param srcRBextension valid extension for properties files. |
81 | * @param recursiveCheck indicate if the the check recurses in root's subdirectories. |
82 | * @throws org.apache.commons.lang.NullArgumentException if <code>root</code> is |
83 | * <code>null</code>. |
84 | * @throws FileNotFoundException if <code>root</code> does not exist. |
85 | * @throws org.ktc.rbutils.api.file.NotDirectoryException if <code>root</code> is not a |
86 | * directory. |
87 | */ |
88 | private MainChecker(final File root, final String srcRBextension, |
89 | final boolean recursiveCheck) throws FileNotFoundException |
90 | { |
91 | // Validate arguments |
92 | ValidateFile.isDirectory(root); |
93 | |
94 | // Set class attributes |
95 | this.root = FileTools.getNewInstance(root); |
96 | this.srcRBextension = srcRBextension; |
97 | this.recursiveCheck = recursiveCheck; |
98 | } |
99 | |
100 | /** |
101 | * Launches the check of the properties source files against the runtime files (properties or |
102 | * class files). |
103 | * @return the number of errors that occur during the audit. |
104 | * @throws ClassNotFoundException if the provided class can not be load. |
105 | * @throws ClassCastException if the provided class does not extend the default one. |
106 | */ |
107 | public int process() throws ClassNotFoundException { |
108 | finalizeCheckerInitialization(); |
109 | return doCheck(); |
110 | } |
111 | |
112 | /** |
113 | * Achieves the check of the files. |
114 | * @return the number of errors that occur during the audit. |
115 | */ |
116 | private int doCheck() { |
117 | // Start audit |
118 | fireProcessStarted(); |
119 | |
120 | // Check all found files |
121 | for (final Iterator iter = filesList.iterator(); iter.hasNext();) { |
122 | final File file = (File) iter.next(); |
123 | try { |
124 | // RBUTILS-1 |
125 | // TODO Code - Could be put in a method |
126 | // final FileChecker fileChecker = new FileChecker(root, file); |
127 | // Invoke the FileChecker |
128 | final Class fileCheckerClass = Class.forName(fileCheckerClassname); |
129 | final Class[] constructorArgsClass = {File.class, File.class}; |
130 | final Object[] constructorArgsValue = {root, file}; |
131 | |
132 | final FileChecker fileChecker = |
133 | (FileChecker) ReflectUtils.invokeConstructor(fileCheckerClass, |
134 | constructorArgsClass, |
135 | constructorArgsValue); |
136 | // end of RBUTILS-1 |
137 | |
138 | // Add listeners and perform the check |
139 | fileChecker.addLoggers(loggers); |
140 | fileChecker.performCheck(); |
141 | } |
142 | catch (final Exception exception) { |
143 | fireException(exception); |
144 | } |
145 | } |
146 | |
147 | fireProcessEnded(); |
148 | // Return the number of errors |
149 | return getErrorsCount(); |
150 | } |
151 | |
152 | /** |
153 | * Finalizes initialization as {@link AbstractMainProcessor#finalizeInitialization()} does and |
154 | * also validates the provided filechecker (class loading and extends the default class). |
155 | * @throws ClassNotFoundException if the provided file checker class can not be load. |
156 | * @throws ClassCastException if the provided file checker class does not extend the default |
157 | * one. |
158 | */ |
159 | protected void finalizeCheckerInitialization() throws ClassNotFoundException { |
160 | // Call super method |
161 | finalizeInitialization(); |
162 | |
163 | // Validate the filechecker |
164 | validateFileChecker(); |
165 | } |
166 | |
167 | /** |
168 | * Validates the FileChecker. |
169 | * <p> |
170 | * If not set, uses the default one. Then, try to get an instance of the Class specified by this |
171 | * class name. Finally, tests if this class extends this of the default file checker. |
172 | * @throws ClassNotFoundException if the provided class can not be load. |
173 | * @throws ClassCastException if the provided class does not extend the default one. |
174 | */ |
175 | protected void validateFileChecker() throws ClassNotFoundException { |
176 | if (fileCheckerClassname == null) { |
177 | fileCheckerClassname = DEFAULT_FILECHECKER_CLASSNAME; |
178 | } |
179 | |
180 | final Class theClass = Class.forName(fileCheckerClassname); |
181 | // if the specified class does not extend the default filechecker one, throw an exception |
182 | if (!Class.forName(DEFAULT_FILECHECKER_CLASSNAME).isAssignableFrom(theClass)) { |
183 | throw new ClassCastException(theClass + " must extend " |
184 | + DEFAULT_FILECHECKER_CLASSNAME); |
185 | } |
186 | } |
187 | |
188 | /** |
189 | * {@inheritDoc} |
190 | */ |
191 | public void addConsoleLogger() { |
192 | addLogger(new ConsoleLogger()); |
193 | } |
194 | |
195 | // //////////////////////////////////////////////////////////////////////////////////////////// |
196 | // Getters, setters, add methods |
197 | // //////////////////////////////////////////////////////////////////////////////////////////// |
198 | /** |
199 | * Sets the name of the class to be used as FileChecker. In order to work, it must extend the |
200 | * default FileChecker class. If sets to <code>null</code>, the default FileChecker will be |
201 | * used. |
202 | * @param fileCheckerClassname the name of the class to be used as FileChecker. |
203 | */ |
204 | public void setFileCheckerClassname(final String fileCheckerClassname) { |
205 | this.fileCheckerClassname = fileCheckerClassname; |
206 | } |
207 | } |