KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > xml > internal > 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.xml.internal;
11
12 import java.io.*;
13 import org.apache.log4j.*;
14 import org.apache.log4j.spi.*;
15 import org.apache.log4j.varia.NullAppender;
16
17 import org.jdom.input.SAXBuilder;
18 import org.jdom.Document;
19 import org.jdom.Element;
20 import org.jdom.JDOMException;
21 import org.openlaszlo.utils.FileUtils;
22 import org.openlaszlo.xml.*;
23
24 /**
25  * Log4j appender that simply keeps state as to
26  * whether or not there has been an error, warning, or fatal
27  * log message
28  */

29 class ErrorChecker extends NullAppender {
30     public boolean hadError = false;
31     public void doAppend(LoggingEvent e) {
32         super.doAppend(e);
33         if (e.level == Level.WARN ||
34             e.level == Level.FATAL ||
35             e.level == Level.ERROR) {
36             hadError = true;
37         }
38     }
39 }
40
41 /**
42  * Test class for media converter
43  */

44 public class Main {
45
46     /**
47      * Usage: java org.openlaszlo.xml.Main in out
48      *
49      * Converts input xml to output swf.
50      *
51      * @param args the command line arguments
52      */

53     public static void main(String JavaDoc args[]) {
54         lzdc(args);
55     }
56
57    /**
58      * Allow execution from the command line
59      *
60      * @param args[] 2 arguments - [XML input document],
61      * [SWF output document]
62      */

63
64     public static int lzdc(String JavaDoc[] args) {
65
66         int exitStatus = 0;
67
68         // Configure logging
69
Logger logger = Logger.getRootLogger();
70         logger.setLevel(Level.ERROR);
71
72         logger.addAppender(new ConsoleAppender(
73            new PatternLayout("%r msecs [%p] - %m%n" )));
74
75         ErrorChecker errorChecker = new ErrorChecker();
76         logger.addAppender(errorChecker);
77
78         int swfversionNum = 5;
79
80         try {
81             if (args.length < 2) {
82                 System.err.println("Usage: lzdc [-v] [-i] from to");
83                 return -1;
84             }
85
86             for (int i = 0; i < args.length; i++) {
87                 String JavaDoc arg = args[i].intern();
88                 if (arg == "-v") {
89                     logger.setLevel(Level.ALL);
90                 } else if (arg == "-i") {
91                     logger.setLevel(Level.INFO);
92                 } else if (arg == "-5") {
93                     swfversionNum = 5;
94                 } else if (arg == "-6") {
95                     swfversionNum = 6;
96                 } else if (arg == "-7") {
97                     swfversionNum = 7;
98                 }
99             }
100
101             String JavaDoc fromFileName = args[args.length-2];
102             String JavaDoc toFileName = args[args.length-1];
103
104             String JavaDoc xmlstring = FileUtils.readFileString(new File(fromFileName), "UTF-8");
105             // Create and load properties
106
logger.info("Reading XML from " + fromFileName);
107
108             InputStream is = DataCompiler.compile(xmlstring, swfversionNum);
109             FileOutputStream w = new FileOutputStream(toFileName);
110
111             logger.info("Writing to " + toFileName);
112             
113             //de.buildFromDocument(doc);
114
FileUtils.send(is, w);
115             w.flush();
116
117             logger.info("Done");
118
119         } catch (DataCompilerException e) {
120             exitStatus = -1;
121             System.err.println("DataCompilerException: " + e.getMessage());
122             e.printStackTrace();
123         } catch (IOException e) {
124             exitStatus = -1;
125             System.err.println("IO exception: " + e.getMessage());
126             e.printStackTrace();
127         }
128
129         if (errorChecker.hadError)
130             exitStatus = -1;
131
132         return exitStatus;
133     }
134 }
135
Popular Tags