Package test.xmldb.other

Source Code of test.xmldb.other.StoreDocument

// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
package test.xmldb.other;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StringWriter;
import java.util.Iterator;

import org.ozoneDB.ExternalDatabase;
import org.ozoneDB.OzoneInterface;
import org.ozoneDB.OzoneProxy;
import org.ozoneDB.xml.core.XMLCollectionImpl;
import org.ozoneDB.xml.core.XMLCollection;
import org.xmldb.api.modules.XMLResource;

import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.DatabaseManager;

import org.xml.sax.InputSource;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;

import org.ozoneDB.xml.util.*;
import org.ozoneDB.ExternalTransaction;
import org.xml.sax.helpers.*;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Node;
import org.w3c.dom.Document;

import test.xmldb.*;
/**
* @author  Per Nyfelt
*/
public class StoreDocument implements Constants{
   
    ExternalDatabase db = null;
   
    /** Creates new StoreDocument */
    public StoreDocument() {
        try {
            createCollection();
            Class c = Class.forName(driver);

            //Database database = (Database) c.newInstance();     
            System.out.println("collectionURI is " + collectionURI);
            Collection col = DatabaseManager.getCollection(collectionURI);
            //Collection col = database.getCollection(collectionURI);
            Document doc = parseDocument();
            XMLResource resource = (XMLResource)col.getResource(resourceName);
            if (resource == null) {
                OzoneProxy o = db.objectForName(resourceName);
                if (o != null) {
                    System.out.println("Object found as db oject, deleting it..");
                    db.deleteObject(o);
                }
                System.out.println("resource is null, create the resource with name " + resourceName);
                resource = (XMLResource) col.createResource(resourceName, XMLResource.RESOURCE_TYPE);
            }
            else {
                System.out.println("deleting resource" + resourceName);
                col.removeResource(resource);
                resource = (XMLResource) col.createResource(resourceName, XMLResource.RESOURCE_TYPE);
            }
            System.out.println("Created resource " + resourceName + " with id " + resource.getId());
            // double check to see if it actually works
            resource = (XMLResource)col.getResource(resourceName);
            System.out.println("Resource found with name " + resource.getId());          
            resource.setContentAsDOM(doc);
            System.out.println("Updated content: " + resource.getContent());
            col.storeResource(resource);
            //System.out.println("Stored the following Document:\n" + toString(doc));
            System.out.println("Stored the following Document:\n" + doc.getDocumentElement().getTagName());
            col.close();
            db.close();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    private void createCollection() throws Exception {
        try {
            connect();
            // check if there is an object there already in that case delete it
            // we assume we are cleaning up after an usuccessful testrun
            XMLCollection test = (XMLCollection)db.objectForName(collectionName);
            if (test != null) {
                deleteCollection();
                connect();
            }
            // create a new Collection
            org.ozoneDB.xml.cli.CollectionFactory.create( db, collectionName );
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }       
    }
   
    private void connect() throws Exception {
        if (db == null || !db.isOpen()) {      
            db = ExternalDatabase.openDatabase(dbURI);
            System.out.println("StoreDocument.connect() - connected");
            db.reloadClasses();           
        }
    }
    public void deleteCollection() {
        try {
            connect();
            XMLCollection collection = (XMLCollection)db.objectForName(collectionName);
            Iterator it = collection.getResources().iterator();
            while (it.hasNext()) {
                XMLContainer.forName(db,(String)it.next()).delete();
            }
            db.deleteObject(collection);
            System.out.println("StoreDocument.deleteCollection() - deleted " + collectionName);
            db.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } 
    Document parseDocument() throws Exception {
        BufferedReader in = new BufferedReader(new FileReader(fileName));
        InputSource source = new InputSource(in);
        DOMParser parser = new DOMParser();
        parser.parse(source);    
        return parser.getDocument();
    }

    private XMLSerializer getSerializer() throws Exception{
        StringWriter writer = new StringWriter();
        XMLSerializer serializer = new XMLSerializer(writer, new OutputFormat("xml", "UTF-8", true));
        return serializer;
    }
   
    protected static void domStore( String id, Node doc ) throws Exception {
        String dbURI = "ozonedb:remote://localhost:3333";  
        ExternalDatabase db = ExternalDatabase.openDatabase(dbURI);
        XMLContainer container = XMLContainer.forName( db, id );
        if (container == null) {
            container = XMLContainer.newContainer( db, id );
        }
       
        ExternalTransaction tx = db.newTransaction();
        try {
            long start = System.currentTimeMillis();
    
            tx.begin();
            start = System.currentTimeMillis();                   
            container.storeDOM( null, doc );
            System.out.println( "DOM store: store time: " + (System.currentTimeMillis() - start) + " ms" );
           
            start = System.currentTimeMillis();
            tx.commit();
            System.out.println( "DOM store: commit time: " + (System.currentTimeMillis() - start) + " ms" );
        } catch (Exception e) {
            tx.rollback();
            throw e;
        }
    }
    public static void main( String args[] ) {
        new StoreDocument();
    }
}
TOP

Related Classes of test.xmldb.other.StoreDocument

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.