use of org.olat.core.util.xml.XMLParser in project openolat by klemens.
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;
}
}
use of org.olat.core.util.xml.XMLParser in project openolat by klemens.
the class ScormCPManifestTreeModel method loadDocument.
private Document loadDocument(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 (IOException e) {
throw new OLATRuntimeException(ScormCPManifestTreeModel.class, "could not read and parse from file " + documentF.getAbsolutePath(), e);
} 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;
}
use of org.olat.core.util.xml.XMLParser in project openolat by klemens.
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();
}
}
}
use of org.olat.core.util.xml.XMLParser in project openolat by klemens.
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();
}
}
}
use of org.olat.core.util.xml.XMLParser in project openolat by klemens.
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;
}
}
Aggregations