use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException in project muikku by otavanopisto.
the class DeusNexStructureParser method parseLink.
private Resource parseLink(Element resourceElement) throws DeusNexInternalException {
// TODO: Add support for proper link materials
Document document = new Document();
Element linkElement = resourceElement.getOwnerDocument().createElement("a");
try {
linkElement.setAttribute("href", DeusNexXmlUtils.getChildValue(resourceElement, "path"));
linkElement.setTextContent(DeusNexXmlUtils.getChildValue(resourceElement, "title"));
parseBasicResourceProperties(resourceElement, document);
Element documentElement = resourceElement.getOwnerDocument().createElement("document");
Element fckDocumentElement = resourceElement.getOwnerDocument().createElement("fckdocument");
fckDocumentElement.setAttribute("lang", "fi");
fckDocumentElement.appendChild(linkElement);
documentElement.appendChild(fckDocumentElement);
document.setDocument(documentElement);
} catch (DOMException | XPathExpressionException | DeusNexSyntaxException e) {
throw new DeusNexInternalException("Link parsing failed", e);
}
return document;
}
use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException in project muikku by otavanopisto.
the class MaterialUnEmbedder method unembedHtmlMaterial.
private void unembedHtmlMaterial(WorkspaceNode parent, WorkspaceMaterial workspaceMaterial, HtmlMaterial htmlMaterial) throws DeusNexInternalException, IOException {
String html = htmlMaterial.getHtml();
if (StringUtils.isNotBlank(html)) {
InputStream is = null;
logger.info("Unembedding html material " + htmlMaterial.getId());
try {
is = new ByteArrayInputStream(html.getBytes("UTF-8"));
DocumentBuilder builder = DeusNexXmlUtils.createDocumentBuilder();
Document document = builder.parse(is);
if (document != null) {
NodeList iframes = DeusNexXmlUtils.findNodesByXPath(document.getDocumentElement(), "//iframe[@data-type='embedded-document']");
if (iframes.getLength() > 0) {
List<Document> splittedHtmlDocument = splitHtmlDocument(document);
for (int i = 0; i < splittedHtmlDocument.size(); i++) {
List<Long> pieceList = new ArrayList<Long>();
htmlMaterialPieces.put(htmlMaterial.getId(), pieceList);
HashMap<Long, WorkspaceMaterialAssignmentType> assignmentTypes = new HashMap<Long, WorkspaceMaterialAssignmentType>();
Document documentPiece = splittedHtmlDocument.get(i);
List<HtmlMaterial> pieceHtmlMaterials;
if (isEmbedPiece(documentPiece)) {
WorkspaceMaterialAssignmentType assignmentType = embeddedHtmlMaterialAssignmentType(documentPiece);
pieceHtmlMaterials = new ArrayList<HtmlMaterial>();
long embeddedHtmlMaterialId = embeddedHtmlMaterialId(documentPiece);
if (htmlMaterialPieces.containsKey(embeddedHtmlMaterialId)) {
for (Long htmlMaterialId : htmlMaterialPieces.get(embeddedHtmlMaterialId)) {
logger.info("Existing html material " + htmlMaterialId + " embedded in " + htmlMaterial.getId());
HtmlMaterial pieceHtmlMaterial = htmlMaterialController.findHtmlMaterialById(htmlMaterialId);
pieceHtmlMaterials.add(pieceHtmlMaterial);
pieceList.add(pieceHtmlMaterial.getId());
assignmentTypes.put(pieceHtmlMaterial.getId(), assignmentType);
}
} else {
HtmlMaterial pieceHtmlMaterial = htmlMaterialController.findHtmlMaterialById(embeddedHtmlMaterialId);
logger.info("Existing html material " + embeddedHtmlMaterialId + " embedded in " + htmlMaterial.getId());
pieceHtmlMaterials.add(pieceHtmlMaterial);
pieceList.add(pieceHtmlMaterial.getId());
assignmentTypes.put(pieceHtmlMaterial.getId(), assignmentType);
}
} else {
String license = null;
HtmlMaterial pieceHtmlMaterial = htmlMaterialController.createHtmlMaterial(htmlMaterial.getTitle() + " (" + i + ")", DeusNexXmlUtils.serializeElement(documentPiece.getDocumentElement(), true, false, "xml"), "text/html; editor=CKEditor", 0l, license);
logger.info("New html material piece " + pieceHtmlMaterial.getId() + " split from " + htmlMaterial.getId());
pieceHtmlMaterials = new ArrayList<HtmlMaterial>();
pieceHtmlMaterials.add(pieceHtmlMaterial);
pieceList.add(pieceHtmlMaterial.getId());
}
for (HtmlMaterial pieceHtmlMaterial : pieceHtmlMaterials) {
WorkspaceNode newNode = workspaceMaterialController.createWorkspaceMaterial(parent, pieceHtmlMaterial, assignmentTypes.get(pieceHtmlMaterial.getId()), WorkspaceMaterialCorrectAnswersDisplay.ALWAYS);
workspaceMaterialController.moveAbove(newNode, workspaceMaterial);
}
}
workspaceMaterialController.deleteWorkspaceMaterial(workspaceMaterial, true);
htmlMaterialController.deleteHtmlMaterial(htmlMaterial);
} else {
logger.info("Html material " + htmlMaterial.getId() + " has no embeds");
}
}
} catch (Exception e) {
logger.log(Level.SEVERE, "Html material " + htmlMaterial.getId() + " unembed fail", e);
throw new DeusNexInternalException("MaterialUnEmbedder:unembedHtmlMaterial", e);
} finally {
if (is != null) {
is.close();
}
}
}
}
Aggregations