package org.madore.damlengine; import java.util.ArrayList; import java.util.regex.Pattern; import org.w3c.dom.*; public class TodoDamlElement extends TodoDefaultElement { public static class Factory extends TodoElement.Factory { public TodoDamlElement newItem(Element node, Context ctx, TodoItem.Options options) { return new TodoDamlElement(node, ctx, options); } } public TodoDamlElement(Element node, Context ctx, TodoItem.Options options) { super(node, ctx, options); } public static class DamlOptions extends TodoItem.Options { } public void handleNodeOnly() { if ( ! ( options instanceof DamlEngine.RootOptions ) ) throw new Error("daml node can only be root node"); final String uriToTopName = "uri-to-top"; if ( node.hasAttributeNS(null, uriToTopName) ) ctx.uriToTop = node.getAttributeNS(null, uriToTopName); final String fileNameName = "file.name"; if ( node.hasAttributeNS(null, fileNameName) ) ctx.fileName = node.getAttributeNS(null, fileNameName); if ( ctx.htmlNode != null ) throw new Error("html node already defined at daml node"); ctx.htmlNode = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "html"); String lang = LangHelper.getLangNorec(node); if ( lang != null ) LangHelper.setLangNorec(ctx.htmlNode, lang); node.getParentNode().replaceChild(ctx.htmlNode, node); ctx.htmlNode.appendChild(ctx.doc.createTextNode("\n")); ctx.htmlNode.appendChild(ctx.doc.createComment(" This file is automatically generated. Do not edit! ")); ctx.htmlNode.appendChild(ctx.doc.createTextNode("\n")); if ( ctx.headNode != null ) throw new Error("head node already defined at daml node"); ctx.headNode = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "head"); ctx.htmlNode.appendChild(ctx.headNode); ctx.htmlNode.appendChild(ctx.doc.createTextNode("\n")); ctx.headNode.appendChild(ctx.doc.createTextNode("\n")); ctx.styleContent = new StringBuffer(); ctx.scriptContent = new StringBuffer(); 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.headNode.appendChild(meta); ctx.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.headNode.appendChild(meta); ctx.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.uriToTop!=null)?ctx.uriToTop:"") +"favicon.ico")); ctx.headNode.appendChild(meta); ctx.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.htmlNode.appendChild(ctx.doc.createTextNode("\n")); ctx.htmlNode.appendChild(child); ctx.htmlNode.appendChild(ctx.doc.createTextNode("\n")); } else { ctx.headNode.appendChild(child); ctx.headNode.appendChild(ctx.doc.createTextNode("\n")); } TodoElement it = TodoElement.getTodoElement((Element)child, this.ctx, new DamlOptions()); 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 Error("daml element cannot contain text"); } } this.ownerDeque.registerAtStart(toProcess); this.ownerDeque. registerAtEnd(new TodoStyleOrScript(TodoStyleOrScript.Type.STYLE, this.ctx, new DamlOptions())); this.ownerDeque. registerAtEnd(new TodoStyleOrScript(TodoStyleOrScript.Type.SCRIPT, this.ctx, new DamlOptions())); } }