use of org.olat.ims.resources.IMSEntityResolver in project OpenOLAT by OpenOLAT.
the class CPManifestTreeModel method loadDocument.
private Document loadDocument(String documentStr) throws IOException {
InputStream in = null;
Document doc = null;
try {
in = new ByteArrayInputStream(documentStr.getBytes());
XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
doc = xmlParser.parse(in, false);
in.close();
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new IOException("could not read and parse from string " + documentStr, e);
} finally {
IOUtils.closeQuietly(in);
}
return doc;
}
use of org.olat.ims.resources.IMSEntityResolver in project OpenOLAT by OpenOLAT.
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;
}
}
use of org.olat.ims.resources.IMSEntityResolver in project OpenOLAT by OpenOLAT.
the class QTIImportProcessorTest method testImport_QTI12_multipleItems.
@Test
public void testImport_QTI12_multipleItems() throws IOException, URISyntaxException {
URL itemsUrl = QTIImportProcessorTest.class.getResource("multiple_items.zip");
Assert.assertNotNull(itemsUrl);
File itemFile = new File(itemsUrl.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 ("Export (blue)".equals(itemFull.getTitle())) {
Assert.assertTrue(exists(itemFull, "media/blue.png"));
Assert.assertFalse(exists(itemFull, "media/purple.png"));
} else if ("Export (purple)".equals(itemFull.getTitle())) {
Assert.assertFalse(exists(itemFull, "media/blue.png"));
Assert.assertTrue(exists(itemFull, "media/purple.png"));
} else {
Assert.fail();
}
}
}
use of org.olat.ims.resources.IMSEntityResolver in project OpenOLAT by OpenOLAT.
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.ims.resources.IMSEntityResolver in project OpenOLAT by OpenOLAT.
the class QTI21QPoolServiceProvider method extractTextContent.
@Override
public String extractTextContent(QuestionItemFull item) {
String content = null;
if (item.getRootFilename() != null) {
String dir = item.getDirectory();
VFSContainer container = qpoolFileStorage.getContainer(dir);
VFSItem file = container.resolve(item.getRootFilename());
if (file instanceof VFSLeaf) {
VFSLeaf leaf = (VFSLeaf) file;
if (leaf.getSize() <= 0l) {
return "";
}
QTI21SAXHandler handler = new QTI21SAXHandler();
try (InputStream is = leaf.getInputStream()) {
XMLReader parser = XMLReaderFactory.createXMLReader();
parser.setContentHandler(handler);
parser.setEntityResolver(new IMSEntityResolver());
parser.setFeature("http://xml.org/sax/features/validation", false);
parser.parse(new InputSource(is));
} catch (Exception e) {
log.error("Cannot read the XML file of the question item: " + leaf, e);
}
return handler.toString();
}
}
return content;
}
Aggregations