/* * This file contains functions to create the dbXML enviroments and load XML files to dbXML database * * 08/2008 Originally created by Jiemin Zhang jiemin@cs.ubc.ca * * Code taken and edited from Berkeley DB XML: dbxml.gettingStarted.exampleLoadContainer.java http://www.oracle.com/technology/products/berkeley-db/xml/index.html * * */ package query; import java.io.*; import java.util.*; import com.sleepycat.db.*; import com.sleepycat.dbxml.*; public class loadXML { private static String theContainer; public static XmlManager theMgr = null; public static XmlContainer openedContainer = null; private static XmlTransaction txn = null; public static Environment env = null; public static String envdir; private static File path2DbEnv; /* Mail function to load XML files into dbXML environment; called by runGUI -- taking from dbXML example and edited by Jiemin*/ public static void loadXML(String env, String mtype) throws Throwable { envdir = env; path2DbEnv = new File(envdir + "dbEnv"); //Path to dbXML environment File filePath = new File(envdir + "xmlFile"); //Path to dicrectory of xml files to load theContainer = "test.dbxml"; //Set the container where files will be loaded try { List files2add = new LinkedList();// A list to store files to load File xmlfile = new File(filePath.getPath()); /*If this is a new model, create and add the initial FBM xml first*/ if (mtype.equals(".xml")) { File fbm = new File("xmlFile/FBM.xml"); //this is an empty XML database (just has the root) (AW) files2add.add(fbm); } /*Add the XML file of the model: for a new model will be the ifcXML file, *for existing model will be fbm file.*/ File model = new File("xmlFile/" + query.runGUI.modelName + mtype); files2add.add(model); loadXmlFiles(path2DbEnv, theContainer, files2add); //Call the loading function files2add.clear(); } catch (Exception e) { System.err.println("Error loading XML file into " + theContainer); System.err.println(" Message: " + e.getMessage()); throw e; } } /* Get all files in the directory * * private static void getXmlFiles(File filePath, List files2add) { boolean filesFound = false; String[] dirContents = filePath.list(); if (dirContents != null) { for (int i = 0; i < dirContents.length; i++) { File entry = new File(filePath + File.separator + dirContents[i]); if (entry.isFile() && entry.toString().toLowerCase().endsWith(".xml")) { files2add.add(entry); filesFound = true; } } } if (!filesFound) { System.out.println("\nError: No XML files found at " + filePath.getPath()); // usage(); } }*/ // Take a vector of Files and load each element into a DB XML container -- taking from dbXML example private static void loadXmlFiles(File path2DbEnv, String theContainer, List files2add) throws Throwable { // Open a container in the db environment try { env = createEnv(path2DbEnv); theMgr = new XmlManager(env, new XmlManagerConfig()); // create a transactional container XmlContainerConfig config = new XmlContainerConfig(); config.setTransactional(true); if (theMgr.existsContainer(theContainer) != 0) theMgr.removeContainer(theContainer); // Now it is safe to create the container openedContainer = theMgr.createContainer(theContainer); // Get an update context. XmlUpdateContext updateContext = theMgr.createUpdateContext(); // Get another transaction, via DB. This just // demonstrates that a Transaction created from DB can be // passed to XmlManager.createTransaction. Transaction dbtxn = env.beginTransaction(null, null); txn = theMgr.createTransaction(dbtxn); Iterator filesIterator = files2add.iterator(); while (filesIterator.hasNext()) { File file = (File) filesIterator.next(); String theFile = file.toString(); // Load the contents of the XML file into a String String theLine = null; String xmlString = new String(); FileInputStream fis = new FileInputStream(theFile); BufferedReader br = new BufferedReader(new InputStreamReader( fis)); while ((theLine = br.readLine()) != null) { xmlString += theLine; xmlString += "\n"; } xmlString = xmlString.replaceAll("xmlns=", "xmlns:XML="); br.close(); // Declare an xml document XmlDocument xmlDoc = theMgr.createDocument(); // Set the xml document's content to the xmlString we just // obtained. xmlDoc.setContent(xmlString); // Set the document name xmlDoc.setName(file.getName()); Date theDate = new Date(); xmlDoc.setMetaData("http://dbxmlExamples/timestamp", "timeStamp", new XmlValue(theDate.toString())); // Place that document into the container if (gui.fileFilter.getExtension(file).equals("fbm")) { openedContainer.putDocument("FBM.xml", xmlString, updateContext, null); } else openedContainer.putDocument(file.getName(), xmlString, updateContext, null); /* System.out.println("Added " + theFile + " to container " + theContainer);*/ xmlDoc.delete(); } txn.commit(); txn.delete(); updateContext.delete(); // XmlException extends DatabaseException, which in turn extends // Exception. // Catching Exception catches them all. } catch (Exception e) { e.printStackTrace(); System.err.println("Error loading files into container " + theContainer); System.err.println(" Message: " + e.getMessage()); // In the event of an error, we abort the operation // The database is left in the same state as it was in before // we started this operation. if (txn != null) { txn.abort(); txn.delete(); } throw e; } } /* Set up the environment -- taking from dbXML example*/ private static Environment createEnv(File home) throws DatabaseException, FileNotFoundException { EnvironmentConfig config = new EnvironmentConfig(); config.setInitializeCache(true); config.setCacheSize(100 * 1024 * 1024); config.setAllowCreate(true); config.setInitializeCache(true); config.setTransactional(true); config.setInitializeLocking(true); config.setInitializeLogging(true); //config.setMutexIncrement(100); return new Environment(home, config); } // Utility function to clean up objects, exceptions or not // XmlContainer and XmlManager objects must be closed. -- taking from dbXML example public static void cleanup() { try { if (openedContainer != null) openedContainer.delete(); if (theMgr != null) theMgr.delete(); } catch (Exception e) { // ignore exceptions on close } } // Delete files in dbEnv --Jiemin public static void cleanEnv() { try { if (path2DbEnv.exists()) { File[] files = path2DbEnv.listFiles(); for (int i = 0; i < files.length; i++) { files[i].delete(); } } } catch (Exception e) { } } // Delete container --Jiemin public static void delContainer() { try { if (path2DbEnv.exists()) { File containerPath = new File(path2DbEnv + "/" + theContainer); containerPath.delete(); } } catch (Exception e) { } } }