jvanzyl 02/01/14 21:05:46
Modified: src/java/org/apache/stratum/xo Mapper.java
Log:
- getting the tests working again
- adding map(InputStream, String) so that the decision to use the filesystem,
classpath or any other source is left up to the client code.
- i will add tests for classpath locating and then update turbine
we are back to the pattern of using the attributes for mapping information
and the body text as class attributes.
Revision Changes Path
1.7 +124 -44
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.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- Mapper.java 11 Jan 2002 23:37:56 -0000 1.6
+++ Mapper.java 15 Jan 2002 05:05:46 -0000 1.7
@@ -54,6 +54,7 @@
* <http://www.apache.org/>.
*/
+import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
@@ -138,7 +139,7 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Daniel Rall</a>
- * @version $Id: Mapper.java,v 1.6 2002/01/11 23:37:56 dlr Exp $
+ * @version $Id: Mapper.java,v 1.7 2002/01/15 05:05:46 jvanzyl Exp $
*/
// How to use the resources package to pull in the
@@ -191,6 +192,12 @@
private String basePath;
/**
+ * Base package of all the classes we may need to
+ * create the specified object.
+ */
+ private String basePackage;
+
+ /**
* Turn on debugging or not.
*/
private boolean debug = false;
@@ -220,6 +227,15 @@
introspector.clearCache();
}
+ public Object map(File file, String beanClass)
+ throws Exception
+ {
+ // Get the base directory
+ basePath = FileUtils.dirname(file.getAbsolutePath());
+
+ return map(new FileInputStream(file), beanClass);
+ }
+
/**
* Loads a XML document (first trying the classpath, then the file
* system), maps it to an instance of the JavaBean
@@ -234,19 +250,11 @@
* @return The fleshed-out instance of <code>beanClass</code>.
* @exception DocumentException If the buildprocess fails.
*/
- public Object map(String source, String beanClass)
+ public Object map(InputStream inputStream, String beanClass)
throws Exception
{
- // 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);
+ document = xmlReader.read(inputStream);
// Assure we have the beanClass
Element rootElement = document.getRootElement();
@@ -255,13 +263,13 @@
beanClass = elementClassName(rootElement);
}
- // Get the base directory
- basePath = FileUtils.dirname(source);
-
// 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("."));
+
+ // Determine the base package to use for object creation.
return treeWalk(rootElement, bean);
}
@@ -313,6 +321,7 @@
if (hasChildren(node))
{
debug("Node " + node.getName() + " has children");
+
// We now have the situation where we need to
// instantiate a parent object so that elements
// that are subsequently processed can have their
@@ -323,12 +332,12 @@
// can be populated. We assume here that the
// object that we are creating is in the same
// package as the parent object.
- String className = elementClassName((Element) node);
-
+ String className = basePackage + "." +
elementToClassName(name);
+
// We have to check for inclusion rules and build
// and object from an external XML file if that's
// the case.
- Object o = inclusion((Element) node);
+ Object o = inclusion(node,name,bean);
if (o == null)
{
@@ -336,24 +345,14 @@
try
{
// Should probably use the factory manager.
- Class c = Class.forName(className);
- debug("c=" + c);
- o = c.newInstance();
- debug("o=" + o);
+ o = Class.forName(className).newInstance();
}
catch (Exception e)
{
- debug("Problem instantiating class " +
- className + ": " + e);
- //e.printStackTrace(System.out);
+ debug("Problem instantiating class: " + e);
}
- }
-
- if (o != null)
- {
- debug("Created new object -> " + className);
- }
-
+ }
+
// Now we have to attach the newly created object
// to its parent object.
if (isPlural(node.getParent().getName()))
@@ -380,19 +379,51 @@
else
{
debug("Node " + node.getName() + " has no children");
- // Now we are dealing with elements that refer
- // to a noun singular and these elements have
- // no children.
-
- Object o = inclusion((Element) node);
- Class type = null;
-
- if (o == null)
+
+ Object o = null;
+ // We will check for the special case where a
+ // class name is being specified as an attribute.
+ if (((Element)node).attributeValue(CLASS_NAME) != null)
{
- type = PropertyUtils.getPropertyType(bean,name);
- o = ConvertUtils.convert(node.getText(), type);
+ debug("Using className metadata to build object");
+
+ String className = elementClassName((Element) node);
+
+ try
+ {
+ // Should probably use the factory manager.
+ Class c = Class.forName(className);
+ debug("c=" + c);
+ o = c.newInstance();
+ debug("o=" + o);
+ }
+ catch (Exception e)
+ {
+ debug("Problem instantiating class " +
+ className + ": " + e);
+ }
+
+ if (o != null)
+ {
+ debug("Created new object -> " + className);
+ }
}
+ else
+ {
+ // Now we are dealing with elements that refer
+ // to a noun singular and these elements have
+ // no children.
+ o = inclusion((Element) node);
+ Class type = null;
+
+ if (o == null)
+ {
+ type = PropertyUtils.getPropertyType(bean,name);
+ o = ConvertUtils.convert(node.getText(), type);
+ }
+ }
+
if (isPlural(node.getParent().getName()))
{
// This noun singular element's parent element
@@ -422,7 +453,7 @@
// 'van Zyl' we attempt to set simple bean
// properties.
setProperty(bean,name,o);
- }
+ }
}
}
}
@@ -460,6 +491,9 @@
if (beanClass == null)
{
// Try for a nested element of the same name.
+
+ debug(element.toString());
+
beanClass = element.element(CLASS_NAME).getText();
if (beanClass == null)
{
@@ -512,7 +546,7 @@
// Passing null as the second arg will tell map() to look for
// a className attribute on the root element.
- Object x = map(path.toString(), null);
+ Object x = map(new File(path.toString()), null);
if (x == null)
{
@@ -522,6 +556,40 @@
}
/**
+ * Build up and object by pulling in an external XML
+ * document.
+ *
+ * @param node
+ * @param name
+ * @param bean
+ * @return Object The Java object build up by the inclusion process
+ */
+ private Object inclusion(Node node,String name, Object bean)
+ throws Exception
+ {
+ String inclusionRule = (String) inclusionRules.get(name);
+ Object x = null;
+
+ if (inclusionRule == null)
+ {
+ return x;
+ }
+
+ String className = basePackage + "." +
+ elementToClassName(name);
+
+ String id = ((Element)node).attributeValue(EID);
+ x = map(new File(basePath + "/" + inclusionRule + "/" + id + ".xml"),
className);
+
+ if (x == null)
+ {
+ debug("ERROR: Problem loading -> " + node.getName() + ":" + x);
+ }
+
+ return x;
+ }
+
+ /**
* Add an object to a parent object
*
* @param bean
@@ -637,7 +705,19 @@
return (text == null ? null : prefix +
text.substring(0, 1).toUpperCase() + text.substring(1));
}
-
+
+ /**
+ * Makes the first letter capital and leaves the rest as is.
+ *
+ * @param text The text to modify.
+ * @return The modified text.
+ */
+ private String elementToClassName(String text)
+ {
+ return (text == null ? null :
+ text.substring(0, 1).toUpperCase() + text.substring(1));
+ }
+
/**
* Simple debug printer.
*/
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>