001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.config;
038:
039: import edu.rice.cs.util.UnexpectedException;
040:
041: import java.util.Properties;
042: import java.util.Iterator;
043: import java.io.*;
044: import java.lang.reflect.*;
045:
046: public class OptionMapLoader implements OptionConstants {
047:
048: /** bag of default options (programmatically defined, instead of in an options file) */
049: private static DefaultOptionMap DEFAULTS = new DefaultOptionMap();
050: private static Properties DEFAULT_STRINGS = new Properties();
051:
052: static {
053: // initialize DEFAULTS objects, based on OptionConstants using reflection.
054: Field[] fields = OptionConstants.class.getDeclaredFields();
055: for (int i = 0; i < fields.length; i++) {
056: Field field = fields[i];
057: int mods = field.getModifiers();
058: if (Modifier.isStatic(mods) && Modifier.isPublic(mods)
059: && Modifier.isFinal(mods)) {
060: // field is public static and final.
061: Option<?> option;
062: try {
063: Object o = field.get(null); // we should be able to pass in null as the 'receiver', since it's static.
064: //System.out.println("field name: "+field.getName()+" o: "+o);
065: if (o == null || !(o instanceof Option)) {
066: continue; // Development options can be null in the stable version of the code
067: }
068:
069: option = (Option<?>) o;
070: } catch (IllegalAccessException e) {
071: // this cannot happen, since we don't get in here unless the field is public.
072: throw new UnexpectedException(e);
073: }
074:
075: String sval = option.getDefaultString();
076: DEFAULT_STRINGS.setProperty(option.name, sval);
077: DEFAULTS.setString(option, sval);
078: }
079: }
080: }
081:
082: /** Default OptionMapLoader. */
083: public static final OptionMapLoader DEFAULT = new OptionMapLoader(
084: DEFAULT_STRINGS);
085:
086: /** Creates an OptionMapLoader from a given input stream. Does not maintain a reference to this input stream.
087: * @param is the input stream to read.
088: */
089: public OptionMapLoader(InputStream is) throws IOException {
090: this (new Properties(DEFAULT_STRINGS));
091: try {
092: prop.load(is);
093: } finally {
094: is.close();
095: }
096: }
097:
098: private final Properties prop;
099:
100: private OptionMapLoader(Properties prop) {
101: this .prop = prop;
102: }
103:
104: public void loadInto(OptionMap map) {
105: Iterator<OptionParser<?>> options = DEFAULTS.keys();
106: while (options.hasNext()) {
107: OptionParser<?> option = options.next();
108: String val = prop.getProperty(option.name);
109: map.setString(option, val);
110: }
111: }
112: }
|