use of org.sirix.service.xml.shredder.XMLShredder in project sirix by sirixdb.
the class WorkerHelper method shredInputStream.
/**
* Shreds a given InputStream
*
* @param wtx
* current write transaction reference
* @param value
* InputStream to be shred
*/
public static void shredInputStream(final NodeWriteTrx wtx, final InputStream value, final Insert child) {
final XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLEventReader parser;
try {
parser = factory.createXMLEventReader(value);
} catch (final XMLStreamException xmlse) {
throw new WebApplicationException(xmlse);
}
try {
final XMLShredder shredder = new XMLShredder.Builder(wtx, parser, child).commitAfterwards().build();
shredder.call();
} catch (final Exception exce) {
throw new WebApplicationException(exce);
}
}
use of org.sirix.service.xml.shredder.XMLShredder in project sirix by sirixdb.
the class DatabaseRepresentation method shred.
/**
* This method is responsible to save the XML file, which is in an
* {@link InputStream}, as a sirix object.
*
* @param xmlInput
* XML file wrapped in an {@link InputStream}
* @param resource
* name of the resource
* @return {@code true} if shredding process has been successful,
* {@code false} otherwise.
* @throws SirixException
* if any sirix related exception occurs
*/
public final boolean shred(final InputStream xmlInput, final String resource) throws SirixException {
boolean allOk = false;
NodeWriteTrx wtx = null;
Database database = null;
Session session = null;
boolean abort = false;
try {
final DatabaseConfiguration dbConf = new DatabaseConfiguration(mStoragePath);
Databases.createDatabase(dbConf);
database = Databases.openDatabase(dbConf.getFile());
// Shredding the database to the file as XML
final ResourceConfiguration resConf = new ResourceConfiguration.Builder(resource, dbConf).revisionsToRestore(1).build();
if (database.createResource(resConf)) {
session = database.getSession(new SessionConfiguration.Builder(resource).build());
wtx = session.beginNodeWriteTrx();
final XMLShredder shredder = new XMLShredder.Builder(wtx, RESTXMLShredder.createReader(xmlInput), Insert.ASFIRSTCHILD).commitAfterwards().build();
shredder.call();
allOk = true;
}
} catch (final Exception exce) {
abort = true;
throw new JaxRxException(exce);
} finally {
WorkerHelper.closeWTX(abort, wtx, session, database);
}
return allOk;
}
use of org.sirix.service.xml.shredder.XMLShredder in project sirix by sirixdb.
the class TestNodeWrapper method testGetBaseURI.
@Test
public void testGetBaseURI() throws Exception {
// Test with xml:base specified.
final File source = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "data" + File.separator + "testBaseURI.xml");
final Session session = generateSession();
final NodeWriteTrx wtx = session.beginNodeWriteTrx();
final XMLEventReader reader = XMLShredder.createFileReader(source);
final XMLShredder shredder = new XMLShredder.Builder(wtx, reader, Insert.ASFIRSTCHILD).commitAfterwards().build();
shredder.call();
wtx.close();
final Processor proc = new Processor(false);
final NodeInfo doc = new DocumentWrapper(session, proc.getUnderlyingConfiguration());
doc.getNamePool().allocate("xml", "http://www.w3.org/XML/1998/namespace", "base");
doc.getNamePool().allocate("", "", "baz");
final NameTest test = new NameTest(Type.ELEMENT, "", "baz", doc.getNamePool());
final AxisIterator iterator = doc.iterateAxis(Axis.DESCENDANT, test);
final NodeInfo baz = (NodeInfo) iterator.next();
assertEquals("http://example.org", baz.getBaseURI());
session.close();
mDatabase.close();
}
use of org.sirix.service.xml.shredder.XMLShredder 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.shredder.XMLShredder in project sirix by sirixdb.
the class DocumentCreater method createRevisioned.
/**
* Create revisioned document.
*
* @throws SirixException if shredding fails
* @throws XMLStreamException if StAX reader couldn't be created
* @throws IOException if reading XML string fails
*/
public static void createRevisioned(final Database database) throws SirixException, IOException, XMLStreamException {
try (final XdmNodeWriteTrx firstWtx = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build()).beginNodeWriteTrx()) {
final XMLShredder shredder = new XMLShredder.Builder(firstWtx, XMLShredder.createStringReader(REVXML), Insert.ASFIRSTCHILD).commitAfterwards().build();
shredder.call();
}
try (final XdmNodeWriteTrx secondWtx = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build()).beginNodeWriteTrx()) {
secondWtx.moveToFirstChild();
secondWtx.moveToFirstChild();
secondWtx.moveToFirstChild();
secondWtx.setValue("A Contrived Test Document");
secondWtx.moveToParent();
secondWtx.moveToRightSibling();
secondWtx.moveToRightSibling();
secondWtx.moveToFirstChild();
secondWtx.moveToRightSibling();
final long key = secondWtx.getNodeKey();
secondWtx.insertAttribute(new QNm("role"), "bold");
secondWtx.moveTo(key);
secondWtx.moveToRightSibling();
secondWtx.setValue("changed in it.");
secondWtx.moveToParent();
secondWtx.insertElementAsRightSibling(new QNm("para"));
secondWtx.insertTextAsFirstChild("This is a new para 2b.");
secondWtx.moveToParent();
secondWtx.moveToRightSibling();
secondWtx.moveToRightSibling();
secondWtx.moveToFirstChild();
secondWtx.setValue("This is a different para 4.");
secondWtx.moveToParent();
secondWtx.insertElementAsRightSibling(new QNm("para"));
secondWtx.insertTextAsFirstChild("This is a new para 4b.");
secondWtx.moveToParent();
secondWtx.moveToRightSibling();
secondWtx.moveToRightSibling();
secondWtx.remove();
secondWtx.remove();
secondWtx.commit();
secondWtx.moveToDocumentRoot();
secondWtx.moveToFirstChild();
secondWtx.moveToFirstChild();
secondWtx.remove();
secondWtx.commit();
}
}
Aggregations