1 17 18 19 20 package org.apache.fop.cli; 21 22 import java.io.File ; 23 import java.io.FileFilter ; 24 import java.io.OutputStream ; 25 import java.lang.reflect.Method ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.List ; 29 30 import org.apache.commons.io.IOUtils; 31 import org.apache.fop.apps.FOUserAgent; 32 import org.apache.fop.apps.MimeConstants; 33 34 37 public class Main { 38 39 44 public static URL [] getJARList() throws MalformedURLException { 45 File baseDir = new File (".").getAbsoluteFile().getParentFile(); 46 File buildDir; 47 if ("build".equals(baseDir.getName())) { 48 buildDir = baseDir; 49 baseDir = baseDir.getParentFile(); 50 } else { 51 buildDir = new File (baseDir, "build"); 52 } 53 File fopJar = new File (buildDir, "fop.jar"); 54 if (!fopJar.exists()) { 55 fopJar = new File (baseDir, "fop.jar"); 56 } 57 if (!fopJar.exists()) { 58 throw new RuntimeException ("fop.jar not found in directory: " 59 + baseDir.getAbsolutePath() + " (or below)"); 60 } 61 List jars = new java.util.ArrayList (); 62 jars.add(fopJar.toURL()); 63 File [] files; 64 FileFilter filter = new FileFilter () { 65 public boolean accept(File pathname) { 66 return pathname.getName().endsWith(".jar"); 67 } 68 }; 69 File libDir = new File (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 optionalLib = System.getProperty("fop.optional.lib"); 80 if (optionalLib != null) { 81 files = new File (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 [] urls = (URL [])jars.toArray(new URL [jars.size()]); 89 93 return urls; 94 } 95 96 99 public static boolean checkDependencies() { 100 try { 101 Class 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 e) { 108 return false; 109 } 110 } 111 112 116 public static void startFOPWithDynamicClasspath(String [] args) { 117 try { 118 URL [] urls = getJARList(); 119 ClassLoader loader = new java.net.URLClassLoader (urls, null); 122 Thread.currentThread().setContextClassLoader(loader); 123 Class clazz = Class.forName("org.apache.fop.cli.Main", true, loader); 124 Method mainMethod = clazz.getMethod("startFOP", new Class [] {String [].class}); 126 mainMethod.invoke(null, new Object [] {args}); 127 } catch (Exception e) { 128 System.err.println("Unable to start FOP:"); 129 e.printStackTrace(); 130 System.exit(-1); 131 } 132 } 133 134 138 public static void startFOP(String [] args) { 139 CommandLineOptions options = null; 143 FOUserAgent foUserAgent = null; 144 OutputStream out = null; 145 146 try { 147 options = new CommandLineOptions(); 148 options.parse(args); 149 150 foUserAgent = options.getFOUserAgent(); 151 String outputFormat = options.getOutputFormat(); 152 153 try { 154 if (options.getOutputFile() != null) { 155 out = new java.io.BufferedOutputStream ( 156 new java.io.FileOutputStream (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 if (!MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputFormat)) { 172 System.exit(0); 173 } 174 } catch (Exception 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 189 public static void main(String [] args) { 190 if (checkDependencies()) { 191 startFOP(args); 192 } else { 193 startFOPWithDynamicClasspath(args); 194 } 195 } 196 197 } 198 | Popular Tags |