use of org.sirix.service.xml.serialize.XMLSerializer 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 in project sirix by sirixdb.
the class NodeIdRepresentation method serialize.
/**
* This method serializes requested resource
*
* @param resource
* The requested resource
* @param nodeId
* The node id of the requested resource.
* @param revision
* The revision of the requested resource.
* @param doNodeId
* Specifies whether the node id's have to be shown in the result.
* @param output
* The output stream to be written.
* @param wrapResult
* Specifies whether the result has to be wrapped with a result
* element.
*/
private void serialize(final String resource, final long nodeId, final Long revision, final boolean doNodeId, final OutputStream output, final boolean wrapResult) {
if (WorkerHelper.checkExistingResource(mStoragePath, resource)) {
Session session = null;
Database database = null;
try {
database = Databases.openDatabase(mStoragePath);
session = database.getSession(new SessionConfiguration.Builder(resource).build());
if (wrapResult) {
output.write(BEGINRESULT);
final XMLSerializerProperties props = new XMLSerializerProperties();
final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, nodeId, output, props);
if (doNodeId) {
builder.emitRESTful().emitIDs();
}
final XMLSerializer serializer = builder.build();
serializer.call();
output.write(ENDRESULT);
} else {
final XMLSerializerProperties props = new XMLSerializerProperties();
final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, nodeId, output, props);
if (doNodeId) {
builder.emitRESTful().emitIDs();
}
final XMLSerializer serializer = builder.build();
serializer.call();
}
} catch (final SirixException ttExcep) {
throw new JaxRxException(ttExcep);
} catch (final IOException ioExcep) {
throw new JaxRxException(ioExcep);
} catch (final Exception globExcep) {
if (globExcep instanceof JaxRxException) {
// types
throw (JaxRxException) globExcep;
} else {
throw new JaxRxException(globExcep);
}
} finally {
try {
WorkerHelper.closeRTX(null, session, database);
} catch (final SirixException exce) {
throw new JaxRxException(exce);
}
}
} else {
throw new JaxRxException(404, "Resource does not exist");
}
}
Aggregations