Search in sources :

Example 1 with FieldMeta

use of fi.otavanopisto.muikku.plugins.material.fieldmeta.FieldMeta in project muikku by otavanopisto.

the class MaterialFieldCollection method parseFields.

public void parseFields(String html) {
    if (!materialFields.isEmpty()) {
        materialFields.clear();
    }
    if (StringUtils.isNotBlank(html)) {
        StringReader htmlReader = new StringReader(html);
        try {
            DOMParser parser = new DOMParser();
            InputSource inputSource = new InputSource(htmlReader);
            parser.parse(inputSource);
            Document document = parser.getDocument();
            NodeList objectNodeList = document.getElementsByTagName("object");
            for (int i = 0, l = objectNodeList.getLength(); i < l; i++) {
                Node objectNode = objectNodeList.item(i);
                if (objectNode instanceof Element) {
                    Element objectElement = (Element) objectNode;
                    if (isMuikkuField(objectElement)) {
                        String fieldType = objectElement.getAttribute("type");
                        NodeList paramNodes = objectElement.getElementsByTagName("param");
                        String content = null;
                        for (int j = 0, jl = paramNodes.getLength(); j < jl; j++) {
                            Node paramNode = paramNodes.item(j);
                            if (paramNode instanceof Element) {
                                Element paramElement = (Element) paramNode;
                                if ("content".equals(paramElement.getAttribute("name"))) {
                                    content = paramElement.getAttribute("value");
                                    break;
                                }
                            }
                        }
                        if (StringUtils.isNotBlank(content)) {
                            ObjectMapper objectMapper = new ObjectMapper();
                            objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                            FieldMeta fieldMeta = objectMapper.readValue(content, FieldMeta.class);
                            materialFields.put(fieldMeta.getName(), new MaterialField(fieldMeta.getName(), fieldType, content));
                        }
                    }
                }
            }
        } catch (Exception e) {
            // TODO Proper exception handling
            throw new IllegalArgumentException("Malformed document structure: " + html);
        } finally {
            htmlReader.close();
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) FieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.FieldMeta) Document(org.w3c.dom.Document) StringReader(java.io.StringReader) DOMParser(org.cyberneko.html.parsers.DOMParser) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

FieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.FieldMeta)1 StringReader (java.io.StringReader)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 DOMParser (org.cyberneko.html.parsers.DOMParser)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 InputSource (org.xml.sax.InputSource)1