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 XmlDbStorage.java
Date Sat, 18 May 2002 20:43:47 GMT
baliuka     02/05/18 13:43:47

  Modified:    simplestore build.xml
               simplestore/src/test/org/apache/commons/simplestore
                        TestAll.java TestSample.java
               simplestore/src/xmldb/org/apache/commons/simplestore/xmldb
                        XmlDbStorage.java
  Log:
  
  
  Revision  Changes    Path
  1.29      +27 -5     jakarta-commons-sandbox/simplestore/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/build.xml,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- build.xml	15 May 2002 18:15:40 -0000	1.28
  +++ build.xml	18 May 2002 20:43:47 -0000	1.29
  @@ -1,7 +1,7 @@
   <?xml version="1.0" encoding="UTF-8"?>
   <project basedir="." default="compile" name="simplestore">
   
  -<!--         "simplestore" component of the Jakarta Commons Subproject         $Id:
build.xml,v 1.28 2002/05/15 18:15:40 baliuka Exp $ -->
  +<!--         "simplestore" component of the Jakarta Commons Subproject         $Id:
build.xml,v 1.29 2002/05/18 20:43:47 baliuka Exp $ -->
   
   <!-- ========== Executable Targets ======================================== -->
   
  @@ -33,6 +33,9 @@
       <property name="block.build.home" value="build/block"/>
       <property name="test.build.home" value="build/test"/>
   
  +    <property name="xmldb.build.home" value="build/xmldb"/>
  +    <property name="xmldb.home" value="src/xmldb"/>
  +
       <property name="dist.home" value="dist"/>
       <property name="source.home" value="src/java"/>
       <property name="source.test" value="src/test"/>
  @@ -75,6 +78,9 @@
       <mkdir dir="${block.build.home}/conf"/>
       <mkdir dir="${test.build.home}/classes"/>
       <mkdir dir="${test.build.home}/conf"/>
  +    <mkdir dir="${xmldb.build.home}/classes"/>
  +    <mkdir dir="${xmldb.build.home}/conf"/>
  + 
    
     </target>
   
  @@ -108,7 +114,7 @@
     </target>
   
    <target depends="compile"
  -         description="Compile shareable components" 
  +         description="Compile Phoenix Block implementation" 
            name="compile-block">
       <javac debug="${compile.debug}" 
              deprecation="${compile.deprecation}" 
  @@ -119,6 +125,17 @@
       </javac>
     </target>
   
  + <target depends="compile"
  +         description="Compile XmlDb Storage" 
  +         name="compile-xmldb">
  +    <javac debug="${compile.debug}" 
  +           deprecation="${compile.deprecation}" 
  +           destdir="${xmldb.build.home}/classes" 
  +           optimize="${compile.optimize}" 
  +           srcdir="${xmldb.home}">
  +      <classpath refid="compile.classpath"/>
  +    </javac>
  +  </target>
   
    <target depends="compile-block" description="Create binary distribution" name="block">
      <mkdir dir="${dist.home}"/> 
  @@ -136,14 +153,18 @@
     </target>
     
     
  -  <target depends="compile" name="build-test">
  +  <target depends="compile,compile-xmldb" name="build-test">
       <javac debug="${compile.debug}" 
              deprecation="${compile.deprecation}" 
              destdir="${test.build.home}/classes" 
              optimize="${compile.optimize}" 
  -           srcdir="${source.test}" >
  -
  +           srcdir="${source.test}"
  +         >
  +   
      <classpath refid="compile.classpath"/>
  +   <classpath>
  +   <pathelement path="${xmldb.build.home}/classes"/>
  +   </classpath> 
      </javac>
     
     <copy todir="${test.build.home}/classes" >
  @@ -193,6 +214,7 @@
             <pathelement path="${classpath}"/>
             <pathelement path="${java.class.path}"/>
             <pathelement path="${source.test}"/>
  +          <pathelement path="${xmldb.build.home}/classes" />  
             <fileset dir="./lib">
   		<include name="*.*"/>
             </fileset>
  
  
  
  1.12      +4 -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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- TestAll.java	16 Mar 2002 18:55:45 -0000	1.11
  +++ TestAll.java	18 May 2002 20:43:47 -0000	1.12
  @@ -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.11 2002/03/16 18:55:45 baliuka Exp $
  + *@version    $Id: TestAll.java,v 1.12 2002/05/18 20:43:47 baliuka Exp $
    */
   public class TestAll extends TestCase {
       public TestAll(String testName) {
  @@ -68,8 +68,9 @@
   
       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.28      +2 -2      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.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- TestSample.java	15 May 2002 18:15:40 -0000	1.27
  +++ TestSample.java	18 May 2002 20:43:47 -0000	1.28
  @@ -72,13 +72,13 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: TestSample.java,v 1.27 2002/05/15 18:15:40 baliuka Exp $
  + *@version    $Id: TestSample.java,v 1.28 2002/05/18 20:43:47 baliuka Exp $
    */
   public class TestSample extends TestCase implements org.apache.commons.simplestore.tools.Constants{
       
       
       
  -    PersistenceManager pm;
  +   protected PersistenceManager pm;
       
       public TestSample(String testName) {
           super(testName);
  
  
  
  1.2       +254 -58   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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XmlDbStorage.java	17 May 2002 18:33:55 -0000	1.1
  +++ XmlDbStorage.java	18 May 2002 20:43:47 -0000	1.2
  @@ -68,6 +68,8 @@
   import org.apache.commons.simplestore.persistence.impl.AbstractStorage;
   
   import org.xmldb.api.base.*;
  +import org.xmldb.api.modules.*;
  +import org.w3c.dom.*;
   
   import java.util.ArrayList;
   import java.util.Enumeration;
  @@ -82,20 +84,21 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: XmlDbStorage.java,v 1.1 2002/05/17 18:33:55 baliuka Exp $
  + *@version    $Id: XmlDbStorage.java,v 1.2 2002/05/18 20:43:47 baliuka Exp $
    */
   
   
   public class XmlDbStorage extends AbstractStorage {
  -   
  -   final static String BEGIN_XUPDATE = "<xu:modifications version=\"1.0\"" +
  -            "      xmlns:xu=\"http://www.xmldb.org/xupdate\">";
  -   
  -   final static String REMOVE_XUPDATE ="<xu:remove select=\"{0}\"/>";
  -   final static String UPDATE_XUPDATE ="<xu:update select=\"{0}\">{1}</xu:update>"
;
  -            
  -   final static String END_XUPDATE ="</xu:modifications>";
  -
  +    
  +    final static String BEGIN_XUPDATE = "<xu:modifications version=\"1.0\"" +
  +    "      xmlns:xu=\"http://www.xmldb.org/xupdate\">\n";
  +    
  +    final static String REMOVE_XUPDATE ="<xu:remove select=\"{0}\"/>";
  +    final static String UPDATE_XUPDATE ="<xu:update select=\"{0}\">{1}</xu:update>"
;
  +    
  +    final static String END_XUPDATE ="</xu:modifications>\n";
  +    
  +    final static String UPDATE ="org.apache.commons.simplestore.xmldb.XmlDbStorage.UPDATE";
       
       CollectionFactory collectionFactory;
       
  @@ -103,47 +106,164 @@
       public XmlDbStorage(CollectionFactory collectionFactory) {
           this.collectionFactory = collectionFactory;
       }
  -    
  +    String getRootNode(){
  +      return "document";
  +    }
       private Collection getCollection(){
  -       return collectionFactory.getCollection();
  +        return collectionFactory.getCollection();
       }
       protected  String getPath(MetaObject properties){
  -      return "/" + properties.getMetaClass().getName();
  +        return "//" + properties.getMetaClass().getName() +"[@"+
  +        properties.getMetaClass().getOIDName() +"='" +
  +        properties.getOID() + "']/descendant-or-self::*";
       }
  -    protected  String getPath(MetaObject properties, String storageName){
  -      return getPath(properties) + "/" + storageName;
  +    // Trancient at this time
  +    private void appendUpdate(String update,MetaClass mclass){
  +        
  +        StringBuffer updateBuff = (StringBuffer)context.getTransactionManager().
  +        getTransaction().getAttribute(UPDATE);
  +        if( updateBuff == null ){
  +            updateBuff = new StringBuffer();
  +            context.getTransactionManager().
  +            getTransaction().setAttribute(UPDATE,updateBuff);
  +        }
  +        updateBuff.append(update + '\n');
       }
  -   // Trancient at this time
  -   private void appendUpdate(String update,MetaClass mclass){
  -    System.out.println(update);
  -   } 
       
       public  void storeObject(MetaObject properties)
  -                   throws StorageException{
  -    
  +    throws StorageException{
  +        MetaClass mClass = context.getMetaClass(properties.getPersistentClass());
  +        
  +        final Object id = properties.getOID();
  +        final java.beans.PropertyDescriptor[] beanProps = mClass.getProperties();
  +        
  +        
  +        
  +        for (int i = 0; i < beanProps.length; i++) {
  +            
  +            java.beans.PropertyDescriptor descriptor = beanProps[i];
  +            if (descriptor.getWriteMethod() != null) {
  +                
  +                Object value =  properties.getProperty(i) ;
  +                if (value == null) {
  +                    continue;
  +                }
  +                
  +                String name = mClass.getPropertyName(i);
  +              appendUpdate(
  +                  java.text.MessageFormat.format(UPDATE_XUPDATE, 
  +                  new Object[]{ getPath(properties) + "/" + name ,value } ),
  +                  properties.getMetaClass()
  +                );
  +       
  +                
  +            }
  +            
  +        }
  +        
  +        internalCommit();
  +        
  +    }
       
  -    }  
  +     class Handler implements org.xml.sax.ContentHandler{
  +        MetaClass  mclasz;
  +        Set objects;
  +        EnumeratorCallback callback;
  +       
  +        Object id;
  +         Handler( MetaClass  mclasz, Set objects, final EnumeratorCallback callback){
  +         
  +         }
  +         
  +         public void characters(char[] values, int param, int param2) throws org.xml.sax.SAXException
{
  +         }
  +         
  +         public void endDocument() throws org.xml.sax.SAXException {
  +            // System.out.println("END DOCUMENT");
  +         }
  +         
  +         public void endElement(String str, String str1, String str2) throws org.xml.sax.SAXException
{
  +            // System.out.println("END:" + str + ":" + str1 + ":" + str2 );
  +         }
  +         
  +         public void endPrefixMapping(String str) throws org.xml.sax.SAXException {
  +         }
  +         
  +         public void ignorableWhitespace(char[] values, int param, int param2) throws org.xml.sax.SAXException
{
  +         }
  +         
  +         public void processingInstruction(String str, String str1) throws org.xml.sax.SAXException
{
  +         }
  +         
  +         public void setDocumentLocator(org.xml.sax.Locator locator) {
  +         }
  +         
  +         public void skippedEntity(String str) throws org.xml.sax.SAXException {
  +         }
  +         
  +         public void startDocument() throws org.xml.sax.SAXException {
  +            // System.out.println("START DOCUMENT");
  +         }
  +         
  +         public void startElement(String str, String str1, String str2, org.xml.sax.Attributes
attributes) throws org.xml.sax.SAXException {
  +             
  +            //  System.out.println("START:" + str + ":" + str1 + ":" + str2 );
  +             
  +             
  +         }
  +         
  +         public void startPrefixMapping(String str, String str1) throws org.xml.sax.SAXException
{
  +         }
  +         
  +    }
       
       public  void enumerateInternal(final Class clasz, Set objects, final EnumeratorCallback
callback)
  -                                     throws StorageException {
  -    
  -    
  +    throws StorageException {
  +        try{
  +        
  +         MetaClass mClass = context.getMetaClass(clasz);
  +         
  +         String xpath = "//"+ context.getMetaClass(clasz).getName() + "/descendant-or-self::*"
;
  +       
  +         Collection cl =  getCollection();
  +          
  +        XPathQueryService service =
  +        (XPathQueryService)cl.getService("XPathQueryService", "1.0");
  +        
  +        ResourceSet resultSet = service.query(xpath);
  +        System.out.println("Query " + xpath);
  +        ResourceIterator results = resultSet.getIterator();
  +        while (results.hasMoreResources()) {
  +            XMLResource resource =
  +            (XMLResource) results.nextResource();
  +            System.out.println(resource.getContent() );
  +            resource.getContentAsSAX(    
  +            new Handler(mClass,objects,callback)
  +           ) ;
  +            
  +             
  +        }
  +        cl.close();
  +        }catch(XMLDBException xe){
  +            xe.printStackTrace();
  +            throw new StorageException(xe.getMessage(),xe);
  +        }
       }
       
       protected  void createObject(MetaObject properties) throws StorageException{
  -       MetaClass mClass = context.getMetaClass(properties.getPersistentClass());
  -       
  +        MetaClass mClass = context.getMetaClass(properties.getPersistentClass());
  +        
           final Object id = properties.getOID();
           final java.beans.PropertyDescriptor[] beanProps = mClass.getProperties();
  -       
  -       appendUpdate( "<xupdate:append select=\"/\" >", mClass );
  -       
  -       appendUpdate("<xupdate:element name=\"" + mClass.getName() +"\">", mClass);
  +        
  +        appendUpdate( "<xu:append select=\"/" + getRootNode() + "\" >", mClass );
  +        
  +        appendUpdate("<xu:element name=\"" + mClass.getName() +"\">", mClass);
           String oidName = mClass.getOIDName();
  -        appendUpdate("<"+ oidName +">" + id + "</"+ oidName +">",mClass);
  -        appendUpdate("</xupdate:element>",mClass);
  -       
  -       for (int i = 0; i < beanProps.length; i++) {
  +        appendUpdate("<xu:attribute name=\"" + mClass.getOIDName() + "\">" + id +
 "</xu:attribute>",mClass);
  +        
  +        
  +        for (int i = 0; i < beanProps.length; i++) {
               
               java.beans.PropertyDescriptor descriptor = beanProps[i];
               if (descriptor.getWriteMethod() != null) {
  @@ -152,48 +272,124 @@
                   if (value == null) {
                       continue;
                   }
  -              appendUpdate("<xupdate:element name=\"" + mClass.getName() +"\">",
mClass);
  -              String name = mClass.getPropertyName(i); 
  -              appendUpdate("<"+ name +">" + value + "</"+ name +">",mClass);
  -              appendUpdate("</xupdate:element>",mClass);
  +                
  +                String name = mClass.getPropertyName(i);
  +                appendUpdate("<"+ name +">" + value + "</"+ name +">",mClass);
  +                
               }
  +            
           }
  -      
  -      appendUpdate( "</xupdate:append>",mClass );
  +        appendUpdate("</xu:element>",mClass);
  +        appendUpdate( "</xu:append>",mClass );
           
  -    
  +        internalCommit();
       }
       
       protected  void removeObject(MetaObject properties) throws StorageException{
           
  -       appendUpdate( 
  -        java.text.MessageFormat.format(REMOVE_XUPDATE, new Object[]{ getPath(properties)
} ),
  +        appendUpdate(
  +        java.text.MessageFormat.format(REMOVE_XUPDATE, new Object[]{ getPath(properties)
+ "/descendant-or-self::*" } ),
           properties.getMetaClass()
  -       );
  -    
  +        );
  +       
  +    }
  +    private void clearBuff(){
  +        context.getTransactionManager().
  +        getTransaction().removeAttribute(UPDATE);
       }
  -    
       protected  void internalCommit() throws StorageException{
  -    
  -    
  +        
  +           try{
  +           
  +            StringBuffer updateBuff = (StringBuffer)context.getTransactionManager().
  +            getTransaction().getAttribute(UPDATE);
  +            if( updateBuff != null ){
  +                Collection cl = getCollection();
  +                XUpdateQueryService   service =(XUpdateQueryService) cl.
  +                getService("XUpdateQueryService", "1.0");
  +               // System.out.println(BEGIN_XUPDATE +  updateBuff.toString() + END_XUPDATE);
  +                service.update(BEGIN_XUPDATE + updateBuff.toString() + END_XUPDATE);
  +                clearBuff();
  +                cl.close();
  +            }
  +            
  +        }catch(XMLDBException xe){
  +            xe.printStackTrace();
  +            throw new StorageException(xe.getMessage(),xe);
  +        }
  +         
       }
       
       protected  void internalRollback() throws StorageException{
  -    
  -    
  +        clearBuff();
       }
       
       protected  void internalBegin() throws StorageException{
  -    
  -    
  +        clearBuff();
       }
  -  
       
  -    public  void retrieveInternal(Class clasz, int index, Object value,java.util.Set objects)throws
StorageException{
       
  +    public  void retrieveInternal(Class clasz, int index, Object value,java.util.Set objects)throws
StorageException{
  +        
  +        try{
  +         MetaClass mClass = context.getMetaClass(clasz);
  +         
  +         String xpath = "//"+ context.getMetaClass(clasz).getName() + "/[" +
  +               mClass.getPropertyName(index)  + "='" + value + "']/descendant-or-self::*";
  +       
  +         Collection cl =  getCollection();
  +          
  +        XPathQueryService service =
  +        (XPathQueryService)cl.getService("XPathQueryService", "1.0");
  +        
  +        ResourceSet resultSet = service.query(xpath);
  +        System.out.println("Query " + xpath);
  +        ResourceIterator results = resultSet.getIterator();
  +        while (results.hasMoreResources()) {
  +            XMLResource resource =
  +            (XMLResource) results.nextResource();
  +            System.out.println(resource.getContent() );
  +            
  +             
  +        }
  +        cl.close();
  +        }catch(XMLDBException xe){
  +            xe.printStackTrace();
  +            throw new StorageException(xe.getMessage(),xe);
  +        }
  +   
  +        
  +        
       }
       
  -     public Object retrieveObject( final Class clasz, Object id ) throws StorageException
{
  -       return null;
  -     }
  +    public Object retrieveObject( final Class clasz, Object id ) throws StorageException
{
  +        try{
  +         MetaClass mClass = context.getMetaClass(clasz);
  +         
  +         String xpath = "//"+ context.getMetaClass(clasz).getName() + "/[" +
  +               mClass.getOIDName() + "='" + id + "']/*";
  +       
  +         Collection cl =  getCollection();
  +          
  +        XPathQueryService service =
  +        (XPathQueryService)cl.getService("XPathQueryService", "1.0");
  +        
  +        ResourceSet resultSet = service.query(xpath);
  +        System.out.println("Query " + xpath);
  +        ResourceIterator results = resultSet.getIterator();
  +        while (results.hasMoreResources()) {
  +            XMLResource resource =
  +            (XMLResource) results.nextResource();
  +            System.out.println(resource.getContent() );
  +            
  +             
  +        }
  +        cl.close();
  +        }catch(XMLDBException xe){
  +            xe.printStackTrace();
  +            throw new StorageException(xe.getMessage(),xe);
  +        }
  +   
  +        return null;
  +    }
   }
  
  
  

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