📄 client.java
字号:
SAXParser parser = parserFactory.newSAXParser(); ParserAdapter adapter = new ParserAdapter( parser.getParser() ); adapter.setContentHandler( container.storeSAX() ); adapter.parse( filename ); System.out.println( "SAX store: parse+store time: " + (System.currentTimeMillis() - start) + " ms" ); start = System.currentTimeMillis(); tx.commit(); System.out.println( "SAX store: commit time: " + (System.currentTimeMillis() - start) + " ms" ); } catch (Exception e) { tx.rollback(); throw e; } } protected static void xupdateQuery( String filename ) throws Exception { String prefix = "XUpdate query: "; // all files and names are hard wired yet filename = "address.xml"; qstring = "<lexus:updates version=\"1.0\" xmlns:lexus=\"http://www.xmldb.org/xupdate\"><lexus:remove select=\"//*[@name='Lars']\"/></lexus:updates>"; /* */ System.out.println( prefix + "filename=" + filename + ", qstring=" + qstring ); // delete the document if it's already there XMLContainer container = XMLContainer.forName( db, filename ); if (container != null) { System.out.println( prefix + "document already there; deleting..." ); container.delete(); } // store the document saxStore( filename ); // show the original document // dump( filename, null ); printResult = true; print( filename ); System.out.println( prefix + "dump ready." ); // do the query container = XMLContainer.forName( db, filename ); System.out.println( prefix + "forName() ready." ); OzoneXUpdateQuery query = container.newXUpdateQuery(); query.setQString( qstring ); System.out.println( prefix + "query init ready." ); long start = System.currentTimeMillis(); query.execute( ); System.out.println( prefix + "update time: " + (System.currentTimeMillis() - start) + " ms" ); // show modified file print( filename ); System.out.println( prefix + "2nd dump ready." ); } protected static void xpathQuery( String filename ) throws Exception { System.out.println( "XPath query: filename=" + filename + ", qstring=" + qstring ); XMLContainer container = XMLContainer.forName( db, filename ); if (container == null) { System.out.println( "No such document." ); return; } // String path = container.xpathForNode( container.getPDocument().getFirstChild() );// System.out.println( "XPath query: xpathForNode=" + path ); long start = System.currentTimeMillis(); OzoneXPathQuery query = container.newXPathQuery(); query.setQString( qstring ); XObject result = query.execute(); System.out.println( "XPath query: query time: " + (System.currentTimeMillis() - start) + " ms" ); if (printResult) { if (result == null) { System.out.println( "XPath query: result: null" ); } else { System.out.print( "XPATH query: result: " ); // cast the query result to switch (result.getType()) { case XObject.CLASS_BOOLEAN: System.out.println( "(Boolean): " + result.bool() ); break; case XObject.CLASS_NUMBER: System.out.println( "(Number): " + result.num() ); break; case XObject.CLASS_STRING: System.out.println( "(String): " + result.str() ); break; case XObject.CLASS_RTREEFRAG: System.out.println( "(DocumentFragment): -" ); break; case XObject.CLASS_NODESET: NodeList nodeList = result.nodeset(); System.out.println( "(NodeList): " + nodeList.getLength() + " Entries" ); DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); for (int i = 0; i < nodeList.getLength(); i++) { System.out.print( i + 1 + " Entry: " ); ExternalTransaction tx = db.newTransaction(); tx.begin(); // extract the result node from the persistent document first start = System.currentTimeMillis(); Document doc = documentBuilder.newDocument(); Node extractedNode = container.extractDOM( doc, nodeList.item( i ), null, depth ); System.out.println( "[extract time: " + (System.currentTimeMillis() - start) + " ms]" ); tx.commit(); printNodeTree( extractedNode, new StringBuffer( " " ) ); } break; default: System.out.println( "(Unknown): -" ); break; } } } } protected static void dump( String filename, String targetFile ) throws Exception { System.out.println( "Dump: filename=" + filename + " -> target=" + (targetFile == null ? "#stdout" : targetFile) ); XMLContainer container = XMLContainer.forName( db, filename ); if (container == null) { System.out.println( "No such document." ); return; } ExternalTransaction tx = db.newTransaction(); tx.begin(); try { PrintWriter writer = new PrintWriter( System.out, false ); if (targetFile != null) { writer = new PrintWriter( new FileOutputStream( targetFile ), false ); } XMLSerializer serializer = new XMLSerializer( writer, new OutputFormat( "xml", "UTF-8", true ) ); long start = System.currentTimeMillis(); container.extractSAX( serializer.asContentHandler(), null, depth ); System.out.println( "Dump: extract/serialize time: " + (System.currentTimeMillis() - start) + " ms" ); writer.close(); tx.commit(); } catch (Exception e) { tx.rollback(); throw e; } } protected static void print( String filename ) throws Exception { System.out.println( "Print: filename=" + filename ); XMLContainer container = XMLContainer.forName( db, filename ); if (container == null) { System.out.println( "No such document." ); return; } DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); Document doc = documentBuilder.newDocument(); Node resultNode = null; ExternalTransaction tx = db.newTransaction(); tx.begin(); try { long start = System.currentTimeMillis(); resultNode = container.extractDOM( doc, (Node)null /*container.getPDocument().getFirstChild()*/, null, depth ); System.out.println( "Print: with depth: " + depth ); System.out.println( "Print: extract DOM time: " + (System.currentTimeMillis() - start) + " ms" ); tx.commit(); } catch (Exception e) { tx.rollback(); throw e; } if (printResult) { System.out.println( "Print: result: " ); printNodeTree( resultNode, new StringBuffer( " " ) ); } } /** * Prints a short hierarchical summary of the specified node and all sub * nodes. */ protected static void printNodeTree( Node node, StringBuffer indent ) { System.out.print( indent.toString() ); System.out.println( "Node name [" + node.getNodeName() + "] value [" + node.getNodeValue() + "]" ); Node _child = node.getFirstChild(); for (; _child != null; _child = _child.getNextSibling()) { indent.append( " " ); printNodeTree( _child, indent ); indent.setLength( indent.length() - 3 ); } } protected static void rename( String filename ) throws Exception { System.out.println( "Rename: filename=" + filename ); XMLContainer container = XMLContainer.forName( db, filename ); if (container == null) { System.out.println( "No such document." ); return; } if (newName == null) { System.out.println( "No target name specified." ); return; } container.setName( newName ); } protected static void delete( String filename ) throws Exception { System.out.println( "Delete: filename=" + filename ); XMLContainer container = XMLContainer.forName( db, filename ); if (container == null) { System.out.println( "No such document." ); return; } container.delete(); } static void printUsage() { System.err.println( "Usage: ojvm Client <command> [options] file|document" ); System.err.println( "Commands: " + COMMAND_STORE_SAX + " " + COMMAND_STORE_DOM + " " + COMMAND_XPATH + " " + COMMAND_XUPDATE + " " + COMMAND_DUMP + " " + COMMAND_PRINT + " " + COMMAND_RENAME + " " + COMMAND_DELETE ); System.err.println( "Options:" ); System.err.println( " -q<qstring> The XPath query string." ); System.err.println( " -p Print the results of the operation." ); System.err.println( " -f<filename> Write the result to this file instead of stdout." ); System.err.println( " -n<name> The new name of the specified document." ); System.err.println( " -u<dbURL> The URL of the database. (default: " + dbURL + ")" ); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -