001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TemplateDeployer.java 3816 2007-06-27 09:06:35Z gbevin $
007: */
008: package com.uwyn.rife.template;
009:
010: import com.uwyn.rife.config.RifeConfig;
011: import com.uwyn.rife.resources.ResourceFinderClasspath;
012: import com.uwyn.rife.resources.ResourceFinderDirectories;
013: import com.uwyn.rife.resources.ResourceFinderGroup;
014: import com.uwyn.rife.template.exceptions.TemplateException;
015: import com.uwyn.rife.tools.FileUtils;
016: import com.uwyn.rife.tools.SortListComparables;
017: import com.uwyn.rife.tools.StringUtils;
018: import java.io.File;
019: import java.util.ArrayList;
020: import java.util.regex.Pattern;
021:
022: public class TemplateDeployer {
023: private boolean mVerbose = false;
024: private File[] mDirectories = null;
025: private TemplateFactory mTemplateFactory = null;
026: private Pattern mInclude = null;
027: private Pattern mExclude = null;
028:
029: private TemplateDeployer(boolean verbose,
030: ArrayList<String> directoryPaths,
031: TemplateFactory templateFactory, Pattern include,
032: Pattern exclude) {
033: assert directoryPaths != null;
034: assert directoryPaths.size() > 0;
035:
036: mVerbose = verbose;
037: mTemplateFactory = templateFactory;
038: mInclude = include;
039: mExclude = exclude;
040: ArrayList<File> directories = new ArrayList<File>();
041:
042: File directory_file = null;
043:
044: for (String directory_path : directoryPaths) {
045: directory_file = new File(directory_path);
046: if (!directory_file.exists()) {
047: System.err.println("The path '" + directory_path
048: + "' doesn't exist.");
049: System.exit(1);
050: }
051: if (!directory_file.isDirectory()) {
052: System.err.println("The path '" + directory_path
053: + "' is not a directory.");
054: System.exit(1);
055: }
056: if (!directory_file.canRead()) {
057: System.err.println("The directory '" + directory_path
058: + "' is not readable.");
059: System.exit(1);
060: }
061:
062: directories.add(directory_file);
063: }
064:
065: mDirectories = new File[directories.size()];
066: mDirectories = directories.toArray(mDirectories);
067: }
068:
069: private void execute() throws TemplateException {
070: ArrayList<String> files = null;
071: String classname = null;
072:
073: for (File directory : mDirectories) {
074: ResourceFinderGroup group = new ResourceFinderGroup().add(
075: new ResourceFinderDirectories(
076: new File[] { directory })).add(
077: ResourceFinderClasspath.getInstance());
078: mTemplateFactory.setResourceFinder(group);
079: files = FileUtils.getFileList(directory, Pattern
080: .compile(".*\\"
081: + mTemplateFactory.getParser()
082: .getExtension() + "$"), Pattern
083: .compile(".*(SCCS|CVS|\\.svn).*"));
084:
085: for (String file : files) {
086: if (!StringUtils.filter(file, mInclude, mExclude)) {
087: continue;
088: }
089:
090: if (mVerbose) {
091: System.out.print(directory.getPath() + " : " + file
092: + " ... ");
093: }
094: classname = file.replace(File.separatorChar, '.');
095: classname = classname.substring(0, classname.length()
096: - mTemplateFactory.getParser().getExtension()
097: .length());
098: mTemplateFactory.parse(classname, null, null);
099: if (mVerbose) {
100: System.out.println("done.");
101: }
102: }
103: }
104: }
105:
106: private static void listTemplateTypes() {
107: ArrayList<String> types = new ArrayList<String>(TemplateFactory
108: .getFactoryTypes());
109: SortListComparables sort = new SortListComparables();
110:
111: sort.sort(types);
112: for (Object type : types) {
113: System.err.println(" " + type);
114: }
115: }
116:
117: public static void main(String[] arguments) {
118: boolean valid_arguments = true;
119: boolean verbose = false;
120: ArrayList<String> directory_paths = new ArrayList<String>();
121: String template_type = "enginehtml";
122: Pattern include = null;
123: Pattern exclude = null;
124:
125: if (arguments.length < 1) {
126: valid_arguments = false;
127: } else {
128: for (int i = 0; i < arguments.length; i++) {
129: if (arguments[i].startsWith("-")) {
130: if (arguments[i].equals("-t")) {
131: i++;
132: if (arguments[i].startsWith("-")) {
133: valid_arguments = false;
134: } else {
135: template_type = arguments[i];
136: }
137: } else if (arguments[i].equals("-l")) {
138: System.err
139: .println("The supported template types are:");
140: listTemplateTypes();
141: System.exit(0);
142: } else if (arguments[i].equals("-verbose")) {
143: verbose = true;
144: } else if (arguments[i].equals("-d")) {
145: i++;
146: if (arguments[i].startsWith("-")) {
147: valid_arguments = false;
148: } else {
149: RifeConfig.Template
150: .setGenerationPath(arguments[i]);
151: }
152: } else if (arguments[i].equals("-encoding")) {
153: i++;
154: if (arguments[i].startsWith("-")) {
155: valid_arguments = false;
156: } else {
157: RifeConfig.Template
158: .setDefaultEncoding(arguments[i]);
159: }
160: } else if (arguments[i].equals("-preload")) {
161: i++;
162: if (arguments[i].startsWith("-")) {
163: valid_arguments = false;
164: } else {
165: ArrayList<String> class_names = StringUtils
166: .split(arguments[i], ":");
167: for (String class_name : class_names) {
168: try {
169: Class.forName(class_name);
170: } catch (ClassNotFoundException e) {
171: throw new RuntimeException(e);
172: }
173: }
174: }
175: } else if (arguments[i].equals("-i")) {
176: i++;
177: if (arguments[i].startsWith("-")) {
178: valid_arguments = false;
179: } else {
180: include = Pattern.compile(arguments[i]);
181: }
182: } else if (arguments[i].equals("-e")) {
183: i++;
184: if (arguments[i].startsWith("-")) {
185: valid_arguments = false;
186: } else {
187: exclude = Pattern.compile(arguments[i]);
188: }
189: } else {
190: valid_arguments = false;
191: }
192: } else {
193: directory_paths.add(arguments[i]);
194: }
195:
196: if (!valid_arguments) {
197: break;
198: }
199: }
200: }
201:
202: if (0 == directory_paths.size()) {
203: valid_arguments = false;
204: }
205:
206: if (!valid_arguments) {
207: System.err.println("Usage : java "
208: + TemplateDeployer.class.getName()
209: + " <options> <directories>");
210: System.err
211: .println("Compiles RIFE templates to class files.");
212: System.err
213: .println("All the files of the active template type that are found in the provided");
214: System.err
215: .println("directories will be parsed and compiled to java bytecode into the");
216: System.err.println("destination directory.");
217: System.err
218: .println(" -t <type> Specify which template type to use (default enginehtml)");
219: System.err
220: .println(" -l List the known template types");
221: System.err
222: .println(" -verbose Output messages about what the parser is doing");
223: System.err
224: .println(" -d <directory> Specify where to place generated class files");
225: System.err
226: .println(" -encoding <encoding> Specify character encoding used by template files");
227: System.err
228: .println(" -preload <classes> Colon seperated list of classes to preload");
229: System.err
230: .println(" -i <regexp> Regexp to include certain files");
231: System.err
232: .println(" -e <regexp> Regexp to exclude certain files");
233: System.err
234: .println(" -help Print a synopsis of standard options");
235: System.exit(1);
236: }
237:
238: TemplateFactory factory = null;
239: factory = TemplateFactory.getFactory(template_type);
240: if (null == factory) {
241: System.err.println("The template type '" + template_type
242: + "' is not supported.");
243: System.err.println("The list of valid types is:");
244: listTemplateTypes();
245: System.exit(1);
246: }
247:
248: RifeConfig.Template.setGenerateClasses(true);
249: TemplateDeployer deployer = new TemplateDeployer(verbose,
250: directory_paths, factory, include, exclude);
251: deployer.execute();
252: }
253: }
|