Search in sources :

Example 1 with QTI21Infos

use of org.olat.ims.qti21.model.xml.QTI21Infos in project OpenOLAT by OpenOLAT.

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;
    }
}
Also used : QTI21IMSManifestExplorerVisitor(org.olat.ims.qti21.repository.handlers.QTI21IMSManifestExplorerVisitor) IOException(java.io.IOException)

Example 2 with QTI21Infos

use of org.olat.ims.qti21.model.xml.QTI21Infos 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;
    }
}
Also used : Path(java.nio.file.Path) AssessmentObjectXmlLoader(uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader) ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) QuestionItemImpl(org.olat.modules.qpool.model.QuestionItemImpl) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentItem(uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem) ResourceType(org.olat.imscp.xml.manifest.ResourceType) URI(java.net.URI) IOException(java.io.IOException) ManifestMetadataBuilder(org.olat.ims.qti21.model.xml.ManifestMetadataBuilder) QtiXmlReader(uk.ac.ed.ph.jqtiplus.reading.QtiXmlReader) QTI21Infos(org.olat.ims.qti21.model.xml.QTI21Infos) FileResourceLocator(uk.ac.ed.ph.jqtiplus.xmlutils.locators.FileResourceLocator) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentItemMetadata(org.olat.ims.qti21.model.xml.AssessmentItemMetadata) File(java.io.File) FileResourceLocator(uk.ac.ed.ph.jqtiplus.xmlutils.locators.FileResourceLocator) ResourceLocator(uk.ac.ed.ph.jqtiplus.xmlutils.locators.ResourceLocator)

Example 3 with QTI21Infos

use of org.olat.ims.qti21.model.xml.QTI21Infos 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;
    }
}
Also used : Onyx38ToQtiWorksHandler(org.olat.ims.qti21.model.xml.Onyx38ToQtiWorksHandler) DefaultHandler2(org.xml.sax.ext.DefaultHandler2) QTI21Infos(org.olat.ims.qti21.model.xml.QTI21Infos) OnyxToQtiWorksHandler(org.olat.ims.qti21.model.xml.OnyxToQtiWorksHandler) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) IOException(java.io.IOException) FactoryConfigurationError(javax.xml.stream.FactoryConfigurationError)

Example 4 with QTI21Infos

use of org.olat.ims.qti21.model.xml.QTI21Infos in project OpenOLAT by OpenOLAT.

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();
}
Also used : QTI21ExplorerHandler(org.olat.ims.qti21.model.xml.QTI21ExplorerHandler) InputStream(java.io.InputStream) SAXParser(javax.xml.parsers.SAXParser) URISyntaxException(java.net.URISyntaxException) QtiXmlInterpretationException(uk.ac.ed.ph.jqtiplus.reading.QtiXmlInterpretationException) BadResourceException(uk.ac.ed.ph.jqtiplus.provision.BadResourceException) IOException(java.io.IOException)

Example 5 with QTI21Infos

use of org.olat.ims.qti21.model.xml.QTI21Infos in project openolat by klemens.

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);
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) OnyxToQtiWorksHandler(org.olat.ims.qti21.model.xml.OnyxToQtiWorksHandler) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) InputStream(java.io.InputStream) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SAXParser(javax.xml.parsers.SAXParser) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)10 InputStream (java.io.InputStream)4 SAXParser (javax.xml.parsers.SAXParser)4 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)4 OnyxToQtiWorksHandler (org.olat.ims.qti21.model.xml.OnyxToQtiWorksHandler)4 QTI21Infos (org.olat.ims.qti21.model.xml.QTI21Infos)4 File (java.io.File)2 Writer (java.io.Writer)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Path (java.nio.file.Path)2 FactoryConfigurationError (javax.xml.stream.FactoryConfigurationError)2 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)2 ShieldInputStream (org.olat.core.util.io.ShieldInputStream)2 AssessmentItemMetadata (org.olat.ims.qti21.model.xml.AssessmentItemMetadata)2 ManifestBuilder (org.olat.ims.qti21.model.xml.ManifestBuilder)2 ManifestMetadataBuilder (org.olat.ims.qti21.model.xml.ManifestMetadataBuilder)2 Onyx38ToQtiWorksHandler (org.olat.ims.qti21.model.xml.Onyx38ToQtiWorksHandler)2 QTI21ExplorerHandler (org.olat.ims.qti21.model.xml.QTI21ExplorerHandler)2 QTI21IMSManifestExplorerVisitor (org.olat.ims.qti21.repository.handlers.QTI21IMSManifestExplorerVisitor)2