1 6 package org.objectweb.jac.aspects.gui.web.html; 7 8 11 import java.io.IOException ; 12 import java.io.OutputStream ; 13 import java.io.OutputStreamWriter ; 14 import java.io.Writer ; 15 import java.util.ArrayList ; 16 import org.mortbay.util.Code; 17 import org.mortbay.html.Page; 18 19 20 35 public class Composite extends Element 36 { 37 38 40 protected ArrayList elements= new ArrayList (8); 41 42 43 protected Composite nest=null; 44 45 46 48 public Composite() 49 {} 50 51 52 54 public Composite(String attributes) 55 { 56 super(attributes); 57 } 58 59 60 66 public Composite add(Object o) 67 { 68 return add(elements.size(),o); 69 } 70 71 72 73 80 public Composite add(int position, Object o) 81 { 82 if (nest!=null) 83 nest.add(position,o); 84 else 85 { 86 if (o!=null) 87 { 88 if (o instanceof Element) 89 { 90 Code.assertTrue(!(o instanceof Page), 91 "Can't insert Page in Composite"); 92 elements.add(position,o); 93 } 94 else if (o instanceof String ) 95 elements.add(position,o); 96 else 97 elements.add(position,o.toString()); 98 } 99 } 100 return this; 101 } 102 103 104 111 public Composite nest(Composite c) 112 { 113 if (nest!=null) 114 return nest.nest(c); 115 else 116 { 117 add(c); 118 nest=c; 119 } 120 return this; 121 } 122 123 124 128 public Composite setNest(Composite c) 129 { 130 if (nest!=null) 131 nest.setNest(c); 132 else 133 nest=c; 134 return this; 135 } 136 137 138 140 public Composite unnest() 141 { 142 if (nest!=null) 143 nest.unnest(); 144 nest = null; 145 return this; 146 } 147 148 149 150 153 public int size() 154 { 155 return elements.size(); 156 } 157 158 159 164 public void write(Writer out) 165 throws IOException 166 { 167 for (int i=0; i <elements.size() ; i++) 168 { 169 Object element = elements.get(i); 170 171 if (element instanceof Element) 172 ((Element)element).write(out); 173 else if (element==null) 174 out.write("null"); 175 else 176 out.write(element.toString()); 177 } 178 } 179 180 181 183 public String contents() 184 { 185 StringBuffer buf = new StringBuffer (); 186 synchronized(buf) 187 { 188 for (int i=0; i <elements.size() ; i++) 189 { 190 Object element = elements.get(i); 191 if (element==null) 192 buf.append("null"); 193 else 194 buf.append(element.toString()); 195 } 196 } 197 return buf.toString(); 198 } 199 200 201 203 public Composite reset() 204 { 205 elements.clear(); 206 return unnest(); 207 } 208 209 210 213 void flush(Writer out) 214 throws IOException 215 { 216 if (nest!=null) 217 nest.flush(out); 218 else 219 { 220 write(out); 221 elements.clear(); 222 } 223 } 224 225 226 229 void flush(OutputStream out) 230 throws IOException 231 { 232 flush(new OutputStreamWriter (out)); 233 } 234 235 236 239 void flush(OutputStream out, String encoding) 240 throws IOException 241 { 242 flush(new OutputStreamWriter (out,encoding)); 243 } 244 245 246 248 public boolean replace(Object oldObj, Object newObj) 249 { 250 if (nest != null) 251 { 252 return nest.replace(oldObj, newObj); 253 } 254 else 255 { 256 int sz = elements.size(); 257 for (int i = 0; i < sz; i++) 258 { 259 if (elements.get(i) == oldObj) 260 { 261 elements.set(i,newObj); 262 return true; 263 } 264 } 265 } 266 267 return false; 268 } 269 270 } 271 | Popular Tags |