Search in sources :

Example 11 with DeusNexInternalException

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;
}
Also used : DOMException(org.w3c.dom.DOMException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Element(org.w3c.dom.Element) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) DeusNexSyntaxException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexSyntaxException) Document(fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Document)

Example 12 with DeusNexInternalException

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();
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) ArrayList(java.util.ArrayList) WorkspaceMaterialAssignmentType(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType) Document(org.w3c.dom.Document) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) XPathExpressionException(javax.xml.xpath.XPathExpressionException) TransformerException(javax.xml.transform.TransformerException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) JsonParseException(org.codehaus.jackson.JsonParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)

Aggregations

DeusNexInternalException (fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException)12 XPathExpressionException (javax.xml.xpath.XPathExpressionException)10 IOException (java.io.IOException)8 TransformerException (javax.xml.transform.TransformerException)7 SAXException (org.xml.sax.SAXException)7 WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)6 ArrayList (java.util.ArrayList)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 Element (org.w3c.dom.Element)5 Resource (fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Resource)4 DeusNexException (fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexException)3 DeusNexSyntaxException (fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexSyntaxException)3 DeusNexDocument (fi.otavanopisto.muikku.plugins.dnm.parser.structure.DeusNexDocument)3 WorkspaceFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)3 WorkspaceMaterialAssignmentType (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType)3 HashMap (java.util.HashMap)3 Document (org.w3c.dom.Document)3 Node (org.w3c.dom.Node)3 NodeList (org.w3c.dom.NodeList)3 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)2