You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avalon.apache.org by "Hamilton Verissimo de Oliveira (Engenharia - SPO)" <ha...@agenciaclick.com.br> on 2004/04/01 20:09:36 UTC

Re: DefaultConfigurationBuilder doesn't support external entities .

Done. I'll apply the patch tonight.

-----------------------------------------------------

RCS file:
/home/cvspublic/avalon/framework/impl/src/java/org/apache/avalon/framework/c
onfiguration/DefaultConfigurationBuilder.java,v
retrieving revision 1.32
diff -u -r1.32 DefaultConfigurationBuilder.java
---
framework/impl/src/java/org/apache/avalon/framework/configuration/DefaultCon
figurationBuilder.java	11 Feb 2004 14:34:25 -0000	1.32
+++
framework/impl/src/java/org/apache/avalon/framework/configuration/DefaultCon
figurationBuilder.java	1 Apr 2004 18:00:01 -0000
@@ -20,6 +20,8 @@
 import java.io.InputStream;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
+
+import org.xml.sax.EntityResolver;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 import org.xml.sax.XMLReader;
@@ -251,5 +253,20 @@
             m_parser.parse( input );
             return m_handler.getConfiguration();
         }
+    }
+    
+    /**
+     * Sets the <code>EntityResolver</code> to 
+     * be used by parser. Useful when dealing with xml
+     * files that reference external entities.
+     * 
+     * @param resolver implementation of <code>EntityResolver</code>
+     */
+    public void setEntityResolver( final EntityResolver resolver )
+    {
+		synchronized( this )
+		{
+			m_parser.setEntityResolver( resolver );
+		}
     }
 }
Index:
framework/impl/src/test/org/apache/avalon/framework/configuration/test/Defau
ltConfigurationBuilderTestCase.java
===================================================================
RCS file:
/home/cvspublic/avalon/framework/impl/src/test/org/apache/avalon/framework/c
onfiguration/test/DefaultConfigurationBuilderTestCase.java,v
retrieving revision 1.10
diff -u -r1.10 DefaultConfigurationBuilderTestCase.java
---
framework/impl/src/test/org/apache/avalon/framework/configuration/test/Defau
ltConfigurationBuilderTestCase.java	24 Feb 2004 22:31:22 -0000	1.10
+++
framework/impl/src/test/org/apache/avalon/framework/configuration/test/Defau
ltConfigurationBuilderTestCase.java	1 Apr 2004 18:00:02 -0000
@@ -17,12 +17,18 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.FileReader;
 import java.io.FileWriter;
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.Reader;
+
 import junit.framework.TestCase;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
 import
org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
 /**
@@ -34,17 +40,22 @@
 public final class DefaultConfigurationBuilderTestCase
     extends TestCase
 {
+	private static final String SIMPLE_FILE_NAME = "config_simple.xml";
+	private static final String NS_FILE_NAME = "config_namespaces.xml";
+	private static final String EXTERNAL_FILE_NAME =
"config_usingexternal.xml";
+	private static final String INNER_FILE_NAME = "config_inner.xml";
+	private static final String TEST_PATH = "test/framework/io/";
 
     private DefaultConfigurationBuilder m_builder;
     private DefaultConfigurationBuilder m_nsBuilder;
+    
     private File m_file;
     private File m_nsFile;
+	private File m_fileWithExternalEntity;
+	private File m_innerXmlFile;
     private File m_testDirectory;
-    private final String m_simpleFileName = "config_simple.xml";
-    private final String m_nsFileName = "config_namespaces.xml";
-    private final String m_path = "test/framework/io/";
 
-    private final String simpleXML =
+    private static final String SIMPLE_XML =
     "<?xml version=\"1.0\" ?>"+
     "<config boolAttr=\"true\" floatAttr=\"1.32\">"+
     "   <elements-a>"+
@@ -114,7 +125,7 @@
         assertEquals( "A value-containing element should have no child
nodes", 0, conf.getChild("elements-c").getChildren().length );
     }
 
-    private final String nsXML =
+    private final String NS_XML =
     "<?xml version=\"1.0\" ?>"+
     "<conf:config"+
     "       boolAttr=\"true\" floatAttr=\"1.32\""+
@@ -132,13 +143,6 @@
     "   <d:element>d:element</d:element>"+
     "   <e:element>e:element</e:element>"+        
     "</conf:config>";
-    /*
-    "<?xml version=\"1.0\"?>"+
-    "<my-system version='1.3'
xmlns:doc=\"http://myco.com/documentation\">"+
-    "   <doc:desc>This is a highly fictitious config file</doc:desc>"+
-    "   <widget name=\"fooWidget\" initOrder=\"1\" threadsafe=\"true\"/>"+
-    "</my-system>";
-    */
 
     /**
      * These assertions apply when the default builder is used to create a
@@ -202,7 +206,6 @@
         assertEquals( "e:element", conf.getChild("e:element").getValue() );
     }
 
-
     /**
      * These assertions apply when the namespace-enabled builder is used to
      * create a Configuration from <code>nsXML</code>, ie namespace support
is
@@ -262,7 +265,6 @@
         assertEquals( "A value-containing element should have no child
nodes", 0, conf.getChild("elements-c").getChildren().length );
     }
 
-
     public DefaultConfigurationBuilderTestCase()
     {
         this("DefaultConfigurationBuilder Test Case");
@@ -271,7 +273,7 @@
     public DefaultConfigurationBuilderTestCase( final String name )
     {
         super( name );
-        m_testDirectory = (new File( m_path)).getAbsoluteFile();
+        m_testDirectory = (new File( TEST_PATH )).getAbsoluteFile();
         if( !m_testDirectory.exists() )
         {
             m_testDirectory.mkdirs();
@@ -281,41 +283,42 @@
     protected void setUp()
         throws Exception
     {
-        m_file = new File( m_testDirectory, m_simpleFileName );
-        m_nsFile = new File( m_testDirectory, m_nsFileName );
+        m_file = new File( m_testDirectory, SIMPLE_FILE_NAME);
+        m_nsFile = new File( m_testDirectory, NS_FILE_NAME );
+		m_fileWithExternalEntity = new File( m_testDirectory,
EXTERNAL_FILE_NAME );
+		m_innerXmlFile = new File( m_testDirectory, INNER_FILE_NAME
);
+        
         FileWriter writer = new FileWriter( m_file );
-        writer.write( simpleXML );
+        writer.write( SIMPLE_XML );
         writer.close();
         writer = new FileWriter( m_nsFile );
-        writer.write( nsXML );
+        writer.write( NS_XML );
         writer.close();
-
+		writer = new FileWriter( m_fileWithExternalEntity );
+		writer.write( XML_WITH_EXTERNAL_ENTITY );
+		writer.close();
+		writer = new FileWriter( m_innerXmlFile );
+		writer.write( INNER_XML );
+		writer.close();
     }
 
     protected  void tearDown()
         throws Exception
     {
-        /*FileWriter writer = new FileWriter( m_file );
-        writer.write( "" );
-        writer.close();
-        writer = new FileWriter( m_nsFile );
-        writer.write( "" );
-        writer.close();*/
         m_builder = null;
         m_nsBuilder = null;
     }
 
