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 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); if ( ctx.gc.htmlNode != null ) throw new IllegalStateException("html node already defined at daml node"); ctx.gc.htmlNode = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "html"); String lang = LangHelper.getLangNorec(node); if ( lang != null ) LangHelper.setLangNorec(ctx.gc.htmlNode, lang); node.getParentNode().replaceChild(ctx.gc.htmlNode, node); ctx.gc.htmlNode.appendChild(ctx.doc.createTextNode("\n")); ctx.gc.htmlNode.appendChild(ctx.doc.createComment(" This file is automatically generated. Do not edit! ")); ctx.gc.htmlNode.appendChild(ctx.doc.createTextNode("\n")); if ( ctx.gc.headNode != null ) throw new IllegalStateException("head node already defined at daml node"); ctx.gc.headNode = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "head"); ctx.gc.htmlNode.appendChild(ctx.gc.headNode); ctx.gc.htmlNode.appendChild(ctx.doc.createTextNode("\n")); ctx.gc.headNode.appendChild(ctx.doc.createTextNode("\n")); ctx.gc.styleContent = new StringBuffer(); try { Reader rd = new BufferedReader(new InputStreamReader(DamlEngine.class.getResourceAsStream("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(DamlEngine.class.getResourceAsStream("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, "http-equiv", "Content-Type"); meta.setAttributeNS(null, "content", "text/html; charset=utf-8"); ctx.gc.headNode.appendChild(meta); ctx.gc.headNode.appendChild(ctx.doc.createTextNode("\n")); if ( lang != null ) { meta = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "meta"); meta.setAttributeNS(null, "http-equiv", "Content-Language"); meta.setAttributeNS(null, "content", lang); ctx.gc.headNode.appendChild(meta); ctx.gc.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")); ctx.gc.headNode.appendChild(meta); ctx.gc.headNode.appendChild(ctx.doc.createTextNode("\n")); ArrayList childList = getChildList(this.node); ArrayList toProcess = new ArrayList(childList.size()); for ( Node child : childList ) { if ( child.getNodeType() == Node.ELEMENT_NODE ) { if ( child.getLocalName().equals("body") ) { ctx.gc.htmlNode.appendChild(ctx.doc.createTextNode("\n")); ctx.gc.htmlNode.appendChild(child); ctx.gc.htmlNode.appendChild(ctx.doc.createTextNode("\n")); } else { ctx.gc.headNode.appendChild(child); ctx.gc.headNode.appendChild(ctx.doc.createTextNode("\n")); } TodoElement it = TodoElement.getTodoElement((Element)child, this.ctx, this); toProcess.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"); } } this.ownerDeque.registerAtStart(toProcess); this.ownerDeque. registerAtEnd(new TodoStyleOrScript(TodoStyleOrScript.Type.STYLE, this.ctx, this)); this.ownerDeque. registerAtEnd(new TodoStyleOrScript(TodoStyleOrScript.Type.SCRIPT, this.ctx, this)); } }