1 58 package org.krysalis.barcode.cli; 59 60 import java.awt.image.BufferedImage ; 61 import java.io.File ; 62 import java.io.FileNotFoundException ; 63 import java.io.IOException ; 64 import java.io.OutputStream ; 65 import java.io.PrintStream ; 66 import java.io.PrintWriter ; 67 import java.util.Set ; 68 69 import javax.xml.transform.Result ; 70 import javax.xml.transform.Source ; 71 import javax.xml.transform.Transformer ; 72 import javax.xml.transform.TransformerException ; 73 import javax.xml.transform.TransformerFactory ; 74 75 import org.krysalis.barcode.BarcodeException; 76 import org.krysalis.barcode.BarcodeGenerator; 77 import org.krysalis.barcode.BarcodeUtil; 78 import org.krysalis.barcode.output.bitmap.BitmapCanvasProvider; 79 import org.krysalis.barcode.output.bitmap.BitmapEncoderRegistry; 80 import org.krysalis.barcode.output.eps.EPSCanvasProvider; 81 import org.krysalis.barcode.output.svg.SVGCanvasProvider; 82 import org.krysalis.barcode.tools.MimeTypes; 83 84 import org.apache.avalon.framework.configuration.Configuration; 85 import org.apache.avalon.framework.configuration.DefaultConfiguration; 86 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 87 import org.apache.avalon.framework.logger.Logger; 88 import org.apache.commons.cli.CommandLine; 89 import org.apache.commons.cli.CommandLineParser; 90 import org.apache.commons.cli.HelpFormatter; 91 import org.apache.commons.cli.MissingOptionException; 92 import org.apache.commons.cli.Option; 93 import org.apache.commons.cli.OptionBuilder; 94 import org.apache.commons.cli.OptionGroup; 95 import org.apache.commons.cli.Options; 96 import org.apache.commons.cli.ParseException; 97 import org.apache.commons.cli.PosixParser; 98 99 104 public class Main { 105 106 private static final String [] APP_HEADER = { 107 "Krysalis Barcode command-line application, Version 1.0beta", 108 "Copyright (c) 2000-2003 " 109 + "The Krysalis Community Project. All rights reserved.", 110 ""}; 111 112 113 public static PrintStream stdout = System.out; 114 115 public static PrintStream stderr = System.err; 116 117 private static ExitHandler exitHandler = new DefaultExitHandler(); 118 private Options options; 119 private boolean headerPrinted = false; 120 private Logger log; 121 122 126 public static void main(String [] args) { 127 Main app = new Main(); 128 app.handleCommandLine(args); 129 135 } 136 137 141 public static void setExitHandler(ExitHandler handler) { 142 exitHandler = handler; 143 } 144 145 150 public void handleCommandLine(String [] args) { 151 CommandLine cl; 152 String [] msg; 153 try { 154 CommandLineParser clp = new PosixParser(); 155 cl = clp.parse(getOptions(), args); 156 157 msg = cl.getArgs(); 159 if (msg.length == 0) { 160 throw new ParseException("No message"); 161 } 162 if (msg.length > 1) { 163 throw new ParseException("Too many parameters: " + msg.length); 164 } 165 } catch (MissingOptionException moe) { 166 printHelp(new PrintWriter (stdout)); 167 exitHandler.failureExit(this, 168 "Bad command line. Missing option: " + moe.getMessage(), null, -2); 169 return; } catch (ParseException pe) { 171 printHelp(new PrintWriter (stdout)); 172 exitHandler.failureExit(this, 174 "Bad command line: " + pe.getMessage(), null, -2); 175 return; } 177 try { 178 OutputStream out; 179 if (!cl.hasOption("o")) { 180 log = new AdvancedConsoleLogger(AdvancedConsoleLogger.LEVEL_ERROR, 181 false, stderr, stderr); 182 printAppHeader(); 183 out = stdout; 184 } else { 185 int logLevel = AdvancedConsoleLogger.LEVEL_INFO; 186 if (cl.hasOption('v')) { 187 logLevel = AdvancedConsoleLogger.LEVEL_DEBUG; 188 } 189 log = new AdvancedConsoleLogger(logLevel, false, stdout, stderr); 190 printAppHeader(); 191 File outFile = new File (cl.getOptionValue("o")); 192 if (log.isDebugEnabled()) { 193 log.debug("Output to: " + outFile.getCanonicalPath()); 194 } 195 out = new java.io.FileOutputStream (outFile); 196 } 197 198 log.debug("Message: " + msg[0]); 199 200 String format = MimeTypes.expandFormat( 202 cl.getOptionValue("f", MimeTypes.MIME_SVG)); 203 log.info("Generating " + format + "..."); 204 BarcodeUtil util = BarcodeUtil.getInstance(); 205 BarcodeGenerator gen = util.createBarcodeGenerator( 206 getConfiguration(cl), log); 207 208 if (MimeTypes.MIME_SVG.equals(format)) { 209 SVGCanvasProvider svg = new SVGCanvasProvider(false); 211 gen.generateBarcode(svg, msg[0]); 212 213 try { 215 TransformerFactory factory = TransformerFactory.newInstance(); 216 Transformer trans = factory.newTransformer(); 217 Source src = new javax.xml.transform.dom.DOMSource ( 218 svg.getDOMFragment()); 219 Result res = new javax.xml.transform.stream.StreamResult (out); 220 trans.transform(src, res); 221 } catch (TransformerException te) { 222 exitHandler.failureExit(this, "XML/XSLT library error", te, -6); 223 } 224 } else if (MimeTypes.MIME_EPS.equals(format)) { 225 EPSCanvasProvider eps = new EPSCanvasProvider(out); 226 gen.generateBarcode(eps, msg[0]); 227 eps.finish(); 228 } else { 229 int dpi = Integer.parseInt(cl.getOptionValue('d', "300")); 230 log.debug("Resolution: " + dpi + "dpi"); 231 BitmapCanvasProvider bitmap; 232 if (cl.hasOption("bw")) { 233 log.debug("Black/white image (1-bit)"); 234 bitmap = new BitmapCanvasProvider(out, 235 format, dpi, BufferedImage.TYPE_BYTE_BINARY, false); 236 } else { 237 log.debug("Grayscale image (8-bit) with anti-aliasing"); 238 bitmap = new BitmapCanvasProvider(out, 239 format, dpi, BufferedImage.TYPE_BYTE_GRAY, true); 240 } 241 gen.generateBarcode(bitmap, msg[0]); 242 bitmap.finish(); 243 } 244 245 out.close(); 246 log.info("done."); 247 exitHandler.successfulExit(this); 248 } catch (IOException ioe) { 249 exitHandler.failureExit(this, 250 "Error writing output file: " + ioe.getMessage(), null, -5); 251 } catch (BarcodeException be) { 252 exitHandler.failureExit(this, 253 "Error generating the barcode", be, -3); 254 } 255 } 256 257 private Options getOptions() { 258 if (options == null) { 259 this.options = new Options(); 260 Option opt; 261 262 this.options.addOption(OptionBuilder 263 .withLongOpt("verbose") 264 .withDescription("enable debug output") 265 .create('v')); 266 267 this.options.addOption(OptionBuilder 269 .withLongOpt("output") 270 .withArgName("file") 271 .hasArg() 272 .withDescription("the output filename") 273 .create('o')); 274 275 OptionGroup group = new OptionGroup(); 277 group.setRequired(true); 278 group.addOption(OptionBuilder 279 .withArgName("file") 280 .withLongOpt("config") 281 .hasArg() 282 .withDescription("the config file") 283 .create('c')); 284 group.addOption(OptionBuilder 285 .withArgName("name") 286 .withLongOpt("symbol") 287 .hasArg() 288 .withDescription("the barcode symbology to select " 289 + "(default settings, use -c if you want to customize)") 290 .create('s')); 291 this.options.addOptionGroup(group); 292 293 this.options.addOption(OptionBuilder 295 .withArgName("format") 296 .withLongOpt("format") 297 .hasArg() 298 .withDescription("the output format: MIME type or file " 299 + "extension\n" 300 + "Default: " + MimeTypes.MIME_SVG + " (SVG)") 301 .create('f')); 302 303 this.options.addOption(OptionBuilder 305 .withArgName("integer") 306 .withLongOpt("dpi") 307 .hasArg() 308 .withDescription("(for bitmaps) the image resolution in dpi\n" 309 + "Default: 300") 310 .create('d')); 311 this.options.addOption(OptionBuilder 312 .withLongOpt("bw") 313 .withDescription("(for bitmaps) create monochrome (1-bit) " 314 + "image instead of grayscale (8-bit)") 315 .create()); 316 } 317 return this.options; 318 } 319 320 private Configuration getConfiguration(CommandLine cl) { 321 if (cl.hasOption("s")) { 322 String sym = cl.getOptionValue("s"); 323 DefaultConfiguration cfg = new DefaultConfiguration("cfg"); 324 DefaultConfiguration child = new DefaultConfiguration(sym); 325 cfg.addChild(child); 326 return cfg; 327 } 328 if (cl.hasOption("c")) { 329 try { 330 String filename = cl.getOptionValue("c"); 331 File cfgFile = new File (filename); 332 if (!cfgFile.exists() || !cfgFile.isFile()) { 333 throw new FileNotFoundException ( 334 "Config file not found: " + cfgFile); 335 } 336 log.info("Using configuration: " + cfgFile); 337 338 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 339 return builder.buildFromFile(cfgFile); 340 } catch (Exception e) { 341 exitHandler.failureExit(this, 342 "Error reading configuration file: " + e.getMessage(), null, -3); 343 } 344 } 345 return new DefaultConfiguration("cfg"); 346 } 347 348 352 public void printAppHeader() { 353 if (!headerPrinted) { 354 if (log != null) { 355 for (int i = 0; i < APP_HEADER.length; i++) { 356 log.info(APP_HEADER[i]); 357 } 358 } else { 359 for (int i = 0; i < APP_HEADER.length; i++) { 360 stdout.println(APP_HEADER[i]); 361 } 362 } 363 headerPrinted = true; 364 } 365 } 366 367 private void printHelp(PrintWriter writer) { 368 printAppHeader(); 369 370 Set knownMimes = new java.util.HashSet (); 372 knownMimes.add(null); 373 knownMimes.add(""); 374 knownMimes.add(MimeTypes.MIME_PNG); 375 knownMimes.add("image/png"); 376 knownMimes.add(MimeTypes.MIME_JPEG); 377 knownMimes.add(MimeTypes.MIME_TIFF); 378 knownMimes.add(MimeTypes.MIME_GIF); 379 Set additionalMimes = BitmapEncoderRegistry.getSupportedMIMETypes(); 380 additionalMimes.removeAll(knownMimes); 381 382 HelpFormatter help = new HelpFormatter(); 383 final String unavailable = " (unavailable)"; 384 help.printHelp(writer, HelpFormatter.DEFAULT_WIDTH, 385 "java -jar krysalis-barcode.jar " 386 + "[-o <file>] [-t <type>] [-c <cfg-file>] <message>", 387 null, 388 getOptions(), 389 HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, 390 "Valid output formats:" 391 + "\nSVG: " + MimeTypes.MIME_SVG + ", svg" 392 + "\nEPS: " + MimeTypes.MIME_EPS + ", eps" 393 + "\nPNG: " + MimeTypes.MIME_PNG + ", png" 394 + (BitmapEncoderRegistry.supports(MimeTypes.MIME_PNG) 395 ? "" : unavailable) 396 + "\nTIFF: " + MimeTypes.MIME_TIFF + ", tiff, tif" 397 + (BitmapEncoderRegistry.supports(MimeTypes.MIME_TIFF) 398 ? "" : unavailable) 399 + "\nJPEG: " + MimeTypes.MIME_JPEG + ", jpeg, jpg" 400 + (BitmapEncoderRegistry.supports(MimeTypes.MIME_JPEG) 401 ? "" : unavailable) 402 + "\nGIF: " + MimeTypes.MIME_GIF + ", gif" 403 + (BitmapEncoderRegistry.supports(MimeTypes.MIME_GIF) 404 ? "" : unavailable) 405 + (additionalMimes.size() > 0 406 ? "\nAdditional supported formats:\n" + additionalMimes 407 : "") 408 + "\n" 409 + "\nIf -o is omitted the output is written to stdout."); 410 writer.flush(); 411 412 } 413 414 } | Popular Tags |