Search in sources :

Example 16 with IMSEntityResolver

use of org.olat.ims.resources.IMSEntityResolver in project openolat by klemens.

the class TestFileResource method getQTIDocument.

public static QTIDocument getQTIDocument(OLATResource resource) {
    File packageDir = FileResourceManager.getInstance().unzipFileResource(resource);
    File qtiFile = new File(packageDir, ImsRepositoryResolver.QTI_FILE);
    try (InputStream in = new FileInputStream(qtiFile)) {
        XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
        Document doc = xmlParser.parse(in, true);
        ParserManager parser = new ParserManager();
        QTIDocument document = (QTIDocument) parser.parse(doc);
        return document;
    } catch (Exception e) {
        log.error("Exception when parsing input QTI input stream for ", e);
        return null;
    }
}
Also used : ParserManager(org.olat.ims.qti.editor.beecom.parser.ParserManager) QTIDocument(org.olat.ims.qti.editor.beecom.objects.QTIDocument) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XMLParser(org.olat.core.util.xml.XMLParser) Document(org.dom4j.Document) QTIDocument(org.olat.ims.qti.editor.beecom.objects.QTIDocument) File(java.io.File) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 17 with IMSEntityResolver

use of org.olat.ims.resources.IMSEntityResolver in project openolat by klemens.

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)

Example 18 with IMSEntityResolver

use of org.olat.ims.resources.IMSEntityResolver in project openolat by klemens.

the class ItemFileResourceValidator method readDocument.

private Document readDocument(InputStream in) {
    try {
        SAXReader reader = new SAXReader();
        reader.setEntityResolver(new IMSEntityResolver());
        reader.setValidation(false);
        return reader.read(in, "");
    } catch (Exception e) {
        return null;
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 19 with IMSEntityResolver

use of org.olat.ims.resources.IMSEntityResolver in project openolat by klemens.

the class QTIEditHelper method readItemXml.

public static Item readItemXml(VFSLeaf leaf) {
    Document doc = null;
    try {
        InputStream is = leaf.getInputStream();
        XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
        doc = xmlParser.parse(is, false);
        Element item = (Element) doc.selectSingleNode("questestinterop/item");
        ParserManager parser = new ParserManager();
        Item qtiItem = (Item) parser.parse(item);
        is.close();
        return qtiItem;
    } catch (Exception e) {
        log.error("", e);
        return null;
    }
}
Also used : ParserManager(org.olat.ims.qti.editor.beecom.parser.ParserManager) Item(org.olat.ims.qti.editor.beecom.objects.Item) VFSItem(org.olat.core.util.vfs.VFSItem) InputStream(java.io.InputStream) Element(org.dom4j.Element) Document(org.dom4j.Document) QTIDocument(org.olat.ims.qti.editor.beecom.objects.QTIDocument) XMLParser(org.olat.core.util.xml.XMLParser) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver) IOException(java.io.IOException)

Example 20 with IMSEntityResolver

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

the class QTIImportProcessorTest method testImport_OpenOLATTest_processAttachments_mattext.

@Test
public void testImport_OpenOLATTest_processAttachments_mattext() throws IOException, URISyntaxException {
    URL itemUrl = QTIImportProcessorTest.class.getResource("oo_test_qti_attachments.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(2, 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 (itemFull.getType().getType().equalsIgnoreCase(QuestionType.SC.name())) {
            Assert.assertTrue(exists(itemFull, "media/image1.gif"));
            Assert.assertTrue(exists(itemFull, "media/image2.gif"));
            Assert.assertFalse(exists(itemFull, "media/image3.gif"));
        } else if (itemFull.getType().getType().equalsIgnoreCase(QuestionType.MC.name())) {
            Assert.assertFalse(exists(itemFull, "media/image1.gif"));
            Assert.assertTrue(exists(itemFull, "media/image2.gif"));
            Assert.assertTrue(exists(itemFull, "media/image3.gif"));
        } 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)

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