1 16 package org.apache.cocoon.webapps.session.context; 17 18 import java.io.IOException ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import org.apache.cocoon.ProcessingException; 23 import org.apache.cocoon.components.source.SourceUtil; 24 import org.apache.cocoon.xml.IncludeXMLConsumer; 25 import org.apache.cocoon.xml.dom.DOMUtil; 26 import org.apache.excalibur.source.Source; 27 import org.apache.excalibur.source.SourceException; 28 import org.apache.excalibur.source.SourceParameters; 29 import org.apache.excalibur.source.SourceResolver; 30 import org.apache.excalibur.xml.xpath.NodeListImpl; 31 import org.apache.excalibur.xml.xpath.XPathProcessor; 32 import org.w3c.dom.Attr ; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.DocumentFragment ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.NodeList ; 37 import org.xml.sax.ContentHandler ; 38 import org.xml.sax.SAXException ; 39 import org.xml.sax.ext.LexicalHandler ; 40 41 47 public final class SimpleSessionContext 48 implements SessionContext { 49 50 51 private String name; 52 53 54 private Document data; 55 56 57 private Map attributes = new HashMap (); 58 59 60 private String loadResource; 61 62 63 private String saveResource; 64 65 66 private XPathProcessor xpathProcessor; 67 68 69 private SourceResolver resolver; 70 71 74 public SimpleSessionContext(XPathProcessor xPathProcessor, SourceResolver resolver) 75 throws ProcessingException { 76 this.data = DOMUtil.createDocument(); 77 this.data.appendChild(data.createElementNS(null, "context")); 78 this.xpathProcessor = xPathProcessor; 79 this.resolver = resolver; 80 } 81 82 85 public String getName() { 86 return this.name; 87 } 88 89 92 public void setup(String value, String loadResource, String saveResource) { 93 this.name = value; 94 this.loadResource = loadResource; 95 this.saveResource = saveResource; 96 } 97 98 public synchronized DocumentFragment getXML(String path) 99 throws ProcessingException { 100 DocumentFragment result = null; 101 NodeList list; 102 path = this.createPath(path); 103 104 String [] pathComponents = DOMUtil.buildPathArray(path); 105 if (pathComponents == null) { 106 list = this.xpathProcessor.selectNodeList(this.data, path); 107 } else { 108 list = DOMUtil.getNodeListFromPath(data, pathComponents); 109 } 110 111 if (list != null && list.getLength() > 0) { 112 Document doc = DOMUtil.createDocument(); 113 result = doc.createDocumentFragment(); 114 115 for(int i = 0; i < list.getLength(); i++) { 116 117 if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) { 119 result.appendChild(doc.createTextNode(list.item(i).getNodeValue())); 121 } else { 122 NodeList childs = list.item(i).getChildNodes(); 125 if (childs != null) { 126 for(int m = 0; m < childs.getLength(); m++) { 127 result.appendChild(doc.importNode(childs.item(m), true)); 128 } 129 } 130 } 131 } 132 } 133 134 return result; 135 } 136 137 138 public synchronized void setXML(String path, DocumentFragment fragment) 139 throws ProcessingException { 140 path = this.createPath(path); 141 Node node = DOMUtil.selectSingleNode(data, path, this.xpathProcessor); 142 if (node.getNodeType() == Node.ATTRIBUTE_NODE) { 143 Attr attr = (Attr )node; 145 attr.setNodeValue(DOMUtil.getValueOfNode(fragment)); 146 147 } else { 148 149 while (node.hasChildNodes() == true) { 151 node.removeChild(node.getFirstChild()); 152 } 153 154 NodeList childs = fragment.getChildNodes(); 156 if (childs != null && childs.getLength() > 0) { 157 for(int i = 0; i < childs.getLength(); i++) { 158 Node n = data.importNode(childs.item(i), true); 159 node.appendChild(n); 160 } 161 } 162 } 163 } 164 165 172 public synchronized void appendXML(String path, DocumentFragment fragment) 173 throws ProcessingException { 174 path = this.createPath(path); 175 Node node = DOMUtil.selectSingleNode(data, path, this.xpathProcessor); 176 if (node.getNodeType() == Node.ATTRIBUTE_NODE) { 177 Attr attr; 178 179 if (node.getNodeValue() != null || node.getNodeValue().trim().length() > 0) { 180 attr = node.getOwnerDocument().createAttributeNS(null, node.getNodeName()); 182 node.getParentNode().appendChild(attr); 183 } else { 184 attr = (Attr )node; 185 } 186 187 attr.setNodeValue(DOMUtil.getValueOfNode(fragment)); 189 } else { 190 191 NodeList childs = fragment.getChildNodes(); 193 if (childs != null && childs.getLength() > 0) { 194 for(int i = 0; i < childs.getLength(); i++) { 195 Node n = data.importNode(childs.item(i), true); 196 node.appendChild(n); 197 } 198 } 199 } 200 } 201 202 205 private String createPath(String path) { 206 if (path == null) path ="/"; 207 if (!path.startsWith("/") ) path = "/" + path; 208 path = "context" + path; 209 if (path.endsWith("/")) path = path.substring(0, path.length() - 1); 210 return path; 211 } 212 213 216 public synchronized void removeXML(String path) 217 throws ProcessingException { 218 NodeList list; 219 path = this.createPath(path); 220 221 String [] pathComponents = DOMUtil.buildPathArray(path); 222 if (pathComponents == null) { 223 list = this.xpathProcessor.selectNodeList(this.data, path); 224 } else { 225 list = DOMUtil.getNodeListFromPath(data, pathComponents); 226 } 227 if (list != null && list.getLength() > 0) { 228 int len = list.getLength(); 229 Node child; 230 for(int i = 0; i < len; i++) { 231 child = list.item(len - 1 -i); 232 child.getParentNode().removeChild(child); 233 } 234 } 235 } 236 237 240 public synchronized Node getSingleNode(String path) 241 throws ProcessingException { 242 Node result = null; 243 244 path = this.createPath(path); 245 246 try { 247 result = DOMUtil.getSingleNode(data, path, this.xpathProcessor); 248 if (result != null) result = result.cloneNode(true); 249 } catch (javax.xml.transform.TransformerException localException) { 250 throw new ProcessingException("TransformerException: " + localException, localException); 251 } 252 253 return result; 254 } 255 256 259 public synchronized NodeList getNodeList(String path) 260 throws ProcessingException { 261 NodeList result = null; 262 263 path = this.createPath(path); 264 265 String [] pathComponents = DOMUtil.buildPathArray(path); 266 if (pathComponents == null) { 267 result = this.xpathProcessor.selectNodeList(this.data, path); 268 } else { 269 result = DOMUtil.getNodeListFromPath(data, pathComponents); 270 } 271 if (result != null) { 273 result = new NodeListImpl(result); 274 } 275 276 return result; 277 } 278 279 282 public synchronized void setNode(String path, Node node) 283 throws ProcessingException { 284 if ( path == null || path.equals("/")) { 285 data = DOMUtil.createDocument(); 286 data.appendChild(data.createElementNS(null, "context")); 287 data.getFirstChild().appendChild(data.importNode(node, true)); 288 } else { 289 path = this.createPath(path); 290 Node removeNode = DOMUtil.selectSingleNode(data, path, this.xpathProcessor); 291 removeNode.getParentNode().replaceChild(data.importNode(node, true), removeNode); 292 } 293 } 294 295 296 299 public synchronized void setAttribute(String key, Object value) { 300 if (value == null) { 301 attributes.remove(key); 302 } else { 303 attributes.put(key, value); 304 } 305 } 306 307 310 public synchronized Object getAttribute(String key) { 311 return attributes.get(key); 312 } 313 314 317 public synchronized Object getAttribute(String key, Object defaultObject) { 318 Object value = attributes.get(key); 319 if (value == null) value = defaultObject; 320 return value; 321 } 322 323 327 public synchronized String getValueOfNode(String path) 328 throws ProcessingException { 329 String value = null; 330 331 path = this.createPath(path); value = DOMUtil.getValueOf(data, path, this.xpathProcessor); 333 334 return value; 335 } 336 337 340 public synchronized void setValueOfNode(String path, String value) 341 throws ProcessingException { 342 path = this.createPath(path); 344 Node node = DOMUtil.selectSingleNode(data, path, this.xpathProcessor); 345 if (node.getNodeType() == Node.ATTRIBUTE_NODE) { 346 Attr attr = (Attr )node; 347 attr.setNodeValue(value); 348 349 } else { 350 351 while (node.hasChildNodes() == true) { 353 node.removeChild(node.getFirstChild()); 354 } 355 356 node.appendChild(node.getOwnerDocument().createTextNode(value)); 357 } 358 } 359 360 365 public synchronized boolean streamXML(String path, ContentHandler contentHandler, 366 LexicalHandler lexicalHandler) 367 throws SAXException , ProcessingException { 368 NodeList list; 369 boolean streamed = false; 370 path = this.createPath(path); 371 372 String [] pathComponents = DOMUtil.buildPathArray(path); 373 if (pathComponents == null) { 374 list = this.xpathProcessor.selectNodeList(this.data, path); 375 } else { 376 list = DOMUtil.getNodeListFromPath(data, pathComponents); 377 } 378 if (list != null && list.getLength() > 0) { 379 streamed = true; 380 for(int i = 0; i < list.getLength(); i++) { 381 382 if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) { 384 String value = list.item(i).getNodeValue(); 386 contentHandler.characters(value.toCharArray(), 0, value.length()); 387 } else { 388 NodeList childs = list.item(i).getChildNodes(); 391 if (childs != null) { 392 for(int m = 0; m < childs.getLength(); m++) { 393 IncludeXMLConsumer.includeNode(childs.item(m), contentHandler, lexicalHandler); 394 } 395 } 396 } 397 } 398 } 399 400 return streamed; 401 } 402 403 408 public void loadXML(String path, 409 SourceParameters parameters) 410 throws SAXException , ProcessingException, IOException { 411 if (this.loadResource == null) { 412 throw new ProcessingException("The context " + this.name + " does not support loading."); 413 } 414 Source source = null; 415 try { 416 source = SourceUtil.getSource(this.loadResource, null, parameters, this.resolver); 417 Document doc = SourceUtil.toDOM(source); 418 DocumentFragment df = doc.createDocumentFragment(); 419 df.appendChild(doc.getDocumentElement()); 420 this.setXML(path, df); 421 } catch (SourceException se) { 422 throw SourceUtil.handle(se); 423 } finally { 424 resolver.release(source); 425 } 426 } 427 428 433 public void saveXML(String path, 434 SourceParameters parameters) 435 throws SAXException , ProcessingException, IOException { 436 if (this.saveResource == null) { 437 throw new ProcessingException("The context " + this.name + " does not support saving."); 438 } 439 DocumentFragment frag = this.getXML(path); 440 if (frag == null) { 441 frag = DOMUtil.createDocument().createDocumentFragment(); 443 } 444 445 SourceUtil.writeDOM(this.saveResource, 446 null, 447 parameters, 448 frag, 449 this.resolver, 450 "xml"); 451 } 452 453 } 454 455 | Popular Tags |