1 51 package org.apache.fop.apps; 52 53 import org.xml.sax.InputSource ; 55 56 import java.io.File ; 58 import java.io.InputStream ; 59 60 import org.apache.fop.messaging.MessageHandler; 62 import org.apache.fop.configuration.Configuration; 63 import org.apache.fop.configuration.ConfigurationReader; 64 65 69 public class Options { 70 boolean errorDump = false; 71 72 public Options() throws FOPException { 73 this.loadStandardConfiguration(); 74 initOptions(); 75 } 76 77 public Options(InputStream userConfig) throws FOPException { 78 this(); 79 this.loadUserconfiguration(userConfig); 80 } 81 82 public Options(InputSource userConfig) throws FOPException { 83 this(); 84 this.loadUserconfiguration(userConfig); 85 } 86 87 public Options(File userConfigFile) throws FOPException { 88 this(); 89 this.loadUserconfiguration(userConfigFile); 90 } 91 92 public Options(CommandLineOptions clOptions) throws FOPException { 93 this(); 94 this.setCommandLineOptions(clOptions); 95 } 96 97 void initOptions() { 99 if (Configuration.getBooleanValue("quiet").booleanValue()) { 100 MessageHandler.setQuiet(true); 101 } 102 if (Configuration.getBooleanValue("debugMode").booleanValue()) { 103 errorDump = true; 104 } 105 if (Configuration.getBooleanValue("dumpConfiguration").booleanValue()) { 106 Configuration.put("dumpConfiguration", "true"); 107 Configuration.dumpConfiguration(); 108 } 109 } 110 111 void setCommandLineOptions(CommandLineOptions clOptions) { 113 File userConfigFile = clOptions.getUserConfigFile(); 115 if (userConfigFile != null) { 116 this.loadUserconfiguration(userConfigFile); 117 } 118 119 if (clOptions.isDebugMode() != null) { 121 errorDump = clOptions.isDebugMode().booleanValue(); 122 Configuration.put("debugMode", new Boolean (errorDump)); 123 } 124 125 boolean dumpConfiguration; 127 if (clOptions.dumpConfiguration() != null) { 128 dumpConfiguration = clOptions.dumpConfiguration().booleanValue(); 129 } else { 130 dumpConfiguration = 131 Configuration.getBooleanValue("dumpConfiguration").booleanValue(); 132 } 133 if (dumpConfiguration) { 134 Configuration.put("dumpConfiguration", "true"); 135 Configuration.dumpConfiguration(); 136 System.exit(0); 137 } 138 139 if (clOptions.isQuiet() != null) { 141 MessageHandler.setQuiet(clOptions.isQuiet().booleanValue()); 142 } 143 144 String baseDir = Configuration.getStringValue("baseDir"); 146 if (baseDir == null) { 147 try { 148 baseDir = 149 new File (clOptions.getInputFile().getAbsolutePath()).getParentFile().toURL().toExternalForm(); 150 Configuration.put("baseDir", baseDir); 151 } catch (Exception e) {} 152 } 153 if (errorDump) { 154 MessageHandler.logln("base directory: " + baseDir); 155 } 156 } 157 158 161 public void loadStandardConfiguration() throws FOPException { 162 String file = "config.xml"; 163 InputStream configfile = null; 164 165 try { 167 java.lang.reflect.Method getCCL = 168 Thread .class.getMethod("getContextClassLoader", new Class [0]); 169 if (getCCL != null) { 170 ClassLoader contextClassLoader = 171 (ClassLoader )getCCL.invoke(Thread.currentThread(), 172 new Object [0]); 173 configfile = contextClassLoader.getResourceAsStream("conf/" 174 + file); 175 } 176 } catch (Exception e) {} 177 178 if (configfile == null) 180 configfile = 181 ConfigurationReader.class.getResourceAsStream("/conf/" 182 + file); 183 if (configfile == null) { 184 throw new FOPException("can't find default configuration file"); 185 } 186 if (errorDump) { 187 MessageHandler.logln("reading default configuration file"); 188 } 189 ConfigurationReader reader = 190 new ConfigurationReader(new InputSource (configfile)); 191 if (errorDump) { 192 reader.setDumpError(true); 193 } 194 reader.start(); 195 196 } 197 198 public void loadUserconfiguration(String userConfigFile) { 199 loadUserconfiguration(new File (userConfigFile)); 200 } 201 202 public void loadUserconfiguration(File userConfigFile) { 203 if (userConfigFile != null) { 204 loadUserconfiguration(InputHandler.fileInputSource(userConfigFile)); 205 } 206 } 207 208 public void loadUserconfiguration(InputStream userConfig) { 209 loadUserconfiguration(new InputSource (userConfig)); 210 } 211 212 public void loadUserconfiguration(InputSource userConfigSource) { 213 ConfigurationReader reader = 215 new ConfigurationReader(userConfigSource); 216 if (errorDump) { 217 reader.setDumpError(true); 218 } 219 try { 220 reader.start(); 221 } catch (org.apache.fop.apps.FOPException error) { 222 MessageHandler.errorln("Could not load user configuration " 223 + userConfigSource.getSystemId() + " - error: " 224 + error.getMessage()); 225 MessageHandler.errorln("using default values"); 226 if (errorDump) { 227 reader.dumpError(error); 228 } 229 } 230 } 231 232 } 233 234 235 | Popular Tags |