src/share/classes/sun/util/xml/DefaultPropertiesXMLUtils.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 24 Jun 2009 16:38:24 +0200
branchxml-sax-and-dom-2
changeset 1262 52864f10883d
parent 1261 29f4f9b451b8
permissions -rw-r--r--
By default (when no XML SAX and DOM 2 present) parsing the properties stream with simple nanoXML parser
     1 /*
     2  * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     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.
    10  *
    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).
    16  *
    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.
    20  *
    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
    23  * have any questions.
    24  */
    25 
    26 package sun.util.xml;
    27 
    28 import java.io.*;
    29 import java.util.Properties;
    30 import java.util.InvalidPropertiesFormatException;
    31 import java.util.Map.Entry;
    32 
    33 /**
    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.
    36  *
    37  * @author  Jaroslav Tulach
    38  * @since   1.7
    39  */
    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());
    49             }
    50         }
    51     }
    52     public void save(Properties props, OutputStream out, String comment, String encoding)
    53     throws IOException {
    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) {
    59             w.print("<comment>");
    60             w.print(comment);
    61             w.println("</comment>");
    62         }
    63         for (Entry<Object, Object> entry : props.entrySet()) {
    64             w.print("<entry key=\"");
    65             w.print(entry.getKey());
    66             w.print("\">");
    67             w.print(entry.getValue());
    68             w.println("</entry>");
    69         }
    70         w.println("</properties>");
    71         w.flush();
    72     }
    73 
    74 }