| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.apache.commons.configuration.plist; |
| 19 | |
|
| 20 | |
import java.io.File; |
| 21 | |
import java.io.PrintWriter; |
| 22 | |
import java.io.Reader; |
| 23 | |
import java.io.Writer; |
| 24 | |
import java.net.URL; |
| 25 | |
import java.util.ArrayList; |
| 26 | |
import java.util.Iterator; |
| 27 | |
import java.util.List; |
| 28 | |
import java.util.Map; |
| 29 | |
|
| 30 | |
import org.apache.commons.codec.binary.Hex; |
| 31 | |
import org.apache.commons.configuration.AbstractHierarchicalFileConfiguration; |
| 32 | |
import org.apache.commons.configuration.Configuration; |
| 33 | |
import org.apache.commons.configuration.ConfigurationException; |
| 34 | |
import org.apache.commons.configuration.HierarchicalConfiguration; |
| 35 | |
import org.apache.commons.configuration.MapConfiguration; |
| 36 | |
import org.apache.commons.lang.StringUtils; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
public class PropertyListConfiguration extends AbstractHierarchicalFileConfiguration |
| 69 | |
{ |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
private static final long serialVersionUID = 3227248503779092127L; |
| 74 | |
|
| 75 | |
|
| 76 | |
private static final int INDENT_SIZE = 4; |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public PropertyListConfiguration() |
| 84 | 126 | { |
| 85 | 126 | } |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
public PropertyListConfiguration(String fileName) throws ConfigurationException |
| 94 | |
{ |
| 95 | 0 | super(fileName); |
| 96 | 0 | } |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public PropertyListConfiguration(File file) throws ConfigurationException |
| 105 | |
{ |
| 106 | 1 | super(file); |
| 107 | 1 | } |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public PropertyListConfiguration(URL url) throws ConfigurationException |
| 116 | |
{ |
| 117 | 0 | super(url); |
| 118 | 0 | } |
| 119 | |
|
| 120 | |
public void load(Reader in) throws ConfigurationException |
| 121 | |
{ |
| 122 | 15 | PropertyListParser parser = new PropertyListParser(in); |
| 123 | |
try |
| 124 | |
{ |
| 125 | |
|
| 126 | 15 | HierarchicalConfiguration config = parser.parse(); |
| 127 | 14 | setRoot(config.getRoot()); |
| 128 | 14 | } |
| 129 | |
catch (ParseException e) |
| 130 | |
{ |
| 131 | 1 | throw new ConfigurationException(e); |
| 132 | |
} |
| 133 | 14 | } |
| 134 | |
|
| 135 | |
public void save(Writer out) throws ConfigurationException |
| 136 | |
{ |
| 137 | 1 | PrintWriter writer = new PrintWriter(out); |
| 138 | 1 | printNode(writer, 0, getRoot()); |
| 139 | 1 | writer.flush(); |
| 140 | 1 | } |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
private void printNode(PrintWriter out, int indentLevel, Node node) |
| 146 | |
{ |
| 147 | 22 | String padding = StringUtils.repeat(" ", indentLevel * INDENT_SIZE); |
| 148 | |
|
| 149 | 22 | if (node.getName() != null) |
| 150 | |
{ |
| 151 | 19 | out.print(padding + quoteString(node.getName()) + " = "); |
| 152 | |
} |
| 153 | |
|
| 154 | |
|
| 155 | 22 | List children = new ArrayList(node.getChildren()); |
| 156 | 22 | Iterator it = children.iterator(); |
| 157 | 64 | while (it.hasNext()) |
| 158 | |
{ |
| 159 | 20 | Node child = (Node) it.next(); |
| 160 | 20 | if (child.getValue() == null && (child.getChildren() == null || child.getChildren().isEmpty())) |
| 161 | |
{ |
| 162 | 1 | it.remove(); |
| 163 | |
} |
| 164 | |
} |
| 165 | |
|
| 166 | 22 | if (!children.isEmpty()) |
| 167 | |
{ |
| 168 | |
|
| 169 | 7 | if (indentLevel > 0) |
| 170 | |
{ |
| 171 | 6 | out.println(); |
| 172 | |
} |
| 173 | |
|
| 174 | 7 | out.println(padding + "{"); |
| 175 | |
|
| 176 | |
|
| 177 | 7 | it = children.iterator(); |
| 178 | 33 | while (it.hasNext()) |
| 179 | |
{ |
| 180 | 19 | Node child = (Node) it.next(); |
| 181 | |
|
| 182 | 19 | printNode(out, indentLevel + 1, child); |
| 183 | |
|
| 184 | |
|
| 185 | 19 | Object value = child.getValue(); |
| 186 | 19 | if (value != null && !(value instanceof Map) && !(value instanceof Configuration)) |
| 187 | |
{ |
| 188 | 15 | out.println(";"); |
| 189 | |
} |
| 190 | |
|
| 191 | |
|
| 192 | 19 | if (it.hasNext() && (value == null || value instanceof List)) |
| 193 | |
{ |
| 194 | 6 | out.println(); |
| 195 | |
} |
| 196 | |
} |
| 197 | |
|
| 198 | 7 | out.print(padding + "}"); |
| 199 | |
|
| 200 | |
|
| 201 | 7 | if (node.getParent() != null) |
| 202 | |
{ |
| 203 | 4 | out.println(); |
| 204 | |
} |
| 205 | |
} |
| 206 | |
else |
| 207 | |
{ |
| 208 | |
|
| 209 | 15 | Object value = node.getValue(); |
| 210 | 15 | printValue(out, indentLevel, value); |
| 211 | |
} |
| 212 | 22 | } |
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
private void printValue(PrintWriter out, int indentLevel, Object value) |
| 218 | |
{ |
| 219 | 26 | String padding = StringUtils.repeat(" ", indentLevel * INDENT_SIZE); |
| 220 | |
|
| 221 | 26 | if (value instanceof List) |
| 222 | |
{ |
| 223 | 6 | out.print("( "); |
| 224 | 6 | Iterator it = ((List) value).iterator(); |
| 225 | 23 | while (it.hasNext()) |
| 226 | |
{ |
| 227 | 11 | printValue(out, indentLevel + 1, it.next()); |
| 228 | 11 | if (it.hasNext()) |
| 229 | |
{ |
| 230 | 6 | out.print(", "); |
| 231 | |
} |
| 232 | |
} |
| 233 | 6 | out.print(" )"); |
| 234 | |
} |
| 235 | 20 | else if (value instanceof HierarchicalConfiguration) |
| 236 | |
{ |
| 237 | 2 | printNode(out, indentLevel, ((HierarchicalConfiguration) value).getRoot()); |
| 238 | |
} |
| 239 | 18 | else if (value instanceof Configuration) |
| 240 | |
{ |
| 241 | |
|
| 242 | 0 | out.println(); |
| 243 | 0 | out.println(padding + "{"); |
| 244 | |
|
| 245 | 0 | Configuration config = (Configuration) value; |
| 246 | 0 | Iterator it = config.getKeys(); |
| 247 | 0 | while (it.hasNext()) |
| 248 | |
{ |
| 249 | 0 | String key = (String) it.next(); |
| 250 | 0 | Node node = new Node(key); |
| 251 | 0 | node.setValue(config.getProperty(key)); |
| 252 | |
|
| 253 | 0 | printNode(out, indentLevel + 1, node); |
| 254 | 0 | out.println(";"); |
| 255 | |
} |
| 256 | 0 | out.println(padding + "}"); |
| 257 | |
} |
| 258 | 18 | else if (value instanceof Map) |
| 259 | |
{ |
| 260 | |
|
| 261 | 0 | Map map = (Map) value; |
| 262 | 0 | printValue(out, indentLevel, new MapConfiguration(map)); |
| 263 | |
} |
| 264 | 18 | else if (value instanceof byte[]) |
| 265 | |
{ |
| 266 | 2 | out.print("<" + new String(Hex.encodeHex((byte[]) value)) + ">"); |
| 267 | |
} |
| 268 | 16 | else if (value != null) |
| 269 | |
{ |
| 270 | 16 | out.print(quoteString(String.valueOf(value))); |
| 271 | |
} |
| 272 | 26 | } |
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
String quoteString(String s) |
| 292 | |
{ |
| 293 | 40 | if (s == null) |
| 294 | |
{ |
| 295 | 1 | return null; |
| 296 | |
} |
| 297 | |
|
| 298 | 39 | if (s.indexOf(' ') != -1 |
| 299 | |
|| s.indexOf('\t') != -1 |
| 300 | |
|| s.indexOf('\r') != -1 |
| 301 | |
|| s.indexOf('\n') != -1 |
| 302 | |
|| s.indexOf('"') != -1 |
| 303 | |
|| s.indexOf('(') != -1 |
| 304 | |
|| s.indexOf(')') != -1 |
| 305 | |
|| s.indexOf('{') != -1 |
| 306 | |
|| s.indexOf('}') != -1 |
| 307 | |
|| s.indexOf('=') != -1 |
| 308 | |
|| s.indexOf(',') != -1 |
| 309 | |
|| s.indexOf(';') != -1) |
| 310 | |
{ |
| 311 | 5 | s = StringUtils.replace(s, "\"", "\\\""); |
| 312 | 5 | s = "\"" + s + "\""; |
| 313 | |
} |
| 314 | |
|
| 315 | 39 | return s; |
| 316 | |
} |
| 317 | |
} |