Search in sources :

Example 36 with XMLParser

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

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 37 with XMLParser

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

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 38 with XMLParser

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

the class QTIImportProcessor method readXml.

private Document readXml(InputStream in) {
    Document doc = null;
    try {
        XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
        doc = xmlParser.parse(in, false);
        return doc;
    } catch (Exception e) {
        return null;
    }
}
Also used : 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) NoSuchFileException(java.nio.file.NoSuchFileException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException)

Example 39 with XMLParser

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

the class QTIExportProcessor method readItemXml.

private Element 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");
        is.close();
        return item;
    } catch (Exception e) {
        log.error("", e);
        return null;
    }
}
Also used : InputStream(java.io.InputStream) Element(org.dom4j.Element) Document(org.dom4j.Document) XMLParser(org.olat.core.util.xml.XMLParser) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver) IOException(java.io.IOException)

Example 40 with XMLParser

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

the class IMSLoader method loadIMSDocument.

public static Document loadIMSDocument(Path documentPath) {
    Document doc = null;
    try (InputStream in = Files.newInputStream(documentPath)) {
        XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
        doc = xmlParser.parse(in, false);
    } catch (Exception e) {
        return null;
    }
    return doc;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Document(org.dom4j.Document) XMLParser(org.olat.core.util.xml.XMLParser)

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