1 16 package org.mortbay.html; 17 import java.io.IOException ; 18 import java.io.OutputStream ; 19 import java.io.OutputStreamWriter ; 20 import java.io.Writer ; 21 import java.util.ArrayList ; 22 23 24 25 40 public class Composite extends Element 41 { 42 43 45 protected ArrayList elements= new ArrayList (8); 46 47 48 protected Composite nest=null; 49 50 51 53 public Composite() 54 {} 55 56 57 59 public Composite(String attributes) 60 { 61 super(attributes); 62 } 63 64 65 71 public Composite add(Object o) 72 { 73 if (nest!=null) 74 nest.add(o); 75 else 76 { 77 if (o!=null) 78 { 79 if (o instanceof Element) 80 { 81 if(o instanceof Page) 82 throw new IllegalArgumentException ("Can't insert Page in Composite"); 83 elements.add(o); 84 } 85 else if (o instanceof String ) 86 elements.add(o); 87 else 88 elements.add(o.toString()); 89 } 90 } 91 return this; 92 } 93 94 95 102 public Composite nest(Composite c) 103 { 104 if (nest!=null) 105 return nest.nest(c); 106 else 107 { 108 add(c); 109 nest=c; 110 } 111 return this; 112 } 113 114 115 119 public Composite setNest(Composite c) 120 { 121 if (nest!=null) 122 nest.setNest(c); 123 else 124 nest=c; 125 return this; 126 } 127 128 129 131 public Composite unnest() 132 { 133 if (nest!=null) 134 nest.unnest(); 135 nest = null; 136 return this; 137 } 138 139 140 141 144 public int size() 145 { 146 return elements.size(); 147 } 148 149 150 155 public void write(Writer out) 156 throws IOException 157 { 158 for (int i=0; i <elements.size() ; i++) 159 { 160 Object element = elements.get(i); 161 162 if (element instanceof Element) 163 ((Element)element).write(out); 164 else if (element==null) 165 out.write("null"); 166 else 167 out.write(element.toString()); 168 } 169 } 170 171 172 174 public String contents() 175 { 176 StringBuffer buf = new StringBuffer (); 177 synchronized(buf) 178 { 179 for (int i=0; i <elements.size() ; i++) 180 { 181 Object element = elements.get(i); 182 if (element==null) 183 buf.append("null"); 184 else 185 buf.append(element.toString()); 186 } 187 } 188 return buf.toString(); 189 } 190 191 192 194 public Composite reset() 195 { 196 elements.clear(); 197 return unnest(); 198 } 199 200 201 204 void flush(Writer out) 205 throws IOException 206 { 207 if (nest!=null) 208 nest.flush(out); 209 else 210 { 211 write(out); 212 elements.clear(); 213 } 214 } 215 216 217 220 void flush(OutputStream out) 221 throws IOException 222 { 223 flush(new OutputStreamWriter (out)); 224 } 225 226 227 230 void flush(OutputStream out, String encoding) 231 throws IOException 232 { 233 flush(new OutputStreamWriter (out,encoding)); 234 } 235 236 237 239 public boolean replace(Object oldObj, Object newObj) 240 { 241 if (nest != null) 242 { 243 return nest.replace(oldObj, newObj); 244 } 245 else 246 { 247 int sz = elements.size(); 248 for (int i = 0; i < sz; i++) 249 { 250 if (elements.get(i) == oldObj) 251 { 252 elements.set(i,newObj); 253 return true; 254 } 255 } 256 } 257 258 return false; 259 } 260 261 } 262 | Popular Tags |