db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject svn commit: r421030 - /db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/RepositoryPersistor.java
Date Tue, 11 Jul 2006 23:39:48 GMT
Author: arminw
Date: Tue Jul 11 16:39:48 2006
New Revision: 421030

URL: http://svn.apache.org/viewvc?rev=421030&view=rev
Log:
allow to set a custom EntityResolver, if not set use OJB's default EntityResolver - fix for
OJB-117

Modified:
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/RepositoryPersistor.java

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/RepositoryPersistor.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/RepositoryPersistor.java?rev=421030&r1=421029&r2=421030&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/RepositoryPersistor.java
(original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/RepositoryPersistor.java
Tue Jul 11 16:39:48 2006
@@ -37,6 +37,7 @@
 import org.apache.ojb.broker.util.logging.Logger;
 import org.apache.ojb.broker.util.logging.LoggerFactory;
 import org.xml.sax.ContentHandler;
+import org.xml.sax.EntityResolver;
 import org.xml.sax.ErrorHandler;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
@@ -55,6 +56,7 @@
     // TODO: Refactoring of the metadata reading/handling?
 
     private static Logger log = LoggerFactory.getLogger(RepositoryPersistor.class);
+    private EntityResolver entityResolver;
 
     public RepositoryPersistor()
     {
@@ -235,7 +237,7 @@
         parser needs a base url to find referenced entities.
         */
         // InputSource source = new InputSource(url.openStream());
-        
+
         String pathName = url.toExternalForm();
 
         log.info("Building repository from :" + pathName);
@@ -246,20 +248,20 @@
         InputStream in = conn.getInputStream();
         source.setByteStream(in);
         try
-		{
+        {
         	return readMetadataFromXML(source, targetRepository);
-		}
+        }
         finally
-		{
-        	try
-			{
-        		in.close();
-			}
-        	catch (IOException x)
-			{
+        {
+            try
+            {
+                in.close();
+            }
+            catch (IOException x)
+            {
                 log.warn("unable to close repository input stream [" + x.getMessage() + "]",
x);
-			}
-		}
+            }
+        }
     }
 
     /**
@@ -271,7 +273,7 @@
     {
         // TODO: make this configurable
         boolean validate = false;
-        
+
         // get a xml reader instance:
         SAXParserFactory factory = SAXParserFactory.newInstance();
         log.info("RepositoryPersistor using SAXParserFactory : " + factory.getClass().getName());
@@ -281,11 +283,21 @@
         }
         SAXParser p = factory.newSAXParser();
         XMLReader reader = p.getXMLReader();
+
+        // use extended resolver
+        wrapResolver(reader);
+
         if (validate)
         {
             reader.setErrorHandler(new OJBErrorHandler());
         }
 
+        return createRepository(target, reader, source);
+    }
+
+    private Object createRepository(Class target, XMLReader reader, InputSource source)
+            throws IOException, SAXException
+    {
         Object result;
         if (DescriptorRepository.class.equals(target))
         {
@@ -316,7 +328,7 @@
         return result;
     }
 
-    private URL buildURL(String repositoryFileName) throws MalformedURLException
+    protected URL buildURL(String repositoryFileName) throws MalformedURLException
     {
         //j2ee compliant lookup of resources
         URL url = ClassHelper.getResource(repositoryFileName);
@@ -345,7 +357,115 @@
         return url;
     }
 
+    /**
+     * Override to change handling of specific
+     * {@link org.xml.sax.EntityResolver} implementations.
+     * If no {@link org.xml.sax.EntityResolver} was set with
+     * {@link #setEntityResolver(org.xml.sax.EntityResolver)}, this class
+     * use a default entity resolver implementation
+     * ({@link org.apache.ojb.broker.metadata.RepositoryPersistor.OjbEntityResolver}).
+     *
+     * @param reader The {@link org.xml.sax.XMLReader} used by this class.
+     */
+    protected void wrapResolver(XMLReader reader)
+    {
+        if(getEntityResolver() != null)
+        {
+            reader.setEntityResolver(getEntityResolver());
+        }
+        else
+        {
+            EntityResolver r = reader.getEntityResolver();
+            reader.setEntityResolver(new OjbEntityResolver(r));
+        }
+    }
+
+    /**
+     * Get the custom {@link org.xml.sax.EntityResolver}.
+     *
+     * @return The custom {@link org.xml.sax.EntityResolver} or <em>null</em>
+     * if not set (in this case OJB's entity resolver was used).
+     */
+    public EntityResolver getEntityResolver()
+    {
+        return entityResolver;
+    }
+
+    /**
+     * Set a custom {@link org.xml.sax.EntityResolver}.
+     * If not set OJB's entity resolver was used.
+     *
+     * @param entityResolver
+     */
+    public void setEntityResolver(EntityResolver entityResolver)
+    {
+        this.entityResolver = entityResolver;
+    }
+
+    //===================================================================
+    // inner class
+    //===================================================================
+
+    /**
+     * OJB's default {@link org.xml.sax.EntityResolver} implementation. If possible
+     * the resolver specified in class constructor is used, else the URI is used
+     * to create an {@link org.xml.sax.InputSource}.
+     */
+    class OjbEntityResolver implements EntityResolver
+    {
+        private EntityResolver resolver;
+
+        public OjbEntityResolver(EntityResolver resolver)
+        {
+            this.resolver = resolver;
+        }
+
+        public InputSource resolveEntity(String name, String uri) throws IOException, SAXException
+        {
+            InputSource result = null;
+            if(resolver != null)
+            {
+                try
+                {
+                    result = resolver.resolveEntity(name, uri);
+                }
+                catch(Exception e)
+                {
+                    log.warn("Default EntityResolver " + resolver + " can't resolve resource
'" + uri
+                            + "', will use OJB's extended implementation to resolve entity",
e);
+                }
+            }
+            if(result == null)
+            {
+                result = internalResolve(name, uri);
+            }
+            return result;
+        }
+
+        InputSource internalResolve(String name, String uri)
+        {
+            //System.out.println("uri: " + uri);
+            InputStream inputStream = ClassLoader.getSystemResourceAsStream(uri.substring(uri.lastIndexOf('/')
+ 1));
+            if(inputStream != null)
+            {
+                InputSource inputSource = new InputSource(inputStream);
+                inputSource.setSystemId(uri);
+                inputSource.setPublicId(name);
+                return inputSource;
+            }
+            else
+            {
+                return null;
+            }
+        }
+    }
+
+    //===================================================================
     // inner class
+    //===================================================================
+    /**
+     * OJB's simple error handler to log SAX error events.
+     */
     class OJBErrorHandler implements ErrorHandler
     {
         public void warning(SAXParseException exception)



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


Mime
View raw message