001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: package test.xmldb.other;
008:
009: import java.io.BufferedReader;
010: import java.io.FileReader;
011: import java.io.StringWriter;
012: import java.util.Iterator;
013:
014: import org.ozoneDB.ExternalDatabase;
015: import org.ozoneDB.OzoneInterface;
016: import org.ozoneDB.OzoneProxy;
017: import org.ozoneDB.xml.core.XMLCollectionImpl;
018: import org.ozoneDB.xml.core.XMLCollection;
019: import org.xmldb.api.modules.XMLResource;
020:
021: import org.xmldb.api.base.Collection;
022: import org.xmldb.api.base.Database;
023: import org.xmldb.api.DatabaseManager;
024:
025: import org.xml.sax.InputSource;
026: import org.apache.xerces.parsers.DOMParser;
027: import org.w3c.dom.Document;
028: import org.apache.xml.serialize.OutputFormat;
029: import org.apache.xml.serialize.XMLSerializer;
030:
031: import org.ozoneDB.xml.util.*;
032: import org.ozoneDB.ExternalTransaction;
033: import org.xml.sax.helpers.*;
034: import org.apache.xerces.parsers.DOMParser;
035: import org.w3c.dom.Node;
036: import org.w3c.dom.Document;
037:
038: import test.xmldb.*;
039:
040: /**
041: * @author Per Nyfelt
042: */
043: public class StoreDocument implements Constants {
044:
045: ExternalDatabase db = null;
046:
047: /** Creates new StoreDocument */
048: public StoreDocument() {
049: try {
050: createCollection();
051: Class c = Class.forName(driver);
052:
053: //Database database = (Database) c.newInstance();
054: System.out.println("collectionURI is " + collectionURI);
055: Collection col = DatabaseManager
056: .getCollection(collectionURI);
057: //Collection col = database.getCollection(collectionURI);
058: Document doc = parseDocument();
059: XMLResource resource = (XMLResource) col
060: .getResource(resourceName);
061: if (resource == null) {
062: OzoneProxy o = db.objectForName(resourceName);
063: if (o != null) {
064: System.out
065: .println("Object found as db oject, deleting it..");
066: db.deleteObject(o);
067: }
068: System.out
069: .println("resource is null, create the resource with name "
070: + resourceName);
071: resource = (XMLResource) col.createResource(
072: resourceName, XMLResource.RESOURCE_TYPE);
073: } else {
074: System.out.println("deleting resource" + resourceName);
075: col.removeResource(resource);
076: resource = (XMLResource) col.createResource(
077: resourceName, XMLResource.RESOURCE_TYPE);
078: }
079: System.out.println("Created resource " + resourceName
080: + " with id " + resource.getId());
081: // double check to see if it actually works
082: resource = (XMLResource) col.getResource(resourceName);
083: System.out.println("Resource found with name "
084: + resource.getId());
085: resource.setContentAsDOM(doc);
086: System.out.println("Updated content: "
087: + resource.getContent());
088: col.storeResource(resource);
089: //System.out.println("Stored the following Document:\n" + toString(doc));
090: System.out.println("Stored the following Document:\n"
091: + doc.getDocumentElement().getTagName());
092: col.close();
093: db.close();
094: } catch (Exception e) {
095: System.out.println(e.getMessage());
096: e.printStackTrace();
097: }
098: }
099:
100: private void createCollection() throws Exception {
101: try {
102: connect();
103: // check if there is an object there already in that case delete it
104: // we assume we are cleaning up after an usuccessful testrun
105: XMLCollection test = (XMLCollection) db
106: .objectForName(collectionName);
107: if (test != null) {
108: deleteCollection();
109: connect();
110: }
111: // create a new Collection
112: org.ozoneDB.xml.cli.CollectionFactory.create(db,
113: collectionName);
114: } catch (Exception e) {
115: e.printStackTrace();
116: throw e;
117: }
118: }
119:
120: private void connect() throws Exception {
121: if (db == null || !db.isOpen()) {
122: db = ExternalDatabase.openDatabase(dbURI);
123: System.out.println("StoreDocument.connect() - connected");
124: db.reloadClasses();
125: }
126: }
127:
128: public void deleteCollection() {
129: try {
130: connect();
131: XMLCollection collection = (XMLCollection) db
132: .objectForName(collectionName);
133: Iterator it = collection.getResources().iterator();
134: while (it.hasNext()) {
135: XMLContainer.forName(db, (String) it.next()).delete();
136: }
137: db.deleteObject(collection);
138: System.out
139: .println("StoreDocument.deleteCollection() - deleted "
140: + collectionName);
141: db.close();
142: } catch (Exception e) {
143: e.printStackTrace();
144: }
145: }
146:
147: Document parseDocument() throws Exception {
148: BufferedReader in = new BufferedReader(new FileReader(fileName));
149: InputSource source = new InputSource(in);
150: DOMParser parser = new DOMParser();
151: parser.parse(source);
152: return parser.getDocument();
153: }
154:
155: private XMLSerializer getSerializer() throws Exception {
156: StringWriter writer = new StringWriter();
157: XMLSerializer serializer = new XMLSerializer(writer,
158: new OutputFormat("xml", "UTF-8", true));
159: return serializer;
160: }
161:
162: protected static void domStore(String id, Node doc)
163: throws Exception {
164: String dbURI = "ozonedb:remote://localhost:3333";
165: ExternalDatabase db = ExternalDatabase.openDatabase(dbURI);
166: XMLContainer container = XMLContainer.forName(db, id);
167: if (container == null) {
168: container = XMLContainer.newContainer(db, id);
169: }
170:
171: ExternalTransaction tx = db.newTransaction();
172: try {
173: long start = System.currentTimeMillis();
174:
175: tx.begin();
176: start = System.currentTimeMillis();
177: container.storeDOM(null, doc);
178: System.out.println("DOM store: store time: "
179: + (System.currentTimeMillis() - start) + " ms");
180:
181: start = System.currentTimeMillis();
182: tx.commit();
183: System.out.println("DOM store: commit time: "
184: + (System.currentTimeMillis() - start) + " ms");
185: } catch (Exception e) {
186: tx.rollback();
187: throw e;
188: }
189: }
190:
191: public static void main(String args[]) {
192: new StoreDocument();
193: }
194: }
|