[Yanel-commits] rev 43906 - public/yanel/trunk/src/core/java/org/wyona/yanel/core/source

guillaume at wyona.com guillaume at wyona.com
Tue Jul 28 13:20:35 CEST 2009


Author: guillaume
Date: 2009-07-28 13:20:35 +0200 (Tue, 28 Jul 2009)
New Revision: 43906

Modified:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTHtdocsResolver.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTYanelHtdocsResolver.java
Log:
Touched up the code so that almost all the code of these two nearly identical classes
 can be later merged into an abstract class.

Also cleaned up logging a bit, there is still some work left though...


Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTHtdocsResolver.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTHtdocsResolver.java	2009-07-28 10:31:30 UTC (rev 43905)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTHtdocsResolver.java	2009-07-28 11:20:35 UTC (rev 43906)
@@ -28,15 +28,19 @@
 
     private static Logger log = Logger.getLogger(RTHtdocsResolver.class);
     private static final String SCHEME = "rthtdocs";
+    private static final String PATH_PREFIX = "htdocs";
     private Resource resource;
     
     public RTHtdocsResolver(Resource resource) {
         this.resource = resource;
     }
 
+    protected String getScheme() { return SCHEME; }
+    protected String getPathPrefix() { return PATH_PREFIX; }
+
     public Source resolve(String href, String base) throws SourceException {
-        String prefix = SCHEME + ":";
-        // only accept 'rthtdocs:' URIs
+        String prefix = getScheme() + ":";
+        // only accept '<scheme>:' URIs
         if (href == null || !href.startsWith(prefix)) {
             return null;
         }
@@ -52,8 +56,10 @@
             if (log.isDebugEnabled()) {
                 log.debug("Package: " + packageName);
             }
-            
-            URL url = resource.getClass().getClassLoader().getResource(packageName.replace('.','/') + "/htdocs" + path);
+            URL url = resource.getClass().getClassLoader().getResource(packageName.replace('.','/') + "/" + getPathPrefix() + path);
+            if (url == null) {
+                log.error("Path " + getPathPrefix() + path + " does not seem to be contained within package " + packageName + " of resource " + resource.getResourceTypeUniversalName());
+            }
             InputStream in = url.openStream();
             YanelStreamSource source = new YanelStreamSource(in);
             URLConnection uc = url.openConnection();
@@ -61,15 +67,19 @@
             source.setLastModified(resourceLastModifier);
             return source;
         } catch (Exception e) {
+            File resourceConfigDir = resource.getRTD().getConfigFile().getParentFile();
+            log.info("Fallback to resource config location: " + resourceConfigDir);
             try {
-                File resourceFile = new File(resource.getRTD().getConfigFile().getParentFile().getAbsolutePath() + "/htdocs" + path.replace('/', File.separatorChar));
+                File resourceFile = new File(resourceConfigDir.getAbsolutePath() + "/" + getPathPrefix() + path.replace('/', File.separatorChar));
                 InputStream in = new java.io.FileInputStream(resourceFile);
                 YanelStreamSource source = new YanelStreamSource(in);
-                long resourceLastModifier = resourceFile.lastModified();
-                source.setLastModified(resourceLastModifier);
+                long resourceLastModified = resourceFile.lastModified();
+                source.setLastModified(resourceLastModified);
                 return source;
             } catch (Exception ex) {
-                String errorMsg = "Could not resolve URI: " + path + ": " + e.toString();
+                //FIXME(?): ex is never logged or thrown
+                // but the previously caught e is used instead?!?
+                String errorMsg = "Could not resolve URI: " + path + " (" + resourceConfigDir + ")" + ": " + e.toString();
                 log.error(errorMsg, e);
                 throw new SourceException(errorMsg, e);
             }

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTYanelHtdocsResolver.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTYanelHtdocsResolver.java	2009-07-28 10:31:30 UTC (rev 43905)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTYanelHtdocsResolver.java	2009-07-28 11:20:35 UTC (rev 43906)
@@ -7,6 +7,7 @@
 
 import javax.xml.transform.Source;
 import javax.xml.transform.URIResolver;
+
 import org.apache.log4j.Logger;
 import org.wyona.yanel.core.Resource;
 
@@ -27,19 +28,20 @@
 
     private static Logger log = Logger.getLogger(RTYanelHtdocsResolver.class);
     private static final String SCHEME = "rtyanelhtdocs";
+    private static final String PATH_PREFIX = "yanel-htdocs";
     private Resource resource;
     
     public RTYanelHtdocsResolver(Resource resource) {
         this.resource = resource;
     }
 
-    /**
-     *
-     */
+    protected String getScheme() { return SCHEME; }
+    protected String getPathPrefix() { return PATH_PREFIX; }
+
     public Source resolve(String href, String base) throws SourceException {
-        String prefix = SCHEME + ":";
-        // only accept 'rtyanelhtdocs:' URIs
-        if (href == null || !href.startsWith(prefix)){
+        String prefix = getScheme() + ":";
+        // only accept '<scheme>:' URIs
+        if (href == null || !href.startsWith(prefix)) {
             return null;
         }
         // we can't resolve to a Collection (indicated by a trailing '/')
@@ -54,9 +56,9 @@
             if (log.isDebugEnabled()) {
                 log.debug("Package: " + packageName);
             }
-            URL url = resource.getClass().getClassLoader().getResource(packageName.replace('.','/') + "/yanel-htdocs" + path);
+            URL url = resource.getClass().getClassLoader().getResource(packageName.replace('.','/') + "/" + getPathPrefix() + path);
             if (url == null) {
-                log.info("Path " + path + " does not seem to be contained within package " + packageName + " of resource " + resource.getResourceTypeUniversalName());
+                log.error("Path " + getPathPrefix() + path + " does not seem to be contained within package " + packageName + " of resource " + resource.getResourceTypeUniversalName());
             }
             InputStream in = url.openStream();
             YanelStreamSource source = new YanelStreamSource(in);
@@ -68,13 +70,16 @@
             File resourceConfigDir = resource.getRTD().getConfigFile().getParentFile();
             log.info("Fallback to resource config location: " + resourceConfigDir);
             try {
-                File resourceFile = new File(resourceConfigDir.getAbsolutePath() + "/yanel-htdocs" + path.replace('/', File.separatorChar));
+                File resourceFile = new File(resourceConfigDir.getAbsolutePath() + "/" + getPathPrefix() + path.replace('/', File.separatorChar));
                 InputStream in = new java.io.FileInputStream(resourceFile);
                 YanelStreamSource source = new YanelStreamSource(in);
-                source.setLastModified(resourceFile.lastModified());
+                long resourceLastModified = resourceFile.lastModified();
+                source.setLastModified(resourceLastModified);
                 return source;
             } catch (Exception ex) {
-                String errorMsg = "Could not resolve URI: " + path + " (" + e.toString() + ", " + resourceConfigDir + ")";
+                //FIXME(?): ex is never logged or thrown
+                // but the previously catched e is used instead?!?
+                String errorMsg = "Could not resolve URI: " + path + " (" + resourceConfigDir + ")" + ": " + e.toString();
                 log.error(errorMsg, e);
                 throw new SourceException(errorMsg, e);
             }



More information about the Yanel-commits mailing list