Search in sources :

Example 16 with XMLParser

use of org.olat.core.util.xml.XMLParser 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 17 with XMLParser

use of org.olat.core.util.xml.XMLParser in project openolat by klemens.

the class IMSLoader method loadIMSDocument.

/**
 * Reads an IMS XML Document if supported by /org/olat/ims/resources.
 * @param documentF
 * @return document
 */
public static Document loadIMSDocument(VFSLeaf documentF) {
    InputStream in = null;
    BufferedInputStream bis = null;
    Document doc = null;
    try {
        in = documentF.getInputStream();
        bis = new BufferedInputStream(in);
        XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
        doc = xmlParser.parse(bis, false);
    } catch (Exception e) {
        return null;
    } finally {
        try {
            if (in != null)
                in.close();
            if (bis != null)
                bis.close();
        } catch (Exception e) {
        // we did our best to close the inputStream
        }
    }
    return doc;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Document(org.dom4j.Document) XMLParser(org.olat.core.util.xml.XMLParser)

Example 18 with XMLParser

use of org.olat.core.util.xml.XMLParser in project openolat by klemens.

the class IMSLoader method loadIMSDocument.

/**
 * Reads an IMS XML Document if supported by /org/olat/ims/resources.
 * @param documentF
 * @return document
 */
public static Document loadIMSDocument(File documentF) {
    FileInputStream in = null;
    BufferedInputStream bis = null;
    Document doc = null;
    try {
        in = new FileInputStream(documentF);
        bis = new BufferedInputStream(in);
        XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
        doc = xmlParser.parse(bis, false);
    } catch (Exception e) {
        return null;
    } finally {
        try {
            if (in != null)
                in.close();
            if (bis != null)
                bis.close();
        } catch (Exception e) {
        // we did our best to close the inputStream
        }
    }
    return doc;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) Document(org.dom4j.Document) XMLParser(org.olat.core.util.xml.XMLParser) FileInputStream(java.io.FileInputStream)

Example 19 with XMLParser

use of org.olat.core.util.xml.XMLParser 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)

Example 20 with XMLParser

use of org.olat.core.util.xml.XMLParser 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)

Aggregations

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