KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > media > Main


1 /* *****************************************************************************
2  * Main.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

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 /**
20  * Log4j appender that simply keeps state as to
21  * whether or not there has been an error, warning, or fatal
22  * log message
23  */

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 /**
37  * Test class for media converter
38  */

39 public class Main {
40
41     /**
42      * Usage: java org.openlaszlo.media.Main in out
43      *
44      * Converts input ttf to output fft.
45      *
46      * @param args the command line arguments
47      */

48     public static void main(String JavaDoc args[]) {
49         lzmc(args);
50     }
51
52     /** This method implements the behavior described in main
53      * but also returns an integer error code.
54      */

55     public static int lzmc(String JavaDoc args[]) {
56
57         int exitStatus = 0;
58
59         // Configure logging
60
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 JavaDoc 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 JavaDoc fromFileName = args[args.length-2];
85             String JavaDoc toFileName = args[args.length-1];
86             String JavaDoc fromType = MimeType.fromExtension(fromFileName);
87             String JavaDoc toType = MimeType.fromExtension(toFileName);
88
89             // Assume it's a font.
90
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