1 33 34 37 package com.icesoft.faces.context; 38 39 import com.icesoft.faces.util.HalterDump; 40 import com.icesoft.faces.webapp.http.common.Configuration; 41 import com.icesoft.faces.webapp.http.common.ConfigurationException; 42 import org.w3c.dom.DOMException ; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Element ; 45 import org.w3c.dom.NamedNodeMap ; 46 import org.w3c.dom.Node ; 47 import org.w3c.dom.NodeList ; 48 import org.w3c.dom.Text ; 49 50 import javax.faces.component.UIComponent; 51 import javax.faces.context.FacesContext; 52 import javax.faces.context.ResponseWriter; 53 import java.io.IOException ; 54 import java.util.ArrayList ; 55 import java.util.HashMap ; 56 import java.util.List ; 57 import java.util.Map ; 58 59 63 public class DOMContext implements java.io.Serializable { 64 private transient DOMResponseWriter writer; 65 private Node cursor; 66 private Document document; 67 private Node rootNode; 68 private Node parentElement; 69 private boolean initialized; 70 71 protected DOMContext(DOMResponseWriter writer, Document document, 72 Node parentElement) { 73 this.writer = writer; 74 this.document = document; 75 this.cursor = parentElement; 76 this.parentElement = parentElement; 77 this.initialized = false; 78 } 79 80 86 public boolean isInitialized() { 87 return initialized; 88 } 89 90 99 public static DOMContext attachDOMContext(FacesContext facesContext, 100 UIComponent component) { 101 ResponseWriter responseWriter = facesContext.getResponseWriter(); 102 DOMResponseWriter domWriter; 103 if (responseWriter instanceof DOMResponseWriter) { 104 domWriter = (DOMResponseWriter) responseWriter; 105 } else { 106 domWriter = createTemporaryDOMResponseWriter(responseWriter, facesContext); 107 } 108 Node cursorParent = domWriter.getCursorParent(); 109 Document doc = domWriter.getDocument(); 110 Map domContexts = domWriter.getDomResponseContexts(); 111 112 DOMContext context = null; 113 String clientId = 114 component.getClientId(FacesContext.getCurrentInstance()); 115 if (clientId != null && domContexts.containsKey(clientId)) { 116 context = (DOMContext) domContexts.get(clientId); 117 } 118 if (null == context) { 119 context = new DOMContext(domWriter, doc, cursorParent); 120 domContexts.put(clientId, context); 121 } 122 if (context.isInitialized()) { 124 if (!(cursorParent instanceof Element )) { 125 context.stepOver(); 126 return context; 127 } 128 context.attach((Element ) cursorParent); 129 } 130 context.stepOver(); 131 132 return context; 133 } 134 135 private static DOMResponseWriter createTemporaryDOMResponseWriter( 136 ResponseWriter responseWriter, FacesContext facesContext) { 137 DOMResponseWriter domWriter; 138 domWriter = new DOMResponseWriter(facesContext, null, new Configuration() { 139 public String getName() { 140 return "noop configuration"; 141 } 142 143 public Configuration getChild(String child) throws ConfigurationException { 144 throw new ConfigurationException("child not available"); 145 } 146 147 public Configuration[] getChildren(String name) throws ConfigurationException { 148 throw new ConfigurationException("children not available"); 149 } 150 151 public String getAttribute(String paramName) throws ConfigurationException { 152 throw new ConfigurationException("attribute not available"); 153 } 154 155 public String getValue() throws ConfigurationException { 156 throw new ConfigurationException("value not available"); 157 } 158 }); 159 Document doc = domWriter.getDocument(); 160 Element html = doc.createElement("html"); 161 doc.appendChild(html); 162 Element body = doc.createElement("body"); 163 html.appendChild(body); 164 domWriter.setCursorParent(body); 165 return domWriter; 166 } 167 168 177 public static DOMContext getDOMContext(FacesContext facesContext, 178 UIComponent component) { 179 ResponseWriter responseWriter = facesContext.getResponseWriter(); 180 DOMResponseWriter domWriter; 181 if (responseWriter instanceof DOMResponseWriter) { 182 domWriter = (DOMResponseWriter) responseWriter; 183 } else { 184 domWriter = createTemporaryDOMResponseWriter(responseWriter, facesContext); 185 } 186 Document doc = domWriter.getDocument(); 187 Map domContexts = domWriter.getDomResponseContexts(); 188 189 DOMContext context = null; 190 String clientId = 191 component.getClientId(FacesContext.getCurrentInstance()); 192 if (domContexts.containsKey(clientId)) { 193 context = (DOMContext) domContexts.get(clientId); 194 } 195 if (null == context) { 196 Node cursorParent = domWriter.getCursorParent(); 197 context = new DOMContext(domWriter, doc, cursorParent); 198 domContexts.put(clientId, context); 199 } 200 return context; 201 } 202 203 private void attach(Element cursorParent) { 204 if (null == rootNode) { return; 206 } 207 if (rootNode.equals(cursorParent)) { 208 return; 209 } 210 211 if (rootNode.getParentNode() != cursorParent) { 215 try { 216 cursorParent.appendChild(rootNode); 219 } catch (DOMException e) { 220 } 222 } 223 } 224 225 226 237 public Element createElement(String name) { 238 return document.createElement(name); 239 } 240 241 248 public Text createTextNode(String cData) { 249 return document.createTextNode(cData); 250 } 251 252 258 public void setRootNode(Node rootNode) { 259 this.rootNode = rootNode; 260 parentElement.appendChild(rootNode); 261 initialized = true; 262 } 263 264 277 public Element createRootElement(String name) { 278 Element rootElement = createElement(name); 279 setRootNode(rootElement); 280 return rootElement; 281 } 282 283 void setIsolatedRootNode(Node rootElement) { 284 this.rootNode = rootElement; 285 initialized = true; 286 } 287 288 293 public Node getRootNode() { 294 return rootNode; 295 } 296 297 302 public void setCursorParent(Node cursorParent) { 303 this.cursor = cursorParent; 304 writer.setCursorParent(cursorParent); 305 } 306 307 311 public Node getCursorParent() { 312 return cursor; 313 } 314 315 316 320 public void stepOver() { 321 if (null != rootNode && rootNode.getParentNode() != null) { 322 setCursorParent(rootNode.getParentNode()); 323 } 324 } 325 326 332 public void stepInto(UIComponent component) { 333 if (rootNode != null) { 334 setCursorParent(rootNode); 337 } 338 } 339 340 341 347 public Document getDocument() { 348 return document; 349 } 350 351 356 public static void removeChildren(Node parent) { 357 while (parent.hasChildNodes()) { 358 parent.removeChild(parent.getFirstChild()); 359 } 360 } 361 362 369 public static void removeChildrenByTagName(Element rootElement, 370 String name) { 371 372 Node nextChildToRemove = null; 373 while (rootElement.hasChildNodes() 374 && ((nextChildToRemove = findChildWithNodeName(rootElement, 375 name)) != null)) { 376 rootElement.removeChild(nextChildToRemove); 377 } 378 } 379 380 384 private static Node findChildWithNodeName(Element root, String nodeName) { 385 NodeList children = root.getChildNodes(); 386 int length = children.getLength(); 387 for (int i = 0; i < length; i++) { 388 Node nextChildNode = children.item(i); 389 String name = nextChildNode.getNodeName(); 390 if (name.equalsIgnoreCase(nodeName)) { 391 return nextChildNode; 392 } 393 } 394 return null; 395 } 396 397 public static List findChildrenWithNodeName(Element root, String nodeName) { 398 NodeList children = root.getChildNodes(); 399 int length = children.getLength(); 400 List foundItems = new ArrayList (); 401 for (int i = 0; i < length; i++) { 402 Node nextChildNode = children.item(i); 403 String name = nextChildNode.getNodeName(); 404 if (name.equalsIgnoreCase(nodeName)) { 405 foundItems.add(nextChildNode); 406 } 407 } 408 return foundItems; 409 } 410 411 412 private HashMap halterDumps = new HashMap (); 413 414 426 public void streamWrite(FacesContext facesContext, UIComponent component, 427 Node root, Node halter) throws IOException { 428 if (!DOMResponseWriter.isStreamWriting()) { 429 return; 430 } 431 HalterDump halterDump = (HalterDump) halterDumps.get(root); 432 if (null == halterDump) { 433 halterDump = new HalterDump(facesContext.getResponseWriter(), 434 component, root); 435 halterDumps.put(root, halterDump); 436 } 437 halterDump.streamWrite(halter); 438 } 439 440 447 public void streamWrite(FacesContext facesContext, UIComponent component) 448 throws IOException { 449 streamWrite(facesContext, component, rootNode, null); 450 } 451 452 456 public boolean isStreamWriting() { 457 return DOMResponseWriter.isStreamWriting(); 458 } 459 460 464 public void startNode(FacesContext facesContext, UIComponent component, 465 Node node) 466 throws IOException { 467 if (!isStreamWriting()) { 468 return; 469 } else { 470 ResponseWriter writer = facesContext.getResponseWriter(); 472 473 switch (node.getNodeType()) { 475 476 case Node.DOCUMENT_NODE: 477 break; 478 479 case Node.ELEMENT_NODE: 480 writer.startElement(node.getNodeName().toLowerCase(), 482 component); 483 NamedNodeMap attributes = node.getAttributes(); 485 486 for (int i = 0; i < attributes.getLength(); i++) { 487 Node current = attributes.item(i); 488 writer.writeAttribute(current.getNodeName(), 489 current.getNodeValue(), 490 current.getNodeName()); 491 } 492 break; 493 494 case Node.TEXT_NODE: 495 writer.writeText(node.getNodeValue(), "text"); 496 break; 497 } 498 } 499 } 500 501 505 public void endNode(FacesContext facesContext, UIComponent component, 506 Node node) 507 throws IOException { 508 if (!isStreamWriting()) { 509 return; 510 } else { 511 ResponseWriter writer = facesContext.getResponseWriter(); 513 switch (node.getNodeType()) { 514 515 case Node.DOCUMENT_NODE: 516 break; 517 518 case Node.ELEMENT_NODE: 519 writer.endElement(node.getNodeName().toLowerCase()); 520 break; 521 522 case Node.TEXT_NODE: 523 break; 524 } 525 } 526 } 527 528 } 529 | Popular Tags |