1 4 5 9 10 package org.openlaszlo.media; 11 12 import org.openlaszlo.utils.FileUtils; 13 14 import java.io.*; 15 import org.apache.log4j.*; 16 import org.apache.log4j.spi.*; 17 import org.apache.log4j.varia.NullAppender; 18 19 24 class ErrorChecker extends NullAppender { 25 public boolean hadError = false; 26 public void doAppend(LoggingEvent e) { 27 super.doAppend(e); 28 if (e.level == Level.WARN || 29 e.level == Level.FATAL || 30 e.level == Level.ERROR) { 31 hadError = true; 32 } 33 } 34 } 35 36 39 public class Main { 40 41 48 public static void main(String args[]) { 49 lzmc(args); 50 } 51 52 55 public static int lzmc(String args[]) { 56 57 int exitStatus = 0; 58 59 Logger logger = Logger.getRootLogger(); 61 logger.setLevel(Level.ERROR); 62 63 logger.addAppender(new ConsoleAppender( 64 new PatternLayout("%r msecs [%p] - %m%n" ))); 65 66 ErrorChecker errorChecker = new ErrorChecker(); 67 logger.addAppender(errorChecker); 68 69 try { 70 for (int i = 0; i < args.length; i++) { 71 String arg = args[i].intern(); 72 if (arg == "-v") { 73 logger.setLevel(Level.ALL); 74 } else if (arg == "-i") { 75 logger.setLevel(Level.INFO); 76 } 77 } 78 79 if (args.length < 2) { 80 System.err.println("Usage: lzmc [-v] [-i] from to"); 81 return -1; 82 } 83 84 String fromFileName = args[args.length-2]; 85 String toFileName = args[args.length-1]; 86 String fromType = MimeType.fromExtension(fromFileName); 87 String toType = MimeType.fromExtension(toFileName); 88 89 if (fromType.equalsIgnoreCase("UNKNOWN")) { 91 fromType = FontType.TTF; 92 toType = FontType.FFT; 93 } 94 95 logger.info("Converting from " + fromType + " to " + toType); 96 File from = new File(fromFileName); 97 OutputStream to = new FileOutputStream(toFileName); 98 InputStream cvt = Transcoder.transcode(from, fromType, toType); 99 logger.info("Finished conversion"); 100 FileUtils.send(cvt, to); 101 to.flush(); 102 to.close(); 103 logger.info("Finished writing file"); 104 } catch (TranscoderException e) { 105 exitStatus = -1; 106 System.err.println("Transcoder Exception:" + e.getMessage()); 107 } catch (IOException e) { 108 exitStatus = -1; 109 System.err.println("IO exception: " + e.getMessage()); 110 e.printStackTrace(); 111 } 112 113 if (errorChecker.hadError) 114 exitStatus = -1; 115 116 return exitStatus; 117 } 118 } 119 | Popular Tags |