001    package net.sf.jolene.constants;
002    
003    import org.apache.log4j.LogManager;
004    import org.apache.log4j.Logger;
005    
006    import java.lang.reflect.Field;
007    import java.util.HashMap;
008    import java.util.Map;
009    
010    /**
011     * Defined preferences for jolene.properties file. TODO maybe conver to Enum.
012     *
013     * @author Dan Howard
014     * @since Dec 31, 2005 10:40 PM
015     */
016    public class Prefs {
017    
018        private static final Logger log = LogManager.getLogger(Prefs.class);
019    
020        /**
021         * Default: 32. The initial size of the internal maps used to store documents.
022         *
023         * @see net.sf.jolene.factories.DocumentFactory
024         */
025        public static final Prefs CacheSize = new Prefs("CacheSize", 32);
026    
027        /**
028         * Default: true. Indicates whether or not document cacheing is enabled. Cached documents
029         * are read once when requested and copies are returned from the DocumentFactory. If cacheing
030         * is disabled then documents are read and parsed whenever they are requested.
031         *
032         * @see net.sf.jolene.factories.DocumentFactory
033         */
034        public static final Prefs DocumentCaching = new Prefs("DocumentCaching", true);
035    
036        /**
037         * When set to true /resources/application.properites file is updated based on the template labels.
038         */
039        public static final Prefs GenerateLocaleStrings = new Prefs("GenerateLocaleStrings", false);
040    
041        /**
042         * Default comma (,). Used by Multi-Select objects when setting or getting option values.
043         */
044        public static final Prefs SelectValueSeperator = new Prefs("SelectValueSeperator", ",");
045    
046        /**
047         * Default true. Indicates whether Label objects should stream back their label tags. You
048         * might want to set this to false if you intend to use Labels to stream back arbitrary
049         * HTML or JavaScript.  In these cases you could also use the Text object.
050         *
051         * @see net.sf.jolene.dom.Text
052         */
053        public static final Prefs StreamLabelTags = new Prefs("StreamLabelTags", true);
054    
055        /**
056         * Default false. Indicates whether or not the streaming engine and the string utilities should
057         * output in XHTML or HTML format.  It does not affect the parser.
058         */
059        public static final Prefs XHTMLStrict = new Prefs("XHTMLStrict", false);
060    
061        private static Map<String, Prefs> prefMap = null;
062    
063    
064        private final String name;
065        private Object value;
066    
067        private Prefs(String name, Object value) {
068            this.name = name;
069            this.value = value;
070        }
071    
072        @Override
073        public String toString() {
074            return name;
075        }
076    
077        public String getValue() {
078            return value.toString();
079        }
080    
081        public void setValue(Object value) {
082            this.value = value;
083        }
084    
085        public static Prefs valueOf(String name) throws IllegalArgumentException {
086            if (name == null) {
087                throw new IllegalArgumentException("valueOf parameter is null");
088            }
089    
090            if (prefMap == null) {
091                initPrefMap();
092            }
093            return prefMap.get(name.toLowerCase());
094        }
095    
096        /**
097         * Create a map using the pref strings as keys.
098         */
099        private static void initPrefMap() {
100            Object o = new Object();
101            synchronized (o) {
102                prefMap = new HashMap<String, Prefs>(12);
103    
104                Field[] fields = Prefs.class.getFields();
105                for (int j = 0; j < fields.length; j++) {
106    
107                    try {
108                        if (Prefs.class.equals(fields[j].getType())) {
109                            prefMap.put(fields[j].getName().toLowerCase(), (Prefs) fields[j].get(j));
110                        }
111                    } catch (IllegalAccessException e) {
112                        log.error(e.getMessage(), e);
113                    }
114                }
115            }
116        }
117    }
118