From: Jean-Marc V. <jm...@us...> - 2004-07-13 15:07:46
|
Update of /cvsroot/exist/eXist-1.0/src/org/exist/xquery/test In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27449/src/org/exist/xquery/test Added Files: TabularXMLReader.java SAXStorageTest.java Log Message: storing SAX events directly in the database --- NEW FILE: TabularXMLReader.java --- /* * Created on 11 juil. 2004 $Id: TabularXMLReader.java,v 1.1 2004/07/13 15:07:37 jmvanel Exp $ */ package org.exist.xquery.test; import java.io.IOException; import org.xml.sax.ContentHandler; import org.xml.sax.DTDHandler; import org.xml.sax.EntityResolver; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.AttributesImpl; /** A test data source producing adjustable tabular data */ class TabularXMLReader implements XMLReader { private int lines = 10; private int columns = 10; TabularXMLReader() {} TabularXMLReader( int lines , int columns) { this.lines = lines; this.columns = columns; } ContentHandler contentHandler; private static final boolean DIFFERENT_TAG_EACH_LINE = false; void writeDocument( ContentHandler xmldb) throws SAXException { xmldb.startDocument(); AttributesImpl attributesImpl = new AttributesImpl(); xmldb.startElement( "", "root", "root", attributesImpl ); for (int i = 0; i < lines; i++) { String line = "line"; if ( DIFFERENT_TAG_EACH_LINE) line += i; xmldb.startElement( "", line, line, attributesImpl ); for (int j = 0; j < columns; j++) { String column = "col" + j; xmldb.startElement( "", column, column, attributesImpl ); char ch[] = new char[20]; column.getChars(0, column.length(), ch, 0); xmldb.characters(ch, 0, column.length() ); xmldb.endElement("", column, column); } xmldb.endElement("", line, line); } xmldb.endElement( "", "root", "root" ); xmldb.endDocument(); } /** ? @see org.xml.sax.XMLReader#parse(java.lang.String) */ public void parse(String systemId) throws IOException, SAXException { writeDocument(contentHandler); } /** ? @see org.xml.sax.XMLReader#getFeature(java.lang.String) */ public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException { return false; } /** ? @see org.xml.sax.XMLReader#setFeature(java.lang.String, boolean) */ public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {} /** ? @see org.xml.sax.XMLReader#getContentHandler() */ public ContentHandler getContentHandler() { return contentHandler; } /** ? @see org.xml.sax.XMLReader#setContentHandler(org.xml.sax.ContentHandler) */ public void setContentHandler(ContentHandler handler) { this.contentHandler = handler; } /** ? @see org.xml.sax.XMLReader#getDTDHandler() */ public DTDHandler getDTDHandler() { return null; } /** ? @see org.xml.sax.XMLReader#setDTDHandler(org.xml.sax.DTDHandler) */ public void setDTDHandler(DTDHandler handler) {} /** ? @see org.xml.sax.XMLReader#getEntityResolver() */ public EntityResolver getEntityResolver() { return null; } /** ? @see org.xml.sax.XMLReader#setEntityResolver(org.xml.sax.EntityResolver) */ public void setEntityResolver(EntityResolver resolver) {} /** ? @see org.xml.sax.XMLReader#getErrorHandler() */ public ErrorHandler getErrorHandler() { return null; } /** ? @see org.xml.sax.XMLReader#setErrorHandler(org.xml.sax.ErrorHandler) */ public void setErrorHandler(ErrorHandler handler) {} /** ? @see org.xml.sax.XMLReader#parse(org.xml.sax.InputSource) */ public void parse(InputSource input) throws IOException, SAXException { writeDocument(contentHandler); } /** ? @see org.xml.sax.XMLReader#getProperty(java.lang.String) */ public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException { return null; } /** ? @see org.xml.sax.XMLReader#setProperty(java.lang.String, java.lang.Object) */ public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {} /** * @return Returns the number of lines. */ public int getLines() { return lines; } /** * @return Returns the number of columns. */ public int getColumns() { return columns; } } --- NEW FILE: SAXStorageTest.java --- /* * Created on 23 juin 2004 $Id: SAXStorageTest.java,v 1.1 2004/07/13 15:07:37 jmvanel Exp $ */ package org.exist.xquery.test; import java.io.File; import java.io.IOException; import junit.framework.TestCase; import org.exist.xmldb.DatabaseInstanceManager; import org.exist.xmldb.LocalCollection; import org.xml.sax.ContentHandler; import org.xml.sax.DTDHandler; import org.xml.sax.EntityResolver; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.AttributesImpl; import org.xmldb.api.DatabaseManager; import org.xmldb.api.base.Collection; import org.xmldb.api.base.Database; import org.xmldb.api.base.ResourceSet; import org.xmldb.api.base.XMLDBException; import org.xmldb.api.modules.CollectionManagementService; import org.xmldb.api.modules.XMLResource; import org.xmldb.api.modules.XPathQueryService; /** This TestCase is for direct storage of SAX events in the database; one has to implement an XMLReader. * It is also a stress test that creates large documents using SAX, use main() for this. * @author jmv */ public class SAXStorageTest extends TestCase { /** */ public SAXStorageTest(String s) { super(s); } private XMLResource doc; private Collection root; private static String FILE_STORED; protected void setUp() { try { // initialize driver Class cl = Class.forName("org.exist.xmldb.DatabaseImpl"); Database database = (Database) cl.newInstance(); database.setProperty("create-database", "true"); DatabaseManager.registerDatabase(database); root = DatabaseManager.getCollection( "xmldb:exist:///db", "admin", null); CollectionManagementService service = (CollectionManagementService) root.getService( "CollectionManagementService", "1.0"); root = service.createCollection("test"); FILE_STORED = "big.xml"; doc = (XMLResource) root.createResource(FILE_STORED, "XMLResource"); } catch (ClassNotFoundException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } catch (XMLDBException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * @param xquery * @param mess * @return TODO * @throws XMLDBException */ private ResourceSet querySingleLine(String xquery, String mess) throws XMLDBException { // query a single line: XPathQueryService service = (XPathQueryService) root.getService( "XPathQueryService", "1.0"); ResourceSet result = null; if ( xquery != "") { // xquery = "/*/*[2]"; System.out.println("Querying \""+xquery+"\" ..." ); long t0 = System.currentTimeMillis(); result = service.queryResource( "big.xml", xquery ); // assertEquals(1, result.getSize()); long t1 = System.currentTimeMillis(); System.out.println("Time for query \""+xquery+"\" on "+ mess + ": " + ( t1-t0) + " ms." ); } return result; } /** Store in the "classical" eXist way: the XMLResource stores an XML string before * storeResource() stores it in the database. * @throws XMLDBException * @throws SAXException */ public void testQueryStoreContentAsSAX() throws XMLDBException, SAXException { ContentHandler databaseInserter = doc.setContentAsSAX(); (new TabularXMLReader()).writeDocument(databaseInserter); root.storeResource(doc); querySingleLine("", "testQueryStoreContentAsSAX"); } /** Store in the new way: the XMLResource stores just a File object before * storeResource() stores the SAX events in the database. * @throws XMLDBException */ public void testQueryBigDocument() throws XMLDBException{ XMLReader dataSource = new TabularXMLReader(); storeSAXEvents(dataSource); ResourceSet result = querySingleLine("", "testQueryBigDocument"); assertEquals(1, result.getSize()); } /** * @param dataSource * @throws XMLDBException */ private void storeSAXEvents(XMLReader dataSource) throws XMLDBException { if ( root instanceof LocalCollection ) { long t0 = System.currentTimeMillis(); LocalCollection coll = (LocalCollection)root; coll.setReader(dataSource); doc.setContent(new File(FILE_STORED)); coll.storeResource(doc); long t1 = System.currentTimeMillis(); System.out.println("Time for storing: " + ( t1-t0) + " ms." ); } } /** arguments: lines , columns, XQuery string */ public static void main(String[] args) throws XMLDBException { String xquery = ""; int lines = 20; int columns = 20; if ( args.length >= 2 ) { lines = Integer.parseInt(args[0]); columns = Integer.parseInt(args[1]); } if ( args.length == 3 ) { xquery = args[2]; } if ( args.length < 2 ) { System.out.println("Taking default values"); } SAXStorageTest tester = new SAXStorageTest(null); tester.setUp(); XMLReader dataSource = new TabularXMLReader( lines , columns); tester.storeSAXEvents(dataSource); System.out.println("Stored tabular data, " +lines+" lines, "+columns+" columns"); if ( xquery != "" ) { ResourceSet result = tester.querySingleLine(xquery, "testQueryBigDocument" ); System.out.println("result size: " + result.getSize() ); } shutdown( tester.root ); } private static void shutdown(Collection collection) throws XMLDBException { // shutdown the database gracefully DatabaseInstanceManager manager = (DatabaseInstanceManager) collection.getService("DatabaseInstanceManager", "1.0"); manager.shutdown(); } } |