< prev index next >

src/java.base/share/classes/jdk/internal/util/xml/PropertiesDefaultHandler.java

Print this page


   1 /*
   2  * Copyright (c) 2012, 2013, 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 jdk.internal.util.xml;
  27 
  28 import java.io.*;

  29 import java.util.InvalidPropertiesFormatException;
  30 import java.util.Map.Entry;
  31 import java.util.Properties;
  32 import jdk.internal.org.xml.sax.Attributes;
  33 import jdk.internal.org.xml.sax.InputSource;
  34 import jdk.internal.org.xml.sax.SAXException;
  35 import jdk.internal.org.xml.sax.SAXParseException;
  36 import jdk.internal.org.xml.sax.helpers.DefaultHandler;
  37 import jdk.internal.util.xml.impl.SAXParserImpl;
  38 import jdk.internal.util.xml.impl.XMLStreamWriterImpl;
  39 
  40 /**
  41  * A class used to aid in Properties load and save in XML. This class is
  42  * re-implemented using a subset of SAX
  43  *
  44  * @author Joe Wang
  45  * @since 1.8
  46  */
  47 public class PropertiesDefaultHandler extends DefaultHandler {
  48 


  77     {
  78         this.properties = props;
  79 
  80         try {
  81             SAXParser parser = new SAXParserImpl();
  82             parser.parse(in, this);
  83         } catch (SAXException saxe) {
  84             throw new InvalidPropertiesFormatException(saxe);
  85         }
  86 
  87         /**
  88          * String xmlVersion = propertiesElement.getAttribute("version"); if
  89          * (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0) throw new
  90          * InvalidPropertiesFormatException( "Exported Properties file format
  91          * version " + xmlVersion + " is not supported. This java installation
  92          * can read" + " versions " + EXTERNAL_XML_VERSION + " or older. You" +
  93          * " may need to install a newer version of JDK.");
  94          */
  95     }
  96 
  97     public void store(Properties props, OutputStream os, String comment, String encoding)
  98         throws IOException
  99     {
 100         try {
 101             XMLStreamWriter writer = new XMLStreamWriterImpl(os, encoding);
 102             writer.writeStartDocument();
 103             writer.writeDTD(PROPS_DTD_DECL);
 104             writer.writeStartElement(ELEMENT_ROOT);
 105             if (comment != null && comment.length() > 0) {
 106                 writer.writeStartElement(ELEMENT_COMMENT);
 107                 writer.writeCharacters(comment);
 108                 writer.writeEndElement();
 109             }
 110 
 111             synchronized(props) {
 112                 for (Entry<Object, Object> e : props.entrySet()) {
 113                     final Object k = e.getKey();
 114                     final Object v = e.getValue();
 115                     if (k instanceof String && v instanceof String) {
 116                         writer.writeStartElement(ELEMENT_ENTRY);
 117                         writer.writeAttribute(ATTR_KEY, (String)k);
 118                         writer.writeCharacters((String)v);
 119                         writer.writeEndElement();
 120                     }
 121                 }


   1 /*
   2  * Copyright (c) 2012, 2017, 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 jdk.internal.util.xml;
  27 
  28 import java.io.*;
  29 import java.nio.charset.Charset;
  30 import java.util.InvalidPropertiesFormatException;
  31 import java.util.Map.Entry;
  32 import java.util.Properties;
  33 import jdk.internal.org.xml.sax.Attributes;
  34 import jdk.internal.org.xml.sax.InputSource;
  35 import jdk.internal.org.xml.sax.SAXException;
  36 import jdk.internal.org.xml.sax.SAXParseException;
  37 import jdk.internal.org.xml.sax.helpers.DefaultHandler;
  38 import jdk.internal.util.xml.impl.SAXParserImpl;
  39 import jdk.internal.util.xml.impl.XMLStreamWriterImpl;
  40 
  41 /**
  42  * A class used to aid in Properties load and save in XML. This class is
  43  * re-implemented using a subset of SAX
  44  *
  45  * @author Joe Wang
  46  * @since 1.8
  47  */
  48 public class PropertiesDefaultHandler extends DefaultHandler {
  49 


  78     {
  79         this.properties = props;
  80 
  81         try {
  82             SAXParser parser = new SAXParserImpl();
  83             parser.parse(in, this);
  84         } catch (SAXException saxe) {
  85             throw new InvalidPropertiesFormatException(saxe);
  86         }
  87 
  88         /**
  89          * String xmlVersion = propertiesElement.getAttribute("version"); if
  90          * (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0) throw new
  91          * InvalidPropertiesFormatException( "Exported Properties file format
  92          * version " + xmlVersion + " is not supported. This java installation
  93          * can read" + " versions " + EXTERNAL_XML_VERSION + " or older. You" +
  94          * " may need to install a newer version of JDK.");
  95          */
  96     }
  97 
  98     public void store(Properties props, OutputStream os, String comment, Charset charset)
  99         throws IOException
 100     {
 101         try {
 102             XMLStreamWriter writer = new XMLStreamWriterImpl(os, charset);
 103             writer.writeStartDocument();
 104             writer.writeDTD(PROPS_DTD_DECL);
 105             writer.writeStartElement(ELEMENT_ROOT);
 106             if (comment != null && comment.length() > 0) {
 107                 writer.writeStartElement(ELEMENT_COMMENT);
 108                 writer.writeCharacters(comment);
 109                 writer.writeEndElement();
 110             }
 111 
 112             synchronized(props) {
 113                 for (Entry<Object, Object> e : props.entrySet()) {
 114                     final Object k = e.getKey();
 115                     final Object v = e.getValue();
 116                     if (k instanceof String && v instanceof String) {
 117                         writer.writeStartElement(ELEMENT_ENTRY);
 118                         writer.writeAttribute(ATTR_KEY, (String)k);
 119                         writer.writeCharacters((String)v);
 120                         writer.writeEndElement();
 121                     }
 122                 }


< prev index next >