1 54 55 package junitx.util; 56 57 import org.jaxen.jdom.JDOMXPath; 58 import org.jdom.Document; 59 import org.jdom.Element; 60 import org.jdom.input.SAXBuilder; 61 62 import java.io.FileInputStream ; 63 import java.util.List ; 64 65 103 public class XMLPropertyManager { 104 105 private static Document props; 106 107 static { 108 try { 109 String filename = System.getProperty("XMLPropertyManager.file"); 110 FileInputStream is = new FileInputStream (filename); 111 props = new SAXBuilder(false).build(is); 112 } catch (Exception e) { 113 e.printStackTrace(System.err); 114 } 115 } 116 117 120 private XMLPropertyManager() { 121 } 122 123 131 public static String getProperty(String key) { 132 return getProperty(key, null); 133 } 134 135 144 public static String getProperty(String key, 145 String defaultValue) { 146 if (props == null) { 147 return null; 148 } 149 150 String value = null; 151 try { 152 JDOMXPath path = new JDOMXPath("/properties/property[@name = \"" + key + "\"]"); 153 List res = path.selectNodes(props); 154 if (res != null && res.size() != 0) { 155 value = ((Element) res.get(0)).getAttribute("value").getValue(); 156 } 157 } catch (Exception e) { 158 e.printStackTrace(System.err); 159 } 160 return value == null ? defaultValue : value; 161 } 162 163 } 164 | Popular Tags |