Search in sources :

Example 11 with IMSEntityResolver

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

the class QTI21QPoolServiceProvider method extractTextContent.

@Override
public String extractTextContent(QuestionItemFull item) {
    String content = null;
    if (item.getRootFilename() != null) {
        String dir = item.getDirectory();
        VFSContainer container = qpoolFileStorage.getContainer(dir);
        VFSItem file = container.resolve(item.getRootFilename());
        if (file instanceof VFSLeaf) {
            VFSLeaf leaf = (VFSLeaf) file;
            if (leaf.getSize() <= 0l) {
                return "";
            }
            QTI21SAXHandler handler = new QTI21SAXHandler();
            try (InputStream is = leaf.getInputStream()) {
                XMLReader parser = XMLReaderFactory.createXMLReader();
                parser.setContentHandler(handler);
                parser.setEntityResolver(new IMSEntityResolver());
                parser.setFeature("http://xml.org/sax/features/validation", false);
                parser.parse(new InputSource(is));
            } catch (Exception e) {
                log.error("Cannot read the XML file of the question item: " + leaf, e);
            }
            return handler.toString();
        }
    }
    return content;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver) XMLReader(org.xml.sax.XMLReader) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 12 with IMSEntityResolver

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

the class QTIImportProcessorTest method testImport_QTI12_multipleItems.

@Test
public void testImport_QTI12_multipleItems() throws IOException, URISyntaxException {
    URL itemsUrl = QTIImportProcessorTest.class.getResource("multiple_items.zip");
    Assert.assertNotNull(itemsUrl);
    File itemFile = new File(itemsUrl.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 ("Export (blue)".equals(itemFull.getTitle())) {
            Assert.assertTrue(exists(itemFull, "media/blue.png"));
            Assert.assertFalse(exists(itemFull, "media/purple.png"));
        } else if ("Export (purple)".equals(itemFull.getTitle())) {
            Assert.assertFalse(exists(itemFull, "media/blue.png"));
            Assert.assertTrue(exists(itemFull, "media/purple.png"));
        } 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 13 with IMSEntityResolver

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

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 14 with IMSEntityResolver

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

the class QTIStatisticsManagerLargeTest method getItemObjectList.

@SuppressWarnings("rawtypes")
private void getItemObjectList() {
    InputStream in = QTIStatisticsManagerLargeTest.class.getResourceAsStream("qti.xml");
    XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
    Document doc = xmlParser.parse(in, false);
    Element root = doc.getRootElement();
    List items = root.selectNodes("//item");
    itemObjects = new ArrayList<QTIItemObject>();
    for (Iterator iter = items.iterator(); iter.hasNext(); ) {
        Element el_item = (Element) iter.next();
        if (el_item.selectNodes(".//response_lid").size() > 0) {
            itemObjects.add(new ItemWithResponseLid(el_item));
        } else if (el_item.selectNodes(".//response_str").size() > 0) {
            itemObjects.add(new ItemWithResponseStr(el_item));
        }
    }
}
Also used : ItemWithResponseLid(org.olat.ims.qti.export.helper.ItemWithResponseLid) QTIItemObject(org.olat.ims.qti.export.helper.QTIItemObject) InputStream(java.io.InputStream) Element(org.dom4j.Element) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ItemWithResponseStr(org.olat.ims.qti.export.helper.ItemWithResponseStr) XMLParser(org.olat.core.util.xml.XMLParser) Document(org.dom4j.Document) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver)

Example 15 with IMSEntityResolver

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

the class FilePersister method retreiveResultsReporting.

/**
 * Retreive results for this user/aiid
 *
 * @param type The type of results
 * @return
 */
public static Document retreiveResultsReporting(Identity subj, String type, long aiid) {
    File fUserdataRoot = new File(WebappHelper.getUserDataRoot());
    String path = RES_REPORTING + File.separator + subj.getName() + File.separator + type + File.separator + aiid + ".xml";
    File fDoc = new File(fUserdataRoot, path);
    Document doc = null;
    try {
        InputStream is = new FileInputStream(fDoc);
        BufferedInputStream bis = new BufferedInputStream(is);
        XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
        doc = xmlParser.parse(bis, false);
        is.close();
        bis.close();
    } catch (Exception e) {
        throw new OLATRuntimeException(FilePersister.class, "Error retrieving results reporting for subject: '" + subj.getName() + "'; assessment id: '" + aiid + "'", e);
    }
    return doc;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) BufferedInputStream(java.io.BufferedInputStream) ObjectInputStream(java.io.ObjectInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Document(org.dom4j.Document) XMLParser(org.olat.core.util.xml.XMLParser) File(java.io.File) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException)

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