1 package org.ejen.ext; 22 23 import org.ejen.util.XSLUtil; 24 import org.ejen.util.DOMUtil; 25 import java.util.Enumeration ; 26 import java.io.BufferedInputStream ; 27 import java.io.FileInputStream ; 28 import org.w3c.dom.Document ; 29 import org.w3c.dom.Element ; 30 import org.apache.xpath.NodeSet; 31 import org.apache.xml.utils.WrappedRuntimeException; 32 import org.apache.xalan.extensions.ExpressionContext; 33 34 64 public class Properties { 65 66 69 protected Properties() {} 70 71 93 public static NodeSet getSystemProperties(ExpressionContext context) { 94 Document doc = XSLUtil.getContextDocument(context); 95 96 try { 97 java.util.Properties props = System.getProperties(); 98 NodeSet ns = new NodeSet(); 99 100 for (Enumeration e = props.propertyNames(); e.hasMoreElements();) { 101 String name = (String ) (e.nextElement()); 102 Element parent = doc.createElement("property"); 103 104 DOMUtil.createCDATANode(doc, parent, "name", name); 105 DOMUtil.createCDATANode(doc, parent, "value", 106 props.getProperty(name, "")); 107 ns.addElement(parent); 108 } 109 return ns; 110 } catch (WrappedRuntimeException e) { 111 throw e; 112 } catch (Exception e) { 113 throw new WrappedRuntimeException(e); 114 } 115 } 116 117 146 public static NodeSet load(ExpressionContext context, String fileName) { 147 fileName = XSLUtil.evaluate(context, fileName); 148 Document doc = XSLUtil.getContextDocument(context); 149 BufferedInputStream bis = null; 150 151 try { 152 bis = new BufferedInputStream (new FileInputStream (fileName)); 153 java.util.Properties props = new java.util.Properties (); 154 155 props.load(bis); 156 NodeSet ns = new NodeSet(); 157 158 for (Enumeration e = props.propertyNames(); e.hasMoreElements();) { 159 String name = (String ) (e.nextElement()); 160 Element parent = doc.createElement("property"); 161 162 DOMUtil.createCDATANode(doc, parent, "name", name); 163 DOMUtil.createCDATANode(doc, parent, "value", 164 props.getProperty(name, "")); 165 ns.addElement(parent); 166 } 167 return ns; 168 } catch (WrappedRuntimeException e) { 169 throw e; 170 } catch (Exception e) { 171 throw new WrappedRuntimeException(e); 172 } 173 finally { 174 if (bis != null) { 175 try { 176 bis.close(); 177 } catch (Exception e) {} 178 finally { 179 bis = null; 180 } 181 } 182 } 183 } 184 } 185
| Popular Tags
|