KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > apps > Options


1 /*
2  * $Id: Options.java,v 1.9.2.2 2003/02/25 10:18:32 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.apps;
52
53 // SAX
54
import org.xml.sax.InputSource JavaDoc;
55
56 // Java
57
import java.io.File JavaDoc;
58 import java.io.InputStream JavaDoc;
59
60 // FOP
61
import org.apache.fop.messaging.MessageHandler;
62 import org.apache.fop.configuration.Configuration;
63 import org.apache.fop.configuration.ConfigurationReader;
64
65 /**
66  * Options handles loading of configuration files and
67  * additional setting of commandline options
68  */

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 JavaDoc userConfig) throws FOPException {
78         this();
79         this.loadUserconfiguration(userConfig);
80     }
81
82     public Options(InputSource JavaDoc userConfig) throws FOPException {
83         this();
84         this.loadUserconfiguration(userConfig);
85     }
86
87     public Options(File JavaDoc 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     // initializing option settings
98
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     // setting clOptions
112
void setCommandLineOptions(CommandLineOptions clOptions) {
113         // load user configuration file,if there is one
114
File JavaDoc userConfigFile = clOptions.getUserConfigFile();
115         if (userConfigFile != null) {
116             this.loadUserconfiguration(userConfigFile);
117         }
118
119         // debug mode
120
if (clOptions.isDebugMode() != null) {
121             errorDump = clOptions.isDebugMode().booleanValue();
122             Configuration.put("debugMode", new Boolean JavaDoc(errorDump));
123         }
124
125         // show configuration settings
126
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         // quiet mode
140
if (clOptions.isQuiet() != null) {
141             MessageHandler.setQuiet(clOptions.isQuiet().booleanValue());
142         }
143
144         // set base directory
145
String JavaDoc baseDir = Configuration.getStringValue("baseDir");
146         if (baseDir == null) {
147             try {
148                 baseDir =
149                     new File JavaDoc(clOptions.getInputFile().getAbsolutePath()).getParentFile().toURL().toExternalForm();
150                 Configuration.put("baseDir", baseDir);
151             } catch (Exception JavaDoc e) {}
152         }
153         if (errorDump) {
154             MessageHandler.logln("base directory: " + baseDir);
155         }
156     }
157
158     /**
159      * loads standard configuration file and a user file, if it has been specified
160      */

161     public void loadStandardConfiguration() throws FOPException {
162         String JavaDoc file = "config.xml";
163         InputStream JavaDoc configfile = null;
164
165         // Try to use Context Class Loader to load the properties file.
166
try {
167             java.lang.reflect.Method JavaDoc getCCL =
168                 Thread JavaDoc.class.getMethod("getContextClassLoader", new Class JavaDoc[0]);
169             if (getCCL != null) {
170                 ClassLoader JavaDoc contextClassLoader =
171                     (ClassLoader JavaDoc)getCCL.invoke(Thread.currentThread(),
172                                                new Object JavaDoc[0]);
173                 configfile = contextClassLoader.getResourceAsStream("conf/"
174                         + file);
175             }
176         } catch (Exception JavaDoc e) {}
177
178         // the entry /conf/config.xml refers to a directory conf which is a sibling of org
179
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 JavaDoc(configfile));
191         if (errorDump) {
192             reader.setDumpError(true);
193         }
194         reader.start();
195
196     }
197
198     public void loadUserconfiguration(String JavaDoc userConfigFile) {
199         loadUserconfiguration(new File JavaDoc(userConfigFile));
200     }
201
202     public void loadUserconfiguration(File JavaDoc userConfigFile) {
203         if (userConfigFile != null) {
204             loadUserconfiguration(InputHandler.fileInputSource(userConfigFile));
205         }
206     }
207
208     public void loadUserconfiguration(InputStream JavaDoc userConfig) {
209         loadUserconfiguration(new InputSource JavaDoc(userConfig));
210     }
211
212     public void loadUserconfiguration(InputSource JavaDoc userConfigSource) {
213         // read user configuration
214
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