| 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: MainGenerator.java,v 1.4 2007/07/08 01:14:44 moishi Exp $ |
| 19 | |
| 20 | package org.ktc.rbutils.rb.generation; |
| 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.ValidateFile; |
| 28 | |
| 29 | /** |
| 30 | * Generates a set of Java RessourceBundle source files using a set of Properties files. |
| 31 | * @see FileGenerator |
| 32 | * @since RbUtils 0.8.0 |
| 33 | * @version $Revision: 1.4 $ |
| 34 | * @author ktcguru |
| 35 | */ |
| 36 | public class MainGenerator extends AbstractMainProcessor { |
| 37 | /** Root directory where java resourcebundle file will be generated. */ |
| 38 | private File genRootdir; |
| 39 | |
| 40 | /** |
| 41 | * Instanciates a new <code>MainGenerator</code>. |
| 42 | * <p> |
| 43 | * <code>propsRootDir</code> and <code>genRootdir</code> must be directories that exist in |
| 44 | * the filesystem. |
| 45 | * @param propsRootDir the root dir of the properties files used to resolve package name. |
| 46 | * @param genRootdir the target root directory where the java files will be put after package |
| 47 | * name resolution and file generation. |
| 48 | * @throws java.io.FileNotFoundException if a parameter is a <code>File</code> that does not |
| 49 | * exist in the filesystem. |
| 50 | * @throws org.ktc.rbutils.api.file.NotDirectoryException if a parameter is not a directory in |
| 51 | * the filesystem. |
| 52 | */ |
| 53 | public MainGenerator(final File propsRootDir, final File genRootdir) |
| 54 | throws FileNotFoundException |
| 55 | { |
| 56 | // Validate arguments |
| 57 | ValidateFile.isDirectory(propsRootDir); |
| 58 | ValidateFile.isDirectory(genRootdir); |
| 59 | |
| 60 | // Set field |
| 61 | this.root = propsRootDir; |
| 62 | this.genRootdir = genRootdir; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Generates a Java RessourceBundle source class in the <code>genRootdir</code> directory for |
| 67 | * each file found in the <code>propsRootDir</code> directory. |
| 68 | * @return the number of errors that occured during generation. |
| 69 | */ |
| 70 | public int process() { |
| 71 | // Initialization |
| 72 | finalizeInitialization(); |
| 73 | fireProcessStarted(); |
| 74 | |
| 75 | // Create or clean the generation directory |
| 76 | genRootdir.mkdirs(); |
| 77 | // RFE cleanGenRootDir option |
| 78 | // Warning: be careful if the gen dir contains not gernerated code |
| 79 | // final boolean cleanGenRootDir = false; |
| 80 | // if (cleanGenRootDir) { |
| 81 | // FileUtils.cleanDirectory(genRootdir); |
| 82 | // } |
| 83 | |
| 84 | // Generate all found files |
| 85 | for (final Iterator iter = filesList.iterator(); iter.hasNext();) { |
| 86 | final File propsFile = (File) iter.next(); |
| 87 | try { |
| 88 | final FileGenerator generator = new FileGenerator(root, propsFile, genRootdir); |
| 89 | generator.addLoggers(loggers); |
| 90 | generator.performGeneration(); |
| 91 | } |
| 92 | catch (final Exception exception) { |
| 93 | fireException(exception); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | fireProcessEnded(); |
| 98 | // Return the number of errors |
| 99 | return getErrorsCount(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * {@inheritDoc} |
| 104 | */ |
| 105 | public void addConsoleLogger() { |
| 106 | addLogger(new ConsoleLogger()); |
| 107 | } |
| 108 | |
| 109 | } |