use of org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder in project sirix by sirixdb.
the class XMLSerializerTest method testSampleCompleteSerializer.
@Test
public void testSampleCompleteSerializer() 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.close();
XMLSerializer serializerall = new XMLSerializerBuilder(manager, out, -1).emitXMLDeclaration().build();
serializerall.call();
assertEquals(DocumentCreater.VERSIONEDXML, 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.service.xml.serialize.XMLSerializer.XMLSerializerBuilder 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.service.xml.serialize.XMLSerializer.XMLSerializerBuilder 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.service.xml.serialize.XMLSerializer.XMLSerializerBuilder in project sirix by sirixdb.
the class XMLUpdateShredderTest method test.
// /** Not working anymore due to text merging on deletes. */
// @Ignore
// @Test
// public void testAllEighth() throws Exception {
// test(XMLALLEIGHTH);
// }
//
// /** Not working anymore due to text merging on deletes. */
// @Ignore
// @Test
// public void testAllNineth() throws Exception {
// test(XMLALLNINETH);
// }
// @Test
// public void testLinguistics() throws Exception {
// test(XMLLINGUISTICS);
// }
private void test(final Path folder) throws Exception {
final Database database = TestHelper.getDatabase(PATHS.PATH1.getFile());
database.createResource(new ResourceConfiguration.Builder(TestHelper.RESOURCE, PATHS.PATH1.getConfig()).build());
final ResourceManager manager = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
int i = 2;
final List<Path> files = Files.list(folder).filter(file -> file.getFileName().endsWith(".xml")).collect(Collectors.toList());
// Sort files array according to file names.
files.sort((first, second) -> {
final String firstName = first.getFileName().toString().substring(0, second.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 : files) {
if (file.endsWith(".xml")) {
final XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
if (first) {
final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD).commitAfterwards().build();
shredder.call();
first = false;
} else {
@SuppressWarnings("deprecation") final XMLUpdateShredder shredder = new XMLUpdateShredder(wtx, XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD, file, ShredderCommit.COMMIT);
shredder.call();
}
assertEquals(i, wtx.getRevisionNumber());
i++;
final OutputStream out = new ByteArrayOutputStream();
final XMLSerializer serializer = new XMLSerializerBuilder(manager, out).prettyPrint().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.out.println("***********************");
System.out.println(difference);
System.out.println("***********************");
}
assertTrue("pieces of XML are similar " + diff, diff.similar());
assertTrue("but are they identical? " + diff, diff.identical());
wtx.close();
}
}
}
use of org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder in project sirix by sirixdb.
the class XMLSerializerTest method testXMLSerializer.
@Test
public void testXMLSerializer() 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();
DocumentCreater.create(wtx);
wtx.commit();
wtx.close();
// Generate from this session.
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final XMLSerializer serializer = new XMLSerializerBuilder(manager, out).emitXMLDeclaration().build();
serializer.call();
assertEquals(DocumentCreater.XML, out.toString());
manager.close();
}
Aggregations