You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jv...@apache.org on 2002/05/28 17:31:02 UTC

cvs commit: jakarta-commons-sandbox/xo/src/test/org/apache/commons/xo/datamodel TestDatamodelMapping.java datamodel.xml

jvanzyl     02/05/28 08:31:02

  Modified:    xo       project.xml
               xo/src/java/org/apache/commons/xo Mapper.java
               xo/src/test/org/apache/commons/xo/datamodel
                        TestDatamodelMapping.java datamodel.xml
  Log:
  Some refactoring done on the plane ride home. Doesn't really matter
  at this point as betwixt will replace xo completely.
  
  Revision  Changes    Path
  1.8       +1 -1      jakarta-commons-sandbox/xo/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/xo/project.xml,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- project.xml	11 May 2002 14:39:18 -0000	1.7
  +++ project.xml	28 May 2002 15:31:02 -0000	1.8
  @@ -4,7 +4,7 @@
     <version>2</version>
     <name>commons-xo</name>
     <id>commons-xo</id>
  -  <currentVersion>1.0-dev</currentVersion>
  +  <currentVersion>0.8</currentVersion>
     <organization>
   	  <name>Apache Software Foundation</name>
   	  <url>http://www.apache.org</url>
  
  
  
  1.3       +124 -100  jakarta-commons-sandbox/xo/src/java/org/apache/commons/xo/Mapper.java
  
  Index: Mapper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/xo/src/java/org/apache/commons/xo/Mapper.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Mapper.java	4 May 2002 16:00:36 -0000	1.2
  +++ Mapper.java	28 May 2002 15:31:02 -0000	1.3
  @@ -82,19 +82,19 @@
    *
    * <p>
    * <pre>
  - * &lt;person&gt;
  - *   &lt;firstName&gt;Jason&lt;/firstName&gt;
  - *   &lt;lastName&gt;van Zyl&lt;/lastName&gt;
  - *   &lt;hobbies&gt;
  - *     &lt;hobby&gt;somnambulism&lt;/hobby&gt;
  - *     &lt;hobby&gt;squash&lt;/hobby&gt;
  - *   &lt;/hobbies&gt;
  - *   &lt;address&gt;
  - *     &lt;street&gt;50 King&lt;/street&gt;
  - *     &lt;city&gt;Guelph&lt;/city&gt;
  - *     &lt;country&gt;Canada&lt;/country&gt;
  - *   &lt;/address&gt;
  - * &lt;/person&gt;
  + * <person>
  + *   <firstName>Jason</firstName>
  + *   <lastName>van Zyl</lastName>
  + *   <hobbies>
  + *     <hobby>somnambulism</hobby>
  + *     <hobby>squash</hobby>
  + *   </hobbies>
  + *   <address>
  + *     <street>50 King</street>
  + *     <city>Guelph</city>
  + *     <country>Canada</country>
  + *   </address>
  + * </person>
    * </pre>
    * <p>
    * These are some of the assumptions employed to
  @@ -138,24 +138,14 @@
    *           Simple String value to attach
    * </pre>
    * <p>
  + *
    * This class is not thread safe.
    *
    * @author <a href="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
    * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
  - * @version $Id: Mapper.java,v 1.2 2002/05/04 16:00:36 dion Exp $
  + * @version $Id: Mapper.java,v 1.3 2002/05/28 15:31:02 jvanzyl Exp $
    */
   
  -// How to use the resources package to pull in the
  -// XML documents. This will take care of any type specific
  -// issues and will allow multiple sources.
  -
  -// Element name mapping for non conformant XML that you
  -// want to parse anyway.
  -
  -// Jumping stages so that documents like RSS
  -// that don't have noun plural wrappers can still
  -// be parsed. Not necessary but would be nice.
  -
   public class Mapper
   {
       /**
  @@ -363,6 +353,9 @@
           throws Exception
       {
           SAXReader xmlReader = new SAXReader();
  +        xmlReader.setMergeAdjacentText(true);
  +        xmlReader.setStripWhitespaceText(true);
  +        
           document = xmlReader.read(xmlInput);
   
           // Create the parent bean.
  @@ -388,9 +381,17 @@
               // Overloads were not called, assuming classpath loading.
               basePath = Strings.replace(basePackage, ".", "/");
           }
  -
  -        // Determine the base package to use for object creation.
  -        return treeWalk(rootElement, bean);
  +        
  +        Object o = treeWalk(rootElement, bean);
  +        
  +        if (o == null)
  +        {
  +            throw new Exception("Cannot create instance of " + beanClass);
  +        }            
  +        
  +        debug("Created instance of " + beanClass);
  +        
  +        return o;
       }
   
       /**
  @@ -451,28 +452,21 @@
                           // object (1) so that the 'street', 'city',
                           // and 'country' properties can be populated.
   
  -                        // 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 = createInstance((Element) node, null);
  +                            
                           if (o == null)
                           {
  -                            o = createInstance((Element) node, null);
  -                            
  -                            if (o == null)
  -                            {
  -                                continue;
  -                            }                                
  -                        }
  +                            continue;
  +                        }                                
   
                           // Now we have to attach the newly created object
                           // to its parent object.
                           if (isPlural(node.getParent().getName()))
                           {
  -                            // Here we have the case where (A) is enclose by
  +                            // Here we have the case where (A) is enclosed by
                               // a parent element <addresses> so we need to add
                               // Address object to a List of Address objects.
  +                            debug("addY(): " + bean + " " + name + " " + o);
                               addObject(bean,name,o);
                           }
                           else
  @@ -492,32 +486,18 @@
                       else
                       {
                           debug("Node " + node.getName() + " has no children");
  -
  -                        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)
  -                        {
  -                            debug("Using className metadata to build object");
  -
  -                            o = createInstance((Element) node, null);
  -                            debug("Created new object -> " +
  -                                  o.getClass().getName());
  -                        }
  -                        else
  +                        
  +                        // Classname mapping may happen here.
  +                        Object o = createInstance((Element) node, null);
  +                        
  +                        // Now we are dealing with elements that refer
  +                        // to a noun singular and these elements have
  +                        // no children.
  +                        
  +                        if (o == null)
                           {
  -                            // 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);
  -                            }
  +                            Class type = PropertyUtils.getPropertyType(bean,name);
  +                            o = ConvertUtils.convert(node.getText(), type);
                           }
   
                           if (isPlural(node.getParent().getName()))
  @@ -534,6 +514,7 @@
                               // and 'squash' are added to a 'hobbies' list. We
                               // assume a List of Strings with the above pattern.
                               addObject(bean, name, o);
  +                            debug("addX() " + name);
                           }
                           else
                           {
  @@ -593,43 +574,87 @@
       protected Object createInstance(Element element, String className)
           throws Exception
       {
  -        // Should probably use the factory manager.
  -        try
  -        {
  -            if (className == null || className.length() == 0)
  -            {
  -                className = element.attributeValue(CLASS_NAME);
  -                debug(CLASS_NAME + " attribute -> " + className);
  -            }
  -            return Class.forName(className).newInstance();
  -        }
  -        catch (Exception noAttrib)
  -        {
  -            debug("Using " + className + " unsuccessful: " + noAttrib);
  -            
  -            // We assume here that the object that we are creating is
  -            // in the same package as the parent object.
  -            String nodeName = element.getName();
  -            if (basePackage == null)
  -            {
  -                throw new Exception("Base package not known and " + CLASS_NAME +
  -                                    " attribute not specified (or wrong)");
  -            }
  +        String nodeName = null;
  +        Object o = null;
  +        
  +        if (o != null)
  +        {
  +            return o;
  +        }            
  +        
  +        if (className == null)
  +        {
  +            nodeName = element.getName();
  +
  +            // We need to look for the attribute 'className' first
  +            // because we always have an element name and we don't
  +            // want to base an object name on the element name if
  +            // the attribute 'className' exists because it explicitly
  +            // specifies the name to use when attempting to
  +            // instantiating the required class.
  +            className = element.attributeValue(CLASS_NAME);
               
  -            className = (basePackage.length() == 0 ? "" : basePackage + '.') +
  -                nodeName.substring(0, 1).toUpperCase() + nodeName.substring(1);
  -            
  -            // TODO: Check class name validity
  -            try
  +            if (className != null)
               {
  -                debug("Attempting to load " + className);
  -                return Class.forName(className).newInstance();
  +                o = newInstance(className);
  +                
  +                if (o == null)
  +                {
  +                    debug("ERROR: incorrect className attribute.");
  +                }
  +                
  +                return o;
               }
  -            catch (Exception unknownClass)
  +
  +            // We have to check for inclusion rules and build
  +            // and object from an external XML file if that's
  +            // the case.
  +            o = inclusion(element);
  +
  +            if (hasChildren(element) == false)
               {
                   return null;
  +            }                
  +            
  +            if (className == null)
  +            {
  +                className = (basePackage.length() == 0 ? "" : basePackage + '.') +
  +                    nodeName.substring(0, 1).toUpperCase() + nodeName.substring(1);
               }
           }
  +        
  +        o = newInstance(className);
  +        
  +        if (o == null)
  +        {
  +            debug("Problem instantiating " + className);
  +        }
  +        
  +        return o;
  +    }        
  +
  +    private Object newInstance(String className)
  +    {
  +        Object instance = null;
  +        
  +        try
  +        {
  +            instance = Class.forName(className).newInstance();
  +        }
  +        catch (InstantiationException ie)
  +        {
  +            debug("Error " + ie);
  +        }
  +        catch (IllegalAccessException iae)
  +        {
  +            debug("Error " + iae);
  +        }
  +        catch (ClassNotFoundException cnfe)
  +        {
  +            debug("Error " + cnfe);
  +        }
  +        
  +        return instance;
       }
   
       // -------------------------------------------------------------------
  @@ -653,9 +678,9 @@
        */
       private boolean hasChildren(Node node)
       {
  -        return (((Element) node).nodeCount() > 1);
  +        return !((Element) node).isTextOnly();
       }
  -
  +                                            
       /**
        * Does the node in question have only text?
        *
  @@ -719,7 +744,6 @@
           try
           {
               String methodName = makeMethodName("add", elementName);
  -            //Method m = introspector.getMethod(bean.getClass(), methodName, args);
               MethodUtils.invokeMethod(bean, methodName, value);
           }
           catch (Exception e)
  @@ -796,7 +820,7 @@
           {
               return true;
           }
  -
  +        
           // None of the tests detected a plural
           return false;
       }
  
  
  
  1.2       +2 -2      jakarta-commons-sandbox/xo/src/test/org/apache/commons/xo/datamodel/TestDatamodelMapping.java
  
  Index: TestDatamodelMapping.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/xo/src/test/org/apache/commons/xo/datamodel/TestDatamodelMapping.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestDatamodelMapping.java	16 Apr 2002 19:07:56 -0000	1.1
  +++ TestDatamodelMapping.java	28 May 2002 15:31:02 -0000	1.2
  @@ -42,10 +42,10 @@
               m.setDebug(true);
               Database d = (Database) m.map(new File(TEST_DOCUMENT), TEST_CLASS);
               
  -            /*
               // Check database properties
               assertTrue("bookstore".equals(d.getName()));
  -            assertTrue("idbroker".equals(d.getIdMethod()));
  +            
  +            /*
               
               // Check book table
               assertTrue("book".equals(d.getTable(0).getName()));
  
  
  
  1.2       +0 -1      jakarta-commons-sandbox/xo/src/test/org/apache/commons/xo/datamodel/datamodel.xml
  
  Index: datamodel.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/xo/src/test/org/apache/commons/xo/datamodel/datamodel.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- datamodel.xml	16 Apr 2002 19:07:56 -0000	1.1
  +++ datamodel.xml	28 May 2002 15:31:02 -0000	1.2
  @@ -2,7 +2,6 @@
   
   <database>
     <name>bookstore</name>
  -  <idMethod>idbroker</idMethod>
     
     <tables>
     
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>