1 package org.jmux.app; 2 3 import org.jmux.app.component.config.Config; 4 5 import java.io.Serializable; 6 import java.util.ArrayList; 7 import java.util.HashMap; 8 import java.util.List; 9 import java.util.Map; 10 11 /****************************************************************************** 12 jmux - Java Modules Using XML 13 Copyright © 2006 jmux.org 14 15 This library is free software; you can redistribute it and/or 16 modify it under the terms of the GNU Lesser General Public 17 License as published by the Free Software Foundation; either 18 version 2.1 of the License, or (at your option) any later version. 19 20 This library is distributed in the hope that it will be useful, 21 but WITHOUT ANY WARRANTY; without even the implied warranty of 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 23 Lesser General Public License for more details. 24 25 You should have received a copy of the GNU Lesser General Public 26 License along with this library; if not, write to the Free Software 27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 28 *****************************************************************************/ 29 30 /*** 31 * @author donaldw 32 */ 33 public class ConfigImpl implements Config, Serializable { 34 private final String name; 35 private String value; 36 private final Map<String, List<Config>> children = new HashMap<String, List<Config>>(); 37 38 public ConfigImpl(String name) { 39 this.name = name; 40 } 41 42 public String getName() { 43 return name; 44 } 45 46 void setValue(String value) { 47 this.value = value; 48 } 49 50 public Config getConf(String name) { 51 List<Config> list = children.get(name); 52 return (list != null ? list.get(0) : null); 53 } 54 55 public List<Config> getConfs(String name) { 56 return children.get(name); 57 } 58 59 void setConf(String name, ConfigImpl configImpl) { 60 List<Config> list; 61 62 if (children.containsKey(name)) { 63 list = children.get(name); 64 } else { 65 list = new ArrayList<Config>(); 66 children.put(name, list); 67 } 68 69 list.add(configImpl); 70 } 71 72 public boolean getBoolean(boolean def) { 73 if (value.equals("true")) { 74 return true; 75 } else if (value.equals("false")) { 76 return false; 77 } else { 78 throw new AppException("Could not parse " + value); 79 } 80 } 81 82 public String getString(String def) { 83 if (value.equals("")) { 84 return def; 85 } else { 86 return value; 87 } 88 } 89 90 public int getInteger(int def) { 91 if (value.equals("")) { 92 return def; 93 } 94 try { 95 return Integer.parseInt(value); 96 } catch (NumberFormatException ex) { 97 throw new AppException("Could not parse " + value, ex); 98 } 99 } 100 101 public boolean getBoolean(boolean defaultVal, String... names) { 102 Config config = this; 103 for (String name : names) { 104 config = config.getConf(name); 105 if (config == null) { 106 return defaultVal; 107 } 108 } 109 return config.getBoolean(defaultVal); 110 } 111 112 public String getString(String defaultVal, String... names) { 113 Config config = this; 114 for (String name : names) { 115 config = config.getConf(name); 116 if (config == null) { 117 return defaultVal; 118 } 119 } 120 return config.getString(defaultVal); 121 } 122 123 public int getInteger(int defaultVal, String... names) { 124 Config config = this; 125 for (String name : names) { 126 config = config.getConf(name); 127 if (config == null) { 128 return defaultVal; 129 } 130 } 131 return config.getInteger(defaultVal); 132 } 133 134 public String toString() { 135 return "Config(" + name + "=" + value + ((children != null & children.size() > 0) ? ")..." : ")."); 136 } 137 }