src/share/classes/sun/util/xml/PlatformXmlPropertiesProvider.java

Print this page




  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.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 import sun.util.spi.XmlPropertiesProvider;
  38 
  39 /**
  40  * A {@code XmlPropertiesProvider} implementation that uses the JAXP API
  41  * for parsing.
  42  *
  43  * @author  Michael McCloskey
  44  * @since   1.3
  45  */
  46 public class PlatformXmlPropertiesProvider extends XmlPropertiesProvider {
  47 
  48     // XML loading and saving methods for Properties
  49 


 110     static void importProperties(Properties props, Element propertiesElement) {
 111         NodeList entries = propertiesElement.getChildNodes();
 112         int numEntries = entries.getLength();
 113         int start = numEntries > 0 &&
 114             entries.item(0).getNodeName().equals("comment") ? 1 : 0;
 115         for (int i=start; i<numEntries; i++) {
 116             Element entry = (Element)entries.item(i);
 117             if (entry.hasAttribute("key")) {
 118                 Node n = entry.getFirstChild();
 119                 String val = (n == null) ? "" : n.getNodeValue();
 120                 props.setProperty(entry.getAttribute("key"), val);
 121             }
 122         }
 123     }
 124 
 125     @Override
 126     public void store(Properties props, OutputStream os, String comment,
 127                       String encoding)
 128         throws IOException
 129     {







 130         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 131         DocumentBuilder db = null;
 132         try {
 133             db = dbf.newDocumentBuilder();
 134         } catch (ParserConfigurationException pce) {
 135             assert(false);
 136         }
 137         Document doc = db.newDocument();
 138         Element properties =  (Element)
 139             doc.appendChild(doc.createElement("properties"));
 140 
 141         if (comment != null) {
 142             Element comments = (Element)properties.appendChild(
 143                 doc.createElement("comment"));
 144             comments.appendChild(doc.createTextNode(comment));
 145         }
 146 
 147         synchronized (props) {
 148             for (String key : props.stringPropertyNames()) {
 149                 Element entry = (Element)properties.appendChild(




  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 


 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(