commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject cvs commit: jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/digester IDTest1.xml TestIDRead.java
Date Thu, 29 Aug 2002 21:22:52 GMT
rdonkin     2002/08/29 14:22:52

  Modified:    betwixt/src/java/org/apache/commons/betwixt/io
                        BeanCreateRule.java BeanReader.java
               betwixt/src/test/org/apache/commons/betwixt/digester
                        TestIDRead.java
  Added:       betwixt/src/test/org/apache/commons/betwixt/digester
                        IDTest1.xml
  Log:
  Added (basic) support for ID-IDREF matching in reading. More work needs to be done later
for some advanced cases.
  
  Revision  Changes    Path
  1.10      +81 -17    jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanCreateRule.java
  
  Index: BeanCreateRule.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanCreateRule.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- BeanCreateRule.java	29 Aug 2002 19:16:17 -0000	1.9
  +++ BeanCreateRule.java	29 Aug 2002 21:22:52 -0000	1.10
  @@ -113,29 +113,52 @@
       private Class beanClass;
       /** The prefix added to digester rules */
       private String pathPrefix;
  +    /** Beans digested indexed by <code>ID</code> */
  +    private Map beansById = new HashMap();
  +    /** Use id's to match beans */
  +    private boolean matchIDs = true;
       
  -    public BeanCreateRule(ElementDescriptor descriptor, Class beanClass, String pathPrefix)
{
  -        this.descriptor = descriptor;
  -        this.context = new Context();
  -        this.beanClass = beanClass;
  -        this.pathPrefix = pathPrefix;
  -        if (log.isTraceEnabled()) {
  -            log.trace("Created bean create rule");
  -            log.trace("Descriptor=" + descriptor);
  -            log.trace("Class=" + beanClass);
  -            log.trace("Path prefix=" + pathPrefix);
  -        }
  +    public BeanCreateRule(
  +                            ElementDescriptor descriptor, 
  +                            Class beanClass, 
  +                            String pathPrefix, 
  +                            boolean matchIDs) {
  +        this( 
  +                descriptor, 
  +                beanClass, 
  +                new Context(), 
  +                pathPrefix,
  +                matchIDs);
  +    }
  +    
  +    public BeanCreateRule(ElementDescriptor descriptor, Class beanClass, boolean matchIDs)
{
  +        this( descriptor, beanClass, descriptor.getQualifiedName() + "/" , matchIDs);
       }
       
  -    public BeanCreateRule(ElementDescriptor descriptor, Class beanClass) {
  -        this( descriptor, beanClass, descriptor.getQualifiedName() + "/" );
  +    public BeanCreateRule(
  +                            ElementDescriptor descriptor, 
  +                            Context context, 
  +                            String pathPrefix,
  +                            boolean matchIDs) {
  +        this( 
  +                descriptor, 
  +                descriptor.getSingularPropertyType(), 
  +                context, 
  +                pathPrefix,
  +                matchIDs);
       }
       
  -    public BeanCreateRule(ElementDescriptor descriptor, Context context, String pathPrefix)
{
  +    private BeanCreateRule(
  +                            ElementDescriptor descriptor, 
  +                            Class beanClass,
  +                            Context context, 
  +                            String pathPrefix,
  +                            boolean matchIDs) {
           this.descriptor = descriptor;        
           this.context = context;
  -        this.beanClass = descriptor.getSingularPropertyType();
  +        this.beanClass = beanClass;
           this.pathPrefix = pathPrefix;
  +        this.matchIDs = matchIDs;
           if (log.isTraceEnabled()) {
               log.trace("Created bean create rule");
               log.trace("Descriptor=" + descriptor);
  @@ -144,6 +167,7 @@
           }
       }
       
  +    
           
       // Rule interface
       //-------------------------------------------------------------------------    
  @@ -168,6 +192,8 @@
               }
           }
           
  +
  +        
           // XXX: if a single rule instance gets reused and nesting occurs
           // XXX: we should probably use a stack of booleans to test if we created a bean
           // XXX: or let digester take nulls, which would be easier for us ;-)
  @@ -223,6 +249,17 @@
                   }
                   
                   addChildRules();
  +                
  +                // add bean for ID matching
  +                if ( matchIDs ) {
  +                    // XXX need to support custom ID attribute names
  +                    // XXX i have a feeling that the current mechanism might need to change
  +                    // XXX so i'm leaving this till later
  +                    String id = attributes.getValue( "id" );
  +                    if ( id != null ) {
  +                        beansById.put( id, instance );
  +                    }
  +                }
               }
           }
       }
  @@ -254,12 +291,39 @@
           }
       }
   
  +    /** 
  +     * Tidy up.
  +     */
  +    public void finish() {
  +        // clear beans map
  +        beansById.clear();
  +    }
  +
   
       // Implementation methods
       //-------------------------------------------------------------------------    
       
       /** Factory method to create new bean instances */
       protected Object createBean(Attributes attributes) throws Exception {
  +        //
  +        // See if we've got an IDREF
  +        //
  +        // XXX This should be customizable but i'm not really convinced by the existing
system
  +        // XXX maybe it's going to have to change so i'll use 'idref' for nows
  +        //
  +        if ( matchIDs ) {
  +            String idref = attributes.getValue( "idref" );
  +            if ( idref != null ) {
  +                // XXX need to check up about ordering
  +                // XXX this is a very simple system that assumes that id occurs before
idrefs
  +                // XXX would need some thought about how to implement a fuller system
  +                Object bean = beansById.get( idref );
  +                if ( bean != null ) {
  +                    return bean;
  +                }
  +            }
  +        }
  +        
           try {
               return beanClass.newInstance();
           }
  @@ -327,7 +391,7 @@
                               path = "*/"+desc[0].getQualifiedName();
                           }
                       }
  -                    Rule rule = new BeanCreateRule( childDescriptor, context, path);
  +                    Rule rule = new BeanCreateRule( childDescriptor, context, path, matchIDs);
                       addRule(path, rule);
                       continue;
                   }
  @@ -361,7 +425,7 @@
                               addPrimitiveTypeRule(path, childDescriptor);
                           }
                           else {
  -                            Rule rule = new BeanCreateRule( childDescriptor, context, path
+ '/' );
  +                            Rule rule = new BeanCreateRule( childDescriptor, context, path
+ '/', matchIDs );
                               addRule( path, rule );
                           }
                       }
  
  
  
  1.4       +17 -1     jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanReader.java
  
  Index: BeanReader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanReader.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BeanReader.java	29 Aug 2002 19:28:50 -0000	1.3
  +++ BeanReader.java	29 Aug 2002 21:22:52 -0000	1.4
  @@ -96,6 +96,8 @@
       private Log log = LogFactory.getLog( BeanReader.class );
       /** The registered classes */
       private Set registeredClasses = new HashSet();
  +    /** Should the reader use <code>ID</code>'s to match */
  +    private boolean matchIDs = true;
       
       /**
        * Construct a new BeanReader with default properties.
  @@ -203,6 +205,20 @@
           this.log = log;
           setLogger(log);
       }
  +    
  +    /** 
  +     * Should the reader use <code>ID</code> attributes to match beans.
  +     */
  +    public boolean getMatchIDs() {
  +        return matchIDs;
  +    }
  +    
  +    /**
  +     * Set whether the read should use <code>ID</code> attributes to match
beans.
  +     */
  +    public void setMatchIDs(boolean matchIDs) {
  +        this.matchIDs = matchIDs;
  +    }
           
       // Implementation methods
       //-------------------------------------------------------------------------    
  @@ -211,7 +227,7 @@
        * Adds a new bean create rule for the specified path
        */
       protected void addBeanCreateRule( String path, ElementDescriptor elementDescriptor,
Class beanClass ) {
  -        Rule rule = new BeanCreateRule( elementDescriptor, beanClass, path + "/" );
  +        Rule rule = new BeanCreateRule( elementDescriptor, beanClass, path + "/" , matchIDs);
           addRule( path, rule );
   
           if ( log.isDebugEnabled() ) {
  
  
  
  1.2       +55 -6     jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/digester/TestIDRead.java
  
  Index: TestIDRead.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/digester/TestIDRead.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestIDRead.java	29 Aug 2002 19:28:50 -0000	1.1
  +++ TestIDRead.java	29 Aug 2002 21:22:52 -0000	1.2
  @@ -124,9 +124,6 @@
   //        reader.getXMLIntrospector().setLog(log);
           
           reader.registerBeanClass( IDBean.class );
  -        //reader.registerBeanClass( "IDBean/children/IDBean", IDBean.class );
  -
  -        System.out.println(reader.getRules().rules());
   
           InputStream in = new FileInputStream( 
               getTestFile("src/test/org/apache/commons/betwixt/digester/SimpleReadTest.xml")
);
  @@ -144,6 +141,58 @@
               
               IDBean gamma = (IDBean) alpha.getChildren().get(1);
               assertEquals("Wrong name (B)", "gamma" ,  gamma.getName());
  +        }
  +        finally {
  +            in.close();
  +        }
  +    }
  +    
  +    public void testIDRead() throws Exception {
  +        
  +        BeanReader reader = new BeanReader();
  +        
  +//         logging just for this method
  +//        SimpleLog log = new SimpleLog("[XMLIntrospectorHelper]");
  +//        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
  +//        XMLIntrospectorHelper.setLog(log);
  +//        
  +//        log = new SimpleLog("[BeanCreateRule]");
  +//        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
  +//        BeanCreateRule.setLog(log);
  +//
  +//        log = new SimpleLog("[BeanReader]");
  +//        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);        
  +//        reader.setLog(log);
  +//
  +//        log = new SimpleLog("[XMLIntrospector]");
  +//        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
  +//        reader.getXMLIntrospector().setLog(log);
  +        
  +        reader.registerBeanClass( IDBean.class );
  +
  +        InputStream in = new FileInputStream( 
  +            getTestFile("src/test/org/apache/commons/betwixt/digester/IDTest1.xml") );
  +            
  +        try {
  +            Object obj = reader.parse( in );
  +            
  +            assertEquals("Read bean type is incorrect", true, (obj instanceof IDBean) );
  +            IDBean alpha = (IDBean) obj;
  +            
  +            assertEquals("Wrong list size (A)", 2 ,  alpha.getChildren().size());
  +            
  +            IDBean beta = (IDBean) alpha.getChildren().get(0);
  +            assertEquals("Wrong name (A)", "beta" ,  beta.getName());
  +            
  +            IDBean gamma = (IDBean) alpha.getChildren().get(1);
  +            assertEquals("Wrong name (B)", "gamma" ,  gamma.getName());
  +            assertEquals("Wrong list size (B)", 2 ,  gamma.getChildren().size());
  +            
  +            IDBean sonOfGamma = (IDBean) gamma.getChildren().get(1);
  +            assertEquals("Wrong id (A)", "two" ,  sonOfGamma.getId());
  +            assertEquals("Wrong name (C)", "beta" ,  sonOfGamma.getName());
  +            
  +            assertEquals("IDREF bean not equal to ID bean", beta,  sonOfGamma);
           }
           finally {
               in.close();
  
  
  
  1.1                  jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/digester/IDTest1.xml
  
  Index: IDTest1.xml
  ===================================================================
  <?xml version="1.0"?>
  <IDBean id="1">
      <name>alpha</name>
      <children>
          <IDBean id="2">
              <name>beta</name>
              <children/>
              <id>two</id>
          </IDBean>
          <IDBean id="3">
              <name>gamma</name>
              <children>	
                  <IDBean id="4">
                      <name>epsilon</name>
                      <children/>
                      <id>four</id>	
                  </IDBean>
                  <IDBean idref="2"/>
              </children>
              <id>three</id>
          </IDBean>
      </children>
      <id>one</id>
  </IDBean>
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message