package org.madore.damlengine; import org.w3c.dom.*; public final class TodoStyleOrScript extends TodoDefaultElement { 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; } } public static class Factory extends TodoElement.Factory { final Type t; public Factory(Type t) { super(); this.t = t; } @Override public TodoStyleOrScript newItem(Element node, Context ctx, TodoItem caller) { return new TodoStyleOrScript(t, node, ctx, caller); } } final Type t; final CharSequence useThisContent; public TodoStyleOrScript(Type t, Element node, Context ctx, TodoItem caller, CharSequence useThisContent) { super(node, ctx, caller); this.t = t; this.useThisContent = useThisContent; } public TodoStyleOrScript(Type t, Element node, Context ctx, TodoItem caller) { super(node, ctx, caller); this.t = t; this.useThisContent = null; } @Override public void handleNodeOnly() { String content; if ( useThisContent != null ) content = useThisContent.toString(); else content = node.getTextContent(); while ( node.getLastChild() != null ) { node.removeChild(node.getLastChild()); } node.appendChild(ctx.doc.createTextNode("\n"+t.preCdata)); node.appendChild(ctx.doc. createCDATASection(t.postCdata+content +t.preCdata)); node.appendChild(ctx.doc.createTextNode(t.postCdata)); } public static final class HeadStyleOrScript extends TodoItem { final Type t; public HeadStyleOrScript(Type t, Context ctx, TodoItem caller) { super(ctx, caller); this.t = t; } @Override public void handle() { if ( ctx.gc.headNode == null ) throw new IllegalStateException("head node is null when doing style or script"); Element node = ctx.doc.createElementNS(DamlEngine.XHTML_NS, t.eltName); node.setAttributeNS(null, "type", t.mimeType); ctx.gc.headNode.appendChild(node); ctx.gc.headNode.appendChild(ctx.doc.createTextNode("\n")); StringBuffer content = (t==Type.SCRIPT)?ctx.gc.scriptContent:ctx.gc.styleContent; this.ownerDeque. registerAtStart(new TodoStyleOrScript(t, node, this.ctx, this, content)); } } }