public void execute(Connection connection) throws XMLDBException, EXistException { Collection collection = connection.getCollection("/db"); XQueryService service = (XQueryService) collection.getService("XQueryService", "1.0"); service.declareVariable("filename", ""); service.declareVariable("count", "0"); String query = IMPORT + xqueryContent; System.out.println("query: " + query); CompiledExpression compiled = service.compile(query); try { for (int i = 0; i < count; i++) { File nextFile = new File(directory, prefix + i + ".xml"); service.declareVariable("filename", nextFile.getName()); service.declareVariable("count", new IntegerValue(i)); ResourceSet results = service.execute(compiled); Writer out = new OutputStreamWriter(new FileOutputStream(nextFile), "UTF-8"); for (ResourceIterator iter = results.getIterator(); iter.hasMoreResources(); ) { Resource r = iter.nextResource(); out.write(r.getContent().toString()); } out.close(); } } catch (IOException e) { throw new EXistException("exception while storing generated data: " + e.getMessage(), e); } }
private String execQuery(String query) throws XMLDBException { XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0"); service.setProperty("indent", "no"); ResourceSet result = service.query(query); assertEquals(result.getSize(), 1); return result.getResource(0).getContent().toString(); }
/** arguments: lines , columns, XQuery string */ public static void main(String[] args) { 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"); try { if (xquery != "") { ResourceSet result = tester.querySingleLine(xquery, "testQueryBigDocument"); System.out.println("result size: " + result.getSize()); } shutdown(tester.root); } catch (XMLDBException e) { fail(e.getMessage()); } }
/** * This function makes the call to the XML database and retrieves all the file metadata for use in * ECGridToolkit. The data is first retrieved in a series of XML blocks, and then they are * converted into StudyEntry Objects and returned to the user * * @param userID - The ID of the user. This is used to check which files the user has submitted * and thus has access to * @return The list of StudyEntry Objects taken from the file metadata in the XML database */ public ArrayList<StudyEntry> getEntries(String userID) { ArrayList<StudyEntry> tempList = new ArrayList<StudyEntry>(); try { // create first query to get the entire studyEntry block, the collection() XQuery function // does this across // all documents in the XML Collection // The goal of this query is to find all the data we need for the StudyEntry object based on // which user submitted them. For all the documents searched, the query looks to see // if any files for that subject's ECG repository were submitted by that user. If any files // were submitted, retrieve their metadata contained in the studyEntry block. String sQuery = studyBuilder.defaultFor() + studyBuilder.defaultWhere(userID) + studyBuilder.defaultOrderBy() + studyBuilder.defaultReturn(); System.out.println("Query to be executed = " + sQuery); ResourceSet resultSet = executeQuery(sQuery); ResourceIterator iter = resultSet.getIterator(); Resource selection = null; Resource fileSelection = null; int listIndex = 0; int subjectCount = 1; while (iter.hasMoreResources()) { selection = iter.nextResource(); String studyEntryResult = (selection.getContent()).toString(); // Now we will create an XStream object and then put that into our StudyEntry objects // the StudyEntry objects are de-serialized versions of the studyEntry blocks. XStream xmlStream = new XStream(); xmlStream.alias("studyEntry", StudyEntry.class); xmlStream.alias("recordDetails", RecordDetails.class); xmlStream.alias("fileDetails", FileDetails.class); xmlStream.addImplicitCollection(RecordDetails.class, "fileDetails"); StudyEntry newStudy = (StudyEntry) xmlStream.fromXML(studyEntryResult); // System.out.println(newStudy); // Add it to the return array tempList.add(newStudy); } } catch (Exception ex) { System.out.println( "StudyEntryUtility.getEntries(): AN EXCEPTION HAS BEEN CAUGHT! IF A LIST IS RETURNED, IT WILL BE EMPTY!!!"); ex.printStackTrace(); } return tempList; }
/** * 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() { try { XMLReader dataSource = new TabularXMLReader(); storeSAXEvents(dataSource); ResourceSet result = querySingleLine("", "testQueryBigDocument"); assertEquals(1, result.getSize()); } catch (XMLDBException e) { fail(e.getMessage()); } }
@Before public void clearGrammarCache() { LOG.info("Clearing grammar cache"); ResourceSet results = null; try { results = executeQuery("validation:clear-grammar-cache()"); String r = (String) results.getResource(0).getContent(); System.out.println(r); } catch (Exception e) { LOG.error(e); e.printStackTrace(); fail(e.getMessage()); } }
public ArrayList<String> queryStudyTypes(String userID, String dataName) { System.out.println("In function queryStudyTypes"); ArrayList<String> tempList = new ArrayList<String>(); try { // create first query to get the entire studyEntry block, the collection() XQuery function // does this across // all documents in the XML Collection // The goal of this query is to find all the data we need for the StudyEntry object based on // which user submitted them. For all the documents searched, the query looks to see // if any files for that subject's ECG repository were submitted by that user. If any files // were submitted, retrieve their metadata contained in the studyEntry block. System.out.println("queryDatatypes:"); String sQuery = studyBuilder.forStudyEntry() + studyBuilder.customNameBracket(dataName, EnumStudyTreeNode.DATATYPE) + studyBuilder.userDefinedWhere(userID) + studyBuilder.customOrderBySubjectID() + studyBuilder.returnTreeNodeDistinct(EnumStudyTreeNode.STUDY); System.out.println("Query to be executed = " + sQuery); ResourceSet resultSet = executeQuery(sQuery); ResourceIterator iter = resultSet.getIterator(); Resource selection = null; tempList = this.checkForDuplicates(iter, selection); for (String listing : tempList) { int i = 1; System.out.println("Entry " + i + " = " + listing); i++; } } catch (Exception ex) { System.out.println( "ExistMainDatabase.queryStudies(): AN EXCEPTION HAS BEEN CAUGHT! IF A LIST IS RETURNED, IT WILL BE EMPTY!!!"); ex.printStackTrace(); } return tempList; }
private void executeAndEvaluate(String query, String expectedValue) { LOG.info("Query=" + query); LOG.info("ExpectedValue=" + query); try { ResourceSet results = executeQuery(query); assertEquals(1, results.getSize()); String r = (String) results.getResource(0).getContent(); LOG.info(r); assertXpathEvaluatesTo(expectedValue, "//status/text()", r); } catch (Exception ex) { LOG.error(ex); fail(ex.getMessage()); } }
/** * Main method of the example class. * * @param args (ignored) command-line arguments * @throws Exception exception */ public static void main(final String[] args) throws Exception { System.out.println("=== XMLDBQuery ===\n"); System.out.println("* Run query via XML:DB:"); // Collection instance Collection coll = null; try { // Register the database Class<?> c = Class.forName(DRIVER); Database db = (Database) c.newInstance(); DatabaseManager.registerDatabase(db); // Receive the database coll = DatabaseManager.getCollection(DBNAME); // Receive the XPath query service XPathQueryService service = (XPathQueryService) coll.getService("XPathQueryService", "1.0"); // Execute the query and receives all results ResourceSet set = service.query(QUERY); // Create a result iterator ResourceIterator iter = set.getIterator(); // Loop through all result items while (iter.hasMoreResources()) { // Receive the next results Resource res = iter.nextResource(); // Write the result to the console System.out.println(res.getContent()); } } catch (final XMLDBException ex) { // Handle exceptions System.err.println("XML:DB Exception occured " + ex.errorCode); } finally { // Close the collection if (coll != null) coll.close(); } }
private void insertTags() throws Exception { XUpdateQueryService service = (XUpdateQueryService) testCol.getService("XUpdateQueryService", "1.0"); XPathQueryService xquery = (XPathQueryService) testCol.getService("XPathQueryService", "1.0"); String[] tagsWritten = new String[RUNS]; for (int i = 0; i < RUNS; i++) { String tag = tags[i]; String parent; if (i > 0 && rand.nextInt(100) < 70) { parent = "//" + tagsWritten[rand.nextInt(i) / 2]; } else parent = "/root"; String xupdate = "<xupdate:modifications version=\"1.0\" xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" + "<xupdate:append select=\"" + parent + "\">" + "<xupdate:element name=\"" + tag + "\"/>" + "</xupdate:append>" + "</xupdate:modifications>"; long mods = service.updateResource("test.xml", xupdate); System.out.println("Inserted " + tag + ": " + mods + " ; parent = " + parent); assertEquals(mods, 1); tagsWritten[i] = tag; String query = "//" + tagsWritten[rand.nextInt(i + 1)]; ResourceSet result = xquery.query(query); assertEquals(result.getSize(), 1); System.out.println(result.getResource(0).getContent()); } XMLResource res = (XMLResource) testCol.getResource("test.xml"); assertNotNull(res); System.out.println(res.getContent()); }
/** * This method performs an XPath query to the database and returns the results as a Vector of * Strings. * * @param collection The name of the collection to look for the document. * @param query A string containing an XPath expression which shall act as a query against the * database. * @param username The identifier of the user calling the method used for authentication. * @param password The password of the user calling the method used for authentication. * @return A Vector containing the answers to the query as Strings. */ public Vector query(String collection, String query, String username, String password) { Vector response = new Vector(); try { Class cl = Class.forName(_driver); Database database = (Database) cl.newInstance(); DatabaseManager.registerDatabase(database); Collection col = DatabaseManager.getCollection(_URI + collection, username, password); XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0"); service.setProperty("indent", "yes"); ResourceSet result = service.query(query); ResourceIterator i = result.getIterator(); while (i.hasMoreResources()) { Resource r = i.nextResource(); response.add((String) r.getContent()); } } catch (Exception e) { e.printStackTrace(); } return response; }
public static void main(String args[]) throws Exception { String driver = "org.exist.xmldb.DatabaseImpl"; Class cl = Class.forName(driver); Database database = (Database) cl.newInstance(); DatabaseManager.registerDatabase(database); Collection col = DatabaseManager.getCollection("xmldb:exist:///db", "admin", ""); XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0"); service.setProperty("indent", "yes"); ResourceSet result = service.query( "for $s in //intervention/(speaker|writer)/affiliation[@EPparty ='PSE'] return data($s/../../(speech|writing)/@ref)"); ResourceIterator i = result.getIterator(); while (i.hasMoreResources()) { Resource r = i.nextResource(); System.out.println((String) r.getContent()); } // shut down the database DatabaseInstanceManager manager = (DatabaseInstanceManager) col.getService("DatabaseInstanceManager", "1.0"); manager.shutdown(); }
private void fetchDb() throws Exception { XPathQueryService xquery = (XPathQueryService) testCol.getService("XPathQueryService", "1.0"); ResourceSet result = xquery.query( "for $n in collection('" + XmldbURI.ROOT_COLLECTION + "/test')//* return local-name($n)"); for (int i = 0; i < result.getSize(); i++) { Resource r = result.getResource(i); String tag = r.getContent().toString(); System.out.println("Retrieving " + tag); ResourceSet result2 = xquery.query("//" + tag); assertEquals(result2.getSize(), 1); System.out.println(result2.getResource(0).getContent()); } }
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (request.getCharacterEncoding() == null) try { request.setCharacterEncoding("UTF-8"); } catch (final IllegalStateException e) { } // Try to find the XQuery final String qpath = getServletContext().getRealPath(query); final File f = new File(qpath); if (!(f.canRead() && f.isFile())) { throw new ServletException("Cannot read XQuery source from " + f.getAbsolutePath()); } final FileSource source = new FileSource(f, "UTF-8", true); try { // Prepare and execute the XQuery final Collection collection = DatabaseManager.getCollection(collectionURI.toString(), user, password); final XQueryService service = (XQueryService) collection.getService("XQueryService", "1.0"); if (!((CollectionImpl) collection).isRemoteCollection()) { service.declareVariable( RequestModule.PREFIX + ":request", new HttpRequestWrapper(request, "UTF-8", "UTF-8")); service.declareVariable( ResponseModule.PREFIX + ":response", new HttpResponseWrapper(response)); service.declareVariable( SessionModule.PREFIX + ":session", new HttpSessionWrapper(request.getSession(false))); } final ResourceSet result = service.execute(source); String redirectTo = null; String servletName = null; String path = null; RequestWrapper modifiedRequest = null; // parse the query result element if (result.getSize() == 1) { final XMLResource resource = (XMLResource) result.getResource(0); Node node = resource.getContentAsDOM(); if (node.getNodeType() == Node.DOCUMENT_NODE) { node = ((Document) node).getDocumentElement(); } if (node.getNodeType() != Node.ELEMENT_NODE) { response.sendError( HttpServletResponse.SC_BAD_REQUEST, "Redirect XQuery should return an XML element. Received: " + resource.getContent()); return; } Element elem = (Element) node; if (!(Namespaces.EXIST_NS.equals(elem.getNamespaceURI()) && "dispatch".equals(elem.getLocalName()))) { response.sendError( HttpServletResponse.SC_BAD_REQUEST, "Redirect XQuery should return an element <exist:dispatch>. Received: " + resource.getContent()); return; } if (elem.hasAttribute("path")) { path = elem.getAttribute("path"); } else if (elem.hasAttribute("servlet-name")) { servletName = elem.getAttribute("servlet-name"); } else if (elem.hasAttribute("redirect")) { redirectTo = elem.getAttribute("redirect"); } else { response.sendError( HttpServletResponse.SC_BAD_REQUEST, "Element <exist:dispatch> should either provide an attribute 'path' or 'servlet-name'. Received: " + resource.getContent()); return; } // Check for add-parameter elements etc. if (elem.hasChildNodes()) { node = elem.getFirstChild(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE && Namespaces.EXIST_NS.equals(node.getNamespaceURI())) { elem = (Element) node; if ("add-parameter".equals(elem.getLocalName())) { if (modifiedRequest == null) { modifiedRequest = new RequestWrapper(request); } modifiedRequest.addParameter(elem.getAttribute("name"), elem.getAttribute("value")); } } node = node.getNextSibling(); } } } if (redirectTo != null) { // directly redirect to the specified URI response.sendRedirect(redirectTo); return; } // Get a RequestDispatcher, either from the servlet context or the request RequestDispatcher dispatcher; if (servletName != null && servletName.length() > 0) { dispatcher = getServletContext().getNamedDispatcher(servletName); } else { LOG.debug("Dispatching to " + path); dispatcher = getServletContext().getRequestDispatcher(path); if (dispatcher == null) { dispatcher = request.getRequestDispatcher(path); } } if (dispatcher == null) { response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not create a request dispatcher. Giving up."); return; } if (modifiedRequest != null) { request = modifiedRequest; } // store the original request URI to org.exist.forward.request-uri request.setAttribute("org.exist.forward.request-uri", request.getRequestURI()); request.setAttribute("org.exist.forward.servlet-path", request.getServletPath()); // finally, execute the forward dispatcher.forward(request, response); } catch (final XMLDBException e) { throw new ServletException( "An error occurred while initializing RedirectorServlet: " + e.getMessage(), e); } }
public final void testXPathQueryServiceSuccess() throws Exception { XPathQueryService s = (XPathQueryService) this.getService("XPathQueryService"); ResourceSet rs1 = s.query("//author/@id"); assertTrue("Expected a non empty ResourceSet.", rs1.getSize() > 0); ResourceSet rs2 = s.query("//foo/@bar"); assertTrue("Expected a non empty ResourceSet.", rs2.getSize() > 0); int id1[] = new int[(int) rs1.getSize()]; for (int i = 0; i < id1.length; i++) { Resource res = rs1.getResource(i); assertTrue( "Expected instance of VirtualResource and not " + res.getClass(), res instanceof VirtualResource); Document doc = (Document) ((VirtualResource) res).getContentAsDOM(); Element elem = doc.getDocumentElement(); assertEquals( "Expected " + NEXDEngineI.QNAME_QUERY_RESULT + " but it is " + elem.getTagName(), NEXDEngineI.QNAME_QUERY_RESULT, elem.getTagName()); NamedNodeMap nnm = elem.getAttributes(); String value = elem.getAttribute("id"); id1[i] = Integer.parseInt(value); } int id2[] = new int[(int) rs2.getSize()]; for (int i = 0; i < id2.length; i++) { Resource res = rs2.getResource(i); assertTrue( "Expected instance of VirtualResource and not " + res.getClass(), res instanceof VirtualResource); Document doc = (Document) ((VirtualResource) res).getContentAsDOM(); Element elem = doc.getDocumentElement(); assertEquals( "Expected " + NEXDEngineI.QNAME_QUERY_RESULT + " but it is " + elem.getTagName(), NEXDEngineI.QNAME_QUERY_RESULT, elem.getTagName()); String value = elem.getAttribute("bar"); id2[i] = Integer.parseInt(value); } for (int i = 0; i < id1.length; i++) { int id = id1[i]; boolean in1 = false; for (int j = 0; j < id2.length; j++) { if (id == id2[j]) { in1 = true; } boolean in2 = false; for (int k = 0; k < id1.length; k++) { if (id2[j] == id1[k]) { in2 = true; } } assertTrue("Expected id=" + id2[j] + " is in the first array.", in2); } assertTrue("Expected id=" + id + " is in the second array.", in1); } }
/** * This function makes the call to the XML database and retrieves all the file metadata for use in * ECGridToolkit. The data is first retrieved in a series of XML blocks, and then they are * converted into StudyEntry Objects and returned to the user * * @param userID - The ID of the user. This is used to check which files the user has submitted * and thus has access to * @param studyID - The ID of the study selected to search * @param datatype - The type of data that will be observed * @return The list of StudyEntry Objects taken from the file metadata in the XML database */ public ArrayList<StudyEntry> getEntries(String userID, String studyID, String datatype) { System.out.println("In function getEntries"); ArrayList<StudyEntry> tempList = new ArrayList<StudyEntry>(); try { // create first query to get the entire studyEntry block, the collection() XQuery function // does this across // all documents in the XML Collection // The goal of this query is to find all the data we need for the StudyEntry object based on // which user submitted them. For all the documents searched, the query looks to see // if any files for that subject's ECG repository were submitted by that user. If any files // were submitted, retrieve their metadata contained in the studyEntry block. // CompiledExpression query = subjectQuery.compile("for $x in collection('" + // allConstants.getDBCollection() + "')//record where collection('" + // allConstants.getDBCollection() + "')//record/studyEntry/submitterID=\"" + userID + "\" // order by $x/studyEntry/subjectID return $x/studyEntry"); // *** // 02/25/2013 - Brandon Benitez: Commented this out temporarily in case the query builder // version of this failed. // *** // String sQuery = "for $x in collection('" + dbHandle.getMainCollection() + // "')//record/studyEntry[studyID=\"" + studyID + "\"] where $x/submitterID=\"" + userID + "\" // return $x[datatype=\"" + datatype + "\"]"; String sQuery = studyBuilder.forStudyEntry() + // studyBuilder.customNameBracket(studyID, EnumStudyTreeNode.STUDY) + studyBuilder.whereUser2(userID) + studyBuilder.andWhereStudy(studyID) + studyBuilder.customOrderBySubjectID() + studyBuilder.returnX() + studyBuilder.customNameBracket(datatype, EnumStudyTreeNode.DATATYPE); System.out.println("Query to be executed = " + sQuery); ResourceSet resultSet = executeQuery(sQuery); ResourceIterator iter = resultSet.getIterator(); Resource selection = null; Resource fileSelection = null; int listIndex = 0; int subjectCount = 1; while (iter.hasMoreResources()) { selection = iter.nextResource(); String studyEntryResult = (selection.getContent()).toString(); // Now we will create an XStream object and then put that into our StudyEntry objects // the StudyEntry objects are de-serialized versions of the studyEntry blocks. XStream xmlStream = new XStream(); xmlStream.alias("studyEntry", StudyEntry.class); xmlStream.alias("recordDetails", RecordDetails.class); xmlStream.alias("fileDetails", FileDetails.class); xmlStream.addImplicitCollection(RecordDetails.class, "fileDetails"); StudyEntry newStudy = (StudyEntry) xmlStream.fromXML(studyEntryResult); // System.out.println(newStudy); // Add it to the return array tempList.add(newStudy); } } catch (Exception ex) { System.out.println( "StudyEntryUtility.getEntries(): AN EXCEPTION HAS BEEN CAUGHT! IF A LIST IS RETURNED, IT WILL BE EMPTY!!!"); ex.printStackTrace(); } return tempList; }