📄 client.java
字号:
// $Id: Client.java,v 1.2 2003/11/23 14:55:26 per_nyfelt Exp $import java.util.Vector;import java.io.PrintWriter;import java.io.FileOutputStream;import java.io.InputStreamReader;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import javax.xml.parsers.*;import org.xml.sax.helpers.*;import org.ozoneDB.ExternalDatabase;import org.ozoneDB.ExternalTransaction;import org.ozoneDB.xml.util.*;import org.infozone.tools.xml.queries.*;import org.apache.xml.serialize.*;/** * Simple XML sample application that demonstrates the use of the ozone/XML API. * Each command (store, domstore, print, etc.) is handled by a corresponding * method. * * @version $Revision: 1.2 $ $Date: 2003/11/23 14:55:26 $ * @author <a href="http://www.smb-tec.com">SMB</a> * @see org.ozoneDB.xml.util.XMLContainer */public class Client { final static String COMMAND_STORE_SAX = "store"; final static String COMMAND_STORE_DOM = "domstore"; final static String COMMAND_XPATH = "xpath"; final static String COMMAND_XUPDATE = "xupdate"; final static String COMMAND_PRINT = "print"; final static String COMMAND_DUMP = "dump"; final static String COMMAND_RENAME = "rename"; final static String COMMAND_DELETE = "delete"; /** * Create a Xerces specific DocumentBuilderFactory. */ static DocumentBuilderFactory builderFactory = new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl(); /** * Create a Xerces specific SAXParserFactory. */ static SAXParserFactory parserFactory = new org.apache.xerces.jaxp.SAXParserFactoryImpl(); static String dbURL = "ozonedb:remote://localhost:3333"; static ExternalDatabase db; static Vector filenames; static String targetFile; static String qstring; static boolean printResult; static int depth = -1; static String newName; static boolean waitOnExit = false; public static void main( String[] args ) throws Exception { if (args.length == 0) { printUsage(); System.exit( 1 ); } String command = args[0]; filenames = new Vector(); qstring = ""; printResult = false; targetFile = null; // check command line arguments for (int i = 1; i < args.length; i++) { String arg = args[i]; if (!arg.startsWith( "-" )) { filenames.addElement( arg ); } else if (arg.startsWith( "-q" )) { qstring = arg.substring( 2 ); System.out.println ("query string: " + qstring); } else if (arg.startsWith( "-n" )) { newName = arg.substring( 2 ); System.out.println ("new name: " + newName); } else if (arg.startsWith( "-f" )) { targetFile = arg.substring( 2 ); } else if (arg.equals( "-p" )) { printResult = true; System.out.println ("print: " + printResult); } else if (arg.equals( "-w" )) { waitOnExit = true; System.out.println ("waitOnExit: " + waitOnExit); } else if (arg.startsWith( "-u" )) { dbURL = arg.substring( 2 ); System.out.println ("dbURL: " + dbURL); } else if (arg.startsWith( "-d" )) { try { depth = Integer.parseInt( arg.substring( 2 ) ); } catch (NumberFormatException e) { System.err.println( "argument -d requires an integer value" ); throw e; } } } if (filenames.size() == 0) { printUsage(); System.exit( 1 ); } db = ExternalDatabase.openDatabase( dbURL ); db.reloadClasses(); try { // execute corresponding method on all given documents/files for (int i = 0; i < filenames.size(); i++) { if (command.equals( COMMAND_STORE_SAX )) { saxStore( (String)filenames.elementAt( i ) ); } else if (command.equals( COMMAND_STORE_DOM )) { domStore( (String)filenames.elementAt( i ) ); } else if (command.equals( COMMAND_XPATH )) { xpathQuery( (String)filenames.elementAt( i ) ); } else if (command.equals( COMMAND_XUPDATE )) { xupdateQuery( (String)filenames.elementAt( i ) ); } else if (command.equals( COMMAND_PRINT )) { print( (String)filenames.elementAt( i ) ); } else if (command.equals( COMMAND_DUMP )) { dump( (String)filenames.elementAt( i ), targetFile ); } else if (command.equals( COMMAND_RENAME )) { rename( (String)filenames.elementAt( i ) ); } else if (command.equals( COMMAND_DELETE )) { delete( (String)filenames.elementAt( i ) ); } else { System.out.println( "Unknown command: " + command ); printUsage(); } } } finally { db.close(); } if (waitOnExit) { System.out.println( "press any key..." ); InputStreamReader is = new InputStreamReader( System.in ); while (!is.ready()) { Thread.sleep( 1000 ); } } } protected static void domStore( String filename ) throws Exception { System.out.println( "DOM store: filename=" + filename ); XMLContainer container = XMLContainer.forName( db, filename ); boolean newDoc = true; if (container != null) { System.out.println( "Document already found in Database." ); System.out.print( "Would you like to overwrite the existing Document? [y/N]: " ); int answer = System.in.read(); if (answer == 'y' || answer == 'Y') { System.out.println( "ATTENTION: Existing Document will be overwritten!" ); newDoc = false; } else return; } ExternalTransaction tx = db.newTransaction(); tx.begin(); try { long start; if (newDoc) { start = System.currentTimeMillis(); container = XMLContainer.newContainer( db, filename ); System.out.println( "DOM store: new container time: " + (System.currentTimeMillis() - start) + " ms" ); } start = System.currentTimeMillis(); DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); Document doc = documentBuilder.parse( filename ); // printNodeTree (doc, new StringBuffer (" ")); System.out.println( "DOM store: parse time: " + (System.currentTimeMillis() - start) + " ms" ); start = System.currentTimeMillis(); if (newDoc) container.storeDOM( doc ); else 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; } } protected static void saxStore( String filename ) throws Exception { System.out.println( "SAX store: filename=" + filename ); XMLContainer container = XMLContainer.forName( db, filename ); if (container != null) { System.out.println( "Document already found in Database." ); return; } ExternalTransaction tx = db.newTransaction(); tx.begin(); try { long start = System.currentTimeMillis(); container = XMLContainer.newContainer( db, filename ); System.out.println( "SAX store: new container time: " + (System.currentTimeMillis() - start) + " ms" ); start = System.currentTimeMillis();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -