dlr 02/01/10 19:34:39
Modified: src/java/org/apache/stratum/xo Mapper.java
Log:
o Added a note about the lack of thread safety in this class
(inconsequential, but perhaps worth noting).
o Taught map(String, String) to first try using getResourceAsStream()
to load the XML file from the classpath.
o Renamed baseDirectory to basePath, since this class now does loading
from the classpath as well as from the file system.
o If the inclusionRule for an element is empty, don't append / -- it
screws up getResourceAsStream() (and thus resource loading from the
classpath).
Revision Changes Path
1.4 +37 -21
jakarta-turbine-stratum/src/java/org/apache/stratum/xo/Mapper.java
Index: Mapper.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-stratum/src/java/org/apache/stratum/xo/Mapper.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -u -r1.3 -r1.4
--- Mapper.java 2 Jan 2002 09:26:41 -0000 1.3
+++ Mapper.java 11 Jan 2002 03:34:39 -0000 1.4
@@ -67,7 +67,7 @@
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.ConvertUtils;
-import org.apache.commons.util.FileUtils;
+import org.apache.commons.util.StringUtils;
import java.lang.reflect.Method;
import org.apache.stratum.introspection.Introspector;
@@ -133,9 +133,12 @@
* No:
* Simple String value to attach
*
+ * <p>
+ * This class is not thread safe.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
- * @version $Id: Mapper.java,v 1.3 2002/01/02 09:26:41 dlr Exp $
+ * @author <a href="mailto:[EMAIL PROTECTED]">Daniel Rall</a>
+ * @version $Id: Mapper.java,v 1.4 2002/01/11 03:34:39 dlr Exp $
*/
// How to use the resources package to pull in the
@@ -178,7 +181,7 @@
* Base directory where the initial XML file is read
* from if files are being used.
*/
- private String baseDirectory;
+ private String basePath;
/**
* Turn on debugging or not.
@@ -218,30 +221,38 @@
}
/**
- * Loads a document from a file.
+ * Loads a XML document from the classpath, maps it to an instance
+ * of the JavaBean <code>beanClass</code>, and returns the instance.
*
- * @param source The file system path to the XML data file.
+ * @param source The path (either classpath resource or file
+ * system) to the XML data file.
* @param beanClass The fully qualified class name of the parent bean.
+ * @return The fleshed-out instance of <code>beanClass</code>.
* @exception DocumentException If the buildprocess fails.
*/
public Object map(String source, String beanClass)
throws Exception
{
- // We want to be able to use the resource package.
- // What is the best way to hook in?
-
- InputStream in = new FileInputStream(source);
+ // Use the source path to grab a stream from the classpath.
+ InputStream in =
+ getClass().getClassLoader().getResourceAsStream(source);
+ if (in == null)
+ {
+ // Couldn't load from the classpath -- try the file system.
+ in = new FileInputStream(source);
+ }
SAXReader xmlReader = new SAXReader();
document = xmlReader.read(in);
-
- // Get the base directory
- baseDirectory = FileUtils.dirname(source);
-
+
+ // Get the base package and directory
+ int i = beanClass.lastIndexOf('.');
+ basePackage = (i != -1 ? beanClass.substring(0, i) : "");
+ basePath = StringUtils.replace(basePackage, ".", "/");
+
// Create the parent bean.
Object bean = Class.forName(beanClass).newInstance();
// Determine the base package to use for object creation.
- basePackage = beanClass.substring(0, beanClass.lastIndexOf('.'));
return treeWalk(document.getRootElement(), bean);
}
@@ -311,7 +322,8 @@
// can be populated. We assume here that the
// object that we are creating is in the same
// package as the parent object.
- String className = basePackage + "." +
elementToClassName(name);
+ String className = basePackage + "." +
+ elementToClassName(name);
// We have to check for inclusion rules and build
// and object from an external XML file if that's
@@ -458,15 +470,19 @@
if (inclusionRule == null)
{
return x;
- }
+ }
- String className = basePackage + "." +
- elementToClassName(name);
+ String className = basePackage + '.' + elementToClassName(name);
String id = ((Element)node).attributeValue(EID);
- x = map(baseDirectory + "/" + inclusionRule + "/" + id + ".xml",
- className);
-
+ StringBuffer path = new StringBuffer(basePath).append('/');
+ if (inclusionRule.length() > 0)
+ {
+ path.append(inclusionRule).append('/');
+ }
+ path.append(id).append(".xml");
+ x = map(path.toString(), className);
+
if (x == null)
{
debug("ERROR: Problem loading -> " + node.getName() + ":" + x);
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>