package org.madore.damlengine; import java.util.ArrayList; import java.util.regex.Pattern; import java.io.Reader; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import org.w3c.dom.*; public final class TodoDamlElement extends TodoDefaultElement { public static class Factory extends TodoElement.Factory { @Override public TodoDamlElement newItem(Element node, Context ctx, TodoItem caller) { return new TodoDamlElement(node, ctx, caller); } } public TodoDamlElement(Element node, Context ctx, TodoItem caller) { super(node, ctx, caller); } @Override public void handleNodeOnly() { if ( ! ( caller instanceof DamlEngine.RootTodo ) ) throw new IllegalArgumentException("daml node can only be root node"); final String uriToTopName = "uri-to-top"; if ( node.hasAttributeNS(null, uriToTopName) ) ctx.gc.uriToTop = node.getAttributeNS(null, uriToTopName); final String fileNameName = "file.name"; if ( node.hasAttributeNS(null, fileNameName) ) ctx.gc.fileName = node.getAttributeNS(null, fileNameName); Element htmlNode = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "html"); if ( ctx.gc.htmlNode != null ) throw new IllegalStateException("html node already defined at daml node"); ctx.gc.htmlNode = htmlNode; String lang = LangHelper.getLangNorec(node); if ( lang != null ) LangHelper.setLangNorec(htmlNode, lang); node.getParentNode().replaceChild(htmlNode, node); htmlNode.appendChild(ctx.doc.createTextNode("\n")); htmlNode.appendChild(ctx.doc.createComment(" This file is automatically generated. Do not edit! ")); htmlNode.appendChild(ctx.doc.createTextNode("\n")); Element headNode = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "head"); if ( ctx.gc.headNode != null ) throw new IllegalStateException("head node already defined at daml node"); ctx.gc.headNode = headNode; htmlNode.appendChild(headNode); htmlNode.appendChild(ctx.doc.createTextNode("\n")); headNode.appendChild(ctx.doc.createTextNode("\n")); Element bodyNode = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "body"); htmlNode.appendChild(bodyNode); htmlNode.appendChild(ctx.doc.createTextNode("\n")); bodyNode.setAttributeNS(null, "onload", "if (onLoad) { onLoad(); }"); Element earlyScript = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "script"); earlyScript.setAttributeNS(null, "type", "text/javascript"); earlyScript.appendChild(ctx.doc.createTextNode("if (onEarly) { onEarly(); }")); bodyNode.appendChild(ctx.doc.createTextNode("\n")); bodyNode.appendChild(earlyScript); ctx.gc.styleContent = new StringBuffer(); try { Reader rd = new BufferedReader(new InputStreamReader(Files.newInputStream(DamlEngine.templatePath.resolve("included.css")), "UTF-8")); int ch; while ((ch = rd.read()) > -1) ctx.gc.styleContent.append((char)ch); rd.close(); } catch (IOException e) { throw new RuntimeException(e); } ctx.gc.scriptContent = new StringBuffer(); try { Reader rd = new BufferedReader(new InputStreamReader(Files.newInputStream(DamlEngine.templatePath.resolve("included.js")), "UTF-8")); int ch; while ((ch = rd.read()) > -1) ctx.gc.scriptContent.append((char)ch); rd.close(); } catch (IOException e) { throw new RuntimeException(e); } Element meta; meta = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "meta"); meta.setAttributeNS(null, "charset", "utf-8"); headNode.appendChild(meta); headNode.appendChild(ctx.doc.createTextNode("\n")); meta = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "meta"); meta.setAttributeNS(null, "name", "viewport"); meta.setAttributeNS(null, "content", "width=device-width, initial-scale=1"); headNode.appendChild(meta); headNode.appendChild(ctx.doc.createTextNode("\n")); meta = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "link"); meta.setAttributeNS(null, "rel", "Shortcut Icon"); meta.setAttributeNS(null, "href", (((ctx.gc.uriToTop!=null)?ctx.gc.uriToTop:"") +"favicon.ico")); headNode.appendChild(meta); headNode.appendChild(ctx.doc.createTextNode("\n")); // Twitter summary card if ( ctx.wsc != null && ctx.wsc instanceof Context.WeblogSingleSelectionContext ) { meta = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "meta"); meta.setAttributeNS(null, "name", "twitter:card"); meta.setAttributeNS(null, "content", "summary"); headNode.appendChild(meta); headNode.appendChild(ctx.doc.createTextNode("\n")); meta = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "meta"); meta.setAttributeNS(null, "name", "twitter:site"); meta.setAttributeNS(null, "content", "@gro_tsen"); headNode.appendChild(meta); headNode.appendChild(ctx.doc.createTextNode("\n")); final WeblogSummary wsum = WeblogSummary.getSummary(ctx.dc); if ( wsum != null ) { final WeblogSummary.EntrySummary esum = wsum.entries.get(Integer.valueOf(((Context.WeblogSingleSelectionContext)(ctx.wsc)).number)); if ( esum != null && esum.title != null ) { meta = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "meta"); meta.setAttributeNS(null, "name", "twitter:title"); meta.setAttributeNS(null, "content", esum.title); headNode.appendChild(meta); headNode.appendChild(ctx.doc.createTextNode("\n")); } } } ArrayList childList = getChildList(this.node); ArrayList toProcessFirst = new ArrayList(); ArrayList toProcess = new ArrayList(childList.size()+8); Element header = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "header"); bodyNode.appendChild(ctx.doc.createTextNode("\n")); bodyNode.appendChild(header); Element hgroup = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "hgroup"); header.appendChild(ctx.doc.createTextNode("\n")); header.appendChild(hgroup); if ( node.getAttributeNS(null, "notitle").equals("") ) { Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS, "d:implicit-do-title"); hgroup.appendChild(ctx.doc.createTextNode("\n")); hgroup.appendChild(token); toProcess.add(new TodoTitleOrSubtitle(TodoTitleOrSubtitle.Type.TITLE, token, this.ctx, this)); } if ( node.getAttributeNS(null, "nosubtitle").equals("") ) { Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS, "d:implicit-do-subtitle"); hgroup.appendChild(ctx.doc.createTextNode("\n")); hgroup.appendChild(token); toProcess.add(new TodoTitleOrSubtitle(TodoTitleOrSubtitle.Type.SUBTITLE, token, this.ctx, this)); } if ( node.getAttributeNS(null, "nonavbar").equals("") ) { Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS, "d:implicit-do-navbar"); header.appendChild(ctx.doc.createTextNode("\n")); header.appendChild(token); toProcess.add(new TodoNavbar(token, this.ctx, this)); } if ( node.getAttributeNS(null, "notranslations").equals("") ) { Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS, "d:implicit-do-translations"); header.appendChild(ctx.doc.createTextNode("\n")); header.appendChild(token); toProcess.add(new TodoTranslations(token, this.ctx, this)); } for ( Node child : childList ) { if ( child.getNodeType() == Node.ELEMENT_NODE ) { if ( child.getLocalName().equals("body") || child.getLocalName().equals("weblog") ) { bodyNode.appendChild(child); bodyNode.appendChild(ctx.doc.createTextNode("\n")); TodoElement it = TodoElement.getTodoElement((Element)child, this.ctx, this); toProcess.add(it); } else { headNode.appendChild(child); headNode.appendChild(ctx.doc.createTextNode("\n")); TodoElement it = TodoElement.getTodoElement((Element)child, this.ctx, this); toProcessFirst.add(it); } } else if ( child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE ) { if ( ! Pattern.matches("^\\s*$",((CharacterData)child).getData()) ) throw new IllegalArgumentException("daml element cannot contain text"); } } if ( node.getAttributeNS(null, "nofooter").equals("") ) { Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS, "d:implicit-do-footer"); bodyNode.appendChild(token); bodyNode.appendChild(ctx.doc.createTextNode("\n")); toProcess.add(new TodoFooter(token, this.ctx, this)); } this.ownerDeque.registerAtStart(toProcess); this.ownerDeque.registerAtStart(toProcessFirst); this.ownerDeque. registerAtEnd(new TodoStyleOrScript. HeadStyleOrScript(TodoStyleOrScript.Type.STYLE, this.ctx, this)); this.ownerDeque. registerAtEnd(new TodoStyleOrScript. HeadStyleOrScript(TodoStyleOrScript.Type.SCRIPT, this.ctx, this)); } }