001: /*
002: * Created on May 17, 2006
003: */
004: package uk.org.ponder.htmlutil;
005:
006: import java.util.Iterator;
007: import java.util.Map;
008:
009: import uk.org.ponder.stringutil.CharWrap;
010:
011: public class HTMLUtil {
012: public static void appendStyle(String style, String value,
013: Map attrmap) {
014: String oldstyle = (String) attrmap.get("style");
015: if (oldstyle == null)
016: oldstyle = "";
017: oldstyle = oldstyle + " " + style + ": " + value + ";";
018: attrmap.put("style", oldstyle);
019: }
020:
021: /**
022: * Parses a CSS string (separated by ; with keys named with :) into a
023: * key/value map of Strings to Strings
024: */
025: public static void parseStyle(String style, Map toreceive) {
026: String[] styles = style.split(";");
027: for (int i = 0; i < styles.length; ++i) {
028: int colpos = styles[i].indexOf(':');
029: if (colpos == -1) {
030: throw new IllegalArgumentException("Style string "
031: + styles[i]
032: + " does not contain a colon character");
033: }
034: String key = styles[i].substring(0, colpos).trim();
035: String value = styles[i].substring(colpos + 1).trim();
036: toreceive.put(key, value);
037: }
038: }
039:
040: public static String renderStyle(Map torender) {
041: CharWrap togo = new CharWrap();
042: for (Iterator sit = torender.keySet().iterator(); sit.hasNext();) {
043: String key = (String) sit.next();
044: String value = (String) torender.get(key);
045: if (togo.size != 0) {
046: togo.append(' ');
047: }
048: togo.append(key).append(": ").append(value).append(';');
049: }
050: return togo.toString();
051: }
052:
053: /**
054: * Constructs the String representing a Javascript array declaration.
055: *
056: * @param name The Javascript name of the required array
057: * @param elements The values of the elements to be rendered.
058: */
059: public static String emitJavascriptArray(String name,
060: String[] elements) {
061: CharWrap togo = new CharWrap();
062: togo.append(" ").append(name).append(" = ").append("[\"");
063:
064: for (int i = 0; i < elements.length; ++i) {
065: togo.append(elements[i]);
066: if (i != elements.length - 1) {
067: togo.append("\", \"");
068: }
069: }
070: togo.append("\"];\n");
071:
072: return togo.toString();
073: }
074:
075: /** Emits the text for a single Javascript call taking a single argument
076: * @see #emitJavascriptCall(String, String[])
077: * **/
078: public static String emitJavascriptCall(String name, String argument) {
079: return emitJavascriptCall(name, new String[] { argument });
080: }
081:
082: /** Emits the text for a single Javascript call, that is
083: * <code>name(arguments[0], arguments[1]) ...)</code>
084: * @param name The name of the JS function to be invoked
085: * @param arguments The function arguments to be applied.
086: */
087: public static String emitJavascriptCall(String name,
088: String[] arguments) {
089: CharWrap togo = new CharWrap();
090: togo.append(" ").append(name).append("(\"");
091: for (int i = 0; i < arguments.length; ++i) {
092: togo.append(arguments[i]);
093: if (i != arguments.length - 1) {
094: togo.append("\", \"");
095: }
096: }
097: togo.append("\");\n");
098:
099: return togo.toString();
100: }
101:
102: /** The "natural" format accepted by Javascript's Date.parse() method **/
103: public static String JS_DATE_FORMAT = "MMMM, d yyyy HH:mm:ss";
104:
105: public static String emitJavascriptVar(String name, String value) {
106: return " " + name + " = \"" + value + "\";\n";
107: }
108:
109: }
|