00001 import java.io.PrintWriter; 00002 import java.util.Vector; 00003 import java.io.Serializable; 00004 import java.io.InputStream; 00005 import java.io.ByteArrayInputStream; 00006 import java.io.ByteArrayOutputStream; 00007 import java.io.ObjectInputStream; 00008 import java.io.ObjectOutputStream; 00009 00010 public abstract class Widget 00011 { 00012 public String prefix; 00013 public Form form; 00014 00015 public boolean modal = false; 00016 00017 public Vector children = new Vector(); 00018 public Widget modalChild; 00019 00020 public static final int GET = 1; 00021 public static final int POST = 2; 00022 public static final int BOTH = 3; 00023 00024 public String name; 00025 public int method; 00026 00027 public Widget(String prefix, Form form) 00028 { 00029 this.prefix = prefix; 00030 this.form = form; 00031 } 00032 00033 public void addChild(Widget w) 00034 { 00035 children.add(w); 00036 } 00037 00038 public void loadDefaults() 00039 { 00040 int l = children.size(); 00041 for(int i = 0; i < l; ++i) 00042 { 00043 Widget w = (Widget)children.get(i); 00044 w.loadDefaults(); 00045 } 00046 } 00047 00048 public void loadValues() 00049 { 00050 int l = children.size(); 00051 for(int i = 0; i < l; ++i) 00052 { 00053 Widget w = (Widget)children.get(i); 00054 w.loadValues(); 00055 if (w.modal) modalChild = w; 00056 } 00057 } 00058 00059 public void display() 00060 { 00061 display(false); 00062 } 00063 00064 public void display(boolean hidden) 00065 { 00066 if (hidden || modalChild != null) 00067 { 00068 int l = children.size(); 00069 for(int i = 0; i < l; ++i) 00070 { 00071 Widget w = (Widget)children.get(i); 00072 w.display(!w.modal); 00073 } 00074 } 00075 } 00076 00077 public void dumpscript() { } 00078 00079 public String loadAttribute() 00080 { 00081 return form.req.getParameter(prefix); 00082 } 00083 00084 public String loadAttribute(String name) 00085 { 00086 return form.req.getParameter(n(name)); 00087 } 00088 00089 public String n(String suffix) 00090 { 00091 return suffix != null ? prefix + "." + suffix : prefix; 00092 } 00093 00094 public void printAttribute(String name, String value) 00095 { 00096 p("<input type=hidden name=\"" + n(name) + "\" value=\"" + htmlencode(value) + "\">"); 00097 } 00098 00099 public void preserveAttribute(String name) 00100 { 00101 String v = loadAttribute(name); 00102 if (v != null) printAttribute(name, v); 00103 } 00104 00105 public void p(String str) 00106 { 00107 form.pw.print(str); 00108 } 00109 00110 public void p(Exception e) 00111 { 00112 ShowException(e, form.pw); 00113 } 00114 00115 public static String htmlencode(String v) 00116 { 00117 if (v == null) return ""; 00118 StringBuffer b = new StringBuffer(); 00119 int l = v.length(); 00120 00121 for(int i = 0; i < l; ++i) 00122 { 00123 char c = v.charAt(i); 00124 00125 switch (c) 00126 { 00127 case '"': 00128 b.append("""); 00129 break; 00130 case '<': 00131 b.append("<"); 00132 break; 00133 case '>': 00134 b.append(">"); 00135 break; 00136 case '&': 00137 b.append("&"); 00138 break; 00139 default: 00140 b.append(c); 00141 break; 00142 } 00143 } 00144 return b.toString(); 00145 } 00146 00147 public String serialize(Serializable o) 00148 { 00149 String r = null; 00150 try 00151 { 00152 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 00153 try 00154 { 00155 ObjectOutputStream oos = new ObjectOutputStream(bos); 00156 try 00157 { 00158 oos.writeObject(o); 00159 oos.flush(); 00160 r = Base64.encodeBytes(bos.toByteArray()); 00161 } 00162 finally 00163 { 00164 oos.close(); 00165 } 00166 } 00167 finally 00168 { 00169 bos.close(); 00170 } 00171 } 00172 catch (java.io.IOException e) 00173 { 00174 p("<h1>Serialize IO Exception: " + e.toString() + "</h1>"); 00175 } 00176 return r; 00177 } 00178 00179 public Object unserialize(String s) 00180 { 00181 Object o = null; 00182 try 00183 { 00184 InputStream is = new ByteArrayInputStream(Base64.decode(s)); 00185 try 00186 { 00187 ObjectInputStream ois = new ObjectInputStream(is); 00188 try 00189 { 00190 o = ois.readObject(); 00191 } 00192 finally 00193 { 00194 ois.close(); 00195 } 00196 is.close(); 00197 } 00198 finally 00199 { 00200 is.close(); 00201 } 00202 } 00203 catch (java.io.IOException e) 00204 { 00205 p(e); 00206 } 00207 catch (java.lang.ClassNotFoundException e) 00208 { 00209 p(e); 00210 } 00211 return o; 00212 } 00213 00214 public int toInt(String str, int def) 00215 { 00216 try 00217 { 00218 return str == null ? def : Integer.parseInt(str); 00219 } 00220 catch (java.lang.NumberFormatException e) 00221 { 00222 return def; 00223 } 00224 } 00225 00226 public static void ShowException(Throwable t, PrintWriter pw) 00227 { 00228 pw.println("</select>"); 00229 pw.println("<h2>Exception Caught</h2>"); 00230 pw.print("<pre style=\"background-color: #eeeeee\">"); 00231 t.printStackTrace(pw); 00232 pw.println("</pre>"); 00233 } 00234 00235 };