Search in sources :

Example 21 with IMSEntityResolver

use of org.olat.ims.resources.IMSEntityResolver in project OpenOLAT by OpenOLAT.

the class QTIImportProcessorTest method testImport_QTI12_processAttachments_matimg.

@Test
public void testImport_QTI12_processAttachments_matimg() throws IOException, URISyntaxException {
    URL itemUrl = QTIImportProcessorTest.class.getResource("mchc_asmimr_106.zip");
    Assert.assertNotNull(itemUrl);
    File itemFile = new File(itemUrl.toURI());
    // get the document informations
    QTIImportProcessor proc = new QTIImportProcessor(owner, Locale.ENGLISH, itemFile.getName(), itemFile);
    List<QuestionItem> items = proc.process();
    Assert.assertNotNull(items);
    Assert.assertEquals(3, items.size());
    dbInstance.commitAndCloseSession();
    // check the files
    for (QuestionItem item : items) {
        QuestionItemFull itemFull = (QuestionItemFull) item;
        String dir = itemFull.getDirectory();
        String file = itemFull.getRootFilename();
        VFSContainer itemContainer = qpoolFileStorage.getContainer(dir);
        Assert.assertNotNull(itemContainer);
        VFSItem itemLeaf = itemContainer.resolve(file);
        Assert.assertNotNull(itemLeaf);
        Assert.assertTrue(itemLeaf instanceof VFSLeaf);
        // try to parse it
        InputStream is = ((VFSLeaf) itemLeaf).getInputStream();
        XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
        Document doc = xmlParser.parse(is, false);
        Node itemNode = doc.selectSingleNode("questestinterop/item");
        Assert.assertNotNull(itemNode);
        // check the attachments
        if ("Rivers in Germany".equals(itemFull.getTitle())) {
            Assert.assertFalse(exists(itemFull, "image1.gif"));
            Assert.assertFalse(exists(itemFull, "image2.gif"));
            Assert.assertFalse(exists(itemFull, "image3.gif"));
            Assert.assertTrue(exists(itemFull, "images/image02.jpg"));
        } else if ("Capital of France".equals(itemFull.getTitle())) {
            Assert.assertTrue(exists(itemFull, "image1.gif"));
            Assert.assertTrue(exists(itemFull, "image2.gif"));
            Assert.assertFalse(exists(itemFull, "image3.gif"));
            Assert.assertFalse(exists(itemFull, "images/image02.jpg"));
        } else if ("Rivers in France question".equals(itemFull.getTitle())) {
            Assert.assertFalse(exists(itemFull, "image1.gif"));
            Assert.assertTrue(exists(itemFull, "image2.gif"));
            Assert.assertTrue(exists(itemFull, "image3.gif"));
            Assert.assertFalse(exists(itemFull, "images/image02.jpg"));
        } else {
            Assert.fail();
        }
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) InputStream(java.io.InputStream) VFSContainer(org.olat.core.util.vfs.VFSContainer) Node(org.dom4j.Node) VFSItem(org.olat.core.util.vfs.VFSItem) Document(org.dom4j.Document) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver) URL(java.net.URL) QuestionItemFull(org.olat.modules.qpool.QuestionItemFull) XMLParser(org.olat.core.util.xml.XMLParser) File(java.io.File) QuestionItem(org.olat.modules.qpool.QuestionItem) Test(org.junit.Test)

Example 22 with IMSEntityResolver

use of org.olat.ims.resources.IMSEntityResolver in project OpenOLAT by OpenOLAT.

the class QTIImportProcessorTest method testImport_OpenOLATTest_process.

@Test
public void testImport_OpenOLATTest_process() throws IOException, URISyntaxException {
    URL itemUrl = QTIImportProcessorTest.class.getResource("oo_test_qti.xml");
    Assert.assertNotNull(itemUrl);
    File itemFile = new File(itemUrl.toURI());
    // get the document informations
    QTIImportProcessor proc = new QTIImportProcessor(owner, Locale.ENGLISH, itemFile.getName(), itemFile);
    List<QuestionItem> items = proc.process();
    Assert.assertNotNull(items);
    Assert.assertEquals(4, items.size());
    dbInstance.commitAndCloseSession();
    // check
    int sc = 0;
    int mc = 0;
    int kprim = 0;
    int fib = 0;
    for (QuestionItem item : items) {
        Assert.assertEquals(QTIConstants.QTI_12_FORMAT, item.getFormat());
        QItemType itemType = item.getType();
        Assert.assertNotNull(itemType);
        QuestionType type = QuestionType.valueOf(itemType.getType().toUpperCase());
        if (type != null) {
            switch(type) {
                case SC:
                    sc++;
                    break;
                case MC:
                    mc++;
                    break;
                case KPRIM:
                    kprim++;
                    break;
                case FIB:
                    fib++;
                    break;
                default:
                    {
                        Assert.fail("No question type");
                    }
            }
        }
    }
    Assert.assertEquals("1 single choice", 1, sc);
    Assert.assertEquals("1 multiple choice", 1, mc);
    Assert.assertEquals("1 krpim", 1, kprim);
    Assert.assertEquals("1 fill-in-blanck", 1, fib);
    // check the files
    for (QuestionItem item : items) {
        QuestionItemFull itemFull = (QuestionItemFull) item;
        String dir = itemFull.getDirectory();
        String file = itemFull.getRootFilename();
        VFSContainer itemContainer = qpoolFileStorage.getContainer(dir);
        Assert.assertNotNull(itemContainer);
        VFSItem itemLeaf = itemContainer.resolve(file);
        Assert.assertNotNull(itemLeaf);
        Assert.assertTrue(itemLeaf instanceof VFSLeaf);
        // try to parse it
        InputStream is = ((VFSLeaf) itemLeaf).getInputStream();
        XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
        Document doc = xmlParser.parse(is, false);
        Node itemNode = doc.selectSingleNode("questestinterop/item");
        Assert.assertNotNull(itemNode);
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) InputStream(java.io.InputStream) VFSContainer(org.olat.core.util.vfs.VFSContainer) Node(org.dom4j.Node) QuestionType(org.olat.modules.qpool.QuestionType) VFSItem(org.olat.core.util.vfs.VFSItem) Document(org.dom4j.Document) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver) URL(java.net.URL) QItemType(org.olat.modules.qpool.model.QItemType) QuestionItemFull(org.olat.modules.qpool.QuestionItemFull) XMLParser(org.olat.core.util.xml.XMLParser) File(java.io.File) QuestionItem(org.olat.modules.qpool.QuestionItem) Test(org.junit.Test)

