By default (when no XML SAX and DOM 2 present) parsing the properties stream with simple nanoXML parser
2 * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
29 import java.util.Properties;
30 import java.util.InvalidPropertiesFormatException;
31 import java.util.Map.Entry;
34 * A class used to aid in Properties load and save in XML in simple way, e.g.
35 * without dependency on the big machinery of XML, SAX and DOM 2.
37 * @author Jaroslav Tulach
40 public class DefaultPropertiesXMLUtils extends PropertiesXMLUtils {
41 public void load(Properties props, InputStream in)
42 throws IOException, InvalidPropertiesFormatException {
43 XMLElement e = new XMLElement();
44 e.parseFromReader(new InputStreamReader(in, "UTF-8"));
45 for (Object o : e.getChildren()) {
46 XMLElement ch = (XMLElement)o;
47 if (ch.getName().equals("entry")) {
48 props.put(ch.getAttribute("key"), ch.getContent());
52 public void save(Properties props, OutputStream out, String comment, String encoding)
54 PrintWriter w = new PrintWriter(new OutputStreamWriter(out, encoding));
55 w.println("<?xml version=\"1.0\" encoding=\"" + encoding + "\" standalone=\"no\"?>");
56 w.println("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">");
57 w.println("<properties>");
58 if (comment != null) {
61 w.println("</comment>");
63 for (Entry<Object, Object> entry : props.entrySet()) {
64 w.print("<entry key=\"");
65 w.print(entry.getKey());
67 w.print(entry.getValue());
68 w.println("</entry>");
70 w.println("</properties>");