use of org.olat.core.util.xml.XMLParser 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);
}
}
use of org.olat.core.util.xml.XMLParser 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));
}
}
}
use of org.olat.core.util.xml.XMLParser in project openolat by klemens.
the class CPManagerImpl method load.
/**
* @see org.olat.ims.cp.CPManager#load(org.olat.core.util.vfs.VFSContainer)
*/
public ContentPackage load(VFSContainer directory, OLATResourceable ores) {
XMLParser parser = new XMLParser();
ContentPackage cp;
VFSLeaf file = (VFSLeaf) directory.resolve("imsmanifest.xml");
if (file != null) {
try {
DefaultDocument doc = (DefaultDocument) parser.parse(file.getInputStream(), false);
cp = new ContentPackage(doc, directory, ores);
// identifier.
if (cp.getLastError() == null) {
if (cp.isOLATContentPackage() && CPCore.OLAT_ORGANIZATION_IDENTIFIER.equals(cp.getFirstOrganizationInManifest().getIdentifier())) {
setUniqueOrgaIdentifier(cp);
}
}
} catch (OLATRuntimeException e) {
cp = new ContentPackage(null, directory, ores);
logError("Reading imsmanifest failed. Dir: " + directory.getName() + ". Ores: " + ores.getResourceableId(), e);
cp.setLastError("Exception reading XML for IMS CP: invalid xml-file ( " + directory.getName() + ")");
}
} else {
cp = new ContentPackage(null, directory, ores);
cp.setLastError("Exception reading XML for IMS CP: IMS-Manifest not found in " + directory.getName());
logError("IMS manifiest xml couldn't be found in dir " + directory.getName() + ". Ores: " + ores.getResourceableId(), null);
throw new OLATRuntimeException(CPManagerImpl.class, "The imsmanifest.xml file was not found.", new IOException());
}
return cp;
}
use of org.olat.core.util.xml.XMLParser 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;
}
use of org.olat.core.util.xml.XMLParser in project openolat by klemens.
the class TestFileResource method getQTIDocument.
public static QTIDocument getQTIDocument(OLATResource resource) {
File packageDir = FileResourceManager.getInstance().unzipFileResource(resource);
File qtiFile = new File(packageDir, ImsRepositoryResolver.QTI_FILE);
try (InputStream in = new FileInputStream(qtiFile)) {
XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
Document doc = xmlParser.parse(in, true);
ParserManager parser = new ParserManager();
QTIDocument document = (QTIDocument) parser.parse(doc);
return document;
} catch (Exception e) {
log.error("Exception when parsing input QTI input stream for ", e);
return null;
}
}
Aggregations