use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexSyntaxException in project muikku by otavanopisto.
the class DeusNexContentParser method parseContent.
public Map<String, String> parseContent(Element documentElement) throws DeusNexException {
if ("styledocument".equals(documentElement.getTagName())) {
return parseStyleDocument(documentElement);
}
if (!"document".equals(documentElement.getTagName())) {
throw new DeusNexSyntaxException("Invalid content document");
}
Map<String, String> contents = new HashMap<>();
Document ownerDocument = documentElement.getOwnerDocument();
try {
List<Element> localeDocuments = DeusNexXmlUtils.getElementsByXPath(documentElement, "fckdocument");
for (Element localeDocument : localeDocuments) {
String lang = localeDocument.getAttribute("lang");
if (StringUtils.isBlank(lang)) {
throw new DeusNexSyntaxException("Locale document does not specify lang");
}
// TODO: Sometimes document and fckdocument nodes are duplicated, when
// and why?
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 = localeDocument.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 = localeDocument.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 = localeDocument.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 = localeDocument.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 = localeDocument.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 = localeDocument.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 = localeDocument.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(lang, DeusNexXmlUtils.serializeElement(htmlElement, true, false, "xml"));
}
} catch (XPathExpressionException | TransformerException e) {
throw new DeusNexInternalException("Internal Error occurred while processing document", e);
}
return contents;
}
use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexSyntaxException 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;
}
use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexSyntaxException 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;
}
Aggregations