Permalink
Browse files

[MOD] Minor refactorings.

  • Loading branch information...
1 parent 49ac257 commit 446f6b0c2ae4711f8dc53afdfcaf07dcd07fe301 @JohannesLichtenberger JohannesLichtenberger committed Mar 29, 2013
@@ -1,71 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-
- Copyright (c) 2011, University of Konstanz, Distributed Systems Group
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- * Neither the name of the University of Konstanz nor the
- names of its contributors may be used to endorse or promote products
- derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
--->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.github.johanneslichtenberger.sirix</groupId>
- <artifactId>sirix-parent</artifactId>
- <version>0.1.3-SNAPSHOT</version>
- <relativePath>../../pom.xml</relativePath>
- </parent>
-
- <artifactId>sirix-example</artifactId>
- <packaging>jar</packaging>
- <name>sirix-example</name>
- <version>0.1.2-SNAPSHOT</version>
- <description>Sirix example usages.</description>
- <repositories>
- <repository>
- <id>javamaven2</id>
- <name>Repository for Maven2</name>
- <url>http://download.java.net/maven/2</url>
- </repository>
- </repositories>
-
- <dependencies>
- <dependency>
- <groupId>com.github.johanneslichtenberger.sirix</groupId>
- <artifactId>sirix-core</artifactId>
- <version>0.1.2-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>com.github.johanneslichtenberger.sirix</groupId>
- <artifactId>sirix-core</artifactId>
- <version>0.1.2-SNAPSHOT</version>
- <type>test-jar</type>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.github.johanneslichtenberger.sirix</groupId>
- <artifactId>sirix-xquery</artifactId>
- <version>0.1.0</version>
- </dependency>
- </dependencies>
-</project>
@@ -1,295 +0,0 @@
-package org.sirix.examples;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.PrintStream;
-import java.util.Date;
-import java.util.Random;
-
-import org.brackit.xquery.QueryContext;
-import org.brackit.xquery.QueryException;
-import org.brackit.xquery.XQuery;
-import org.sirix.access.Databases;
-import org.sirix.api.Database;
-import org.sirix.exception.SirixException;
-import org.sirix.xquery.node.DBStore;
-
-/**
- * A few examples (some taken from the official brackit examples). Usually you
- * would use a logger for all output!
- *
- * @author Johannes Lichtenberger
- * @author Sebastian Bächle
- *
- */
-public final class XQueryUsage {
-
- /** User home directory. */
- private static final String USER_HOME = System.getProperty("user.home");
-
- /** Storage for databases: Sirix data in home directory. */
- private static final File LOCATION = new File(USER_HOME, "sirix-data");
-
- /** Severity used to build a random sample document. */
- enum Severity {
- low, high, critical
- };
-
- /**
- * Main method.
- *
- * @param args
- * not used
- */
- public static void main(final String[] args) throws SirixException {
- try {
- loadDocumentAndQuery();
- System.out.println();
- loadDocumentAndUpdate();
- System.out.println();
- loadCollectionAndQuery();
- System.out.println();
- loadDocumentAndQueryTemporal();
- } catch (IOException e) {
- System.err.print("I/O error: ");
- System.err.println(e.getMessage());
- } catch (QueryException e) {
- System.err.print("XQuery error ");
- System.err.println(e.getMessage());
- }
- }
-
- /**
- * Load a document and query it.
- */
- private static void loadDocumentAndQuery() throws QueryException,
- IOException, SirixException {
- final File doc = new File(new StringBuilder("src").append(File.separator)
- .append("main").append(File.separator).append("resources")
- .append(File.separator).append("test.xml").toString());
-
- // Initialize query context and store.
- final DBStore store = DBStore.newBuilder().build();
- QueryContext ctx = new QueryContext(store);
-
- // Use XQuery to load sample document into store.
- System.out.println("Loading document:");
- String xq1 = String.format("bit:load('mydoc.xml', '%s')", doc);
- System.out.println(xq1);
- new XQuery(xq1).evaluate(ctx);
-
- try (final Database database = Databases.openDatabase(new File(
- new StringBuilder(3).append(LOCATION).append(File.separator)
- .append("mydoc.xml").toString()))) {
- // Reuse store and query loaded document.
- final QueryContext ctx2 = new QueryContext(store);
- System.out.println();
- System.out.println("Query loaded document:");
- final String xq2 = "doc('mydoc.xml')/nachrichten/nachricht[betreff/text()='sommer' or betreff/text()='strand' or text/text()='sommer' or text/text()='strand']";
- System.out.println(xq2);
- XQuery query = new XQuery(xq2);
- query.prettyPrint().serialize(ctx2, System.out);
- }
-
- System.out.println();
- store.close();
- }
-
- /**
- * Load a document and update it.
- */
- private static void loadDocumentAndUpdate() throws QueryException,
- IOException {
- // Prepare sample document.
- final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
- final File doc = generateSampleDoc(tmpDir, "sample");
- doc.deleteOnExit();
-
- // Initialize query context and store.
- try (final DBStore store = DBStore.newBuilder().isUpdatable().build()) {
- final QueryContext ctx = new QueryContext(store);
-
- // Use XQuery to load sample document into store.
- System.out.println("Loading document:");
- final String xq1 = String.format("bit:load('mydoc.xml', '%s')", doc);
- System.out.println(xq1);
- new XQuery(xq1).evaluate(ctx);
-
- // Reuse store and query loaded document.
- final QueryContext ctx2 = new QueryContext(store);
- System.out.println();
- System.out.println("Query loaded document:");
- final String xq2 = "insert nodes <a><b/></a> into doc('mydoc.xml')/log";
- System.out.println(xq2);
- new XQuery(xq2).execute(ctx2);
- store.commitAll();
- System.out.println();
- }
- }
-
- /**
- * Load a collection and query it.
- */
- private static void loadCollectionAndQuery() throws QueryException,
- IOException {
- // Prepare directory with sample documents.
- final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
- final File dir = new File(tmpDir + File.separator + "docs"
- + System.currentTimeMillis());
- if (!dir.mkdir()) {
- throw new IOException("Directory " + dir + " already exists");
- }
- dir.deleteOnExit();
- for (int i = 0; i < 10; i++) {
- generateSampleDoc(dir, "sample");
- }
-
- // Initialize query context and store.
- try (final DBStore store = DBStore.newBuilder().build()) {
- final QueryContext ctx = new QueryContext(store);
-
- // Use XQuery to load all sample documents into store.
- System.out.println("Load collection from files:");
- final String xq1 = String.format(
- "bit:load('mydocs.col', io:ls('%s', '\\.xml$'))", dir);
- System.out.println(xq1);
- new XQuery(xq1).evaluate(ctx);
-
- // Reuse store and query loaded collection.
- final QueryContext ctx2 = new QueryContext(store);
- System.out.println();
- System.out.println("Query loaded collection:");
- final String xq2 = "for $log in collection('mydocs.col')/log\n"
- + "where $log/@severity='critical'\n" + "return\n" + "<message>\n"
- + " <from>{$log/src/text()}</from>\n"
- + " <body>{$log/msg/text()}</body>\n" + "</message>\n";
- System.out.println(xq2);
- final XQuery q = new XQuery(xq2);
- q.prettyPrint();
- q.serialize(ctx2, System.out);
- System.out.println();
-
- // Use XQuery to load all sample documents once more into store.
- System.out.println("Load collection from files:");
- final String xq3 = String.format(
- "bit:load('mydocs.col', io:ls('%s', '\\.xml$'), fn:false())", dir);
- System.out.println(xq3);
- new XQuery(xq3).evaluate(ctx);
- }
- }
-
- /**
- * Load a document and query it (temporal enhancements).
- */
- private static void loadDocumentAndQueryTemporal() throws QueryException,
- IOException {
- // Prepare sample document.
- File tmpDir = new File(System.getProperty("java.io.tmpdir"));
- File doc = generateSampleDoc(tmpDir, "sample");
- doc.deleteOnExit();
-
- // Initialize query context and store.
- try (final DBStore store = DBStore.newBuilder().isUpdatable().build()) {
- final QueryContext ctx = new QueryContext(store);
-
- // Use XQuery to load sample document into store.
- System.out.println("Loading document:");
- final String xq1 = String.format("bit:load('mydoc.xml', '%s')", doc);
- System.out.println(xq1);
- new XQuery(xq1).evaluate(ctx);
-
- // Reuse store and query loaded document.
- final QueryContext ctx2 = new QueryContext(store);
- System.out.println();
- System.out.println("Query loaded document:");
- final String xq2 = "insert nodes <a><b/></a> into doc('mydoc.xml')/log";
- System.out.println(xq2);
- final XQuery q = new XQuery(xq2);
- q.serialize(ctx2, System.out);
- store.commitAll();
- System.out.println();
- }
- try (final DBStore store = DBStore.newBuilder().build()) {
- final QueryContext ctx3 = new QueryContext(store);
- System.out.println();
- System.out.println("Query loaded document:");
- final String xq3 = "doc('mydoc.xml', 1)/log/all-time::*/*";
- System.out.println(xq3);
- XQuery q = new XQuery(xq3);
- q.prettyPrint();
- q.serialize(ctx3, System.out);
-
- // Serialize first version to XML.
- final QueryContext ctx4 = new QueryContext(store);
- final String xq4 = "bit:serialize(doc('mydoc.xml', 1))";
- q = new XQuery(xq4);
- try (final PrintStream out = new PrintStream(new FileOutputStream(
- new File(new StringBuilder(LOCATION.getAbsolutePath())
- .append(File.separator).append("output-revision-1.xml")
- .toString())))) {
- q.prettyPrint().serialize(ctx4, out);
- }
- System.out.println();
- // Serialize second version to XML.
- final QueryContext ctx5 = new QueryContext(store);
- final String xq5 = "bit:serialize(doc('mydoc.xml', 2))";
- q = new XQuery(xq5);
- try (final PrintStream out = new PrintStream(new FileOutputStream(
- new File(new StringBuilder(LOCATION.getAbsolutePath())
- .append(File.separator).append(File.separator)
- .append("output-revision-2.xml").toString())))) {
- q.prettyPrint().serialize(ctx5, out);
- }
- System.out.println();
- }
- }
-
- /**
- * Generate a small sample document.
- *
- * @param dir
- * the directory
- * @param prefix
- * prefix of name to use
- * @return the generated file
- * @throws IOException
- * if any I/O exception occured
- */
- private static File generateSampleDoc(final File dir, final String prefix)
- throws IOException {
- final File file = File.createTempFile(prefix, ".xml", dir);
- file.deleteOnExit();
- final PrintStream out = new PrintStream(new FileOutputStream(file));
- final Random rnd = new Random();
- final long now = System.currentTimeMillis();
- final int diff = rnd.nextInt(6000 * 60 * 24 * 7);
- final Date tst = new Date(now - diff);
- final Severity sev = Severity.values()[rnd.nextInt(3)];
- final String src = "192.168." + (1 + rnd.nextInt(254)) + "."
- + (1 + rnd.nextInt(254));
- final int mlen = 10 + rnd.nextInt(70);
- final byte[] bytes = new byte[mlen];
- int i = 0;
- while (i < mlen) {
- int wlen = 1 + rnd.nextInt(8);
- int j = i;
- while (j < Math.min(i + wlen, mlen)) {
- bytes[j++] = (byte) ('a' + rnd.nextInt('z' - 'a' + 1));
- }
- i = j;
- if (i < mlen - 1) {
- bytes[i++] = ' ';
- }
- }
- final String msg = new String(bytes);
- out.print("<?xml version='1.0'?>");
- out.print(String.format("<log tstamp='%s' severity='%s'>", tst, sev));
- out.print(String.format("<src>%s</src>", src));
- out.print(String.format("<msg>%s</msg>", msg));
- out.print("</log>");
- out.close();
- return file;
- }
-
-}
Oops, something went wrong.

0 comments on commit 446f6b0

Please sign in to comment.