From 58e0ddcdc2b5d47db753868b3cc937bbac59ec55 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Wed, 7 Sep 2011 14:21:29 +0200 Subject: Move body handling (header, footer, etc.) to the element. The element is now sort of useless. --- org/madore/damlengine/TodoBodyElement.java | 53 ++------------ org/madore/damlengine/TodoDamlElement.java | 96 +++++++++++++++++++------- org/madore/damlengine/TodoTitleOrSubtitle.java | 10 +++ org/madore/damlengine/TodoTranslations.java | 9 +++ 4 files changed, 95 insertions(+), 73 deletions(-) diff --git a/org/madore/damlengine/TodoBodyElement.java b/org/madore/damlengine/TodoBodyElement.java index 4286aa0..1db657b 100644 --- a/org/madore/damlengine/TodoBodyElement.java +++ b/org/madore/damlengine/TodoBodyElement.java @@ -26,68 +26,23 @@ public final class TodoBodyElement extends TodoDefaultElement { if ( ! ( caller instanceof TodoDamlElement ) ) throw new IllegalArgumentException("body node can only be child of daml node"); - Element bodyNode = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "body"); - String lang = LangHelper.getLangNorec(node); - if ( lang != null ) - LangHelper.setLangNorec(bodyNode, lang); - bodyNode.setAttributeNS(null, "onload", "onLoad()"); - node.getParentNode().replaceChild(bodyNode, node); - + Element parent = (Element)(this.node.getParentNode()); ArrayList childList = getChildList(this.node); - ArrayList toProcess = new ArrayList(childList.size()+8); - if ( node.getAttributeNS(null, "notitle").equals("") - && ctx.gc.title != null ) { - Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS, - "d:implicit-do-title"); - bodyNode.appendChild(ctx.doc.createTextNode("\n")); - bodyNode.appendChild(token); - toProcess.add(new TodoTitleOrSubtitle(TodoTitleOrSubtitle.Type.TITLE, - token, this.ctx, this)); - } - if ( node.getAttributeNS(null, "nosubtitle").equals("") - && ctx.gc.subtitle != null ) { - Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS, - "d:implicit-do-subtitle"); - bodyNode.appendChild(ctx.doc.createTextNode("\n")); - bodyNode.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"); - bodyNode.appendChild(ctx.doc.createTextNode("\n")); - bodyNode.appendChild(token); - toProcess.add(new TodoNavbar(token, this.ctx, this)); - } - if ( node.getAttributeNS(null, "notranslations").equals("") - && ctx.gc.translations != null ) { - Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS, - "d:implicit-do-translations"); - bodyNode.appendChild(ctx.doc.createTextNode("\n")); - bodyNode.appendChild(token); - toProcess.add(new TodoTranslations(token, this.ctx, this)); - } + ArrayList toProcess = new ArrayList(childList.size()); for ( Node child : childList ) { if ( child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE ) { if ( ! Pattern.matches("^\\s*$",((CharacterData)child).getData()) ) throw new IllegalArgumentException("body element cannot contain text"); } - bodyNode.appendChild(child); + parent.insertBefore(child, node); if ( child.getNodeType() == Node.ELEMENT_NODE ) { TodoElement it = TodoElement.getTodoElement((Element)child, this.ctx, this); toProcess.add(it); } } - 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)); - } + parent.removeChild(node); this.ownerDeque.registerAtStart(toProcess); } diff --git a/org/madore/damlengine/TodoDamlElement.java b/org/madore/damlengine/TodoDamlElement.java index 10696f1..5237d91 100644 --- a/org/madore/damlengine/TodoDamlElement.java +++ b/org/madore/damlengine/TodoDamlElement.java @@ -36,22 +36,28 @@ public final class TodoDamlElement extends TodoDefaultElement { 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 = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "html"); + ctx.gc.htmlNode = htmlNode; 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")); + 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 = 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.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", "onLoad()"); ctx.gc.styleContent = new StringBuffer(); try { @@ -77,37 +83,79 @@ public final class TodoDamlElement extends TodoDefaultElement { Element meta; meta = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "meta"); meta.setAttributeNS(null, "charset", "utf-8"); - ctx.gc.headNode.appendChild(meta); - ctx.gc.headNode.appendChild(ctx.doc.createTextNode("\n")); + 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")); - ctx.gc.headNode.appendChild(meta); - ctx.gc.headNode.appendChild(ctx.doc.createTextNode("\n")); + headNode.appendChild(meta); + headNode.appendChild(ctx.doc.createTextNode("\n")); ArrayList childList = getChildList(this.node); - ArrayList toProcess = new ArrayList(childList.size()); + ArrayList toProcessFirst = new ArrayList(); + ArrayList toProcess = new ArrayList(childList.size()+8); + if ( node.getAttributeNS(null, "notitle").equals("") ) { + Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS, + "d:implicit-do-title"); + bodyNode.appendChild(ctx.doc.createTextNode("\n")); + bodyNode.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"); + bodyNode.appendChild(ctx.doc.createTextNode("\n")); + bodyNode.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"); + bodyNode.appendChild(ctx.doc.createTextNode("\n")); + bodyNode.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"); + bodyNode.appendChild(ctx.doc.createTextNode("\n")); + bodyNode.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") ) { - ctx.gc.htmlNode.appendChild(ctx.doc.createTextNode("\n")); - ctx.gc.htmlNode.appendChild(child); - ctx.gc.htmlNode.appendChild(ctx.doc.createTextNode("\n")); + 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 { - ctx.gc.headNode.appendChild(child); - ctx.gc.headNode.appendChild(ctx.doc.createTextNode("\n")); + headNode.appendChild(child); + headNode.appendChild(ctx.doc.createTextNode("\n")); + TodoElement it + = TodoElement.getTodoElement((Element)child, this.ctx, this); + toProcessFirst.add(it); } - 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"); } } + 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(TodoStyleOrScript.Type.STYLE, this.ctx, this)); diff --git a/org/madore/damlengine/TodoTitleOrSubtitle.java b/org/madore/damlengine/TodoTitleOrSubtitle.java index 1640b93..01c4973 100644 --- a/org/madore/damlengine/TodoTitleOrSubtitle.java +++ b/org/madore/damlengine/TodoTitleOrSubtitle.java @@ -1,6 +1,7 @@ package org.madore.damlengine; import java.util.ArrayList; +import java.util.regex.Pattern; import org.w3c.dom.*; public final class TodoTitleOrSubtitle extends TodoElement { @@ -28,6 +29,15 @@ public final class TodoTitleOrSubtitle extends TodoElement { @Override public void handle() { + if ( ((t==Type.TITLE)?ctx.gc.title:ctx.gc.subtitle) == null ) { + Node ws = node.getNextSibling(); + if ( ( ws.getNodeType() == Node.TEXT_NODE + || ws.getNodeType() == Node.CDATA_SECTION_NODE ) + && Pattern.matches("^\\s*$",((CharacterData)ws).getData()) ) + node.getParentNode().removeChild(ws); + node.getParentNode().removeChild(node); + return; + } Element elt = ctx.doc.createElementNS(DamlEngine.XHTML_NS, t.eltName); String expLang = (t==Type.TITLE)?ctx.gc.titleLang:ctx.gc.subtitleLang; if ( expLang != null ) diff --git a/org/madore/damlengine/TodoTranslations.java b/org/madore/damlengine/TodoTranslations.java index 95f1ab7..dc67c30 100644 --- a/org/madore/damlengine/TodoTranslations.java +++ b/org/madore/damlengine/TodoTranslations.java @@ -14,6 +14,15 @@ public final class TodoTranslations extends TodoElement { @Override public void handle() { + if ( ctx.gc.translations == null ) { + Node ws = node.getNextSibling(); + if ( ( ws.getNodeType() == Node.TEXT_NODE + || ws.getNodeType() == Node.CDATA_SECTION_NODE ) + && Pattern.matches("^\\s*$",((CharacterData)ws).getData()) ) + node.getParentNode().removeChild(ws); + node.getParentNode().removeChild(node); + return; + } ArrayList toProcess = new ArrayList(ctx.gc.translations.size()+8); for ( String trlang : ctx.gc.translations ) { Element p = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "p"); -- cgit v1.2.3