1 16 package org.apache.cocoon; 17 18 import java.io.File ; 19 import java.util.Arrays ; 20 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.DocumentBuilderFactory ; 23 24 import org.apache.cocoon.bean.CocoonBean; 25 import org.apache.cocoon.bean.helpers.OutputStreamListener; 26 import org.apache.cocoon.bean.helpers.BeanConfigurator; 27 28 import org.apache.commons.cli.CommandLine; 29 import org.apache.commons.cli.HelpFormatter; 30 import org.apache.commons.cli.Option; 31 import org.apache.commons.cli.Options; 32 import org.apache.commons.cli.PosixParser; 33 import org.apache.commons.lang.BooleanUtils; 34 35 import org.w3c.dom.Document ; 36 37 47 public class Main { 48 49 protected static final String HELP_OPT = "h"; 50 protected static final String VERSION_OPT = "v"; 51 protected static final String VERBOSE_OPT = "V"; 52 protected static final String LOG_KIT_OPT = "k"; 53 protected static final String LOGGER_OPT = "l"; 54 protected static final String LOG_LEVEL_OPT = "u"; 55 protected static final String CONTEXT_DIR_OPT = "c"; 56 protected static final String DEST_DIR_OPT = "d"; 57 protected static final String WORK_DIR_OPT = "w"; 58 protected static final String CONFIG_FILE_OPT = "C"; 59 protected static final String BROKEN_LINK_FILE_OPT = "b"; 60 protected static final String URI_FILE_OPT = "f"; 61 protected static final String XCONF_OPT = "x"; 62 protected static final String AGENT_OPT = "a"; 63 protected static final String ACCEPT_OPT = "p"; 64 protected static final String FOLLOW_LINKS_OPT = "r"; 65 protected static final String PRECOMPILE_ONLY_OPT = "P"; 66 protected static final String CONFIRM_EXTENSIONS_OPT = "e"; 67 protected static final String LOAD_CLASS_OPT = "L"; 68 protected static final String DEFAULT_FILENAME_OPT = "D"; 69 protected static final String URI_GROUP_NAME_OPT = "n"; 70 71 protected static final String HELP_LONG = "help"; 72 protected static final String VERSION_LONG = "version"; 73 protected static final String VERBOSE_LONG = "verbose"; 74 protected static final String LOG_KIT_LONG = "logKitconfig"; 75 protected static final String LOGGER_LONG = "Logger"; 76 protected static final String LOG_LEVEL_LONG = "logLevel"; 77 protected static final String CONTEXT_DIR_LONG = "contextDir"; 78 protected static final String DEST_DIR_LONG = "destDir"; 79 protected static final String WORK_DIR_LONG = "workDir"; 80 protected static final String CONFIG_FILE_LONG = "configFile"; 81 protected static final String BROKEN_LINK_FILE_LONG = "brokenLinkFile"; 82 protected static final String URI_FILE_LONG = "uriFile"; 83 protected static final String XCONF_LONG = "xconf"; 84 protected static final String AGENT_LONG = "userAgent"; 85 protected static final String ACCEPT_LONG = "accept"; 86 protected static final String FOLLOW_LINKS_LONG = "followLinks"; 87 protected static final String PRECOMPILE_ONLY_LONG = "precompileOnly"; 88 protected static final String CONFIRM_EXTENSIONS_LONG = "confirmExtensions"; 89 protected static final String LOAD_CLASS_LONG = "loadClass"; 90 protected static final String DEFAULT_FILENAME_LONG = "defaultFilename"; 91 protected static final String URI_LONG = "uri"; 92 protected static final String URI_GROUP_NAME_LONG = "uris"; 93 94 private static Options options; 95 private static OutputStreamListener listener; 96 97 private static void setOptions() { 98 options = new Options(); 99 100 options.addOption(new Option(HELP_OPT, 101 HELP_LONG, 102 false, 103 "print this message and exit")); 104 105 options.addOption(new Option(VERSION_OPT, 106 VERSION_LONG, 107 false, 108 "print the version information and exit")); 109 110 options.addOption(new Option(VERBOSE_OPT, 111 VERBOSE_LONG, 112 false, 113 "enable verbose messages to System.out")); 114 115 options.addOption(new Option(LOG_KIT_OPT, 116 LOG_KIT_LONG, 117 true, 118 "use given file for LogKit Management configuration")); 119 120 options.addOption(new Option(LOGGER_OPT, 121 LOGGER_LONG, 122 true, 123 "use given logger category as default logger for the Cocoon engine")); 124 125 options.addOption(new Option(LOG_LEVEL_OPT, 126 LOG_LEVEL_LONG, 127 true, 128 "choose the minimum log level for logging (DEBUG, INFO, WARN, ERROR, FATAL_ERROR) for startup logging")); 129 130 options.addOption(new Option(CONTEXT_DIR_OPT, 131 CONTEXT_DIR_LONG, 132 true, 133 "use given dir as context")); 134 135 options.addOption(new Option(DEST_DIR_OPT, 136 DEST_DIR_LONG, 137 true, 138 "use given dir as destination")); 139 140 options.addOption(new Option(WORK_DIR_OPT, 141 WORK_DIR_LONG, 142 true, 143 "use given dir as working directory")); 144 145 options.addOption(new Option(CONFIG_FILE_OPT, 146 CONFIG_FILE_LONG, 147 true, 148 "specify alternate location of the configuration" 149 + " file (default is ${contextDir}/cocoon.xconf)")); 150 151 options.addOption(new Option(BROKEN_LINK_FILE_OPT, 152 BROKEN_LINK_FILE_LONG, 153 true, 154 "send a list of broken links to a file (one URI per line)")); 155 156 options.addOption(new Option(URI_FILE_OPT, 157 URI_FILE_LONG, 158 true, 159 "use a text file with uris to process (one URI per line)")); 160 161 options.addOption(new Option(XCONF_OPT, 162 XCONF_LONG, 163 true, 164 "specify a file containing XML configuration details" 165 + " for the command line interface")); 166 167 options.addOption(new Option(AGENT_OPT, 168 AGENT_LONG, 169 true, 170 "use given string for user-agent header")); 171 172 options.addOption(new Option(ACCEPT_OPT, 173 ACCEPT_LONG, 174 true, 175 "use given string for accept header")); 176 177 options.addOption(new Option(FOLLOW_LINKS_OPT, 178 FOLLOW_LINKS_LONG, 179 true, 180 "process pages linked from starting page or not" 181 + " (boolean argument is expected, default is true)")); 182 183 options.addOption(new Option(PRECOMPILE_ONLY_OPT, 184 PRECOMPILE_ONLY_LONG, 185 true, 186 "generate java code for xsp and xmap files")); 187 188 options.addOption(new Option(CONFIRM_EXTENSIONS_OPT, 189 CONFIRM_EXTENSIONS_LONG, 190 true, 191 "confirm that file extensions match mime-type of" 192 + " pages and amend filename accordingly (default" 193 + " is true)")); 194 195 options.addOption(new Option(LOAD_CLASS_OPT, 196 LOAD_CLASS_LONG, 197 true, 198 "specify a class to be loaded at startup (specifically" 199 + " for use with JDBC). Can be used multiple times")); 200 201 options.addOption(new Option(DEFAULT_FILENAME_OPT, 202 DEFAULT_FILENAME_LONG, 203 true, 204 "specify a filename to be appended to a URI when the" 205 + " URI refers to a directory")); 206 options.addOption(new Option(URI_GROUP_NAME_OPT, 207 URI_GROUP_NAME_LONG, 208 true, 209 "specify which <uris> element to process in the configuration" 210 + " file specified with the -x parameter")); 211 } 212 213 219 public static void main(String [] args) throws Exception { 220 221 Main.setOptions(); 222 CommandLine line = new PosixParser().parse( options, args ); 223 listener = new OutputStreamListener(System.out); 224 CocoonBean cocoon = new CocoonBean(); 225 cocoon.addListener(listener); 226 227 if (line.hasOption(HELP_OPT)) { 228 printUsage(); 229 } else if (line.hasOption(VERSION_OPT)) { 230 printVersion(); 231 } 232 233 String uriGroup = null; 234 if (line.hasOption(URI_GROUP_NAME_OPT)) { 235 uriGroup = line.getOptionValue(URI_GROUP_NAME_OPT); 236 } 237 238 String destDir = null; 239 if (line.hasOption(XCONF_OPT)) { 240 destDir = Main.processXConf(cocoon, line.getOptionValue(XCONF_OPT), destDir, uriGroup); 242 } 243 if (line.hasOption(DEST_DIR_OPT)) { 244 destDir = line.getOptionValue(DEST_DIR_OPT); 245 } 246 247 if (line.hasOption(VERBOSE_OPT)) { 248 cocoon.setVerbose(true); 249 } 250 if (line.hasOption(PRECOMPILE_ONLY_OPT)) { 251 cocoon.setPrecompileOnly(true); 252 } 253 254 if (line.hasOption(WORK_DIR_OPT)) { 255 String workDir = line.getOptionValue(WORK_DIR_OPT); 256 if (workDir.equals("")) { 257 listener.messageGenerated( 258 "Careful, you must specify a work dir when using the -w/--workDir argument"); 259 System.exit(1); 260 } else { 261 cocoon.setWorkDir(line.getOptionValue(WORK_DIR_OPT)); 262 } 263 } 264 if (line.hasOption(CONTEXT_DIR_OPT)) { 265 String contextDir = line.getOptionValue(CONTEXT_DIR_OPT); 266 if (contextDir.equals("")) { 267 listener.messageGenerated( 268 "Careful, you must specify a configuration file when using the -c/--contextDir argument"); 269 System.exit(1); 270 } else { 271 cocoon.setContextDir(contextDir); 272 } 273 } 274 if (line.hasOption(CONFIG_FILE_OPT)) { 275 cocoon.setConfigFile(line.getOptionValue(CONFIG_FILE_OPT)); 276 } 277 if (line.hasOption(LOG_KIT_OPT)) { 278 cocoon.setLogKit(line.getOptionValue(LOG_KIT_OPT)); 279 } 280 if (line.hasOption(LOGGER_OPT)) { 281 cocoon.setLogger(line.getOptionValue(LOGGER_OPT)); 282 } 283 if (line.hasOption(LOG_LEVEL_OPT)) { 284 cocoon.setLogLevel(line.getOptionValue(LOG_LEVEL_OPT)); 285 } 286 if (line.hasOption(AGENT_OPT)) { 287 cocoon.setAgentOptions(line.getOptionValue(AGENT_OPT)); 288 } 289 if (line.hasOption(ACCEPT_OPT)) { 290 cocoon.setAcceptOptions(line.getOptionValue(ACCEPT_OPT)); 291 } 292 if (line.hasOption(DEFAULT_FILENAME_OPT)) { 293 cocoon.setDefaultFilename(line.getOptionValue(DEFAULT_FILENAME_OPT)); 294 } 295 if (line.hasOption(BROKEN_LINK_FILE_OPT)) { 296 listener.setReportFile(line.getOptionValue(BROKEN_LINK_FILE_OPT)); 297 } 298 if (line.hasOption(FOLLOW_LINKS_OPT)) { 299 cocoon.setFollowLinks(BooleanUtils.toBoolean(line.getOptionValue(FOLLOW_LINKS_OPT))); 300 } 301 if (line.hasOption(CONFIRM_EXTENSIONS_OPT)) { 302 cocoon.setConfirmExtensions(BooleanUtils.toBoolean(line.getOptionValue(CONFIRM_EXTENSIONS_OPT, "yes"))); 303 } 304 if (line.hasOption(LOAD_CLASS_OPT)){ 305 cocoon.addLoadedClasses(Arrays.asList(line.getOptionValues(LOAD_CLASS_OPT))); 306 } 307 if (line.hasOption(URI_FILE_OPT)) { 308 cocoon.addTargets(BeanConfigurator.processURIFile(line.getOptionValue(URI_FILE_OPT)), destDir); 309 } 310 311 cocoon.addTargets(line.getArgList(), destDir); 312 313 listener.messageGenerated(CocoonBean.getProlog()); 314 315 if (cocoon.getTargetCount() ==0 && cocoon.isPrecompileOnly()) { 316 listener.messageGenerated("Please, specify at least one starting URI."); 317 System.exit(1); 318 } 319 320 cocoon.initialize(); 321 cocoon.process(); 322 cocoon.dispose(); 323 324 listener.complete(); 325 326 327 int exitCode = (listener.isSuccessful() ? 0 : 1); 328 System.exit(exitCode); 329 } 330 331 private static String processXConf(CocoonBean cocoon, String filename, String destDir, String uriGroup) { 332 333 try { 334 final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 335 final Document xconf = builder.parse(new File (filename).toURL().toExternalForm()); 336 return BeanConfigurator.configure(xconf, cocoon, destDir, uriGroup, listener); 337 } catch (Exception e) { 338 System.out.println("ERROR: " + e.getMessage()); 339 return destDir; 340 } 341 } 342 343 346 private static void printUsage() { 347 HelpFormatter formatter = new HelpFormatter(); 348 349 formatter.printHelp("cocoon cli [options] [targets]", 350 CocoonBean.getProlog(), 351 options, 352 "Note: the context directory defaults to '"+ Constants.DEFAULT_CONTEXT_DIR + "'"); 353 System.exit(0); 354 } 355 356 359 private static void printVersion() { 360 System.out.println(Constants.VERSION); 361 System.exit(0); 362 } 363 } 364 | Popular Tags |