001: /* *****************************************************************************
002: * Main.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2001-2007 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: package org.openlaszlo.media;
011:
012: import org.openlaszlo.utils.FileUtils;
013:
014: import java.io.*;
015: import org.apache.log4j.*;
016: import org.apache.log4j.spi.*;
017: import org.apache.log4j.varia.NullAppender;
018:
019: /**
020: * Log4j appender that simply keeps state as to
021: * whether or not there has been an error, warning, or fatal
022: * log message
023: */
024: class ErrorChecker extends NullAppender {
025: public boolean hadError = false;
026:
027: public void doAppend(LoggingEvent e) {
028: super .doAppend(e);
029: if (e.level == Level.WARN || e.level == Level.FATAL
030: || e.level == Level.ERROR) {
031: hadError = true;
032: }
033: }
034: }
035:
036: /**
037: * Test class for media converter
038: */
039: public class Main {
040:
041: /**
042: * Usage: java org.openlaszlo.media.Main in out
043: *
044: * Converts input ttf to output fft.
045: *
046: * @param args the command line arguments
047: */
048: public static void main(String args[]) {
049: lzmc(args);
050: }
051:
052: /** This method implements the behavior described in main
053: * but also returns an integer error code.
054: */
055: public static int lzmc(String args[]) {
056:
057: int exitStatus = 0;
058:
059: // Configure logging
060: Logger logger = Logger.getRootLogger();
061: logger.setLevel(Level.ERROR);
062:
063: logger.addAppender(new ConsoleAppender(new PatternLayout(
064: "%r msecs [%p] - %m%n")));
065:
066: ErrorChecker errorChecker = new ErrorChecker();
067: logger.addAppender(errorChecker);
068:
069: try {
070: for (int i = 0; i < args.length; i++) {
071: String arg = args[i].intern();
072: if (arg == "-v") {
073: logger.setLevel(Level.ALL);
074: } else if (arg == "-i") {
075: logger.setLevel(Level.INFO);
076: }
077: }
078:
079: if (args.length < 2) {
080: System.err.println("Usage: lzmc [-v] [-i] from to");
081: return -1;
082: }
083:
084: String fromFileName = args[args.length - 2];
085: String toFileName = args[args.length - 1];
086: String fromType = MimeType.fromExtension(fromFileName);
087: String toType = MimeType.fromExtension(toFileName);
088:
089: // Assume it's a font.
090: if (fromType.equalsIgnoreCase("UNKNOWN")) {
091: fromType = FontType.TTF;
092: toType = FontType.FFT;
093: }
094:
095: logger.info(
096: /* (non-Javadoc)
097: * @i18n.test
098: * @org-mes="Converting from " + p[0] + " to " + p[1]
099: */
100: org.openlaszlo.i18n.LaszloMessages.getMessage(Main.class
101: .getName(), "051018-100", new Object[] { fromType,
102: toType }));
103: File from = new File(fromFileName);
104: OutputStream to = new FileOutputStream(toFileName);
105: InputStream cvt = Transcoder.transcode(from, fromType,
106: toType);
107: logger.info(
108: /* (non-Javadoc)
109: * @i18n.test
110: * @org-mes="Finished conversion"
111: */
112: org.openlaszlo.i18n.LaszloMessages.getMessage(Main.class
113: .getName(), "051018-111"));
114: FileUtils.send(cvt, to);
115: to.flush();
116: to.close();
117: logger.info(
118: /* (non-Javadoc)
119: * @i18n.test
120: * @org-mes="Finished writing file"
121: */
122: org.openlaszlo.i18n.LaszloMessages.getMessage(Main.class
123: .getName(), "051018-122"));
124: } catch (TranscoderException e) {
125: exitStatus = -1;
126: System.err.println(
127: /* (non-Javadoc)
128: * @i18n.test
129: * @org-mes="Transcoder Exception:" + p[0]
130: */
131: org.openlaszlo.i18n.LaszloMessages.getMessage(Main.class
132: .getName(), "051018-132", new Object[] { e
133: .getMessage() }));
134: } catch (IOException e) {
135: exitStatus = -1;
136: System.err.println(
137: /* (non-Javadoc)
138: * @i18n.test
139: * @org-mes="IO exception: " + p[0]
140: */
141: org.openlaszlo.i18n.LaszloMessages.getMessage(Main.class
142: .getName(), "051018-142", new Object[] { e
143: .getMessage() }));
144: }
145:
146: if (errorChecker.hadError)
147: exitStatus = -1;
148:
149: return exitStatus;
150: }
151: }
|