use of org.sirix.service.xml.serialize.XMLSerializer in project sirix by sirixdb.
the class WorkerHelper method serializeXML.
/**
* This method creates a new XMLSerializer reference
*
* @param session
* Associated session.
* @param out
* OutputStream
*
* @param serializeXMLDec
* specifies whether XML declaration should be shown
* @param serializeRest
* specifies whether node id should be shown
*
* @return new XMLSerializer reference
*/
public static XMLSerializer serializeXML(final Session session, final OutputStream out, final boolean serializeXMLDec, final boolean serializeRest, final Integer revision) {
final XMLSerializerBuilder builder;
if (revision == null)
builder = new XMLSerializerBuilder(session, out);
else
builder = new XMLSerializerBuilder(session, out, revision);
if (serializeRest) {
builder.emitRESTful().emitIDs();
}
if (serializeXMLDec) {
builder.emitXMLDeclaration();
}
final XMLSerializer serializer = builder.build();
return serializer;
}
use of org.sirix.service.xml.serialize.XMLSerializer in project sirix by sirixdb.
the class DatabaseRepresentation method serializIt.
/**
* The XML serializer to a given tnk file.
*
* @param resource
* The resource that has to be serialized.
* @param revision
* The revision of the document.
* @param output
* The output stream where we write the XML file.
* @param nodeid
* <code>true</code> when you want the result nodes with node id's.
* <code>false</code> otherwise.
* @throws WebApplicationException
* The exception occurred.
* @throws SirixException
*/
private void serializIt(final String resource, final Integer revision, final OutputStream output, final boolean nodeid) throws JaxRxException, SirixException {
// Connection to sirix, creating a session
Database database = null;
Session session = null;
// INodeReadTrx rtx = null;
try {
database = Databases.openDatabase(mStoragePath);
session = database.getSession(new SessionConfiguration.Builder(resource).build());
// and creating a transaction
// if (revision == null) {
// rtx = session.beginReadTransaction();
// } else {
// rtx = session.beginReadTransaction(revision);
// }
final XMLSerializerBuilder builder;
if (revision == null)
builder = new XMLSerializerBuilder(session, output);
else
builder = new XMLSerializerBuilder(session, output, revision);
if (nodeid) {
builder.emitRESTful().emitIDs();
}
final XMLSerializer serializer = builder.build();
serializer.call();
} catch (final Exception exce) {
throw new JaxRxException(exce);
} finally {
// closing the sirix storage
WorkerHelper.closeRTX(null, session, database);
}
}
use of org.sirix.service.xml.serialize.XMLSerializer in project sirix by sirixdb.
the class NodeIdRepresentation method serializeAT.
/**
* This method serializes requested resource with an access type.
*
* @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.
* @param accessType
* The {@link IDAccessType} which indicates the access to a special
* node.
*/
private void serializeAT(final String resource, final long nodeId, final Integer revision, final boolean doNodeId, final OutputStream output, final boolean wrapResult, final IDAccessType accessType) {
if (WorkerHelper.checkExistingResource(mStoragePath, resource)) {
Session session = null;
Database database = null;
NodeReadTrx rtx = null;
try {
database = Databases.openDatabase(mStoragePath);
session = database.getSession(new SessionConfiguration.Builder(resource).build());
if (revision == null) {
rtx = session.beginNodeReadTrx();
} else {
rtx = session.beginNodeReadTrx(revision);
}
if (rtx.moveTo(nodeId).hasMoved()) {
switch(accessType) {
case FIRSTCHILD:
if (!rtx.moveToFirstChild().hasMoved())
throw new JaxRxException(404, NOTFOUND);
break;
case LASTCHILD:
if (rtx.moveToFirstChild().hasMoved()) {
long last = rtx.getNodeKey();
while (rtx.moveToRightSibling().hasMoved()) {
last = rtx.getNodeKey();
}
rtx.moveTo(last);
} else {
throw new JaxRxException(404, NOTFOUND);
}
break;
case RIGHTSIBLING:
if (!rtx.moveToRightSibling().hasMoved())
throw new JaxRxException(404, NOTFOUND);
break;
case LEFTSIBLING:
if (!rtx.moveToLeftSibling().hasMoved())
throw new JaxRxException(404, NOTFOUND);
break;
// nothing to do;
default:
}
if (wrapResult) {
output.write(BEGINRESULT);
final XMLSerializerProperties props = new XMLSerializerProperties();
final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, rtx.getNodeKey(), 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, rtx.getNodeKey(), output, props);
if (doNodeId) {
builder.emitRESTful().emitIDs();
}
final XMLSerializer serializer = builder.build();
serializer.call();
}
} else {
throw new JaxRxException(404, NOTFOUND);
}
} 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(rtx, session, database);
} catch (final SirixException exce) {
throw new JaxRxException(exce);
}
}
} else {
throw new JaxRxException(404, "Resource does not exist");
}
}
use of org.sirix.service.xml.serialize.XMLSerializer in project sirix by sirixdb.
the class PICommentTest method testPI.
@Test
public void testPI() throws SirixException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final XMLSerializer serializer = new XMLSerializer.XMLSerializerBuilder(mHolder.getResourceManager(), out).emitXMLDeclaration().build();
serializer.call();
Assert.assertEquals(DocumentCreater.COMMENTPIXML, out.toString());
}
use of org.sirix.service.xml.serialize.XMLSerializer 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();
}
Aggregations