diff options
author | David A. Madore <david+git@madore.org> | 2010-04-22 02:16:29 +0200 |
---|---|---|
committer | David A. Madore <david+git@madore.org> | 2010-04-22 02:16:29 +0200 |
commit | f2745aadc7d0eb02002f667cd72d8536e4f1daf1 (patch) | |
tree | ab03ed4bed1c1cbe9568b3a9f24db15639e41b8d /org/madore/damlengine/TodoStyleOrScript.java | |
parent | 88ef0fa3b0f0ec58cff7d819f9447064be9b6fad (diff) | |
download | damlengine-f2745aadc7d0eb02002f667cd72d8536e4f1daf1.tar.gz damlengine-f2745aadc7d0eb02002f667cd72d8536e4f1daf1.tar.bz2 damlengine-f2745aadc7d0eb02002f667cd72d8536e4f1daf1.zip |
Various "stylistic" improvements suggested by Joshua Bloch's book.
Use @Override annotation. Limit accessibility of fields. Make final
what can be. Use complex enums. Don't ignore exceptions. Some more
changes on exceptions thrown.
Diffstat (limited to 'org/madore/damlengine/TodoStyleOrScript.java')
-rw-r--r-- | org/madore/damlengine/TodoStyleOrScript.java | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/org/madore/damlengine/TodoStyleOrScript.java b/org/madore/damlengine/TodoStyleOrScript.java index 57aefba..dcec137 100644 --- a/org/madore/damlengine/TodoStyleOrScript.java +++ b/org/madore/damlengine/TodoStyleOrScript.java @@ -2,11 +2,25 @@ package org.madore.damlengine; import org.w3c.dom.*; -public class TodoStyleOrScript extends TodoItem { +public final class TodoStyleOrScript extends TodoItem { - public enum Type { STYLE, SCRIPT } + public enum Type { + STYLE("style", "text/css", "/* ", " */\n"), + SCRIPT("script", "text/javascript", "// ", "\n"); + final String eltName; + final String mimeType; + final String preCdata; + final String postCdata; + Type(String eltName, String mimeType, + String preCdata, String postCdata) { + this.eltName = eltName; + this.mimeType = mimeType; + this.preCdata = preCdata; + this.postCdata = postCdata; + } + } - Type t; + final Type t; public TodoStyleOrScript(Type t, Context ctx, @@ -15,27 +29,24 @@ public class TodoStyleOrScript extends TodoItem { this.t = t; } + @Override public void handle() { if ( ctx.headNode == null ) - throw new IllegalArgumentException("head node is null when doing style or script"); + throw new IllegalStateException("head node is null when doing style or script"); Element node - = ctx.doc.createElementNS(DamlEngine.XHTML_NS, - (t==Type.SCRIPT)?"script":"style"); - node.setAttributeNS(null, "type", - (t==Type.SCRIPT)?"text/javascript":"text/css"); + = ctx.doc.createElementNS(DamlEngine.XHTML_NS, t.eltName); + node.setAttributeNS(null, "type", t.mimeType); if ( t==Type.SCRIPT ) node.setAttributeNS(null, "defer", "defer"); ctx.headNode.appendChild(node); ctx.headNode.appendChild(ctx.doc.createTextNode("\n")); + node.appendChild(ctx.doc.createTextNode("\n"+t.preCdata)); + StringBuffer content + = (t==Type.SCRIPT)?ctx.scriptContent:ctx.styleContent; node.appendChild(ctx.doc. - createTextNode((t==Type.SCRIPT)?"\n// ":"\n/* ")); - StringBuffer content = (t==Type.SCRIPT)?ctx.scriptContent:ctx.styleContent; - node.appendChild(ctx.doc. - createCDATASection(((t==Type.SCRIPT)?"\n":" */\n") - +content - +((t==Type.SCRIPT)?"// ":"/* "))); - node.appendChild(ctx.doc. - createTextNode((t==Type.SCRIPT)?"\n":" */\n")); + createCDATASection(t.postCdata+content + +t.preCdata)); + node.appendChild(ctx.doc.createTextNode(t.postCdata)); } } |