baliuka 02/05/19 05:57:45
Modified: simplestore/src/xmldb/org/apache/commons/simplestore/xmldb
XmlDbStorage.java
Log:
XmlDb Storage implementation
Revision Changes Path
1.3 +331 -133
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.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- XmlDbStorage.java 18 May 2002 20:43:47 -0000 1.2
+++ XmlDbStorage.java 19 May 2002 12:57:45 -0000 1.3
@@ -70,6 +70,7 @@
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.w3c.dom.*;
+import org.xml.sax.*;
import java.util.ArrayList;
import java.util.Enumeration;
@@ -84,12 +85,15 @@
/**
*@author Juozas Baliuka <a href="mailto:[EMAIL PROTECTED]">
* [EMAIL PROTECTED]</a>
- *@version $Id: XmlDbStorage.java,v 1.2 2002/05/18 20:43:47 baliuka Exp $
+ *@version $Id: XmlDbStorage.java,v 1.3 2002/05/19 12:57:45 baliuka Exp $
*/
public class XmlDbStorage extends AbstractStorage {
+ 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";
@@ -107,17 +111,17 @@
this.collectionFactory = collectionFactory;
}
String getRootNode(){
- return "document";
+ return "document";
}
private Collection getCollection(){
return collectionFactory.getCollection();
}
protected String getPath(MetaObject properties){
- return "//" + properties.getMetaClass().getName() +"[@"+
+ return "/" + getRootNode() + "/descendant-or-self::" +
properties.getMetaClass().getName() +"[@"+
properties.getMetaClass().getOIDName() +"='" +
- properties.getOID() + "']/descendant-or-self::*";
+ properties.getOID() + "']";
}
- // Trancient at this time
+
private void appendUpdate(String update,MetaClass mclass){
StringBuffer updateBuff = (StringBuffer)context.getTransactionManager().
@@ -132,6 +136,7 @@
public void storeObject(MetaObject properties)
throws StorageException{
+
MetaClass mClass = context.getMetaClass(properties.getPersistentClass());
final Object id = properties.getOID();
@@ -150,100 +155,47 @@
}
String name = mClass.getPropertyName(i);
- appendUpdate(
- java.text.MessageFormat.format(UPDATE_XUPDATE,
- new Object[]{ getPath(properties) + "/" + name ,value } ),
- properties.getMetaClass()
+ appendUpdate(
+ java.text.MessageFormat.format(UPDATE_XUPDATE,
+ new Object[]{ getPath(properties) + "/" + name , format( 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 {
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();
+ MetaClass mClass = context.getMetaClass(clasz);
+
+ String xpath = "/"+ getRootNode()+ "/descendant-or-self::" +
context.getMetaClass(clasz).getName() ;
+ Collection cl = getCollection();
+
+ XPathQueryService service =
+ (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(
+ new Handler(context,mClass,objects,callback)
+ ) ;
+
+
+ }
+ cl.close();
}catch(XMLDBException xe){
xe.printStackTrace();
throw new StorageException(xe.getMessage(),xe);
@@ -274,7 +226,7 @@
}
String name = mClass.getPropertyName(i);
- appendUpdate("<"+ name +">" + value + "</"+ name +">",mClass);
+ appendUpdate("<"+ name +">" + format(value) + "</"+ name
+">",mClass);
}
@@ -282,7 +234,7 @@
appendUpdate("</xu:element>",mClass);
appendUpdate( "</xu:append>",mClass );
- internalCommit();
+
}
protected void removeObject(MetaObject properties) throws StorageException{
@@ -291,7 +243,7 @@
java.text.MessageFormat.format(REMOVE_XUPDATE, new Object[]{
getPath(properties) + "/descendant-or-self::*" } ),
properties.getMetaClass()
);
-
+
}
private void clearBuff(){
context.getTransactionManager().
@@ -299,15 +251,14 @@
}
protected void internalCommit() throws StorageException{
- try{
-
+ 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();
@@ -317,7 +268,7 @@
xe.printStackTrace();
throw new StorageException(xe.getMessage(),xe);
}
-
+
}
protected void internalRollback() throws StorageException{
@@ -332,64 +283,311 @@
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() );
+ MetaClass mClass = context.getMetaClass(clasz);
-
- }
- cl.close();
+ String xpath = "/" + getRootNode() + "/descendant-or-self::"+
context.getMetaClass(clasz).getName() + "[" +
+ mClass.getPropertyName(index) + "='" + value + "']";
+
+ Collection cl = getCollection();
+
+ XPathQueryService service =
+ (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(
+ new Handler(context,mClass,objects,null)
+ );
+
+
+ }
+ cl.close();
}catch(XMLDBException xe){
xe.printStackTrace();
throw new StorageException(xe.getMessage(),xe);
}
-
+
}
public Object retrieveObject( final Class clasz, Object id ) throws
StorageException {
+ java.util.Set objects = new java.util.HashSet();
try{
- MetaClass mClass = context.getMetaClass(clasz);
-
- String xpath = "//"+ context.getMetaClass(clasz).getName() + "/[" +
- mClass.getOIDName() + "='" + id + "']/*";
-
- Collection cl = getCollection();
+
+ MetaClass mClass = context.getMetaClass(clasz);
+
+ String xpath = "/"+ getRootNode() + "/descendant-or-self::"+
context.getMetaClass(clasz).getName() + "[" +
+ mClass.getOIDName() + "='" + id + "']";
+
- 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() );
+ Collection cl = getCollection();
-
- }
- cl.close();
+ XPathQueryService service =
+ (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(
+ 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();
+ }
+ if(objects.size() != 1 ){
+ throw new StorageException("dublicate object",null);
+ }
+
+ return objects.iterator().next();
+ }
+
+
+
+
+
+
+ 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 id;
+
+ Handler( Context cntxt, MetaClass mClasz, Set objects, final
EnumeratorCallback callback){
+ this.cntxt = cntxt;
+ this.mClasz = mClasz;
+ this.objects = objects;
+ this.callback = callback;
+
+ }
+
+ private void dispatch(final boolean fireOnlyIfMixed)
+ 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;
+ try {
+
+ if ( name.equals(mClasz.getName()) ) {
+
+ 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;
+ }
+ }
+ 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 {
+
+ int index = mClasz.getPropertyIndex( name );
+
+ props[ index ] =
parse(mClasz.getPropertyType(index),buffer.toString()) ;
+
+ return ;
+
+
+ }
+
+
+ } catch (Throwable t) {
+ t.printStackTrace();
+ throw new StorageException(t.getMessage(), t);
+ }
+
+
+ buffer.delete(0, buffer.length());
+ }
+
+
+ public void characters(char[] chars, int start, int len) throws
org.xml.sax.SAXException {
+ buffer.append(chars, start, len);
+ }
+
+
+ public void endElement(String str, String str1, String str2) throws
org.xml.sax.SAXException {
+ dispatch(false);
+ 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)});
+
+ }
+
+
+ }
+
+ 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);
+ }
+
+ public Object parse(String str){
+ try{
+ return DATE_FROMAT.parse(str);
+ }catch(Exception e){
+ throw new IllegalArgumentException(str);
+ }
+ }
+ });
+ //-----------------------------------------
+
+
+
+
+ }
- return null;
+ 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:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>