XmlStackingSemanticTranslator extends AbstractStackingSemanticTranslator<XmlStackItem> implements StackingSemanticTranslator<XmlStackItem>, SemanticTranslator { @Override public Model translate(InputStream dataStream, String entityIdentifier, ExtractedOntology managedOntology) throws SemanticTranslationException { final Model resultsModel = modelFactory.createModel(); final XMLStreamReader reader = initXmlStreamReader(dataStream); try { final StringBuilder stringBuffer = new StringBuilder(); for (int event = reader.next(); event != XMLStreamConstants.END_DOCUMENT; event = reader.next()) { final String address = getCurrentLocation(); switch (event) { case XMLStreamConstants.START_ELEMENT: startElement(reader, managedOntology); break; case XMLStreamConstants.COMMENT: handleComment(reader.getText()); break; case XMLStreamConstants.END_ELEMENT: final XmlStackItem endingItem = popStack() .orElseThrow(() -> new SemanticTranslationException("Element ending without a " + "corresponding stack item.")); final String val = stringBuffer.toString(); stringBuffer.setLength(0); LOG.trace("Ending '{}' - Value: '{}'", reader.getLocalName(), val); rootElementEnding(endingItem); handleElementEnd(endingItem, val, managedOntology, address, resultsModel); break; case XMLStreamConstants.CHARACTERS: stringBuffer.append(reader.getText().trim()); break; default: break; } } } catch (XMLStreamException e) { throw new SemanticTranslationException("Issue reading XML from incoming data stream", e); } finally { silentlyCloseReader(reader); } return resultsModel; } XmlStackingSemanticTranslator(); @Reference void setValueFactory(ValueFactory valueFactory); @Reference void setModelFactory(ModelFactory modelFactory); @Reference void setExpressionProcessor(IriExpressionProcessor expressionProcessor); @Reference void setOrmFactoryRegistry(OrmFactoryRegistry ormFactoryRegistry); @Override Model translate(InputStream dataStream, String entityIdentifier, ExtractedOntology managedOntology);  }
@Test(expected = SemanticTranslationException.class) public void testNonXml() throws Exception { final ExtractedOntology ont = getRequiredOrmFactory(ExtractedOntology.class) .createNew(VALUE_FACTORY.createIRI(ONT_URI)); try (InputStream is = new FileInputStream(new File("src/test/resources/ormFactories.conf"))) { xmlStackingSemanticTranslator.translate(is, "ormFactories.conf", ont); fail("Translating non-xml should throw a semantic translation exception"); } }
@Test public void simpleTest() throws Exception { ExtractedOntology ont = getRequiredOrmFactory(ExtractedOntology.class) .createNew(VALUE_FACTORY.createIRI(ONT_URI)); try (InputStream is = new FileInputStream(simpleTestFile)) { final Model results = xmlStackingSemanticTranslator.translate(is, "simpleTest.xml", ont); validateSimpleExtractedOntology(ont); validateSimpleResults(results, ont); System.out.println("Model: \n"); ont.getModel().forEach(System.out::println); System.out.println("\nIndividuals: \n"); results.forEach(System.out::println); } }