jvanzyl 02/01/18 14:19:59
Modified: src/java/org/apache/stratum/xo Mapper.java
Log:
- changes to allow a live object instance to be populated with
additional information
Revision Changes Path
1.12 +96 -9
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.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- Mapper.java 17 Jan 2002 02:03:52 -0000 1.11
+++ Mapper.java 18 Jan 2002 22:19:59 -0000 1.12
@@ -140,7 +140,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.11 2002/01/17 02:03:52 jmcnally Exp $
+ * @version $Id: Mapper.java,v 1.12 2002/01/18 22:19:59 jvanzyl Exp $
*/
// How to use the resources package to pull in the
@@ -247,6 +247,21 @@
}
/**
+ * Loads a XML document (from the file system), maps it to
+ * a live instance of the JavaBean <code>bean</code>, and returns
+ * the modified instance.
+ *
+ * @param xmlInput The file system path to the XML data file to
+ * process.
+ * @see #map(InputStream, String)
+ */
+ public Object map(File xmlInput, Object bean)
+ throws Exception
+ {
+ return map(new FileInputStream(xmlInput), bean);
+ }
+
+ /**
* Loads a XML document (first trying the classpath, then the file
* system), maps it to an instance of the JavaBean
* <code>beanClass</code>, and returns the instance.
@@ -258,6 +273,40 @@
public Object map(String xmlInput, String beanClass)
throws Exception
{
+ InputStream xmlStream = findXmlStream(xmlInput);
+
+ // Get the base directory
+ basePath = FileUtils.dirname(xmlInput);
+
+ return map(xmlStream, beanClass);
+ }
+
+ /**
+ * Loads a XML document (first trying the classpath, then the file
+ * system), maps it to a live instance of the JavaBean
+ * <code>bean</code>, and returns the modified instance.
+ *
+ * @param xmlInput The path (either classpath resource or file
+ * system) to the XML data file to process.
+ * @see #map(InputStream, Object)
+ */
+ public Object map(String xmlInput, Object bean)
+ throws Exception
+ {
+ InputStream xmlStream = findXmlStream(xmlInput);
+ return map(xmlStream, bean);
+ }
+
+ /**
+ * Find the specified XML resource by looking in the
+ * classpath first, and subsequently the file system.
+ *
+ * @param xmlInput The XML resource to find.
+ * @return The found InputStream.
+ */
+ private InputStream findXmlStream(String xmlInput)
+ throws Exception
+ {
// Use the source path to grab a stream from the classpath.
InputStream xmlStream =
getClass().getClassLoader().getResourceAsStream(xmlInput);
@@ -266,14 +315,30 @@
// Couldn't load from the classpath -- try the file system.
xmlStream = new FileInputStream(xmlInput);
}
-
- // Get the base directory
- basePath = FileUtils.dirname(xmlInput);
-
- return map(xmlStream, beanClass);
+
+ return xmlStream;
}
/**
+ * Loads a XML document from <code>xmlInput</code>, maps it to a
+ * live instance of the JavaBean <code>bean</code>, and returns
+ * the modified instance.
+ *
+ * @param xmlInput A stream of the XML data to process.
+ * @param bean The live instance of the parent bean.
+ * bean, or <code>null</code> to read it from the
+ * <code>className</code> attribute of the first <code>Node</code>
+ * in the XML document.
+ * @return The modified instance of <code>bean</code>.
+ * @exception DocumentException If the buildprocess fails.
+ */
+ public Object map(InputStream xmlInput, Object bean)
+ throws Exception
+ {
+ return map(xmlInput,bean,null);
+ }
+
+ /**
* Loads a XML document from <code>xmlInput</code>, maps it to an
* instance of the JavaBean <code>beanClass</code>, and returns
* the instance.
@@ -284,9 +349,28 @@
* <code>className</code> attribute of the first <code>Node</code>
* in the XML document.
* @return The fleshed-out instance of <code>beanClass</code>.
- * @exception DocumentException If the buildprocess fails.
+ * @exception DocumentException If the build process fails.
*/
public Object map(InputStream xmlInput, String beanClass)
+ throws Exception
+ {
+ return map(xmlInput,null,beanClass);
+ }
+
+ /**
+ * Loads a XML document from <code>xmlInput</code>, maps it to
+ * a newly created object if <code>bean</code> is <code>null</null>,
+ * or maps it to a live <code>bean</bean>.
+ *
+ * @param xmlInput A Stream of the XML data to process.
+ * @param bean A live object instance to modify if not <code>null</code>
+ * @param beanClass Full qualified name of class to instantiate
+ * and populate.
+ * @return The modified <code>bean</code> instance or the fleshed-out
+ * instance of <code>beanClass</code>
+ * @exception DocumentException If the build process fails.
+ */
+ protected Object map(InputStream xmlInput, Object bean, String beanClass)
throws Exception
{
SAXReader xmlReader = new SAXReader();
@@ -294,8 +378,11 @@
// Create the parent bean.
Element rootElement = document.getRootElement();
- basePackage = parsePackage(beanClass);
- Object bean = createInstance(rootElement, beanClass);
+
+ if (bean == null)
+ {
+ bean = createInstance(rootElement, beanClass);
+ }
// Assure we have the fully qualified class of the bean.
if (beanClass == null)
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>