1 package test.xmldb.other; 8 9 import org.apache.xerces.parsers.DOMParser; 10 import org.apache.xml.serialize.OutputFormat; 11 import org.apache.xml.serialize.XMLSerializer; 12 import org.ozoneDB.ExternalDatabase; 13 import org.ozoneDB.ExternalTransaction; 14 import org.ozoneDB.OzoneProxy; 15 import org.ozoneDB.xml.core.XMLCollection; 16 import org.ozoneDB.xml.util.XMLContainer; 17 import org.w3c.dom.Document ; 18 import org.w3c.dom.Node ; 19 import org.xml.sax.InputSource ; 20 import org.xmldb.api.DatabaseManager; 21 import org.xmldb.api.base.Collection; 22 import org.xmldb.api.modules.XMLResource; 23 import test.xmldb.Constants; 24 25 import java.io.BufferedReader ; 26 import java.io.FileReader ; 27 import java.io.StringWriter ; 28 import java.util.Iterator ; 29 32 public class StoreDocument implements Constants { 33 34 ExternalDatabase db = null; 35 36 37 public StoreDocument() { 38 try { 39 createCollection(); 40 Class c = Class.forName(driver); 41 42 System.out.println("collectionURI is " + collectionURI); 44 Collection col = DatabaseManager.getCollection(collectionURI); 45 Document doc = parseDocument(); 47 XMLResource resource = (XMLResource)col.getResource(resourceName); 48 if (resource == null) { 49 OzoneProxy o = db.objectForName(resourceName); 50 if (o != null) { 51 System.out.println("Object found as db oject, deleting it.."); 52 db.deleteObject(o); 53 } 54 System.out.println("resource is null, create the resource with name " + resourceName); 55 resource = (XMLResource) col.createResource(resourceName, XMLResource.RESOURCE_TYPE); 56 } 57 else { 58 System.out.println("deleting resource" + resourceName); 59 col.removeResource(resource); 60 resource = (XMLResource) col.createResource(resourceName, XMLResource.RESOURCE_TYPE); 61 } 62 System.out.println("Created resource " + resourceName + " with id " + resource.getId()); 63 resource = (XMLResource)col.getResource(resourceName); 65 System.out.println("Resource found with name " + resource.getId()); 66 resource.setContentAsDOM(doc); 67 System.out.println("Updated content: " + resource.getContent()); 68 col.storeResource(resource); 69 System.out.println("Stored the following Document:\n" + doc.getDocumentElement().getTagName()); 71 col.close(); 72 db.close(); 73 } 74 catch (Exception e) { 75 System.out.println(e.getMessage()); 76 e.printStackTrace(); 77 } 78 } 79 80 private void createCollection() throws Exception { 81 try { 82 connect(); 83 XMLCollection test = (XMLCollection)db.objectForName(collectionName); 86 if (test != null) { 87 deleteCollection(); 88 connect(); 89 } 90 org.ozoneDB.xml.cli.CollectionFactory.create( db, collectionName ); 92 } catch (Exception e) { 93 e.printStackTrace(); 94 throw e; 95 } 96 } 97 98 private void connect() throws Exception { 99 if (db == null || !db.isOpen()) { 100 db = ExternalDatabase.openDatabase(dbURI); 101 System.out.println("StoreDocument.connect() - connected"); 102 db.reloadClasses(); 103 } 104 } 105 public void deleteCollection() { 106 try { 107 connect(); 108 XMLCollection collection = (XMLCollection)db.objectForName(collectionName); 109 Iterator it = collection.getResources().iterator(); 110 while (it.hasNext()) { 111 XMLContainer.forName(db,(String )it.next()).delete(); 112 } 113 db.deleteObject(collection); 114 System.out.println("StoreDocument.deleteCollection() - deleted " + collectionName); 115 db.close(); 116 } catch (Exception e) { 117 e.printStackTrace(); 118 } 119 } 120 Document parseDocument() throws Exception { 121 BufferedReader in = new BufferedReader (new FileReader (fileName)); 122 InputSource source = new InputSource (in); 123 DOMParser parser = new DOMParser(); 124 parser.parse(source); 125 return parser.getDocument(); 126 } 127 128 private XMLSerializer getSerializer() throws Exception { 129 StringWriter writer = new StringWriter (); 130 XMLSerializer serializer = new XMLSerializer(writer, new OutputFormat("xml", "UTF-8", true)); 131 return serializer; 132 } 133 134 protected static void domStore( String id, Node doc ) throws Exception { 135 String dbURI = "ozonedb:remote://localhost:3333"; 136 ExternalDatabase db = ExternalDatabase.openDatabase(dbURI); 137 XMLContainer container = XMLContainer.forName( db, id ); 138 if (container == null) { 139 container = XMLContainer.newContainer( db, id ); 140 } 141 142 ExternalTransaction tx = db.newTransaction(); 143 try { 144 long start = System.currentTimeMillis(); 145 146 tx.begin(); 147 start = System.currentTimeMillis(); 148 container.storeDOM( null, doc ); 149 System.out.println( "DOM store: store time: " + (System.currentTimeMillis() - start) + " ms" ); 150 151 start = System.currentTimeMillis(); 152 tx.commit(); 153 System.out.println( "DOM store: commit time: " + (System.currentTimeMillis() - start) + " ms" ); 154 } catch (Exception e) { 155 tx.rollback(); 156 throw e; 157 } 158 } 159 public static void main( String args[] ) { 160 new StoreDocument(); 161 } 162 } 163 | Popular Tags |