⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 storedocument.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
// 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 org.apache.xerces.parsers.DOMParser;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.ozoneDB.ExternalDatabase;
import org.ozoneDB.ExternalTransaction;
import org.ozoneDB.OzoneProxy;
import org.ozoneDB.xml.core.XMLCollection;
import org.ozoneDB.xml.util.XMLContainer;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.modules.XMLResource;
import test.xmldb.Constants;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StringWriter;
import java.util.Iterator;
/**
 * @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();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -