001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.bean;
018:
019: import java.io.File;
020:
021: import org.apache.cocoon.components.CocoonComponentManager;
022: import org.apache.cocoon.components.language.generator.CompiledComponent;
023: import org.apache.cocoon.components.language.generator.ProgramGenerator;
024: import org.apache.cocoon.environment.Environment;
025: import org.apache.cocoon.environment.commandline.LinkSamplingEnvironment;
026: import org.apache.cocoon.util.IOUtils;
027: import org.apache.commons.cli.CommandLine;
028: import org.apache.commons.cli.HelpFormatter;
029: import org.apache.commons.cli.Option;
030: import org.apache.commons.cli.Options;
031: import org.apache.commons.cli.PosixParser;
032: import org.apache.excalibur.source.Source;
033: import org.apache.excalibur.source.SourceResolver;
034:
035: /**
036: * This is simple Wrapper like CocoonWrapper and can only precompile all XSP in
037: * the context-directory.
038: *
039: * @version $Id: XSPPrecompileWrapper.java 433543 2006-08-22 06:22:54Z crossley $ $Date: 2005/01/16 17:17:34 $
040: */
041: public class XSPPrecompileWrapper extends CocoonWrapper {
042: private SourceResolver sourceResolver;
043: private static Options options;
044: protected static final String HELP_OPT = "h";
045: protected static final String LOG_KIT_OPT = "k";
046: protected static final String CONTEXT_DIR_OPT = "c";
047: protected static final String WORK_DIR_OPT = "w";
048: protected static final String CONFIG_FILE_OPT = "C";
049: protected static final String LOG_KIT_LONG = "logKitconfig";
050: protected static final String CONTEXT_DIR_LONG = "contextDir";
051: protected static final String WORK_DIR_LONG = "workDir";
052: protected static final String HELP_LONG = "help";
053: protected static final String CONFIG_FILE_LONG = "configFile";
054:
055: /**
056: * Allow subclasses to recursively precompile XSPs.
057: */
058: public void precompile() throws Exception {
059: recursivelyPrecompile(context, context);
060: }
061:
062: /**
063: * Recurse the directory hierarchy and process the XSP's.
064: *
065: * @param contextDir
066: * a <code>File</code> value for the context directory
067: * @param file
068: * a <code>File</code> value for a single XSP file or a
069: * directory to scan recursively
070: */
071: private void recursivelyPrecompile(File contextDir, File file)
072: throws Exception {
073: if (file.isDirectory()) {
074: String entries[] = file.list();
075: for (int i = 0; i < entries.length; i++) {
076: recursivelyPrecompile(contextDir, new File(file,
077: entries[i]));
078: }
079: } else if (file.getName().toLowerCase().endsWith(".xsp")) {
080: String contextFilePath = IOUtils.getContextFilePath(
081: contextDir.getCanonicalPath(), file
082: .getCanonicalPath());
083: this .processXSP(contextFilePath);
084: }
085: }
086:
087: /**
088: * Process a single XSP file
089: *
090: * @param uri
091: * a <code>String</code> pointing to an xsp URI
092: * @exception Exception
093: * if an error occurs
094: */
095: protected void processXSP(String uri) throws Exception {
096: String markupLanguage = "xsp";
097: String programmingLanguage = "java";
098: Environment env = new LinkSamplingEnvironment("/", context,
099: null, null, null, cliContext, log);
100: precompile(uri, env, markupLanguage, programmingLanguage);
101: }
102:
103: /**
104: * Process a single XMAP file
105: *
106: * @param uri
107: * a <code>String</code> pointing to an xmap URI
108: * @exception Exception
109: * if an error occurs
110: */
111: protected void processXMAP(String uri) throws Exception {
112: String markupLanguage = "sitemap";
113: String programmingLanguage = "java";
114: Environment env = new LinkSamplingEnvironment("/", context,
115: null, null, null, cliContext, log);
116: precompile(uri, env, markupLanguage, programmingLanguage);
117: }
118:
119: /**
120: * Process the given <code>Environment</code> to generate Java code for
121: * specified XSP files.
122: *
123: * @param fileName
124: * a <code>String</code> value
125: * @param environment
126: * an <code>Environment</code> value
127: * @exception Exception
128: * if an error occurs
129: */
130: public void precompile(String fileName, Environment environment,
131: String markupLanguage, String programmingLanguage)
132: throws Exception {
133:
134: ProgramGenerator programGenerator = null;
135: Source source = null;
136: Object key = CocoonComponentManager
137: .startProcessing(environment);
138: CocoonComponentManager.enterEnvironment(environment,
139: getComponentManager(), cocoon);
140: try {
141: if (log.isDebugEnabled()) {
142: log.debug("XSP generation begin:" + fileName);
143: }
144: System.out.println("Compiling " + fileName);
145:
146: programGenerator = (ProgramGenerator) getComponentManager()
147: .lookup(ProgramGenerator.ROLE);
148: source = sourceResolver.resolveURI(fileName);
149: CompiledComponent xsp = programGenerator.load(
150: getComponentManager(), source, markupLanguage,
151: programmingLanguage, environment);
152: System.out.println("[XSP generated] " + xsp);
153: if (log.isDebugEnabled()) {
154: log.debug("XSP generation complete:" + xsp);
155:
156: }
157: } finally {
158: sourceResolver.release(source);
159: getComponentManager().release(programGenerator);
160:
161: CocoonComponentManager.leaveEnvironment();
162: CocoonComponentManager.endProcessing(environment, key);
163: }
164: }
165:
166: public static void main(String[] args) throws Exception {
167:
168: XSPPrecompileWrapper.setOptions();
169: CommandLine line = new PosixParser().parse(options, args);
170: XSPPrecompileWrapper wrapper = new XSPPrecompileWrapper();
171: if (line.hasOption(HELP_OPT)) {
172: printUsage();
173: }
174:
175: if (line.hasOption(WORK_DIR_OPT)) {
176: String workDir = line.getOptionValue(WORK_DIR_OPT);
177: if (workDir.equals("")) {
178: System.exit(1);
179: } else {
180: wrapper.setWorkDir(line.getOptionValue(WORK_DIR_OPT));
181: }
182: }
183:
184: if (line.hasOption(CONTEXT_DIR_OPT)) {
185: String contextDir = line.getOptionValue(CONTEXT_DIR_OPT);
186: if (contextDir.equals("")) {
187:
188: System.exit(1);
189: } else {
190: wrapper.setContextDir(contextDir);
191: }
192: }
193: if (line.hasOption(LOG_KIT_OPT)) {
194: wrapper.setLogKit(line.getOptionValue(LOG_KIT_OPT));
195: }
196:
197: if (line.hasOption(CONFIG_FILE_OPT)) {
198: wrapper.setConfigFile(line.getOptionValue(CONFIG_FILE_OPT));
199: }
200: wrapper.initialize();
201: wrapper.precompile();
202: wrapper.dispose();
203: System.exit(0);
204: }
205:
206: private static void setOptions() {
207: options = new Options();
208:
209: options.addOption(new Option(LOG_KIT_OPT, LOG_KIT_LONG, true,
210: "use given file for LogKit Management configuration"));
211:
212: options.addOption(new Option(CONTEXT_DIR_OPT, CONTEXT_DIR_LONG,
213: true, "use given dir as context"));
214: options.addOption(new Option(WORK_DIR_OPT, WORK_DIR_LONG, true,
215: "use given dir as working directory"));
216:
217: options.addOption(new Option(HELP_OPT, HELP_LONG, false,
218: "print this message and exit"));
219:
220: options
221: .addOption(new Option(
222: CONFIG_FILE_OPT,
223: CONFIG_FILE_LONG,
224: true,
225: "specify alternate location of the configuration"
226: + " file (default is ${contextDir}/cocoon.xconf)"));
227: }
228:
229: /**
230: * Print the usage message and exit
231: */
232: private static void printUsage() {
233: HelpFormatter formatter = new HelpFormatter();
234:
235: formatter
236: .printHelp(
237: "java org.apache.cocoon.bean.XSPPrecompileWrapper [options] ",
238:
239: options);
240: System.exit(0);
241: }
242:
243: public void initialize() throws Exception {
244: super .initialize();
245: sourceResolver = (SourceResolver) getComponentManager().lookup(
246: SourceResolver.ROLE);
247: }
248: }
|