use of org.olat.ims.qti21.model.xml.QTI21Infos 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;
}
}
use of org.olat.ims.qti21.model.xml.QTI21Infos in project OpenOLAT by OpenOLAT.
the class QTI21ImportProcessor method convertXmlFile.
private void convertXmlFile(Path inputFile, Path outputFile, QTI21Infos infos) {
try (InputStream in = Files.newInputStream(inputFile);
Writer out = Files.newBufferedWriter(outputFile, Charset.forName("UTF-8"))) {
XMLOutputFactory xof = XMLOutputFactory.newInstance();
XMLStreamWriter xtw = xof.createXMLStreamWriter(out);
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
OnyxToQtiWorksHandler myHandler = new OnyxToQtiWorksHandler(xtw, infos);
saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", myHandler);
saxParser.parse(in, myHandler);
} catch (Exception e) {
log.error("", e);
}
}
use of org.olat.ims.qti21.model.xml.QTI21Infos in project openolat by klemens.
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 org.olat.ims.qti21.model.xml.QTI21Infos in project openolat by klemens.
the class QTI21ImportProcessor method getInfos.
private QTI21Infos getInfos(Path imsmanifestPath) {
try {
QTI21IMSManifestExplorerVisitor visitor = new QTI21IMSManifestExplorerVisitor();
Files.walkFileTree(imsmanifestPath, visitor);
return visitor.getInfos();
} catch (IOException e) {
log.error("", e);
return null;
}
}
use of org.olat.ims.qti21.model.xml.QTI21Infos in project openolat by klemens.
the class CopyAndConvertVisitor method scanFile.
private QTI21Infos scanFile(Path inputFile) {
QTI21ExplorerHandler infosHandler = new QTI21ExplorerHandler();
try (InputStream in = Files.newInputStream(inputFile)) {
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", infosHandler);
saxParser.parse(in, infosHandler);
} catch (Exception e1) {
log.error("", e1);
}
return infosHandler.getInfos();
}
Aggregations