use of uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader 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 uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader 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 uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader in project OpenOLAT by OpenOLAT.
the class QTI21ImportProcessor method processResource.
private QuestionItem processResource(ResourceType resource, Path imsmanifestPath, ManifestMetadataBuilder metadataBuilder) {
try {
String href = resource.getHref();
Path parentPath = imsmanifestPath.getParent();
Path assessmentItemPath = parentPath.resolve(href);
if (Files.notExists(assessmentItemPath)) {
return null;
}
String dir = qpoolFileStorage.generateDir();
// storage
File itemStorage = qpoolFileStorage.getDirectory(dir);
File outputFile = new File(itemStorage, href);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
QTI21Infos infos = getInfos(imsmanifestPath);
convertXmlFile(assessmentItemPath, outputFile.toPath(), infos);
QtiXmlReader qtiXmlReader = new QtiXmlReader(qtiService.jqtiExtensionManager());
ResourceLocator fileResourceLocator = new FileResourceLocator();
ResourceLocator inputResourceLocator = ImsQTI21Resource.createResolvingResourceLocator(fileResourceLocator);
URI assessmentObjectSystemId = outputFile.toURI();
AssessmentObjectXmlLoader assessmentObjectXmlLoader = new AssessmentObjectXmlLoader(qtiXmlReader, inputResourceLocator);
ResolvedAssessmentItem resolvedAssessmentItem = assessmentObjectXmlLoader.loadAndResolveAssessmentItem(assessmentObjectSystemId);
AssessmentItem assessmentItem = resolvedAssessmentItem.getRootNodeLookup().extractIfSuccessful();
if (!AssessmentItemChecker.checkAndCorrect(assessmentItem)) {
qtiService.persistAssessmentObject(outputFile, assessmentItem);
}
AssessmentItemMetadata metadata = new AssessmentItemMetadata(metadataBuilder);
String editor = null;
String editorVersion = null;
if (StringHelper.containsNonWhitespace(assessmentItem.getToolName())) {
editor = assessmentItem.getToolName();
}
if (StringHelper.containsNonWhitespace(assessmentItem.getToolVersion())) {
editorVersion = assessmentItem.getToolVersion();
}
QuestionItemImpl qitem = processItem(assessmentItem, null, href, editor, editorVersion, dir, metadata);
// create manifest
ManifestBuilder manifest = ManifestBuilder.createAssessmentItemBuilder();
String itemId = IdentifierGenerator.newAsIdentifier("item").toString();
ResourceType importedResource = manifest.appendAssessmentItem(itemId, href);
ManifestMetadataBuilder importedMetadataBuilder = manifest.getMetadataBuilder(importedResource, true);
importedMetadataBuilder.setMetadata(metadataBuilder.getMetadata());
manifest.write(new File(itemStorage, "imsmanifest.xml"));
// process material
List<String> materials = ImportExportHelper.getMaterials(assessmentItem);
for (String material : materials) {
if (material.indexOf("://") < 0) {
// material can be an external URL
Path materialFile = assessmentItemPath.getParent().resolve(material);
PathUtils.copyFileToDir(materialFile, outputFile.getParentFile(), material);
}
}
return qitem;
} catch (Exception e) {
log.error("", e);
return null;
}
}
use of uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader in project OpenOLAT by OpenOLAT.
the class QTI21ServiceImpl method loadAndResolveAssessmentItemForCopy.
@Override
public ResolvedAssessmentItem loadAndResolveAssessmentItemForCopy(URI assessmentObjectSystemId, File resourceDirectory) {
QtiXmlReader qtiXmlReader = new QtiXmlReader(jqtiExtensionManager());
ResourceLocator fileResourceLocator = new PathResourceLocator(resourceDirectory.toPath());
ResourceLocator inputResourceLocator = ImsQTI21Resource.createResolvingResourceLocator(fileResourceLocator);
AssessmentObjectXmlLoader assessmentObjectXmlLoader = new AssessmentObjectXmlLoader(qtiXmlReader, inputResourceLocator);
return assessmentObjectXmlLoader.loadAndResolveAssessmentItem(assessmentObjectSystemId);
}
use of uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader in project OpenOLAT by OpenOLAT.
the class QTI21ServiceImpl method internalLoadAndResolveAssessmentTest.
private ResolvedAssessmentTest internalLoadAndResolveAssessmentTest(File resourceDirectory, URI assessmentObjectSystemId) {
QtiXmlReader qtiXmlReader = new QtiXmlReader(jqtiExtensionManager());
ResourceLocator fileResourceLocator = new PathResourceLocator(resourceDirectory.toPath());
ResourceLocator inputResourceLocator = ImsQTI21Resource.createResolvingResourceLocator(fileResourceLocator);
AssessmentObjectXmlLoader assessmentObjectXmlLoader = new AssessmentObjectXmlLoader(qtiXmlReader, inputResourceLocator);
return assessmentObjectXmlLoader.loadAndResolveAssessmentTest(assessmentObjectSystemId);
}
Aggregations