1 /*
   2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. 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.  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 java.nio.charset.*;
  31 import org.xml.sax.*;
  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 import sun.util.spi.XmlPropertiesProvider;
  39 
  40 /**
  41  * A {@code XmlPropertiesProvider} implementation that uses the JAXP API
  42  * for parsing.
  43  *
  44  * @author  Michael McCloskey
  45  * @since   1.3
  46  */
  47 public class PlatformXmlPropertiesProvider extends XmlPropertiesProvider {
  48 
  49     // XML loading and saving methods for Properties
  50 
  51     // The required DTD URI for exported properties
  52     private static final String PROPS_DTD_URI =
  53     "http://java.sun.com/dtd/properties.dtd";
  54 
  55     private static final String PROPS_DTD =
  56     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
  57     "<!-- DTD for properties -->"                +
  58     "<!ELEMENT properties ( comment?, entry* ) >"+
  59     "<!ATTLIST properties"                       +
  60         " version CDATA #FIXED \"1.0\">"         +
  61     "<!ELEMENT comment (#PCDATA) >"              +
  62     "<!ELEMENT entry (#PCDATA) >"                +
  63     "<!ATTLIST entry "                           +
  64         " key CDATA #REQUIRED>";
  65 
  66     /**
  67      * Version number for the format of exported properties files.
  68      */
  69     private static final String EXTERNAL_XML_VERSION = "1.0";
  70 
  71     @Override
  72     public void load(Properties props, InputStream in)
  73         throws IOException, InvalidPropertiesFormatException
  74     {
  75         Document doc = null;
  76         try {
  77             doc = getLoadingDoc(in);
  78         } catch (SAXException saxe) {
  79             throw new InvalidPropertiesFormatException(saxe);
  80         }
  81         Element propertiesElement = doc.getDocumentElement();
  82         String xmlVersion = propertiesElement.getAttribute("version");
  83         if (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0)
  84             throw new InvalidPropertiesFormatException(
  85                 "Exported Properties file format version " + xmlVersion +
  86                 " is not supported. This java installation can read" +
  87                 " versions " + EXTERNAL_XML_VERSION + " or older. You" +
  88                 " may need to install a newer version of JDK.");
  89         importProperties(props, propertiesElement);
  90     }
  91 
  92     static Document getLoadingDoc(InputStream in)
  93         throws SAXException, IOException
  94     {
  95         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  96         dbf.setIgnoringElementContentWhitespace(true);
  97         dbf.setValidating(true);
  98         dbf.setCoalescing(true);
  99         dbf.setIgnoringComments(true);
 100         try {
 101             DocumentBuilder db = dbf.newDocumentBuilder();
 102             db.setEntityResolver(new Resolver());
 103             db.setErrorHandler(new EH());
 104             InputSource is = new InputSource(in);
 105             return db.parse(is);
 106         } catch (ParserConfigurationException x) {
 107             throw new Error(x);
 108         }
 109     }
 110 
 111     static void importProperties(Properties props, Element propertiesElement) {
 112         NodeList entries = propertiesElement.getChildNodes();
 113         int numEntries = entries.getLength();
 114         int start = numEntries > 0 &&
 115             entries.item(0).getNodeName().equals("comment") ? 1 : 0;
 116         for (int i=start; i<numEntries; i++) {
 117             Element entry = (Element)entries.item(i);
 118             if (entry.hasAttribute("key")) {
 119                 Node n = entry.getFirstChild();
 120                 String val = (n == null) ? "" : n.getNodeValue();
 121                 props.setProperty(entry.getAttribute("key"), val);
 122             }
 123         }
 124     }
 125 
 126     @Override
 127     public void store(Properties props, OutputStream os, String comment,
 128                       String encoding)
 129         throws IOException
 130     {
 131         // fast-fail for unsupported charsets as UnsupportedEncodingException may
 132         // not be thrown later (see JDK-8000621)
 133         try {
 134             Charset.forName(encoding);
 135         } catch (IllegalCharsetNameException | UnsupportedCharsetException x) {
 136             throw new UnsupportedEncodingException(encoding);
 137         }
 138         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 139         DocumentBuilder db = null;
 140         try {
 141             db = dbf.newDocumentBuilder();
 142         } catch (ParserConfigurationException pce) {
 143             assert(false);
 144         }
 145         Document doc = db.newDocument();
 146         Element properties =  (Element)
 147             doc.appendChild(doc.createElement("properties"));
 148 
 149         if (comment != null) {
 150             Element comments = (Element)properties.appendChild(
 151                 doc.createElement("comment"));
 152             comments.appendChild(doc.createTextNode(comment));
 153         }
 154 
 155         synchronized (props) {
 156             for (String key : props.stringPropertyNames()) {
 157                 Element entry = (Element)properties.appendChild(
 158                     doc.createElement("entry"));
 159                 entry.setAttribute("key", key);
 160                 entry.appendChild(doc.createTextNode(props.getProperty(key)));
 161             }
 162         }
 163         emitDocument(doc, os, encoding);
 164     }
 165 
 166     static void emitDocument(Document doc, OutputStream os, String encoding)
 167         throws IOException
 168     {
 169         TransformerFactory tf = TransformerFactory.newInstance();
 170         Transformer t = null;
 171         try {
 172             t = tf.newTransformer();
 173             t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, PROPS_DTD_URI);
 174             t.setOutputProperty(OutputKeys.INDENT, "yes");
 175             t.setOutputProperty(OutputKeys.METHOD, "xml");
 176             t.setOutputProperty(OutputKeys.ENCODING, encoding);
 177         } catch (TransformerConfigurationException tce) {
 178             assert(false);
 179         }
 180         DOMSource doms = new DOMSource(doc);
 181         StreamResult sr = new StreamResult(os);
 182         try {
 183             t.transform(doms, sr);
 184         } catch (TransformerException te) {
 185             throw new IOException(te);
 186         }
 187     }
 188 
 189     private static class Resolver implements EntityResolver {
 190         public InputSource resolveEntity(String pid, String sid)
 191             throws SAXException
 192         {
 193             if (sid.equals(PROPS_DTD_URI)) {
 194                 InputSource is;
 195                 is = new InputSource(new StringReader(PROPS_DTD));
 196                 is.setSystemId(PROPS_DTD_URI);
 197                 return is;
 198             }
 199             throw new SAXException("Invalid system identifier: " + sid);
 200         }
 201     }
 202 
 203     private static class EH implements ErrorHandler {
 204         public void error(SAXParseException x) throws SAXException {
 205             throw x;
 206         }
 207         public void fatalError(SAXParseException x) throws SAXException {
 208             throw x;
 209         }
 210         public void warning(SAXParseException x) throws SAXException {
 211             throw x;
 212         }
 213     }
 214 
 215 }