Search in sources :

Example 1 with Binary

use of fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Binary 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)

Aggregations

DeusNexInternalException (fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException)1 Binary (fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Binary)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 CDATASection (org.w3c.dom.CDATASection)1 DOMException (org.w3c.dom.DOMException)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 Text (org.w3c.dom.Text)1