1 64 65 package com.jcorporate.expresso.core.controller; 66 67 import com.jcorporate.expresso.core.cache.Cacheable; 68 import com.jcorporate.expresso.core.misc.StringUtil; 69 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 70 import org.w3c.dom.NamedNodeMap ; 71 import org.w3c.dom.Node ; 72 import org.w3c.dom.NodeList ; 73 74 import java.util.Enumeration ; 75 76 77 86 public class Output 87 extends ControllerElement 88 implements Cloneable , 89 Cacheable, 90 java.io.Serializable { 91 92 95 private String content; 96 97 100 private String style; 101 102 105 private String alignment; 106 private long usedCount = 0; 107 108 111 private String key = null; 112 113 116 public Output() { 117 super(); 118 } 119 120 126 public Output(String c) { 127 super(); 128 setContent(c); 129 } 130 131 138 public Output(String name, String c) { 139 setName(name); 140 setContent(c); 141 } 142 143 148 public void addNested(Output o) { 149 super.addNested(o); 150 } 151 152 155 public void clearUsedCount() { 156 usedCount = 0; 157 } 158 159 164 public Object clone() 165 throws CloneNotSupportedException { 166 Output newOutput; 167 168 synchronized (this) { 169 newOutput = (Output) super.clone(); 170 newOutput.alignment = alignment; 171 newOutput.content = content; 172 newOutput.key = key; 173 newOutput.usedCount = usedCount; 174 } 175 176 return newOutput; 177 } 178 179 180 185 public String getAlignment() { 186 return alignment; 187 } 188 189 195 public String getContent() { 196 return StringUtil.notNull(content); 197 } 198 199 204 public String getKey() { 205 return key; 206 } 207 208 214 public String getStyle() { 215 return style; 216 } 217 218 221 public long getUsedCount() { 222 return usedCount; 223 } 224 225 232 public boolean hasNested(String nestedName) 233 throws ControllerException { 234 Output oneElement = null; 235 236 for (Enumeration e = getNested().elements(); e.hasMoreElements();) { 237 oneElement = (Output) e.nextElement(); 238 239 if (oneElement.getName().equals(nestedName)) { 240 return true; 241 } 242 } 243 244 245 return false; 246 } 247 248 249 252 public synchronized void incrementUsedCount() { 253 usedCount++; 254 } 255 256 263 public synchronized void setAlignment(String newAlignment) { 264 alignment = newAlignment; 265 } 266 267 272 public synchronized void setContent(String newContent) { 273 content = newContent; 274 } 275 276 279 public synchronized void setKey(String newKey) { 280 key = newKey; 281 } 282 283 289 public synchronized void setStyle(String newStyle) { 290 style = newStyle; 291 } 292 293 300 public FastStringBuffer toXML(FastStringBuffer stream) { 301 stream.append("<output"); 302 303 if (this.getName() != null && this.getName().length() > 0) { 304 stream.append(" name=\""); 305 stream.append(StringUtil.xmlEscape(this.getName())); 306 stream.append("\""); 307 } 308 if (this.alignment != null && this.alignment.length() > 0) { 309 stream.append(" alignment=\""); 310 stream.append(StringUtil.xmlEscape(this.alignment)); 311 stream.append("\""); 312 } 313 if (this.key != null && this.key.length() > 0) { 314 stream.append(" key=\""); 315 stream.append(StringUtil.xmlEscape(this.key)); 316 stream.append("\""); 317 } 318 if (this.usedCount > 0) { 319 stream.append(" usedCount =\""); 320 stream.append(Long.toString(this.usedCount)); 321 stream.append("\""); 322 } 323 324 stream.append(">\n"); 325 stream.append("<content>"); 326 stream.append(StringUtil.xmlEscape(getContent())); 327 stream.append("</content>\n"); 328 stream = super.toXML(stream); 329 stream.append("</output>\n"); 330 331 return stream; 332 } 333 334 341 public static ControllerElement fromXML(Node n) 342 throws ControllerException { 343 344 if (n.getNodeName().equals("#document")) { 346 return fromXML(n.getChildNodes().item(0)); 347 } 348 if (!n.getNodeName().equals("output")) { 349 return null; 350 } 351 352 Output o = new Output(); 353 354 NamedNodeMap outputAttributes = n.getAttributes(); 356 Node attributeNode = outputAttributes.getNamedItem("name"); 357 358 if (attributeNode != null) { 359 String value = attributeNode.getNodeValue(); 360 361 if (value != null) { 362 o.setName(value); 363 } 364 } 365 366 attributeNode = outputAttributes.getNamedItem("alignment"); 367 368 if (attributeNode != null) { 369 String value = attributeNode.getNodeValue(); 370 371 if (value != null) { 372 o.alignment = value; 373 } 374 } 375 376 attributeNode = outputAttributes.getNamedItem("key"); 377 378 if (attributeNode != null) { 379 String value = attributeNode.getNodeValue(); 380 381 if (value != null) { 382 o.key = value; 383 } 384 } 385 386 attributeNode = outputAttributes.getNamedItem("usedCount"); 387 388 if (attributeNode != null) { 389 String value = attributeNode.getNodeValue(); 390 391 if (value != null) { 392 try { 393 o.usedCount = Long.parseLong(value); 394 } catch (NumberFormatException nfe) { 395 } 396 } 397 } 398 399 NodeList nl = n.getChildNodes(); 400 401 for (int i = 0; i < nl.getLength(); i++) { 402 Node oneChild = nl.item(i); 403 String nodeName = oneChild.getNodeName(); 404 405 if (nodeName.equals("content")) { 406 Node contentNode = oneChild.getFirstChild(); 407 408 if (contentNode != null) { 409 o.content = contentNode.getNodeValue(); 410 } 411 } else if (nodeName.equals("controller-element")) { 412 o = (Output) ControllerElement.fromXML(oneChild, o); 413 } 414 } 415 416 return o; 417 } 418 419 424 public synchronized void setDefaultValue(String newContent) { 425 content = newContent; 426 } 427 428 433 public String getDefaultValue() { 434 return content; 435 } 436 437 } 438 439 | Popular Tags |