1 16 17 package org.apache.commons.configuration; 18 19 import java.io.File ; 20 import java.io.PrintWriter ; 21 import java.io.Reader ; 22 import java.io.Writer ; 23 import java.net.URL ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import org.apache.commons.digester.Digester; 28 import org.apache.commons.lang.StringEscapeUtils; 29 import org.apache.commons.lang.StringUtils; 30 import org.xml.sax.EntityResolver ; 31 import org.xml.sax.InputSource ; 32 33 59 public class XMLPropertiesConfiguration extends PropertiesConfiguration 60 { 61 64 private static final String DEFAULT_ENCODING = "UTF-8"; 65 66 { 68 setEncoding(DEFAULT_ENCODING); 69 } 70 71 78 public XMLPropertiesConfiguration() 79 { 80 super(); 81 } 82 83 91 public XMLPropertiesConfiguration(String fileName) throws ConfigurationException 92 { 93 super(fileName); 94 } 95 96 104 public XMLPropertiesConfiguration(File file) throws ConfigurationException 105 { 106 super(file); 107 } 108 109 117 public XMLPropertiesConfiguration(URL url) throws ConfigurationException 118 { 119 super(url); 120 } 121 122 public void load(Reader in) throws ConfigurationException 123 { 124 126 Digester digester = new Digester(); 128 digester.setEntityResolver(new EntityResolver (){ 129 public InputSource resolveEntity(String publicId, String systemId) 130 { 131 return new InputSource (getClass().getClassLoader().getResourceAsStream("properties.dtd")); 132 } 133 }); 134 135 digester.addCallMethod("properties/comment", "setHeader", 0); 136 137 digester.addCallMethod("properties/entry", "addProperty", 2); 138 digester.addCallParam("properties/entry", 0, "key"); 139 digester.addCallParam("properties/entry", 1); 140 141 143 digester.push(this); 145 try 146 { 147 digester.parse(in); 148 } 149 catch (Exception e) 150 { 151 throw new ConfigurationException("Unable to parse the configuration file", e); 152 } 153 } 154 155 public void save(Writer out) throws ConfigurationException 156 { 157 PrintWriter writer = new PrintWriter (out); 158 159 String encoding = getEncoding() != null ? getEncoding() : DEFAULT_ENCODING; 160 writer.println("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>"); 161 writer.println("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">"); 162 writer.println("<properties>"); 163 164 if (getHeader() != null) 165 { 166 writer.println(" <comment>" + StringEscapeUtils.escapeXml(getHeader()) + "</comment>"); 167 } 168 169 Iterator keys = getKeys(); 170 while (keys.hasNext()) 171 { 172 String key = (String ) keys.next(); 173 Object value = getProperty(key); 174 175 if (value instanceof List ) 176 { 177 writeProperty(writer, key, (List ) value); 178 } 179 else 180 { 181 writeProperty(writer, key, value); 182 } 183 } 184 185 writer.println("</properties>"); 186 writer.flush(); 187 } 188 189 192 private void writeProperty(PrintWriter out, String key, Object value) 193 { 194 String k = StringEscapeUtils.escapeXml(key); 196 197 if (value != null) 198 { 199 String v = StringEscapeUtils.escapeXml(String.valueOf(value)); 201 v = StringUtils.replace(v, String.valueOf(getDelimiter()), "\\" + getDelimiter()); 202 203 out.println(" <entry key=\"" + k + "\">" + v + "</entry>"); 204 } 205 else 206 { 207 out.println(" <entry key=\"" + k + "\"/>"); 208 } 209 } 210 211 214 private void writeProperty(PrintWriter out, String key, List values) 215 { 216 for (int i = 0; i < values.size(); i++) 217 { 218 writeProperty(out, key, values.get(i)); 219 } 220 } 221 } 222 | Popular Tags |