Search in sources :

Example 6 with DeusNexInternalException

use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException in project muikku by otavanopisto.

the class DeusNexContentParser method parseStyleDocument.

private Map<String, String> parseStyleDocument(Element documentElement) throws DeusNexException {
    Map<String, String> contents = new HashMap<>();
    Document ownerDocument = documentElement.getOwnerDocument();
    try {
        NodeList embeddedNodeList = documentElement.getElementsByTagName("ix:embedded");
        for (int i = embeddedNodeList.getLength() - 1; i >= 0; i--) {
            Element embeddedElement = (Element) embeddedNodeList.item(i);
            Node replacement = handleEmbedded(ownerDocument, embeddedElement);
            replaceElement(ownerDocument, embeddedElement, replacement);
        }
        NodeList ixImageNodeList = documentElement.getElementsByTagName("ix:image");
        for (int i = ixImageNodeList.getLength() - 1; i >= 0; i--) {
            Element ixImageElement = (Element) ixImageNodeList.item(i);
            Node replacement = handleIxImage(ownerDocument, ixImageElement);
            replaceElement(ownerDocument, ixImageElement, replacement);
        }
        NodeList embeddedItemNodeList = documentElement.getElementsByTagName("ix:embeddeditem");
        for (int i = embeddedItemNodeList.getLength() - 1; i >= 0; i--) {
            Element embeddedItemElement = (Element) embeddedItemNodeList.item(i);
            Node replacement = handleEmbeddedItem(ownerDocument, embeddedItemElement);
            replaceElement(ownerDocument, embeddedItemElement, replacement);
        }
        NodeList textFieldNodeList = documentElement.getElementsByTagName("ixf:textfield");
        for (int i = textFieldNodeList.getLength() - 1; i >= 0; i--) {
            Element element = (Element) textFieldNodeList.item(i);
            Node replacement = handleTextField(ownerDocument, element);
            replaceElement(ownerDocument, element, replacement);
        }
        NodeList memoFieldNodeList = documentElement.getElementsByTagName("ixf:memofield");
        for (int i = memoFieldNodeList.getLength() - 1; i >= 0; i--) {
            Element element = (Element) memoFieldNodeList.item(i);
            Node replacement = handleMemoField(ownerDocument, element);
            replaceElement(ownerDocument, element, replacement);
        }
        NodeList optionListNodeList = documentElement.getElementsByTagName("ixf:optionlist");
        for (int i = optionListNodeList.getLength() - 1; i >= 0; i--) {
            Element element = (Element) optionListNodeList.item(i);
            Node replacement = handleOptionListField(ownerDocument, element);
            replaceElement(ownerDocument, element, replacement);
        }
        NodeList connectFieldNodeList = documentElement.getElementsByTagName("ixf:connectfield");
        for (int i = connectFieldNodeList.getLength() - 1; i >= 0; i--) {
            Element element = (Element) connectFieldNodeList.item(i);
            Node replacement = handleConnectField(ownerDocument, element);
            replaceElement(ownerDocument, element, replacement);
        }
        // ixf:uploadfilefield
        NodeList fileFieldNodeList = documentElement.getElementsByTagName("ixf:uploadfilefield");
        for (int i = fileFieldNodeList.getLength() - 1; i >= 0; i--) {
            Element element = (Element) fileFieldNodeList.item(i);
            Node replacement = handleFileField(ownerDocument, element);
            replaceElement(ownerDocument, element, replacement);
        }
        Element htmlElement = ownerDocument.createElement("html");
        Element bodyElement = ownerDocument.createElement("body");
        htmlElement.appendChild(bodyElement);
        NodeList childNodes = documentElement.getChildNodes();
        for (int i = childNodes.getLength() - 1; i >= 0; i--) {
            if (bodyElement.getFirstChild() != null) {
                bodyElement.insertBefore(childNodes.item(i), bodyElement.getFirstChild());
            } else {
                bodyElement.appendChild(childNodes.item(i));
            }
        }
        contents.put("fi", DeusNexXmlUtils.serializeElement(htmlElement, true, false, "xml"));
    } catch (XPathExpressionException | TransformerException e) {
        throw new DeusNexInternalException("Internal Error occurred while processing document", e);
    }
    return contents;
}
Also used : HashMap(java.util.HashMap) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException)

Example 7 with DeusNexInternalException

use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException in project muikku by otavanopisto.

the class DeusNexStructureParser method parseBinary.

