Search in sources :

Example 21 with XMLParser

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

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

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

the class MetaInfoFileImpl method parseXMLdom.

/**
 * Parse XML from file with SAX and fill-in MetaInfo attributes.
 * @param fMeta
 */
@Deprecated
public boolean parseXMLdom(File fMeta) {
    if (fMeta == null || !fMeta.exists())
        return false;
    InputStream is;
    try {
        is = new BufferedInputStream(new FileInputStream(fMeta));
    } catch (FileNotFoundException e) {
        return false;
    }
    try {
        XMLParser xmlp = new XMLParser();
        Document doc = xmlp.parse(is, false);
        if (doc == null)
            return false;
        // extract data from XML
        Element root = doc.getRootElement();
        Element n;
        n = root.element("author");
        if (n == null) {
            authorIdentKey = null;
        } else {
            if (n.getText().length() == 0) {
                authorIdentKey = null;
            } else {
                try {
                    authorIdentKey = Long.valueOf(n.getText());
                } catch (NumberFormatException nEx) {
                    authorIdentKey = null;
                }
            }
        }
        n = root.element("comment");
        comment = (n != null) ? n.getText() : "";
        Element lockEl = root.element("lock");
        if (lockEl != null) {
            locked = "true".equals(lockEl.attribute("locked").getValue());
            try {
                lockedByIdentKey = new Long(n.getText());
            } catch (NumberFormatException nEx) {
                lockedByIdentKey = null;
            }
        }
        n = root.element("title");
        title = (n != null) ? n.getText() : "";
        n = root.element("publisher");
        publisher = (n != null) ? n.getText() : "";
        n = root.element("source");
        source = (n != null) ? n.getText() : "";
        n = root.element("creator");
        creator = (n != null) ? n.getText() : "";
        n = root.element("city");
        city = (n != null) ? n.getText() : "";
        n = root.element("pages");
        pages = (n != null) ? n.getText() : "";
        n = root.element("language");
        language = (n != null) ? n.getText() : "";
        n = root.element("url");
        url = (n != null) ? n.getText() : "";
        n = root.element("licenseTypeKey");
        licenseTypeKey = (n != null) ? n.getText() : "";
        n = root.element("licenseName");
        licenseTypeName = (n != null) ? n.getText() : "";
        n = root.element("licenseText");
        licenseText = (n != null) ? n.getText() : "";
        n = root.element("licensor");
        licensor = (n != null) ? n.getText() : "";
        n = root.element("downloadCount");
        downloadCount = (n != null) ? Integer.valueOf(n.getText()) : 0;
        n = root.element("publicationDate");
        if (n != null) {
            Element m = n.element("month");
            pubMonth = (m != null) ? m.getText() : "";
            m = n.element("year");
            pubYear = (m != null) ? m.getText() : "";
        }
        return true;
    } catch (Exception ex) {
        log.warn("Corrupted metadata file: " + fMeta);
        return false;
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Element(org.dom4j.Element) FileNotFoundException(java.io.FileNotFoundException) XMLParser(org.olat.core.util.xml.XMLParser) Document(org.dom4j.Document) FileInputStream(java.io.FileInputStream) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException)

Example 24 with XMLParser

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

the class TextMarkerManagerImpl method loadTextMarkerList.

/**
 * @see org.olat.core.gui.control.generic.textmarker.TextMarkerManager#loadTextMarkerList(org.olat.core.util.vfs.VFSLeaf)
 */
public List<TextMarker> loadTextMarkerList(VFSLeaf textMarkerFile) {
    if (textMarkerFile == null) {
        // filename not defined at all
        return new ArrayList<TextMarker>();
    }
    XMLParser parser = new XMLParser();
    InputStream stream = textMarkerFile.getInputStream();
    if (stream == null) {
        // e.g. file was removed
        return new ArrayList<TextMarker>();
    }
    Document doc = parser.parse(stream, false);
    Element root = doc.getRootElement();
    if (root == null) {
        // file was empty;
        return new ArrayList<TextMarker>();
    }
    // Do version check. Not needed now, for future lazy migration code...
    Attribute versionAttribute = root.attribute(XML_VERSION_ATTRIBUTE);
    int version = (versionAttribute == null ? 1 : Integer.parseInt(versionAttribute.getStringValue()));
    if (version != VERSION) {
        // complain about version conflict or solve it
        throw new OLATRuntimeException("Could not load glossary entries due to version conflict. Loaded version was::" + version, null);
    }
    // parse text marker objects and put them into a list
    List markersElements = root.elements("textMarker");
    List<TextMarker> markers = new ArrayList<TextMarker>();
    Iterator iter = markersElements.iterator();
    while (iter.hasNext()) {
        Element textMarkerElement = (Element) iter.next();
        TextMarker textMarker = new TextMarker(textMarkerElement);
        markers.add(textMarker);
    }
    try {
        stream.close();
    } catch (IOException e) {
        throw new OLATRuntimeException(this.getClass(), "Error while closing text marker file stream", e);
    }
    return markers;
}
Also used : Attribute(org.dom4j.Attribute) InputStream(java.io.InputStream) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.dom4j.Document) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) XMLParser(org.olat.core.util.xml.XMLParser)

Example 25 with XMLParser

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

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