6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27
28 import java.io.*;
29 import org.xml.sax.*;
30 import org.xml.sax.helpers.*;
31 import org.w3c.dom.*;
32 import javax.xml.parsers.*;
33 import javax.xml.transform.*;
34 import javax.xml.transform.dom.*;
35 import javax.xml.transform.stream.*;
36
37 /**
38 * A class used to aid in Properties load and save in XML. Keeping this
39 * code outside of Properties helps reduce the number of classes loaded
40 * when Properties is loaded.
41 *
42 * @author Michael McCloskey
43 * @since 1.3
44 */
45 class XMLUtils {
46
47 // XML loading and saving methods for Properties
48
49 // The required DTD URI for exported properties
50 private static final String PROPS_DTD_URI =
51 "http://java.sun.com/dtd/properties.dtd";
52
53 private static final String PROPS_DTD =
54 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
55 "<!-- DTD for properties -->" +
56 "<!ELEMENT properties ( comment?, entry* ) >"+
57 "<!ATTLIST properties" +
58 " version CDATA #FIXED \"1.0\">" +
59 "<!ELEMENT comment (#PCDATA) >" +
60 "<!ELEMENT entry (#PCDATA) >" +
61 "<!ATTLIST entry " +
62 " key CDATA #REQUIRED>";
63
64 /**
65 * Version number for the format of exported properties files.
66 */
67 private static final String EXTERNAL_XML_VERSION = "1.0";
68
69 static void load(Properties props, InputStream in)
70 throws IOException, InvalidPropertiesFormatException
71 {
72 Document doc = null;
73 try {
74 doc = getLoadingDoc(in);
75 } catch (SAXException saxe) {
76 throw new InvalidPropertiesFormatException(saxe);
77 }
78 Element propertiesElement = (Element)doc.getChildNodes().item(1);
79 String xmlVersion = propertiesElement.getAttribute("version");
80 if (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0)
81 throw new InvalidPropertiesFormatException(
82 "Exported Properties file format version " + xmlVersion +
83 " is not supported. This java installation can read" +
84 " versions " + EXTERNAL_XML_VERSION + " or older. You" +
85 " may need to install a newer version of JDK.");
86 importProperties(props, propertiesElement);
87 }
88
89 static Document getLoadingDoc(InputStream in)
103 } catch (ParserConfigurationException x) {
104 throw new Error(x);
105 }
106 }
107
108 static void importProperties(Properties props, Element propertiesElement) {
109 NodeList entries = propertiesElement.getChildNodes();
110 int numEntries = entries.getLength();
111 int start = numEntries > 0 &&
112 entries.item(0).getNodeName().equals("comment") ? 1 : 0;
113 for (int i=start; i<numEntries; i++) {
114 Element entry = (Element)entries.item(i);
115 if (entry.hasAttribute("key")) {
116 Node n = entry.getFirstChild();
117 String val = (n == null) ? "" : n.getNodeValue();
118 props.setProperty(entry.getAttribute("key"), val);
119 }
120 }
121 }
122
123 static void save(Properties props, OutputStream os, String comment,
124 String encoding)
125 throws IOException
126 {
127 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
128 DocumentBuilder db = null;
129 try {
130 db = dbf.newDocumentBuilder();
131 } catch (ParserConfigurationException pce) {
132 assert(false);
133 }
134 Document doc = db.newDocument();
135 Element properties = (Element)
136 doc.appendChild(doc.createElement("properties"));
137
138 if (comment != null) {
139 Element comments = (Element)properties.appendChild(
140 doc.createElement("comment"));
141 comments.appendChild(doc.createTextNode(comment));
142 }
143
|
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.util.xml;
27
28 import java.io.*;
29 import java.util.*;
30 import org.xml.sax.*;
31 import org.xml.sax.helpers.*;
32 import org.w3c.dom.*;
33 import javax.xml.parsers.*;
34 import javax.xml.transform.*;
35 import javax.xml.transform.dom.*;
36 import javax.xml.transform.stream.*;
37
38 /**
39 * A class used to aid in Properties load and save in XML. Keeping this
40 * code outside of Properties helps reduce the number of classes loaded
41 * when Properties is loaded.
42 *
43 * @author Michael McCloskey
44 * @since 1.3
45 */
46 public class XMLUtils {
47
48 // XML loading and saving methods for Properties
49
50 // The required DTD URI for exported properties
51 private static final String PROPS_DTD_URI =
52 "http://java.sun.com/dtd/properties.dtd";
53
54 private static final String PROPS_DTD =
55 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
56 "<!-- DTD for properties -->" +
57 "<!ELEMENT properties ( comment?, entry* ) >"+
58 "<!ATTLIST properties" +
59 " version CDATA #FIXED \"1.0\">" +
60 "<!ELEMENT comment (#PCDATA) >" +
61 "<!ELEMENT entry (#PCDATA) >" +
62 "<!ATTLIST entry " +
63 " key CDATA #REQUIRED>";
64
65 /**
66 * Version number for the format of exported properties files.
67 */
68 private static final String EXTERNAL_XML_VERSION = "1.0";
69
70 public static void load(Properties props, InputStream in)
71 throws IOException, InvalidPropertiesFormatException
72 {
73 Document doc = null;
74 try {
75 doc = getLoadingDoc(in);
76 } catch (SAXException saxe) {
77 throw new InvalidPropertiesFormatException(saxe);
78 }
79 Element propertiesElement = (Element)doc.getChildNodes().item(1);
80 String xmlVersion = propertiesElement.getAttribute("version");
81 if (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0)
82 throw new InvalidPropertiesFormatException(
83 "Exported Properties file format version " + xmlVersion +
84 " is not supported. This java installation can read" +
85 " versions " + EXTERNAL_XML_VERSION + " or older. You" +
86 " may need to install a newer version of JDK.");
87 importProperties(props, propertiesElement);
88 }
89
90 static Document getLoadingDoc(InputStream in)
104 } catch (ParserConfigurationException x) {
105 throw new Error(x);
106 }
107 }
108
109 static void importProperties(Properties props, Element propertiesElement) {
110 NodeList entries = propertiesElement.getChildNodes();
111 int numEntries = entries.getLength();
112 int start = numEntries > 0 &&
113 entries.item(0).getNodeName().equals("comment") ? 1 : 0;
114 for (int i=start; i<numEntries; i++) {
115 Element entry = (Element)entries.item(i);
116 if (entry.hasAttribute("key")) {
117 Node n = entry.getFirstChild();
118 String val = (n == null) ? "" : n.getNodeValue();
119 props.setProperty(entry.getAttribute("key"), val);
120 }
121 }
122 }
123
124 public static void save(Properties props, OutputStream os, String comment,
125 String encoding)
126 throws IOException
127 {
128 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
129 DocumentBuilder db = null;
130 try {
131 db = dbf.newDocumentBuilder();
132 } catch (ParserConfigurationException pce) {
133 assert(false);
134 }
135 Document doc = db.newDocument();
136 Element properties = (Element)
137 doc.appendChild(doc.createElement("properties"));
138
139 if (comment != null) {
140 Element comments = (Element)properties.appendChild(
141 doc.createElement("comment"));
142 comments.appendChild(doc.createTextNode(comment));
143 }
144
|