Search in sources :

Example 21 with Database

use of org.sirix.api.Database in project sirix by sirixdb.

the class XMLSerializerTest method testKeyStart.

/**
 * This test check the XPath //books expression and expects 6 books as result. But the failure is,
 * that only the children of the books will be serialized and NOT the book node itself.
 */
@Test
public void testKeyStart() throws Exception {
    final Database database = TestHelper.getDatabase(PATHS.PATH1.getFile());
    final ResourceManager manager = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    final XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    // generate serialize all from this session
    DocumentCreater.createVersioned(wtx);
    wtx.commit();
    wtx.close();
    XMLSerializer serializerall = new XMLSerializerBuilder(manager, 5l, out, new XMLSerializerProperties()).emitXMLDeclaration().build();
    serializerall.call();
    final String result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><b>foo<c/></b>";
    assertEquals(result, out.toString());
    out.reset();
    serializerall = new XMLSerializerBuilder(manager, out, 1, 2, 3).emitXMLDeclaration().build();
    serializerall.call();
    assertEquals(DocumentCreater.VERSIONEDXML, out.toString());
    manager.close();
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) Database(org.sirix.api.Database) ResourceManager(org.sirix.api.ResourceManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 22 with Database

use of org.sirix.api.Database in project sirix by sirixdb.

the class FMSETest method test.

/**
 * Test a folder of XML files.
 *
 * @param FOLDER path string
 * @throws Exception if any exception occurs
 */
private void test(final String FOLDER) throws Exception {
    Database database = TestHelper.getDatabase(PATHS.PATH1.getFile());
    ResourceManager resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    final Path folder = Paths.get(FOLDER);
    final List<Path> list = Files.list(folder).filter(path -> path.getFileName().endsWith(".xml")).collect(toList());
    // Sort files list according to file names.
    list.sort((first, second) -> {
        final String firstName = first.getFileName().toString().substring(0, first.getFileName().toString().indexOf('.'));
        final String secondName = second.getFileName().toString().substring(0, second.getFileName().toString().indexOf('.'));
        if (Integer.parseInt(firstName) < Integer.parseInt(secondName)) {
            return -1;
        } else if (Integer.parseInt(firstName) > Integer.parseInt(secondName)) {
            return +1;
        } else {
            return 0;
        }
    });
    boolean first = true;
    // Shredder files.
    for (final Path file : list) {
        if (file.getFileName().endsWith(".xml")) {
            if (first) {
                first = false;
                try (final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
                    final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD).commitAfterwards().build();
                    shredder.call();
                }
            } else {
                FMSEImport.main(new String[] { PATHS.PATH1.getFile().toAbsolutePath().toString(), file.toAbsolutePath().toString() });
            }
            resource.close();
            resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
            final OutputStream out = new ByteArrayOutputStream();
            final XMLSerializer serializer = new XMLSerializerBuilder(resource, out).build();
            serializer.call();
            final StringBuilder sBuilder = TestHelper.readFile(file, false);
            final Diff diff = new Diff(sBuilder.toString(), out.toString());
            final DetailedDiff detDiff = new DetailedDiff(diff);
            @SuppressWarnings("unchecked") final List<Difference> differences = detDiff.getAllDifferences();
            for (final Difference difference : differences) {
                System.err.println("***********************");
                System.err.println(difference);
                System.err.println("***********************");
            }
            assertTrue("pieces of XML are similar " + diff, diff.similar());
            assertTrue("but are they identical? " + diff, diff.identical());
        }
    }
    database.close();
}
Also used : Path(java.nio.file.Path) XMLUnit(org.custommonkey.xmlunit.XMLUnit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Difference(org.custommonkey.xmlunit.Difference) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) XMLTestCase(org.custommonkey.xmlunit.XMLTestCase) PATHS(org.sirix.TestHelper.PATHS) ResourceManagerConfiguration(org.sirix.access.conf.ResourceManagerConfiguration) TestHelper(org.sirix.TestHelper) After(org.junit.After) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Path(java.nio.file.Path) Before(org.junit.Before) OutputStream(java.io.OutputStream) SirixException(org.sirix.exception.SirixException) Files(java.nio.file.Files) IOException(java.io.IOException) Test(org.junit.Test) FMSEImport(org.sirix.diff.service.FMSEImport) File(java.io.File) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) Insert(org.sirix.service.xml.shredder.Insert) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) ResourceManager(org.sirix.api.ResourceManager) Paths(java.nio.file.Paths) Database(org.sirix.api.Database) Diff(org.custommonkey.xmlunit.Diff) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Diff(org.custommonkey.xmlunit.Diff) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ResourceManagerConfiguration(org.sirix.access.conf.ResourceManagerConfiguration) ResourceManager(org.sirix.api.ResourceManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Difference(org.custommonkey.xmlunit.Difference) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) Database(org.sirix.api.Database) XMLShredder(org.sirix.service.xml.shredder.XMLShredder)

Example 23 with Database

use of org.sirix.api.Database in project sirix by sirixdb.

the class XMLShredderTest method testShreddingLargeText.

