use of org.xml.sax.ext.DefaultHandler2 in project OpenOLAT by OpenOLAT.
the class OnyxToQtiWorksAssessementTestsTest method fixAssessmentTest.
@Test
public void fixAssessmentTest() throws IOException, XMLStreamException, SAXException, ParserConfigurationException, URISyntaxException {
URL xmlUrl = OnyxToQtiWorksAssessementTestsTest.class.getResource("resources/onyx/" + xmlFilename);
File xmlFile = new File(xmlUrl.toURI());
File tmpDir = new File(WebappHelper.getTmpDir(), "onyx" + UUID.randomUUID());
tmpDir.mkdirs();
File outputFile = new File(tmpDir, "text.xml");
try (InputStream in = Files.newInputStream(xmlFile.toPath());
Writer out = Files.newBufferedWriter(outputFile.toPath(), Charset.forName("UTF-8"))) {
XMLOutputFactory xof = XMLOutputFactory.newInstance();
XMLStreamWriter xtw = xof.createXMLStreamWriter(out);
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
DefaultHandler2 myHandler = new Onyx38ToQtiWorksHandler(xtw);
saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", myHandler);
saxParser.parse(in, myHandler);
} catch (Exception e1) {
log.error("", e1);
throw e1;
}
QtiXmlReader qtiXmlReader = new QtiXmlReader(new JqtiExtensionManager());
ResourceLocator fileResourceLocator = new PathResourceLocator(outputFile.toPath());
AssessmentObjectXmlLoader assessmentObjectXmlLoader = new AssessmentObjectXmlLoader(qtiXmlReader, fileResourceLocator);
ResolvedAssessmentTest resolvedAssessmentTest = assessmentObjectXmlLoader.loadAndResolveAssessmentTest(outputFile.toURI());
Assert.assertNotNull(resolvedAssessmentTest);
AssessmentTest assessmentTest = resolvedAssessmentTest.getRootNodeLookup().extractIfSuccessful();
TestValidationResult testResult = assessmentObjectXmlLoader.loadResolveAndValidateTest(outputFile.toURI());
BadResourceException e = testResult.getResolvedAssessmentTest().getTestLookup().getBadResourceException();
if (e != null) {
StringBuilder err = new StringBuilder();
BadRessourceHelper.extractMessage(e, err);
log.error(err.toString());
}
FileUtils.deleteDirsAndFiles(tmpDir.toPath());
Assert.assertNotNull(assessmentTest);
Assert.assertFalse(xmlFilename + " has fatal errors", BadRessourceHelper.hasFatalErrors(e));
}
use of org.xml.sax.ext.DefaultHandler2 in project OpenOLAT by OpenOLAT.
the class Onyx38ToQtiWorksAssessementItemsTest method fixItem.
@Test
public void fixItem() throws IOException, XMLStreamException, SAXException, ParserConfigurationException, URISyntaxException {
URL xmlUrl = Onyx38ToQtiWorksAssessementItemsTest.class.getResource("resources/onyx/" + xmlFilename);
File xmlFile = new File(xmlUrl.toURI());
File tmpDir = new File(WebappHelper.getTmpDir(), "onyx" + UUID.randomUUID());
tmpDir.mkdirs();
File outputFile = new File(tmpDir, "text.xml");
try (InputStream in = Files.newInputStream(xmlFile.toPath());
Writer out = Files.newBufferedWriter(outputFile.toPath(), Charset.forName("UTF-8"))) {
XMLOutputFactory xof = XMLOutputFactory.newInstance();
XMLStreamWriter xtw = xof.createXMLStreamWriter(out);
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
DefaultHandler2 myHandler = new Onyx38ToQtiWorksHandler(xtw);
saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", myHandler);
saxParser.parse(in, myHandler);
} catch (Exception e1) {
log.error("", e1);
throw e1;
}
QtiXmlReader qtiXmlReader = new QtiXmlReader(new JqtiExtensionManager());
ResourceLocator fileResourceLocator = new PathResourceLocator(outputFile.toPath());
AssessmentObjectXmlLoader assessmentObjectXmlLoader = new AssessmentObjectXmlLoader(qtiXmlReader, fileResourceLocator);
ResolvedAssessmentItem resolvedAssessmentItem = assessmentObjectXmlLoader.loadAndResolveAssessmentItem(outputFile.toURI());
Assert.assertNotNull(resolvedAssessmentItem);
AssessmentItem assessmentItem = resolvedAssessmentItem.getRootNodeLookup().extractIfSuccessful();
// validation is only
ItemValidationResult itemResult = assessmentObjectXmlLoader.loadResolveAndValidateItem(outputFile.toURI());
BadResourceException e = itemResult.getResolvedAssessmentItem().getItemLookup().getBadResourceException();
if (e != null) {
StringBuilder err = new StringBuilder();
BadRessourceHelper.extractMessage(e, err);
log.error(err.toString());
}
FileUtils.deleteDirsAndFiles(tmpDir.toPath());
Assert.assertNotNull(assessmentItem);
Assert.assertFalse(BadRessourceHelper.hasFatalErrors(e));
}
use of org.xml.sax.ext.DefaultHandler2 in project OpenOLAT by OpenOLAT.
the class OnyxToQtiWorksAssessementItemsTest method fixItem.
@Test
public void fixItem() throws IOException, XMLStreamException, SAXException, ParserConfigurationException, URISyntaxException {
URL xmlUrl = OnyxToQtiWorksAssessementItemsTest.class.getResource("resources/onyx/" + xmlFilename);
File xmlFile = new File(xmlUrl.toURI());
File tmpDir = new File(WebappHelper.getTmpDir(), "onyx" + UUID.randomUUID());
tmpDir.mkdirs();
File outputFile = new File(tmpDir, "text.xml");
try (InputStream in = Files.newInputStream(xmlFile.toPath());
Writer out = Files.newBufferedWriter(outputFile.toPath(), Charset.forName("UTF-8"))) {
XMLOutputFactory xof = XMLOutputFactory.newInstance();
XMLStreamWriter xtw = xof.createXMLStreamWriter(out);
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
DefaultHandler2 myHandler = new OnyxToQtiWorksHandler(xtw, infos);
saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", myHandler);
saxParser.parse(in, myHandler);
} catch (Exception e1) {
log.error("", e1);
throw e1;
}
QtiXmlReader qtiXmlReader = new QtiXmlReader(new JqtiExtensionManager());
ResourceLocator fileResourceLocator = new PathResourceLocator(outputFile.toPath());
AssessmentObjectXmlLoader assessmentObjectXmlLoader = new AssessmentObjectXmlLoader(qtiXmlReader, fileResourceLocator);
ResolvedAssessmentItem resolvedAssessmentItem = assessmentObjectXmlLoader.loadAndResolveAssessmentItem(outputFile.toURI());
Assert.assertNotNull(resolvedAssessmentItem);
AssessmentItem assessmentItem = resolvedAssessmentItem.getRootNodeLookup().extractIfSuccessful();
// validation is only
ItemValidationResult itemResult = assessmentObjectXmlLoader.loadResolveAndValidateItem(outputFile.toURI());
BadResourceException e = itemResult.getResolvedAssessmentItem().getItemLookup().getBadResourceException();
if (e != null) {
StringBuilder err = new StringBuilder();
BadRessourceHelper.extractMessage(e, err);
log.error(err.toString());
}
FileUtils.deleteDirsAndFiles(tmpDir.toPath());
Assert.assertNotNull(assessmentItem);
Assert.assertFalse(BadRessourceHelper.hasFatalErrors(e));
}
use of org.xml.sax.ext.DefaultHandler2 in project OpenOLAT by OpenOLAT.
the class CopyAndConvertVisitor method convertXmlFile.
/**
* Convert the XML files, assessmentItem or assessmentTest
*
* @param inputFile
* @param outputFile
*/
public boolean convertXmlFile(Path inputFile, Path outputFile) {
try {
boolean validated = true;
QTI21Infos fileInfos = scanFile(inputFile);
// inherit from test if needed
if (fileInfos.getEditor() == null && infos.getEditor() != null) {
fileInfos.setEditor(infos.getEditor());
fileInfos.setVersion(infos.getVersion());
}
if (onyx38Family(fileInfos)) {
validated = convertXmlFile(inputFile, outputFile, fileInfos.getType(), new HandlerProvider() {
@Override
public DefaultHandler2 create(XMLStreamWriter xtw) {
return new Onyx38ToQtiWorksHandler(xtw);
}
});
} else if (onyxWebFamily(fileInfos)) {
validated = convertXmlFile(inputFile, outputFile, fileInfos.getType(), new HandlerProvider() {
@Override
public DefaultHandler2 create(XMLStreamWriter xtw) {
return new OnyxToQtiWorksHandler(xtw, infos);
}
});
if (validated && fileInfos.getType() == InputType.assessmentItem) {
// check templateVariables
checkAssessmentItem(outputFile);
}
} else {
Files.copy(inputFile, outputFile, StandardCopyOption.REPLACE_EXISTING);
}
return validated;
} catch (IOException | FactoryConfigurationError e) {
log.error("", e);
return false;
}
}
use of org.xml.sax.ext.DefaultHandler2 in project openolat by klemens.
the class CopyAndConvertVisitor method convertXmlFile.
/**
* Convert the XML files, assessmentItem or assessmentTest
*
* @param inputFile
* @param outputFile
*/
public boolean convertXmlFile(Path inputFile, Path outputFile) {
try {
boolean validated = true;
QTI21Infos fileInfos = scanFile(inputFile);
// inherit from test if needed
if (fileInfos.getEditor() == null && infos.getEditor() != null) {
fileInfos.setEditor(infos.getEditor());
fileInfos.setVersion(infos.getVersion());
}
if (onyx38Family(fileInfos)) {
validated = convertXmlFile(inputFile, outputFile, fileInfos.getType(), new HandlerProvider() {
@Override
public DefaultHandler2 create(XMLStreamWriter xtw) {
return new Onyx38ToQtiWorksHandler(xtw);
}
});
} else if (onyxWebFamily(fileInfos)) {
validated = convertXmlFile(inputFile, outputFile, fileInfos.getType(), new HandlerProvider() {
@Override
public DefaultHandler2 create(XMLStreamWriter xtw) {
return new OnyxToQtiWorksHandler(xtw, infos);
}
});
if (validated && fileInfos.getType() == InputType.assessmentItem) {
// check templateVariables
checkAssessmentItem(outputFile);
}
} else {
Files.copy(inputFile, outputFile, StandardCopyOption.REPLACE_EXISTING);
}
return validated;
} catch (IOException | FactoryConfigurationError e) {
log.error("", e);
return false;
}
}
Aggregations