commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bali...@apache.org
Subject cvs commit: jakarta-commons-sandbox/simplestore/src/xmldb/org/apache/commons/simplestore/xmldb XmlDbPersistenceManagerFactory.java XmlDbStorage.java
Date Mon, 20 May 2002 20:49:09 GMT
baliuka     02/05/20 13:49:09

  Modified:    simplestore/src/test/org/apache/commons/simplestore
                        TestAll.java TestSample.java TestXmlDbStorage.java
               simplestore/src/xmldb/org/apache/commons/simplestore/xmldb
                        XmlDbPersistenceManagerFactory.java
                        XmlDbStorage.java
  Log:
  Fixed XMLDB Storage implementation
  
  Revision  Changes    Path
  1.13      +3 -3      jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/TestAll.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- TestAll.java	18 May 2002 20:43:47 -0000	1.12
  +++ TestAll.java	20 May 2002 20:49:09 -0000	1.13
  @@ -59,7 +59,7 @@
   /**
    *@author     Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">
    *      g-froehlich@gmx.de</a>
  - *@version    $Id: TestAll.java,v 1.12 2002/05/18 20:43:47 baliuka Exp $
  + *@version    $Id: TestAll.java,v 1.13 2002/05/20 20:49:09 baliuka Exp $
    */
   public class TestAll extends TestCase {
       public TestAll(String testName) {
  @@ -68,8 +68,8 @@
   
       public static Test suite() {
           TestSuite suite = new TestSuite();
  -       // suite.addTest(TestSample.suite());
  -       // suite.addTest(TestEnhancer.suite());
  +        suite.addTest(TestSample.suite());
  +        suite.addTest(TestEnhancer.suite());
           suite.addTest(TestXmlDbStorage.suite());
           return suite;
       }
  
  
  
  1.29      +17 -3     jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/TestSample.java
  
  Index: TestSample.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/TestSample.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- TestSample.java	18 May 2002 20:43:47 -0000	1.28
  +++ TestSample.java	20 May 2002 20:49:09 -0000	1.29
  @@ -72,7 +72,7 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: TestSample.java,v 1.28 2002/05/18 20:43:47 baliuka Exp $
  + *@version    $Id: TestSample.java,v 1.29 2002/05/20 20:49:09 baliuka Exp $
    */
   public class TestSample extends TestCase implements org.apache.commons.simplestore.tools.Constants{
       
  @@ -118,6 +118,7 @@
       public void testCreate() throws java.lang.Exception {
           Transaction transaction = pm.getTransaction();
           try{
  +          
               transaction.begin();
               TestPersistentClassType object1 = (TestPersistentClassType) pm.createInstance(TestPersistentClassType.class);
               Object oid = pm.getOID(object1);
  @@ -165,7 +166,7 @@
               
               transaction.commit();
               
  -            
  +          
           }catch( Exception e){
               e.printStackTrace();
               transaction.commit();
  @@ -179,7 +180,7 @@
           
           
           java.util.Set objects = pm.findAll(TestPersistentClassType.class);
  -        
  +       
           assertTrue("Cache findAll ", objects == pm.findAll(TestPersistentClassType.class)
);
           
           java.util.Iterator it = objects.iterator();
  @@ -194,6 +195,19 @@
               object.getIntVal();
               object.getFloatVal();
               object.getStrVal1();
  +            
  +            TestPersistentClassType parent = object.getParent();
  +            if(parent != null){ 
  +                java.util.Iterator iterator = parent.getChildren().iterator();
  +                boolean found = false;
  +                while(iterator.hasNext()){
  +                  Object next = iterator.next();  
  +                 if( next.equals(object) ){
  +                    found = true;
  +                  }
  +               }
  +              assertTrue("Loop Collection",found );  
  +            }
               
           }
           
  
  
  
  1.2       +2 -1      jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/TestXmlDbStorage.java
  
  Index: TestXmlDbStorage.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/TestXmlDbStorage.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestXmlDbStorage.java	18 May 2002 20:44:39 -0000	1.1
  +++ TestXmlDbStorage.java	20 May 2002 20:49:09 -0000	1.2
  @@ -64,7 +64,7 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: TestXmlDbStorage.java,v 1.1 2002/05/18 20:44:39 baliuka Exp $
  + *@version    $Id: TestXmlDbStorage.java,v 1.2 2002/05/20 20:49:09 baliuka Exp $
    */
   public class TestXmlDbStorage extends TestSample {
       
  @@ -90,6 +90,7 @@
               XmlDbPersistenceManagerFactory factory = new XmlDbPersistenceManagerFactory();
               factory.setDriver("org.apache.xindice.client.xmldb.DatabaseImpl");
               factory.setUrl("xmldb:xindice:///db/addressbook");
  +            factory.setRootNode("document");
               factory.setMetaResource("org/apache/commons/simplestore/storage.xml");
               pm = factory.getPersistenceManager();
        
  
  
  
  1.2       +18 -2     jakarta-commons-sandbox/simplestore/src/xmldb/org/apache/commons/simplestore/xmldb/XmlDbPersistenceManagerFactory.java
  
  Index: XmlDbPersistenceManagerFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/xmldb/org/apache/commons/simplestore/xmldb/XmlDbPersistenceManagerFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XmlDbPersistenceManagerFactory.java	17 May 2002 18:33:55 -0000	1.1
  +++ XmlDbPersistenceManagerFactory.java	20 May 2002 20:49:09 -0000	1.2
  @@ -71,7 +71,7 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: XmlDbPersistenceManagerFactory.java,v 1.1 2002/05/17 18:33:55 baliuka
Exp $
  + *@version    $Id: XmlDbPersistenceManagerFactory.java,v 1.2 2002/05/20 20:49:09 baliuka
Exp $
   */
   public class XmlDbPersistenceManagerFactory implements PersistenceManagerFactory{
       
  @@ -92,6 +92,8 @@
       /** Holds value of property loader. */
       private ClassLoader loader;
       
  +    /** Holds value of property rootNode. */
  +    private String rootNode = "document";    
       
       /** Creates a new instance of XmlDbPersistenceManagerFactory */
       public XmlDbPersistenceManagerFactory() {
  @@ -125,7 +127,7 @@
               }
             }
           
  -        } );
  +        }, rootNode );
           ContextImpl context = new ContextImpl(
                 mclass, new DefaultTypeConverter(),
                 new RandomOIDGenerator(),
  @@ -210,4 +212,18 @@
           this.maxStrongRef = maxStrongRef;
       }
          
  +    /** Getter for property rootNode.
  +     * @return Value of property rootNode.
  +     */
  +    public String getRootNode() {
  +        return this.rootNode;
  +    }
  +    
  +    /** Setter for property rootNode.
  +     * @param rootNode New value of property rootNode.
  +     */
  +    public void setRootNode(String rootNode) {
  +        this.rootNode = rootNode;
  +    }
  +    
   }
  
  
  
  1.4       +298 -207  jakarta-commons-sandbox/simplestore/src/xmldb/org/apache/commons/simplestore/xmldb/XmlDbStorage.java
  
  Index: XmlDbStorage.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/xmldb/org/apache/commons/simplestore/xmldb/XmlDbStorage.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- XmlDbStorage.java	19 May 2002 12:57:45 -0000	1.3
  +++ XmlDbStorage.java	20 May 2002 20:49:09 -0000	1.4
  @@ -85,14 +85,14 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: XmlDbStorage.java,v 1.3 2002/05/19 12:57:45 baliuka Exp $
  + *@version    $Id: XmlDbStorage.java,v 1.4 2002/05/20 20:49:09 baliuka Exp $
    */
   
   
   public class XmlDbStorage extends AbstractStorage {
       
  -   final static java.text.SimpleDateFormat DATE_FROMAT = 
  -            new java.text.SimpleDateFormat();
  +    final static java.text.SimpleDateFormat DATE_FROMAT =
  +    new java.text.SimpleDateFormat();
       
       final static String BEGIN_XUPDATE = "<xu:modifications version=\"1.0\"" +
       "      xmlns:xu=\"http://www.xmldb.org/xupdate\">\n";
  @@ -105,16 +105,27 @@
       final static String UPDATE ="org.apache.commons.simplestore.xmldb.XmlDbStorage.UPDATE";
       
       CollectionFactory collectionFactory;
  +    String rootNode;
       
       /** Creates a new instance of XmlDbStorage */
  -    public XmlDbStorage(CollectionFactory collectionFactory) {
  +    public XmlDbStorage(CollectionFactory collectionFactory,String rootNode) {
           this.collectionFactory = collectionFactory;
  +        this.rootNode = rootNode;
       }
       String getRootNode(){
  -        return "document";
  +        return rootNode;
       }
       private Collection getCollection(){
  -        return collectionFactory.getCollection();
  +        Collection cl = (Collection) context.getTransactionManager().getTransaction().
  +        getAttribute( "Collection" );
  +        if(cl == null){
  +            cl =  collectionFactory.getCollection();
  +            context.getTransactionManager().getTransaction().
  +            setAttribute( "Collection" , cl );
  +        }
  +        
  +        
  +        return cl;
       }
       protected  String getPath(MetaObject properties){
           return "/" + getRootNode() + "/descendant-or-self::" + properties.getMetaClass().getName()
+"[@"+
  @@ -184,18 +195,18 @@
               (XPathQueryService)cl.getService("XPathQueryService", "1.0");
               
               ResourceSet resultSet = service.query(xpath);
  -          
  +            
               ResourceIterator results = resultSet.getIterator();
               while (results.hasMoreResources()) {
                   XMLResource resource =
                   (XMLResource) results.nextResource();
  -                 resource.getContentAsSAX(
  +                resource.getContentAsSAX(
                   new Handler(context,mClass,objects,callback)
                   ) ;
                   
                   
               }
  -            cl.close();
  +            
           }catch(XMLDBException xe){
               xe.printStackTrace();
               throw new StorageException(xe.getMessage(),xe);
  @@ -246,23 +257,49 @@
           
       }
       private void clearBuff(){
  -        context.getTransactionManager().
  -        getTransaction().removeAttribute(UPDATE);
  +        
  +        try{
  +            
  +            context.getTransactionManager().
  +            getTransaction().
  +            removeAttribute(UPDATE);
  +            context.getTransactionManager().getTransaction().
  +            removeAttribute( "TransactionService" );
  +            
  +            Collection cl = (Collection)context.getTransactionManager().
  +            getTransaction().getAttribute( "Collection" );
  +            context.getTransactionManager().getTransaction().
  +            removeAttribute( "Collection" );
  +            
  +            
  +            cl.close();
  +        }catch( Exception e ){
  +            throw new StorageException(e.getMessage(),e);
  +        }
  +        
       }
       protected  void internalCommit() throws StorageException{
           
           try{
               
  +            
               StringBuffer updateBuff = (StringBuffer)context.getTransactionManager().
               getTransaction().getAttribute(UPDATE);
  +            Collection cl = getCollection();
               if( updateBuff != null ){
  -                Collection cl = getCollection();
  +                
                   XUpdateQueryService   service =(XUpdateQueryService) cl.
                   getService("XUpdateQueryService", "1.0");
                   service.update(BEGIN_XUPDATE + updateBuff.toString() + END_XUPDATE);
  -                clearBuff();
  -                cl.close();
  +                TransactionService transaction =
  +                (TransactionService) context.getTransactionManager().
  +                getTransaction().getAttribute( "TransactionService");
  +                if( transaction != null ){
  +                    transaction.commit();
  +                }
  +                
               }
  +            clearBuff();
               
           }catch(XMLDBException xe){
               xe.printStackTrace();
  @@ -272,11 +309,38 @@
       }
       
       protected  void internalRollback() throws StorageException{
  -        clearBuff();
  +        try{
  +            Collection cl = getCollection();
  +            TransactionService transaction =
  +            (TransactionService) context.getTransactionManager().getTransaction().
  +            getAttribute( "TransactionService");
  +            
  +            if(transaction != null){
  +                transaction.rollback();
  +            }
  +            clearBuff();
  +        }catch(XMLDBException xe){
  +            xe.printStackTrace();
  +            throw new StorageException(xe.getMessage(),xe);
  +        }
       }
       
       protected  void internalBegin() throws StorageException{
  -        clearBuff();
  +        try{
  +            Collection cl = getCollection();
  +            
  +            TransactionService transaction =
  +            (TransactionService) cl.getService("TransactionService", "1.0");
  +            if(transaction != null){
  +                context.getTransactionManager().getTransaction().
  +                setAttribute( "TransactionService" , transaction );
  +                
  +                transaction.begin();
  +            }
  +        }catch(XMLDBException xe){
  +            xe.printStackTrace();
  +            throw new StorageException(xe.getMessage(),xe);
  +        }
       }
       
       
  @@ -299,14 +363,14 @@
               while (results.hasMoreResources()) {
                   XMLResource resource =
                   (XMLResource) results.nextResource();
  -              
  +                
                   resource.getContentAsSAX(
  -                   new Handler(context,mClass,objects,null)
  +                new Handler(context,mClass,objects,null)
                   );
                   
                   
               }
  -            cl.close();
  +          
           }catch(XMLDBException xe){
               xe.printStackTrace();
               throw new StorageException(xe.getMessage(),xe);
  @@ -317,15 +381,17 @@
       }
       
       public Object retrieveObject( final Class clasz, Object id ) throws StorageException
{
  -         java.util.Set objects = new java.util.HashSet();
  +        java.util.Set objects = new java.util.HashSet();
  +         MetaClass mClass = context.getMetaClass(clasz);
  +         String xpath = "/"+ getRootNode() + "/descendant-or-self::"+ context.getMetaClass(clasz).getName()
+ "[@" +
  +            mClass.getOIDName() + "='" + id + "']";
  +           
           try{
  +            
              
  -            MetaClass mClass = context.getMetaClass(clasz);
               
  -            String xpath = "/"+ getRootNode() + "/descendant-or-self::"+ context.getMetaClass(clasz).getName()
+ "[" +
  -              mClass.getOIDName() + "='" + id + "']";
               
  -          
  +            
               Collection cl =  getCollection();
               
               XPathQueryService service =
  @@ -337,25 +403,26 @@
               while (results.hasMoreResources()) {
                   XMLResource resource =
                   (XMLResource) results.nextResource();
  -              
  -               resource.getContentAsSAX(
  -                   new Handler(context,mClass,objects,null)
  +                
  +                resource.getContentAsSAX(
  +                new Handler(context,mClass,objects,null)
                   );
                   
                   
               }
  -            cl.close();
  +            
           }catch(XMLDBException xe){
               xe.printStackTrace();
               throw new StorageException(xe.getMessage(),xe);
           }
           if(objects.size() == 0 ){
  -           throw new ObjectNotFound();
  +            
  +            throw new ObjectNotFound();
           }
           if(objects.size() != 1 ){
  -           throw new StorageException("dublicate object",null);
  +            throw new StorageException("dublicate object",null);
           }
  -         
  +        
           return objects.iterator().next();
       }
       
  @@ -364,15 +431,15 @@
       
       
       
  -  class Handler extends org.xml.sax.helpers.DefaultHandler{
  -      
  +    class Handler extends org.xml.sax.helpers.DefaultHandler{
  +        
           final MetaClass  mClasz;
           final Set objects;
           final EnumeratorCallback callback;
           final java.util.Stack context = new java.util.Stack();
           final StringBuffer buffer = new StringBuffer(111);
           final Context cntxt;
  -         Object[] props;
  +        Object[] props;
           Object id;
           
           Handler( Context cntxt, MetaClass  mClasz, Set objects, final EnumeratorCallback
callback){
  @@ -383,59 +450,37 @@
               
           }
           
  -        private void dispatch(final boolean fireOnlyIfMixed)
  +        private void dispatch()
           throws  org.xml.sax.SAXException  {
  -            if (fireOnlyIfMixed && buffer.length() == 0) return; 
  -            
  -            Object[] ctx = (Object[]) context.peek();
  -            String name = (String) ctx[0];
  -            Attributes attrs = (Attributes) ctx[1];
  -            Object value;
  +            String name = (String) context.peek();
               try {
  -                
  -                if ( name.equals(mClasz.getName()) ) {
  +               
  +                 if ( props != null && buffer.length() > 0) {
  +                   
  +                    int index = mClasz.getPropertyIndex( name );
                       
  -                    value = parse( mClasz.getOIDType(),attrs.getValue(mClasz.getOIDName()
));
  -    
  -                    Persistent p = (Persistent) cntxt.getCache().get(value);
  -                    if ( p != null ) {
  -                        props = null;
  -                        if( objects.add(p)){
  -                            if( callback != null) 
  -                               callback.nextObject(p);
  -                             return;
  -                        }
  +                    if(mClasz.isReference(index)){
  +                        props[ index  ] = parse(mClasz.getOIDType(),buffer.toString())
;
  +                       
                       }
  -                    p = mClasz.newInstance( value );
  -                    MetaObject pc = p.getMetaObject();
  -                    props = pc.getProperties();
  -                    if( objects.add(p)){
  -                        cntxt.getCache().put(value,p);
  -                       if( callback != null) 
  -                           callback.nextObject(p);
  -                        return;
  +                    else{
  +                       
  +                        props[ index  ] =
  +                        parse(mClasz.getPropertyType(index),
  +                        buffer.toString()) ;
                       }
  -                    
  -                    
  -                }else {
  -              
  -                int index = mClasz.getPropertyIndex( name );
  -                
  -                props[ index  ] = parse(mClasz.getPropertyType(index),buffer.toString())
;
  -                
  -                return ;
  -                  
  +                    return ;
                       
                   }
                   
  -                
               } catch (Throwable t) {
                   t.printStackTrace();
                   throw new StorageException(t.getMessage(), t);
  +            }finally{
  +                buffer.delete(0, buffer.length());
               }
  -        
               
  -            buffer.delete(0, buffer.length());
  +            
           }
           
           
  @@ -444,150 +489,196 @@
           }
           
           
  -        public void endElement(String str, String str1, String str2) throws org.xml.sax.SAXException
{
  -            dispatch(false);
  +        public void endElement(String str, String str1, String name) throws org.xml.sax.SAXException
{
  +            if (!name.equals(mClasz.getName()) ) { 
  +                dispatch();
  +            }
               context.pop();
  -        
  +            
           }
           
  -       
           
  -        public void startElement(String str, String str1, String qname, org.xml.sax.Attributes
attributes) throws org.xml.sax.SAXException {
  -            dispatch(true);
  -            context.push(new Object[] {qname, new org.xml.sax.helpers.AttributesImpl(attributes)});
  +        
  +        public void startElement(String str, String str1, String name, org.xml.sax.Attributes
attributes) throws org.xml.sax.SAXException {
  +            context.push(name);
  +            if ( name.equals(mClasz.getName()) ) {
  +                    
  +                  Object  value = parse( mClasz.getOIDType(),attributes.getValue(mClasz.getOIDName()
));
  +                    
  +                    Persistent p = (Persistent) cntxt.getCache().get(value);
  +                    if ( p != null ) {
  +                       props = null;
  +                        if( objects.add(p)){
  +                            if( callback != null)
  +                                callback.nextObject(p);
  +                            return;
  +                        }
  +                    }
  +                    p = mClasz.newInstance( value );
  +                    MetaObject pc = p.getMetaObject();
  +                    props = pc.getProperties();
  +                  
  +                    if( objects.add(p)){
  +                        cntxt.getCache().put(value,p);
  +                        if( callback != null)
  +                            callback.nextObject(p);
  +                     
  +                    }
  +                     return; 
  +                  
  +                }
  +                 
  +                       
               
           }
           
           
       }
       
  -   interface Parser{
  -     public Object parse( String str );
  -     public String format( Object obj );
  -   } 
  -  static abstract class  ParserImpl implements Parser{
  -   
  -      public String format( Object obj){
  -        return obj.toString();
  -      }
  - 
  -  } 
  -   final static java.util.Map parseMap = new java.util.HashMap();
  -   static{
  -     
  -       parseMap.put(int.class,new ParserImpl(){
  -        public Object  parse(String str){
  -           return new Integer( Integer.parseInt(str) );
  -         }
  -       });
  -       parseMap.put(Integer.class,new ParserImpl(){
  -        public Object  parse(String str){
  -           return new Integer( Integer.parseInt(str) );
  -         }
  -       });
  -   //---------------------------------------    
  -     parseMap.put(byte.class,new ParserImpl(){
  -        public Object  parse(String str){
  -           return new Byte( Byte.parseByte(str) );
  -         }
  -       });
  -       parseMap.put(Byte.class,new ParserImpl(){
  -        public Object  parse(String str){
  -           return new Byte( Byte.parseByte(str) );
  -         }
  -       });
  -     //-----------------------------------------
  -     parseMap.put(short.class,new ParserImpl(){
  -        public Object  parse(String str){
  -           return new Short( Short.parseShort(str) );
  -         }
  -       });
  -       parseMap.put(Short.class,new ParserImpl(){
  -        public Object  parse(String str){
  -           return new Short( Short.parseShort(str) );
  -         }
  -       });
  -     //-----------------------------------------
  -     parseMap.put(long.class,new ParserImpl(){
  -        public Object  parse(String str){
  -           return new Long( Long.parseLong(str) );
  -         }
  -       });
  -       parseMap.put(Long.class,new ParserImpl(){
  -        public Object  parse(String str){
  -           return new Long( Long.parseLong(str) );
  -         }
  -       });
  -     //-----------------------------------------
  -     parseMap.put(float.class,new ParserImpl(){
  -        public Object  parse(String str){
  -           return new Float( Float.parseFloat(str) );
  -         }
  -       });
  -       parseMap.put(Float.class,new ParserImpl(){
  -        public Object  parse(String str){
  -           return new Float( Float.parseFloat(str) );
  -         }
  -       });
  -     //-----------------------------------------
  -     parseMap.put(double.class,new ParserImpl(){
  -        public Object  parse(String str){
  -           return new Double( Double.parseDouble(str) );
  -         }
  -       });
  -       parseMap.put(Double.class,new ParserImpl(){
  -        public Object  parse(String str){
  -           return new Double( Double.parseDouble(str) );
  -         }
  -       });
  -     //-----------------------------------------
  -       parseMap.put(java.util.Date.class,new Parser(){
  -           
  -       public String format(Object obj){
  -        return DATE_FROMAT.format(obj);
  +    interface Parser{
  +        public Object parse( String str );
  +        public String format( Object obj );
  +    }
  +    static abstract class  ParserImpl implements Parser{
  +        
  +        public String format( Object obj){
  +            return obj.toString();
  +        }
  +        
  +    }
  +    final static java.util.Map parseMap = new java.util.HashMap();
  +    static{
  +        parseMap.put(boolean.class,new ParserImpl(){
  +            public Object  parse(String str){
  +                return  Boolean.valueOf(str);
  +            }
  +        });
  +        parseMap.put(Boolean.class,new ParserImpl(){
  +            public Object  parse(String str){
  +                return  Boolean.valueOf(str);
  +            }
  +        });
  +        //---------------------------------------
  +        
  +        parseMap.put(int.class,new ParserImpl(){
  +            public Object  parse(String str){
  +                return new Integer( Integer.parseInt(str) );
  +            }
  +        });
  +        parseMap.put(Integer.class,new ParserImpl(){
  +            public Object  parse(String str){
  +                return new Integer( Integer.parseInt(str) );
  +            }
  +        });
  +        //---------------------------------------
  +        parseMap.put(byte.class,new ParserImpl(){
  +            public Object  parse(String str){
  +                return new Byte( Byte.parseByte(str) );
  +            }
  +        });
  +        parseMap.put(Byte.class,new ParserImpl(){
  +            public Object  parse(String str){
  +                return new Byte( Byte.parseByte(str) );
  +            }
  +        });
  +        //-----------------------------------------
  +        parseMap.put(short.class,new ParserImpl(){
  +            public Object  parse(String str){
  +                return new Short( Short.parseShort(str) );
  +            }
  +        });
  +        parseMap.put(Short.class,new ParserImpl(){
  +            public Object  parse(String str){
  +                return new Short( Short.parseShort(str) );
  +            }
  +        });
  +        //-----------------------------------------
  +        parseMap.put(long.class,new ParserImpl(){
  +            public Object  parse(String str){
  +                return new Long( Long.parseLong(str) );
  +            }
  +        });
  +        parseMap.put(Long.class,new ParserImpl(){
  +            public Object  parse(String str){
  +                return new Long( Long.parseLong(str) );
  +            }
  +        });
  +        //-----------------------------------------
  +        parseMap.put(float.class,new ParserImpl(){
  +            public Object  parse(String str){
  +                return new Float( Float.parseFloat(str) );
  +            }
  +        });
  +        parseMap.put(Float.class,new ParserImpl(){
  +            public Object  parse(String str){
  +                return new Float( Float.parseFloat(str) );
  +            }
  +        });
  +        //-----------------------------------------
  +        parseMap.put(double.class,new ParserImpl(){
  +            public Object  parse(String str){
  +                return new Double( Double.parseDouble(str) );
  +            }
  +        });
  +        parseMap.put(Double.class,new ParserImpl(){
  +            public Object  parse(String str){
  +                return new Double( Double.parseDouble(str) );
  +            }
  +        });
  +        //-----------------------------------------
  +        parseMap.put(java.util.Date.class,new Parser(){
  +            
  +            public String format(Object obj){
  +                return DATE_FROMAT.format(obj);
  +            }
  +            
  +            public Object  parse(String str){
  +                try{
  +                    return DATE_FROMAT.parse(str);
  +                }catch(Exception e){
  +                    throw new IllegalArgumentException(str);
  +                }
  +            }
  +        });
  +        //-----------------------------------------
  +        
  +        
  +        
  +        
  +    }
  +    
  +    static String format(  Object obj ){
  +        if( obj == null ){
  +            return null;
  +        }
  +        Class cls = obj.getClass();
  +        if (  cls == String.class  ){
  +            return ( String )obj;
  +        }
  +        Parser parser = ( Parser)parseMap.get(cls);
  +        if( parser == null)
  +            throw new IllegalArgumentException(cls.getName() + ":" + obj );
  +        
  +        return parser.format(obj);
  +        
  +        
  +    }
  +    static Object parse( Class cls, String str ){
  +        if( str == null){
  +            return null;
  +        }
  +        if( cls == String.class  ){
  +            return str;
  +        }
  +        str = str.trim();
  +        Parser parser = ( Parser)parseMap.get(cls);
  +        if( parser == null)
  +            throw new IllegalArgumentException(cls.getName() + ":" + str );
  +        try{
  +            return parser.parse(str);
  +        }catch(  Exception e ){
  +            throw new IllegalArgumentException(e.getClass().getName() + ":"+ cls.getName()
+ ":" + str );
           }
  -     
  -        public Object  parse(String str){
  -           try{ 
  -           return DATE_FROMAT.parse(str);
  -           }catch(Exception e){
  -             throw new IllegalArgumentException(str);
  -           }
  -         }
  -       });
  -     //-----------------------------------------
  -       
  -       
  -       
  -   
  -   }
  -   
  -   static String format(  Object obj ){
  -      if( obj == null ){
  -         return null;
  -      }
  -    Class cls = obj.getClass(); 
  -    if (  cls == String.class  ){
  -       return ( String )obj;
  -     } 
  -      Parser parser = ( Parser)parseMap.get(cls);
  -      if( parser == null)
  -          throw new IllegalArgumentException(cls.getName() + ":" + obj );
  -      
  -      return parser.format(obj);
  -       
  -      
  -   }
  -   static Object parse( Class cls, String str ){
  -      if( str == null || cls == String.class  ){
  -         return str;
  -      }
  -      Parser parser = ( Parser)parseMap.get(cls);
  -      if( parser == null)
  -          throw new IllegalArgumentException(cls.getName() + ":" + str );
  -      
  -      return parser.parse(str);
  -      
       }
       
   }
  
  
  

--
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