-
     public void testBuildFromFileName()
         throws Exception
     {
         m_builder = new DefaultConfigurationBuilder();
         m_nsBuilder = new DefaultConfigurationBuilder(true); // switch on
namespace support
-        Configuration conf = m_builder.buildFromFile( m_path +
m_simpleFileName );
+        Configuration conf = m_builder.buildFromFile( TEST_PATH +
SIMPLE_FILE_NAME );
         simpleAssertions( conf );
-        conf = m_builder.buildFromFile( m_path + m_nsFileName );
+        conf = m_builder.buildFromFile( TEST_PATH + NS_FILE_NAME );
         simpleAssertionsNS( conf );
-        conf = m_nsBuilder.buildFromFile( m_path + m_nsFileName );
+        conf = m_nsBuilder.buildFromFile( TEST_PATH + NS_FILE_NAME );
         nsAssertions( conf );
     }
 
@@ -386,11 +389,11 @@
                       conf.getChild( "trimmed-again-item" ).getValue() );
     }
 
-    
     private final String mixedContentXML =
         "<?xml version=\"1.0\" ?>"+
         "<a>a<a/></a>"
         ;
+
     public void testMixedContentDetection()
         throws Exception
     {
@@ -403,4 +406,52 @@
         } catch ( SAXException e )
         {}
     }
+    
+	private final String XML_WITH_EXTERNAL_ENTITY =
+	"<?xml version=\"1.0\" ?>"+
+	"<!DOCTYPE document"+
+	"["+
+	"<!ENTITY config SYSTEM \"inner.xml\">"+
+	"]>"+
+	"<config boolAttr=\"true\" floatAttr=\"1.32\">"+
+	"   &config; "+
+	"</config>";
+
+	private final String INNER_XML =
+	"   <elements-a>"+
+	"       <element name=\"a\"/>"+
+	"   </elements-a>"+
+	"   <elements-b>"+
+	"       <element name=\"b\"/> "+
+	"   </elements-b>"+
+	"   <elements-b type=\"type-b\"/>"+
+	"   <elements-c>"+
+	"   true"+
+	"   </elements-c>"; 
+    
+    public void testExternalEntity() throws Exception
+    {
+		DefaultConfigurationBuilder builder = new
DefaultConfigurationBuilder();
+		
+		MyEntityResolver customResolver = new MyEntityResolver();
+		
+		builder.setEntityResolver( new MyEntityResolver() );
+		Configuration conf = builder.buildFromFile( TEST_PATH +
EXTERNAL_FILE_NAME );
+		simpleAssertions( conf );
+    }
+    
+	/**
+	 * Mock implementation for EntityResolver
+	 */
+	class MyEntityResolver implements EntityResolver
+	{
+		public InputSource resolveEntity(String publicId, String
systemId) throws SAXException, IOException
+		{
+    		Reader reader = new FileReader( m_innerXmlFile );
+    		
+			return new InputSource(reader);
+		}
+	}
 }

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