[Yanel-commits] rev 43907 -
public/yanel/trunk/src/core/java/org/wyona/yanel/core/source
guillaume at wyona.com
guillaume at wyona.com
Tue Jul 28 13:35:46 CEST 2009
Author: guillaume
Date: 2009-07-28 13:35:46 +0200 (Tue, 28 Jul 2009)
New Revision: 43907
Added:
public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTabstractResolver.java
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:
Merged identical code into an abstract class.
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 11:20:35 UTC (rev 43906)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTHtdocsResolver.java 2009-07-28 11:35:46 UTC (rev 43907)
@@ -1,14 +1,5 @@
package org.wyona.yanel.core.source;
-import java.io.File;
-import java.io.InputStream;
-import java.net.URL;
-import java.net.URLConnection;
-
-import javax.xml.transform.Source;
-import javax.xml.transform.URIResolver;
-
-import org.apache.log4j.Logger;
import org.wyona.yanel.core.Resource;
@@ -20,69 +11,19 @@
*
* e.g.
* rthtdocs:/foo/bar.xml
- *
- * TODO: support access to other resource types (e.g. rthtdocs:resourcTypeIdentifier:/foo/bar.xml)
- *
*/
-public class RTHtdocsResolver implements URIResolver {
+public class RTHtdocsResolver extends RTabstractResolver {
- 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;
+ super(resource);
}
+ @Override
protected String getScheme() { return SCHEME; }
- protected String getPathPrefix() { return PATH_PREFIX; }
- public Source resolve(String href, String base) throws SourceException {
- 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 '/')
- if (href.endsWith("/")){
- return null;
- }
- String path = href.substring(prefix.length());
- try {
- String fullyQualifiedName = resource.getClass().getName();
- int lastDot = fullyQualifiedName.lastIndexOf ('.');
- String packageName = fullyQualifiedName.substring (0, lastDot);
- if (log.isDebugEnabled()) {
- log.debug("Package: " + packageName);
- }
- 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();
- long resourceLastModifier = uc.getLastModified();
- 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(resourceConfigDir.getAbsolutePath() + "/" + getPathPrefix() + path.replace('/', File.separatorChar));
- InputStream in = new java.io.FileInputStream(resourceFile);
- YanelStreamSource source = new YanelStreamSource(in);
- long resourceLastModified = resourceFile.lastModified();
- source.setLastModified(resourceLastModified);
- return source;
- } catch (Exception ex) {
- //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);
- }
- }
- }
-}
+ @Override
+ protected String getPathPrefix() { return PATH_PREFIX; }
+}
\ No newline at end of file
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 11:20:35 UTC (rev 43906)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTYanelHtdocsResolver.java 2009-07-28 11:35:46 UTC (rev 43907)
@@ -1,14 +1,5 @@
package org.wyona.yanel.core.source;
-import java.io.File;
-import java.io.InputStream;
-import java.net.URL;
-import java.net.URLConnection;
-
-import javax.xml.transform.Source;
-import javax.xml.transform.URIResolver;
-
-import org.apache.log4j.Logger;
import org.wyona.yanel.core.Resource;
@@ -20,69 +11,19 @@
*
* e.g.
* rtyanelhtdocs:/foo/bar.xml
- *
- * TODO: support access to other resource types (e.g. rtyanelhtdocs:resourcTypeIdentifier:/foo/bar.xml)
- *
*/
-public class RTYanelHtdocsResolver implements URIResolver {
+public class RTYanelHtdocsResolver extends RTabstractResolver {
- 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;
+ super(resource);
}
+ @Override
protected String getScheme() { return SCHEME; }
- protected String getPathPrefix() { return PATH_PREFIX; }
- public Source resolve(String href, String base) throws SourceException {
- 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 '/')
- if (href.endsWith("/")){
- return null;
- }
- String path = href.substring(prefix.length());
- try {
- String fullyQualifiedName = resource.getClass().getName();
- int lastDot = fullyQualifiedName.lastIndexOf ('.');
- String packageName = fullyQualifiedName.substring (0, lastDot);
- if (log.isDebugEnabled()) {
- log.debug("Package: " + packageName);
- }
- 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();
- long resourceLastModifier = uc.getLastModified();
- 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(resourceConfigDir.getAbsolutePath() + "/" + getPathPrefix() + path.replace('/', File.separatorChar));
- InputStream in = new java.io.FileInputStream(resourceFile);
- YanelStreamSource source = new YanelStreamSource(in);
- long resourceLastModified = resourceFile.lastModified();
- source.setLastModified(resourceLastModified);
- return source;
- } catch (Exception ex) {
- //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);
- }
- }
- }
+ @Override
+ protected String getPathPrefix() { return PATH_PREFIX; }
}
Copied: public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTabstractResolver.java (from rev 43906, public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTHtdocsResolver.java)
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTabstractResolver.java (rev 0)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTabstractResolver.java 2009-07-28 11:35:46 UTC (rev 43907)
@@ -0,0 +1,77 @@
+package org.wyona.yanel.core.source;
+
+import java.io.File;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.URIResolver;
+
+import org.apache.log4j.Logger;
+import org.wyona.yanel.core.Resource;
+
+
+/**
+ * TODO: support access to other resource types (e.g. rthtdocs:resourcTypeIdentifier:/foo/bar.xml)
+ */
+public abstract class RTabstractResolver implements URIResolver {
+
+ private static Logger log = Logger.getLogger(RTabstractResolver.class);
+ private Resource resource;
+
+ public RTabstractResolver(Resource resource) {
+ this.resource = resource;
+ }
+
+ protected abstract String getScheme();
+ protected abstract String getPathPrefix();
+
+ public Source resolve(String href, String base) throws SourceException {
+ 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 '/')
+ if (href.endsWith("/")){
+ return null;
+ }
+ String path = href.substring(prefix.length());
+ try {
+ String fullyQualifiedName = resource.getClass().getName();
+ int lastDot = fullyQualifiedName.lastIndexOf ('.');
+ String packageName = fullyQualifiedName.substring (0, lastDot);
+ if (log.isDebugEnabled()) {
+ log.debug("Package: " + packageName);
+ }
+ 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();
+ long resourceLastModifier = uc.getLastModified();
+ 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(resourceConfigDir.getAbsolutePath() + "/" + getPathPrefix() + path.replace('/', File.separatorChar));
+ InputStream in = new java.io.FileInputStream(resourceFile);
+ YanelStreamSource source = new YanelStreamSource(in);
+ long resourceLastModified = resourceFile.lastModified();
+ source.setLastModified(resourceLastModified);
+ return source;
+ } catch (Exception ex) {
+ //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);
+ }
+ }
+ }
+}
Property changes on: public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/RTabstractResolver.java
___________________________________________________________________
Name: svn:mergeinfo
+
More information about the Yanel-commits
mailing list