You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by hi...@apache.org on 2001/05/04 17:59:41 UTC

cvs commit: xml-batik/sources/org/apache/batik/util PreferenceManager.java

hillion     01/05/04 08:59:39

  Modified:    sources/org/apache/batik/util PreferenceManager.java
  Added:       sources/org/apache/batik/apps/svgbrowser
                        XMLPreferenceManager.java
  Log:
  Committed an extension to util/PreferenceManager which save and load XML files.
  
  Revision  Changes    Path
  1.1                  xml-batik/sources/org/apache/batik/apps/svgbrowser/XMLPreferenceManager.java
  
  Index: XMLPreferenceManager.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.batik.apps.svgbrowser;
  
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Properties;
  
  import org.apache.batik.dom.GenericDOMImplementation;
  import org.apache.batik.dom.util.DocumentFactory;
  import org.apache.batik.dom.util.DOMUtilities;
  import org.apache.batik.dom.util.SAXDocumentFactory;
  import org.apache.batik.util.PreferenceManager;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  
  /**
   * An extension of {@link PreferenceManager} which store the preference
   * in XML.
   *
   * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
   * @version $Id: XMLPreferenceManager.java,v 1.1 2001/05/04 15:59:21 hillion Exp $
   */
  public class XMLPreferenceManager extends PreferenceManager {
      
      /**
       * The XML parser
       */
      protected String xmlParserClassName;
  
      /**
       * Creates a preference manager.
       * @param prefFileName the name of the preference file.
       * @param parser The XML parser class name.
       */
      public XMLPreferenceManager(String prefFileName, String parser) {
          this(prefFileName, null, parser);
      }
  
      /**
       * Creates a preference manager with a default values
       * initialization map.
       * @param prefFileName the name of the preference file.
       * @param defaults where to get defaults value if the value is
       * not specified in the file.
       * @param parser The XML parser class name.
       */
      public XMLPreferenceManager(String prefFileName, Map defaults, String parser) {
          super(prefFileName, defaults);
          internal = new XMLProperties();
          xmlParserClassName = parser;
      }
  
      /**
       * To store the preferences.
       */
      protected class XMLProperties extends Properties {
  
          /**
           * Reads a property list (key and element pairs) from the input stream.
           * The stream is assumed to be using the ISO 8859-1 character encoding.
           */
          public synchronized void load(InputStream is) throws IOException {
              BufferedReader r;
              r = new BufferedReader(new InputStreamReader(is, "8859_1"));
              DocumentFactory df = new SAXDocumentFactory
                  (GenericDOMImplementation.getDOMImplementation(),
                   xmlParserClassName);
              Document doc = df.createDocument("http://xml.apache.org/batik/preferences",
                                               "preferences",
                                               null,
                                               r);
              Element elt = doc.getDocumentElement();
              for (Node n = elt.getFirstChild(); n != null; n = n.getNextSibling()) {
                  if (n.getNodeType() == Node.ELEMENT_NODE) {
                      if (n.getNodeName().equals("property")) {
                          String name = ((Element)n).getAttributeNS(null, "name");
                          
                          StringBuffer cont = new StringBuffer();
                          for (Node c = n.getFirstChild();
                               c != null;
                               c = c.getNextSibling()) {
                              if (c.getNodeType() == Node.TEXT_NODE) {
                                  cont.append(c.getNodeValue());
                              } else {
                                  break;
                              }
                          }
                          String val = cont.toString();
                          put(name, val);
                      }
                  }
              }
          }
  
          /**
           * Writes this property list (key and element pairs) in this
           * <code>Properties</code> table to the output stream in a format suitable
           * for loading into a <code>Properties</code> table using the
           * <code>load</code> method.
           * The stream is written using the ISO 8859-1 character encoding.
           */
          public synchronized void store(OutputStream os, String header)
              throws IOException {
              BufferedWriter w;
              w = new BufferedWriter(new OutputStreamWriter(os, "8859_1"));
  
              Map m = new HashMap();
              enumerate(m);
  
              w.write("<preferences xmlns=\"http://xml.apache.org/batik/preferences\">\n");
  
              Iterator it = m.keySet().iterator();
              while (it.hasNext()) {
                  String n = (String)it.next();
                  String v = (String)m.get(n);
                  
                  w.write("<property name=\"" + n + "\">");
                  w.write(DOMUtilities.contentToString(v));
                  w.write("</property>\n");
              }
  
              w.write("</preferences>\n");
              w.flush();
          }
  
          /**
           * Enumerates all key/value pairs in the specified m.
           * @param m the map
           */
          private synchronized void enumerate(Map m) {
              if (defaults != null) {
                  Iterator it = m.keySet().iterator();
                  while (it.hasNext()) {
                      Object k = it.next();
                      m.put(k, defaults.get(k));
                  }
              }
              Iterator it = keySet().iterator();
              while (it.hasNext()) {
                  Object k = it.next();
                  m.put(k, get(k));
              }
          }
          
      }
  }
  
  
  
  1.3       +11 -11    xml-batik/sources/org/apache/batik/util/PreferenceManager.java
  
  Index: PreferenceManager.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/util/PreferenceManager.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- PreferenceManager.java	2001/02/02 10:50:40	1.2
  +++ PreferenceManager.java	2001/05/04 15:59:28	1.3
  @@ -58,24 +58,24 @@
    * </pre></blockquote>
    * <p>
    * @author <a href="mailto:cjolif@ilog.fr">Christophe Jolif</a>
  - * @version $Id: PreferenceManager.java,v 1.2 2001/02/02 10:50:40 cjolif Exp $
  + * @version $Id: PreferenceManager.java,v 1.3 2001/05/04 15:59:28 hillion Exp $
    */
   public class PreferenceManager
   {
  -    private Properties internal = null;
  -    private Map defaults = null;
  -    private String prefFileName = null;
  -    private String fullName = null;
  +    protected Properties internal = null;
  +    protected Map defaults = null;
  +    protected String prefFileName = null;
  +    protected String fullName = null;
   
  -    private final static String USER_HOME = System.getProperty("user.home");
  -    private final static String USER_DIR  = System.getProperty("user.dir");
  -    private final static String FILE_SEP  = System.getProperty("file.separator");
  +    protected final static String USER_HOME = System.getProperty("user.home");
  +    protected final static String USER_DIR  = System.getProperty("user.dir");
  +    protected final static String FILE_SEP  = System.getProperty("file.separator");
   
       private static String PREF_DIR = null;
   
       /**
        * Creates a preference manager.
  -     * @prefFileName the name of the preference file.
  +     * @param prefFileName the name of the preference file.
        */
       public PreferenceManager(String prefFileName)
       {
  @@ -85,8 +85,8 @@
       /**
        * Creates a preference manager with a default values
        * initialization map.
  -     * @prefFileName the name of the preference file.
  -     * @defaults where to get defaults value if the value is
  +     * @param prefFileName the name of the preference file.
  +     * @param defaults where to get defaults value if the value is
        * not specified in the file.
        */
       public PreferenceManager(String prefFileName, Map defaults)
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org