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();
}
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();
}
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);
}
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.
}
}
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);
}
}
}
Aggregations