Search in sources :

Example 1 with OnyxToQtiWorksHandler

use of org.olat.ims.qti21.model.xml.OnyxToQtiWorksHandler 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 2 with OnyxToQtiWorksHandler

use of org.olat.ims.qti21.model.xml.OnyxToQtiWorksHandler 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)

Example 3 with OnyxToQtiWorksHandler

use of org.olat.ims.qti21.model.xml.OnyxToQtiWorksHandler 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;
    }
}
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 OnyxToQtiWorksHandler

use of org.olat.ims.qti21.model.xml.OnyxToQtiWorksHandler 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);
    }
}
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)4 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)4 OnyxToQtiWorksHandler (org.olat.ims.qti21.model.xml.OnyxToQtiWorksHandler)4 InputStream (java.io.InputStream)2 Writer (java.io.Writer)2 SAXParser (javax.xml.parsers.SAXParser)2 FactoryConfigurationError (javax.xml.stream.FactoryConfigurationError)2 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)2 ShieldInputStream (org.olat.core.util.io.ShieldInputStream)2 Onyx38ToQtiWorksHandler (org.olat.ims.qti21.model.xml.Onyx38ToQtiWorksHandler)2 QTI21Infos (org.olat.ims.qti21.model.xml.QTI21Infos)2 DefaultHandler2 (org.xml.sax.ext.DefaultHandler2)2