|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ConfigurationParserFactory.java | 60% | 73,1% | 50% | 67,5% |
|
||||||||||||||
| 1 | /* | |
| 2 | * ConfigurationParserFactory.java | |
| 3 | * | |
| 4 | * Created on 3. September 2003, 12:39 | |
| 5 | */ | |
| 6 | ||
| 7 | package org.jconfig.parser; | |
| 8 | ||
| 9 | import java.io.IOException; | |
| 10 | import java.io.InputStream; | |
| 11 | import java.util.Properties; | |
| 12 | /** | |
| 13 | * The ConfigurationParserFactory determines which configuration parser | |
| 14 | * for the specified configuration should be used. This can be defined | |
| 15 | * in the jconfig.properties or as system property. <br> | |
| 16 | * The jconfig.properties must contain a property called "parser.{configname}" | |
| 17 | * (to define the parser for a specific configuration) or a property "parser". | |
| 18 | * This property must contain the fully qualified class name of the parser | |
| 19 | * <br> | |
| 20 | * If no jconfig.properties are found it will look for a system property | |
| 21 | * called "jconfig.parser". <br> | |
| 22 | * If nothing is found then it will use the DefaultConfigParser | |
| 23 | * | |
| 24 | * @author Andreas Mecky andreas.mecky@xcom.de | |
| 25 | * @author Terry Dye terry.dye@xcom.de | |
| 26 | */ | |
| 27 | public class ConfigurationParserFactory { | |
| 28 | ||
| 29 | /** Creates a new instance of ConfigurationParserFactory */ | |
| 30 | 0 | private ConfigurationParserFactory() { |
| 31 | } | |
| 32 | ||
| 33 | 39 | public static ConfigurationParser getParser(String configName) { |
| 34 | 39 | String configParser = getParserClassName(configName); |
| 35 | 39 | if ( configParser == null ) { |
| 36 | 0 | return getDefaultParser(); |
| 37 | } | |
| 38 | else { | |
| 39 | 39 | try { |
| 40 | 39 | Class clazz = Class.forName(configParser); |
| 41 | 39 | return (ConfigurationParser)clazz.newInstance(); |
| 42 | } | |
| 43 | catch (Exception e) { | |
| 44 | 0 | e.printStackTrace(); |
| 45 | } | |
| 46 | 0 | return getDefaultParser(); |
| 47 | } | |
| 48 | } | |
| 49 | ||
| 50 | /** | |
| 51 | * This method will try to find the class name of the specified configuration. | |
| 52 | * The first step is to check if we have jconfig.properties in the classpath | |
| 53 | * and a parser.{configname} entry. If not found the method looks for a parser | |
| 54 | * entry in the properties file. If not found or no properties are found it | |
| 55 | * will check for a system property called "jconfig.parser". At the end it | |
| 56 | * will return "org.jconfig.parser.DefaultConfigParser" is nothing is found. | |
| 57 | */ | |
| 58 | 39 | private static String getParserClassName(String configName) { |
| 59 | 39 | InputStream is = ConfigurationParserFactory.class.getClassLoader().getResourceAsStream("jconfig.properties"); |
| 60 | 39 | if ( is != null ) { |
| 61 | 39 | try { |
| 62 | 39 | Properties props = new Properties(); |
| 63 | 39 | props.load(is); |
| 64 | 39 | String testName = "parser."+configName; |
| 65 | 39 | String value = props.getProperty(testName); |
| 66 | 39 | if ( value != null ) { |
| 67 | 5 | return value; |
| 68 | } | |
| 69 | else { | |
| 70 | 34 | value = props.getProperty("parser"); |
| 71 | 34 | if ( value != null ) { |
| 72 | 0 | return value; |
| 73 | } | |
| 74 | } | |
| 75 | } | |
| 76 | catch (IOException ioe) { | |
| 77 | 0 | ioe.printStackTrace(); |
| 78 | } | |
| 79 | } | |
| 80 | 34 | String configParser = System.getProperty("jconfig.parser"); |
| 81 | 34 | if ( configParser != null ) { |
| 82 | 34 | return configParser; |
| 83 | } | |
| 84 | 0 | return null; |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * This method will return the desired default configuration parser. | |
| 89 | * Currently this is the DefaultConfigParser. If you wish to use | |
| 90 | * a different default parser then change the return value. | |
| 91 | * | |
| 92 | * @return the default configuration parser | |
| 93 | */ | |
| 94 | 0 | private static ConfigurationParser getDefaultParser() { |
| 95 | 0 | return new DefaultConfigParser(); |
| 96 | // return new NestedConfigParser(); | |
| 97 | } | |
| 98 | ||
| 99 | } |
|
||||||||||