From f2745aadc7d0eb02002f667cd72d8536e4f1daf1 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Thu, 22 Apr 2010 02:16:29 +0200 Subject: 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. --- org/madore/damlengine/TodoStyleOrScript.java | 43 +++++++++++++++++----------- 1 file changed, 27 insertions(+), 16 deletions(-) (limited to 'org/madore/damlengine/TodoStyleOrScript.java') 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)); } } -- cgit v1.2.3