KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > cli > Main


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: Main.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.cli;
21
22 import java.io.File JavaDoc;
23 import java.io.FileFilter JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.commons.io.IOUtils;
31 import org.apache.fop.apps.FOUserAgent;
32 import org.apache.fop.apps.MimeConstants;
33
34 /**
35  * Main command-line class for Apache FOP.
36  */

37 public class Main {
38
39     /**
40      * @return the list of URLs to all libraries.
41      * @throws MalformedURLException In case there is a problem converting java.io.File
42      * instances to URLs.
43      */

44     public static URL JavaDoc[] getJARList() throws MalformedURLException JavaDoc {
45         File JavaDoc baseDir = new File JavaDoc(".").getAbsoluteFile().getParentFile();
46         File JavaDoc buildDir;
47         if ("build".equals(baseDir.getName())) {
48             buildDir = baseDir;
49             baseDir = baseDir.getParentFile();
50         } else {
51             buildDir = new File JavaDoc(baseDir, "build");
52         }
53         File JavaDoc fopJar = new File JavaDoc(buildDir, "fop.jar");
54         if (!fopJar.exists()) {
55             fopJar = new File JavaDoc(baseDir, "fop.jar");
56         }
57         if (!fopJar.exists()) {
58             throw new RuntimeException JavaDoc("fop.jar not found in directory: "
59                     + baseDir.getAbsolutePath() + " (or below)");
60         }
61         List JavaDoc jars = new java.util.ArrayList JavaDoc();
62         jars.add(fopJar.toURL());
63         File JavaDoc[] files;
64         FileFilter JavaDoc filter = new FileFilter JavaDoc() {
65             public boolean accept(File JavaDoc pathname) {
66                 return pathname.getName().endsWith(".jar");
67             }
68         };
69         File JavaDoc libDir = new File JavaDoc(baseDir, "lib");
70         if (!libDir.exists()) {
71             libDir = baseDir;
72         }
73         files = libDir.listFiles(filter);
74         if (files != null) {
75             for (int i = 0, size = files.length; i < size; i++) {
76                 jars.add(files[i].toURL());
77             }
78         }
79         String JavaDoc optionalLib = System.getProperty("fop.optional.lib");
80         if (optionalLib != null) {
81             files = new File JavaDoc(optionalLib).listFiles(filter);
82             if (files != null) {
83                 for (int i = 0, size = files.length; i < size; i++) {
84                     jars.add(files[i].toURL());
85                 }
86             }
87         }
88         URL JavaDoc[] urls = (URL JavaDoc[])jars.toArray(new URL JavaDoc[jars.size()]);
89         /*
90         for (int i = 0, c = urls.length; i < c; i++) {
91             System.out.println(urls[i]);
92         }*/

93         return urls;
94     }
95     
96     /**
97      * @return true if FOP's dependecies are available in the current ClassLoader setup.
98      */

99     public static boolean checkDependencies() {
100         try {
101             //System.out.println(Thread.currentThread().getContextClassLoader());
102
Class JavaDoc clazz = Class.forName("org.apache.commons.io.IOUtils");
103             if (clazz != null) {
104                 clazz = Class.forName("org.apache.avalon.framework.configuration.Configuration");
105             }
106             return (clazz != null);
107         } catch (Exception JavaDoc e) {
108             return false;
109         }
110     }
111     
112     /**
113      * Dynamically builds a ClassLoader and executes FOP.
114      * @param args command-line arguments
115      */

116     public static void startFOPWithDynamicClasspath(String JavaDoc[] args) {
117         try {
118             URL JavaDoc[] urls = getJARList();
119             //System.out.println("CCL: "
120
// + Thread.currentThread().getContextClassLoader().toString());
121
ClassLoader JavaDoc loader = new java.net.URLClassLoader JavaDoc(urls, null);
122             Thread.currentThread().setContextClassLoader(loader);
123             Class JavaDoc clazz = Class.forName("org.apache.fop.cli.Main", true, loader);
124             //System.out.println("CL: " + clazz.getClassLoader().toString());
125
Method JavaDoc mainMethod = clazz.getMethod("startFOP", new Class JavaDoc[] {String JavaDoc[].class});
126             mainMethod.invoke(null, new Object JavaDoc[] {args});
127         } catch (Exception JavaDoc e) {
128             System.err.println("Unable to start FOP:");
129             e.printStackTrace();
130             System.exit(-1);
131         }
132     }
133     
134     /**
135      * Executes FOP with the given ClassLoader setup.
136      * @param args command-line arguments
137      */

138     public static void startFOP(String JavaDoc[] args) {
139         //System.out.println("static CCL: "
140
// + Thread.currentThread().getContextClassLoader().toString());
141
//System.out.println("static CL: " + Fop.class.getClassLoader().toString());
142
CommandLineOptions options = null;
143         FOUserAgent foUserAgent = null;
144         OutputStream JavaDoc out = null;
145
146         try {
147             options = new CommandLineOptions();
148             options.parse(args);
149             
150             foUserAgent = options.getFOUserAgent();
151             String JavaDoc outputFormat = options.getOutputFormat();
152
153             try {
154                 if (options.getOutputFile() != null) {
155                     out = new java.io.BufferedOutputStream JavaDoc(
156                             new java.io.FileOutputStream JavaDoc(options.getOutputFile()));
157                     foUserAgent.setOutputFile(options.getOutputFile());
158                 }
159                 if (!MimeConstants.MIME_XSL_FO.equals(outputFormat)) {
160                     options.getInputHandler().renderTo(foUserAgent, outputFormat, out);
161                 } else {
162                     options.getInputHandler().transformTo(out);
163                 }
164              } finally {
165                  IOUtils.closeQuietly(out);
166              }
167
168             // System.exit(0) called to close AWT/SVG-created threads, if any.
169
// AWTRenderer closes with window shutdown, so exit() should not
170
// be called here
171
if (!MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputFormat)) {
172                 System.exit(0);
173             }
174         } catch (Exception JavaDoc e) {
175             if (options != null) {
176                 options.getLogger().error("Exception", e);
177             }
178             if (options.getOutputFile() != null) {
179                 options.getOutputFile().delete();
180             }
181             System.exit(1);
182         }
183     }
184     
185     /**
186      * The main routine for the command line interface
187      * @param args the command line parameters
188      */

189     public static void main(String JavaDoc[] args) {
190         if (checkDependencies()) {
191             startFOP(args);
192         } else {
193             startFOPWithDynamicClasspath(args);
194         }
195     }
196
197 }
198
Popular Tags