private Binary parseBinary(Element resourceElement) throws DeusNexSyntaxException, XPathExpressionException, DeusNexInternalException {
    Binary binary = new Binary();
    parseBasicResourceProperties(resourceElement, binary);
    Element contentElement = (Element) DeusNexXmlUtils.findNodeByXPath(resourceElement, "content");
    Node contentChild = contentElement.getFirstChild();
    String textContent = null;
    if (contentChild instanceof CDATASection) {
        textContent = ((CDATASection) contentChild).getTextContent();
    } else if (contentChild instanceof Text) {
        textContent = ((Text) contentChild).getTextContent();
    }
    byte[] content;
    try {
        content = decodeDeusNexBinary(textContent);
    } catch (UnsupportedEncodingException | DOMException e) {
        throw new DeusNexInternalException("Could not retrieve binary content", e);
    }
    binary.setContent(content);
    binary.setContentType(DeusNexXmlUtils.getChildValue(resourceElement, "contentType"));
    return binary;
}
Also used : DOMException(org.w3c.dom.DOMException) CDATASection(org.w3c.dom.CDATASection) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Text(org.w3c.dom.Text) Binary(fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Binary)

Example 8 with DeusNexInternalException

use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException in project muikku by otavanopisto.

the class DeusNexStructureParser method parseDocument.

public DeusNexDocument parseDocument(InputStream inputStream) throws DeusNexException {
    DeusNexDocumentImpl deusNexDocument = new DeusNexDocumentImpl();
    DocumentBuilder builder = DeusNexXmlUtils.createDocumentBuilder();
    org.w3c.dom.Document domDocument;
    try {
        domDocument = builder.parse(inputStream);
    } catch (SAXException | IOException e1) {
        throw new DeusNexInternalException("XML Parsing failed", e1);
    }
    if (!"doc".equals(domDocument.getDocumentElement().getTagName())) {
        throw new DeusNexSyntaxException("Invalid root element");
    }
    try {
        deusNexDocument.setRootFolder(parseFolder(domDocument.getDocumentElement()));
    } catch (XPathExpressionException e) {
        throw new DeusNexInternalException("Invalid XPath expression", e);
    }
    return deusNexDocument;
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) XPathExpressionException(javax.xml.xpath.XPathExpressionException) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) DeusNexSyntaxException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexSyntaxException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 9 with DeusNexInternalException

use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException in project muikku by otavanopisto.

the class MaterialUnEmbedder method unembedWorkspaceMaterials.

@Lock
public void unembedWorkspaceMaterials(WorkspaceNode parentNode) throws DeusNexInternalException {
    try {
        loadHtmlMaterialPieces();
        for (WorkspaceNode node : workspaceMaterialController.listWorkspaceNodesByParent(parentNode)) {
            unembedWorkspaceNode(parentNode, node);
        }
        saveHtmlMaterialPieces();
    } catch (XPathExpressionException | SAXException | IOException | ParserConfigurationException | TransformerException | WorkspaceMaterialContainsAnswersExeption e) {
        throw new DeusNexInternalException(e.getClass().getName() + ": " + e.getMessage());
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) WorkspaceMaterialContainsAnswersExeption(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialContainsAnswersExeption) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException) Lock(javax.ejb.Lock)

Example 10 with DeusNexInternalException

use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException in project muikku by otavanopisto.

the class DeusNexMachinaController method determineEmbeddedAssignmentType.

private WorkspaceMaterialAssignmentType determineEmbeddedAssignmentType(HtmlMaterial material) throws DeusNexException {
    try {
        if (material.getHtml() == null) {
            return null;
        }
        StringReader htmlReader = new StringReader(material.getHtml());
        DOMParser parser = new DOMParser();
        InputSource inputSource = new InputSource(htmlReader);
        parser.parse(inputSource);
        org.w3c.dom.Document domDocument = parser.getDocument();
        List<Element> elements = DeusNexXmlUtils.getElementsByXPath(domDocument.getDocumentElement(), "//iframe[@data-type=\"embedded-document\"]");
        List<WorkspaceMaterialAssignmentType> assignmentTypes = new ArrayList<>();
        if (!elements.isEmpty()) {
            for (Element element : elements) {
                if ("EXERCISE".equals(element.getAttribute("data-assignment-type"))) {
                    assignmentTypes.add(WorkspaceMaterialAssignmentType.EXERCISE);
                }
                if ("EVALUATED".equals(element.getAttribute("data-assignment-type"))) {
                    assignmentTypes.add(WorkspaceMaterialAssignmentType.EVALUATED);
                }
            }
        }
        if (assignmentTypes.isEmpty() || (assignmentTypes.contains(WorkspaceMaterialAssignmentType.EXERCISE) && assignmentTypes.contains(WorkspaceMaterialAssignmentType.EVALUATED))) {
            return null;
        } else {
            return assignmentTypes.get(0);
        }
    } catch (SAXException | IOException | XPathExpressionException e) {
        throw new DeusNexInternalException("Embedded assignment type handling failed. ", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Element(org.w3c.dom.Element) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) ArrayList(java.util.ArrayList) WorkspaceMaterialAssignmentType(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) StringReader(java.io.StringReader) DOMParser(org.apache.xerces.parsers.DOMParser)

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