jvanzyl 2002/07/17 14:32:38
Modified: src/java/org/apache/maven MavenUtils.java
Log:
o When reading in a project descriptor now look for an <extend> element
which indicates we have a parent to inherit from.
Revision Changes Path
1.30 +71 -7 jakarta-turbine-maven/src/java/org/apache/maven/MavenUtils.java
Index: MavenUtils.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/MavenUtils.java,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -r1.29 -r1.30
--- MavenUtils.java 14 Jul 2002 15:51:23 -0000 1.29
+++ MavenUtils.java 17 Jul 2002 21:32:37 -0000 1.30
@@ -58,12 +58,15 @@
import java.io.File;
import java.io.FileInputStream;
-import java.util.Iterator;
-import java.beans.IntrospectionException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import org.apache.commons.beanutils.BeanUtils;
+import org.apache.commons.collections.BeanMap;
import org.apache.commons.lang.Strings;
-
import org.apache.commons.betwixt.io.BeanReader;
import org.apache.commons.betwixt.XMLIntrospector;
import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
@@ -107,8 +110,69 @@
throws Exception
{
BeanReader beanReader = createBeanReader(Project.class);
- return (Project) beanReader.parse(new FileInputStream(
- projectDescriptor));
+ Project project = (Project) beanReader.parse(new
FileInputStream(projectDescriptor));
+ String pomToExtend = project.getExtend();
+
+ if (pomToExtend != null)
+ {
+ Project parent = (Project) beanReader.parse(new FileInputStream(new
File(pomToExtend)));
+ project = mergeProjects(project, parent);
+ }
+
+ return project;
+ }
+
+ /**
+ * Merge a child and parent Project object.
+ */
+ private static Project mergeProjects(Project child, Project parent)
+ {
+ BeanMap parentBeanMap = new BeanMap(parent);
+ BeanMap childBeanMap = new BeanMap(child);
+
+ for (Iterator i = parentBeanMap.keySet().iterator(); i.hasNext();)
+ {
+ // Take the property for the parent and insert it
+ // into the child bean map.
+ String property = (String) i.next();
+
+ try
+ {
+ // If the childs property is null then take it from
+ // the parent.
+ Object o = childBeanMap.get(property);
+ if (valueNeedsPopulating(o))
+ {
+ childBeanMap.put(property, parentBeanMap.get(property));
+ }
+ }
+ catch (IllegalArgumentException e)
+ {
+ // There is no write method for this property.
+ }
+ }
+ return child;
+ }
+
+ /**
+ * Check a child value to see if it needs populating with
+ * the parent value. The constructor sets List values to []
+ * so we have to check those.
+ */
+ private static boolean valueNeedsPopulating(Object o)
+ {
+ if (o instanceof Collection && ((Collection)o).size() == 0)
+ {
+ return true;
+ }
+ else if (o == null)
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
}
/**
@@ -255,7 +319,7 @@
* properties of the class provided
*/
public static BeanReader createBeanReader(Class clazz)
- throws IntrospectionException
+ throws Exception
{
BeanReader beanReader = new BeanReader();
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>