@Test
public void testShreddingLargeText() throws Exception {
    final Database database = TestHelper.getDatabase(PATHS.PATH2.getFile());
    final ResourceManager manager = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    final XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
    final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(XML3), Insert.ASFIRSTCHILD).commitAfterwards().build();
    shredder.call();
    wtx.close();
    final XdmNodeReadTrx rtx = manager.beginNodeReadTrx();
    assertTrue(rtx.moveToFirstChild().hasMoved());
    assertTrue(rtx.moveToFirstChild().hasMoved());
    final StringBuilder tnkBuilder = new StringBuilder();
    do {
        tnkBuilder.append(rtx.getValue());
    } while (rtx.moveToRightSibling().hasMoved());
    final String tnkString = tnkBuilder.toString();
    rtx.close();
    manager.close();
    final XMLEventReader validater = XMLShredder.createFileReader(XML3);
    final StringBuilder xmlBuilder = new StringBuilder();
    while (validater.hasNext()) {
        final XMLEvent event = validater.nextEvent();
        switch(event.getEventType()) {
            case XMLStreamConstants.CHARACTERS:
                final String text = event.asCharacters().getData().trim();
                if (text.length() > 0) {
                    xmlBuilder.append(text);
                }
                break;
        }
    }
    assertEquals(xmlBuilder.toString(), tnkString);
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) Database(org.sirix.api.Database) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) ResourceManager(org.sirix.api.ResourceManager) Test(org.junit.Test)

Example 24 with Database

use of org.sirix.api.Database in project sirix by sirixdb.

the class ResourceTransactionUsage method main.

public static void main(final String[] args) {
    final Path file = LOCATION.resolve("db");
    final DatabaseConfiguration config = new DatabaseConfiguration(file);
    if (Files.exists(file)) {
        Databases.truncateDatabase(config);
    }
    Databases.createDatabase(config);
    try (final Database database = Databases.openDatabase(file)) {
        database.createResource(new ResourceConfiguration.Builder("resource", config).build());
        try (final ResourceManager resource = database.getResourceManager(new ResourceManagerConfiguration.Builder("resource").build());
            final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
            wtx.insertSubtreeAsFirstChild(XMLShredder.createFileReader(LOCATION.resolve("input.xml")));
            wtx.moveTo(2);
            wtx.moveSubtreeToFirstChild(4).commit();
            final OutputStream out = new ByteArrayOutputStream();
            new XMLSerializer.XMLSerializerBuilder(resource, out).prettyPrint().build().call();
            System.out.println(out);
        }
    } catch (final SirixException | IOException | XMLStreamException e) {
    // LOG or do anything, the database is closed properly.
    }
}
Also used : Path(java.nio.file.Path) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResourceManager(org.sirix.api.ResourceManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) Database(org.sirix.api.Database) SirixException(org.sirix.exception.SirixException)

Example 25 with Database

use of org.sirix.api.Database in project sirix by sirixdb.

the class FileSystemWatcher method main.

/**
 * Main entry point.
 *
 * @param args first argument speficies the path/directory to watch for changes, the second
 *        argument specifies the database to which to append incoming events; <strong>note that
 *        any existing database is truncated first if an optional third argument is set to
 *        {@code true}</strong>
 * @throws SirixException if sirix encounters any error
 * @throws IOException if any I/O error occurs
 */
public static void main(final String[] args) throws SirixException, IOException {
    if (args.length < 2 || args.length > 3) {
        LOGWRAPPER.info("Usage: FileSystemWatcher pathToWatch pathToDatabase [true|false]");
    }
    if (Boolean.parseBoolean(args[2])) {
        Databases.truncateDatabase(new DatabaseConfiguration(Paths.get(args[1])));
    }
    final Path databasePath = Paths.get(args[1]);
    final DatabaseConfiguration conf = new DatabaseConfiguration(databasePath);
    Map<Path, FileSystemPath> index = null;
    if (Files.exists(Paths.get(args[1]))) {
        Databases.truncateDatabase(conf);
    }
    Databases.createDatabase(conf);
    try (final Database database = Databases.openDatabase(databasePath)) {
        database.createResource(new ResourceConfiguration.Builder("shredded", conf).build());
        index = FileHierarchyWalker.parseDir(Paths.get(args[0]), database, Optional.of(new ProcessFileSystemAttributes()));
        assert index != null;
        try (final FileSystemWatcher watcher = FileSystemWatcher.getInstance(Paths.get(args[0]), Databases.openDatabase(databasePath))) {
            watcher.watch(null, index);
        }
    }
}
Also used : Path(java.nio.file.Path) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) Database(org.sirix.api.Database)

Aggregations

Database (org.sirix.api.Database)55 ResourceManager (org.sirix.api.ResourceManager)30 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)22 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)19 Test (org.junit.Test)18 SirixException (org.sirix.exception.SirixException)18 Session (org.sirix.api.Session)16 IOException (java.io.IOException)13 Path (java.nio.file.Path)13 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)13 XMLSerializerBuilder (org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder)12 JaxRxException (org.jaxrx.core.JaxRxException)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 NodeWriteTrx (org.sirix.api.NodeWriteTrx)8 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)7 WebApplicationException (javax.ws.rs.WebApplicationException)6 NodeReadTrx (org.sirix.api.NodeReadTrx)6 XMLSerializer (org.sirix.service.xml.serialize.XMLSerializer)6 XMLEventReader (javax.xml.stream.XMLEventReader)5 DocumentException (org.brackit.xquery.xdm.DocumentException)5