Example 23 with IMSEntityResolver

use of org.olat.ims.resources.IMSEntityResolver in project OpenOLAT by OpenOLAT.

the class QTI12To21ConverterTest method loadDocument.

private QTIDocument loadDocument(String filename) {
    try (InputStream in = QTI12To21ConverterTest.class.getResourceAsStream(filename)) {
        XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
        Document doc = xmlParser.parse(in, true);
        ParserManager parser = new ParserManager();
        return (QTIDocument) parser.parse(doc);
    } catch (Exception e) {
        log.error("Exception when parsing input QTI input stream for " + filename, e);
        return null;
    }
}
Also used : ParserManager(org.olat.ims.qti.editor.beecom.parser.ParserManager) QTIDocument(org.olat.ims.qti.editor.beecom.objects.QTIDocument) InputStream(java.io.InputStream) XMLParser(org.olat.core.util.xml.XMLParser) Document(org.dom4j.Document) QTIDocument(org.olat.ims.qti.editor.beecom.objects.QTIDocument) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver) XmlResourceNotFoundException(uk.ac.ed.ph.jqtiplus.xmlutils.XmlResourceNotFoundException) URISyntaxException(java.net.URISyntaxException) BadResourceException(uk.ac.ed.ph.jqtiplus.provision.BadResourceException) ImsManifestException(uk.ac.ed.ph.jqtiplus.utils.contentpackaging.ImsManifestException) IOException(java.io.IOException)

Example 24 with IMSEntityResolver

use of org.olat.ims.resources.IMSEntityResolver in project OpenOLAT by OpenOLAT.

the class QTIEditorPackageImpl method loadQTIDocument.

/**
 * Load a document from file.
 *
 * @return the loaded document or null if loading failed
 */
private Document loadQTIDocument() {
    File fIn = null;
    FileInputStream in = null;
    BufferedInputStream bis = null;
    Document doc = null;
    try {
        fIn = new File(packageDir, ImsRepositoryResolver.QTI_FILE);
        in = new FileInputStream(fIn);
        bis = new BufferedInputStream(in);
        XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
        doc = xmlParser.parse(bis, true);
    } catch (Exception e) {
        log.warn("Exception when parsing input QTI input stream for " + fIn != null ? fIn.getAbsolutePath() : "qti.xml", e);
        return null;
    } finally {
        try {
            if (in != null)
                in.close();
            if (bis != null)
                bis.close();
        } catch (Exception e) {
            throw new OLATRuntimeException(this.getClass(), "Could not close input file stream ", e);
        }
    }
    return doc;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) Document(org.dom4j.Document) QTIDocument(org.olat.ims.qti.editor.beecom.objects.QTIDocument) XMLParser(org.olat.core.util.xml.XMLParser) File(java.io.File) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver) FileInputStream(java.io.FileInputStream) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) IOException(java.io.IOException)

Example 25 with IMSEntityResolver

use of org.olat.ims.resources.IMSEntityResolver in project OpenOLAT by OpenOLAT.

the class ItemFileResourceValidator method validateDocument.

private boolean validateDocument(Document in) {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(true);
        factory.setNamespaceAware(true);
        SimpleErrorHandler errorHandler = new SimpleErrorHandler();
        ItemContentHandler contentHandler = new ItemContentHandler();
        SAXParser parser = factory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setEntityResolver(new IMSEntityResolver());
        reader.setErrorHandler(errorHandler);
        reader.setContentHandler(contentHandler);
        SAXValidator validator = new SAXValidator(reader);
        validator.validate(in);
        return errorHandler.isValid() && contentHandler.isItem();
    } catch (ParserConfigurationException e) {
        return false;
    } catch (SAXException e) {
        return false;
    } catch (Exception e) {
        return false;
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver) SAXValidator(org.dom4j.io.SAXValidator) XMLReader(org.xml.sax.XMLReader) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Aggregations

IMSEntityResolver (org.olat.ims.resources.IMSEntityResolver)38 Document (org.dom4j.Document)30 XMLParser (org.olat.core.util.xml.XMLParser)30 InputStream (java.io.InputStream)28 IOException (java.io.IOException)22 File (java.io.File)14 VFSItem (org.olat.core.util.vfs.VFSItem)14 VFSContainer (org.olat.core.util.vfs.VFSContainer)12 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)12 QTIDocument (org.olat.ims.qti.editor.beecom.objects.QTIDocument)10 FileInputStream (java.io.FileInputStream)8 URL (java.net.URL)8 Node (org.dom4j.Node)8 Test (org.junit.Test)8 QuestionItem (org.olat.modules.qpool.QuestionItem)8 QuestionItemFull (org.olat.modules.qpool.QuestionItemFull)8 BufferedInputStream (java.io.BufferedInputStream)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Element (org.dom4j.Element)6 AssertException (org.olat.core.logging.AssertException)6