commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r165618 - in /jakarta/commons/proper/configuration/trunk: src/java/org/apache/commons/configuration/XMLPropertiesConfiguration.java xdocs/changes.xml
Date Mon, 02 May 2005 10:55:18 GMT
Author: ebourg
Date: Mon May  2 03:55:17 2005
New Revision: 165618

URL: http://svn.apache.org/viewcvs?rev=165618&view=rev
Log:
Removed the dependency on Digester for XMLPropertiesConfiguration (contributed by Alistair
Young)

Modified:
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLPropertiesConfiguration.java
    jakarta/commons/proper/configuration/trunk/xdocs/changes.xml

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLPropertiesConfiguration.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLPropertiesConfiguration.java?rev=165618&r1=165617&r2=165618&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLPropertiesConfiguration.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLPropertiesConfiguration.java
Mon May  2 03:55:17 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004 The Apache Software Foundation.
+ * Copyright 2004-2005 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License")
  * you may not use this file except in compliance with the License.
@@ -23,12 +23,17 @@
 import java.net.URL;
 import java.util.Iterator;
 import java.util.List;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
 
-import org.apache.commons.digester.Digester;
 import org.apache.commons.lang.StringEscapeUtils;
 import org.apache.commons.lang.StringUtils;
+
+import org.xml.sax.Attributes;
 import org.xml.sax.EntityResolver;
 import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.DefaultHandler;
 
 /**
  * This configuration implements the XML properties format introduced in Java
@@ -44,17 +49,17 @@
  *   <entry key="key2">value2</entry>
  *   <entry key="key3">value3</entry>
  * </properties>
- * </pre>
- *
+ * </pre>
+ * 
  * The Java 5.0 runtime is not required to use this class. The default encoding
  * for this configuration format is UTF-8. Note that unlike
  * <code>PropertiesConfiguration</code>, <code>XMLPropertiesConfiguration</code>
  * does not support includes.
  *
- * @since 1.1
- *
  * @author Emmanuel Bourg
+ * @author Alistair Young
  * @version $Revision$, $Date$
+ * @since 1.1
  */
 public class XMLPropertiesConfiguration extends PropertiesConfiguration
 {
@@ -121,35 +126,31 @@
 
     public void load(Reader in) throws ConfigurationException
     {
-        // todo: replace with a pure SAX implementation to reduce the dependencies
-
-        // set up the digester
-        Digester digester = new Digester();
-        digester.setEntityResolver(new EntityResolver(){
-            public InputSource resolveEntity(String publicId, String systemId)
-            {
-                return new InputSource(getClass().getClassLoader().getResourceAsStream("properties.dtd"));
-            }
-        });
-
-        digester.addCallMethod("properties/comment", "setHeader", 0);
-
-        digester.addCallMethod("properties/entry", "addProperty", 2);
-        digester.addCallParam("properties/entry", 0, "key");
-        digester.addCallParam("properties/entry", 1);
-
-        // todo: support included properties ?
+        SAXParserFactory factory = SAXParserFactory.newInstance();
+        factory.setNamespaceAware(false);
+        factory.setValidating(true);
 
-        // parse the file
-        digester.push(this);
         try
         {
-            digester.parse(in);
+            SAXParser parser = factory.newSAXParser();
+
+            XMLReader xmlReader = parser.getXMLReader();
+            xmlReader.setEntityResolver(new EntityResolver()
+            {
+                public InputSource resolveEntity(String publicId, String systemId)
+                {
+                    return new InputSource(getClass().getClassLoader().getResourceAsStream("properties.dtd"));
+                }
+            });
+            xmlReader.setContentHandler(new XMLPropertiesHandler());
+            xmlReader.parse(new InputSource(in));
         }
         catch (Exception e)
         {
             throw new ConfigurationException("Unable to parse the configuration file", e);
         }
+
+        // todo: support included properties ?
     }
 
     public void save(Writer out) throws ConfigurationException
@@ -218,4 +219,68 @@
             writeProperty(out, key, values.get(i));
         }
     }
-}
+
+    /**
+     * SAX Handler to parse a XML properties file.
+     *
+     * @author Alistair Young
+     * @since 1.2
+     */
+    private class XMLPropertiesHandler extends DefaultHandler
+    {
+        /** The key of the current entry being parsed. */
+        private String key;
+
+        /** The value of the current entry being parsed. */
+        private StringBuffer value = new StringBuffer();
+
+        /** Indicates that a comment is being parsed. */
+        private boolean inCommentElement;
+
+        /** Indicates that an entry is being parsed. */
+        private boolean inEntryElement;
+
+        public void startElement(String uri, String localName, String qName, Attributes attrs)
+        {
+            if ("comment".equals(qName))
+            {
+                inCommentElement = true;
+            }
+
+            if ("entry".equals(qName))
+            {
+                key = attrs.getValue("key");
+                inEntryElement = true;
+            }
+        }
+
+        public void endElement(String uri, String localName, String qName)
+        {
+            if (inCommentElement)
+            {
+                // We've just finished a <comment> element so set the header
+                setHeader(value.toString());
+                inCommentElement = false;
+            }
+
+            if (inEntryElement)
+            {
+                // We've just finished an <entry> element, so add the key/value pair
+                addProperty(key, value.toString());
+                inEntryElement = false;
+            }
+
+            // Clear the element value buffer
+            value = new StringBuffer();
+        }
+
+        public void characters(char[] chars, int start, int length)
+        {
+            /**
+             * We're currently processing an element. All character data from now until
+             * the next endElement() call will be the data for this  element.
+             */
+            value.append(chars, start, length);
+        }
+    }
+}
\ No newline at end of file

Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?rev=165618&r1=165617&r2=165618&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Mon May  2 03:55:17 2005
@@ -23,6 +23,10 @@
   <body>
 
     <release version="1.2-dev" date="in SVN">
+      <action dev="ebourg" type="update" due-to="Alistair Young">
+        XMLPropertiesConfiguration no longer depends on Digester to parse the
+        configuration file, it's now implemented with a pure SAX parser.
+      </action>
       <action dev="oheger" type="update" due-to="Mi Zhang" issue="34442">
         Fixed a bug which causes XMLConfiguration.save to lose attribute values
         under some circumstances. The clear() method now also ensures that the



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


Mime
View raw message