KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > session > context > RequestSessionContext


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.webapps.session.context;
17
18 import java.io.IOException JavaDoc;
19 import java.io.StringReader JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.framework.logger.Logger;
24 import org.apache.avalon.framework.service.ServiceException;
25 import org.apache.avalon.framework.service.ServiceManager;
26 import org.apache.cocoon.ProcessingException;
27 import org.apache.cocoon.environment.Cookie;
28 import org.apache.cocoon.environment.ObjectModelHelper;
29 import org.apache.cocoon.environment.Request;
30 import org.apache.cocoon.transformation.CIncludeTransformer;
31 import org.apache.cocoon.xml.IncludeXMLConsumer;
32 import org.apache.cocoon.xml.dom.DOMUtil;
33 import org.apache.commons.lang.BooleanUtils;
34 import org.apache.excalibur.source.SourceParameters;
35 import org.apache.excalibur.xml.sax.SAXParser;
36 import org.apache.excalibur.xml.xpath.XPathProcessor;
37 import org.w3c.dom.DOMException JavaDoc;
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.DocumentFragment JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41 import org.w3c.dom.Node JavaDoc;
42 import org.w3c.dom.NodeList JavaDoc;
43 import org.xml.sax.ContentHandler JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45 import org.xml.sax.ext.LexicalHandler JavaDoc;
46
47 /**
48  * A SessionContext which encapsulates the current Request object.
49  *
50  * It is not allowed to change this context.
51  * The following paths are valid:
52  * /parameter - lists all parameters, parameter names build the
53  * elements with the value of the first parameter with
54  * this name as text node childs
55  * /parameter/<parameter_name> - one text node containing the value of the first
56  * parameter with this name
57  * /querystring - the querystring with a leading '?' or null (the querystring is only for GET)
58  *
59  * /parametervalues - same as /parameter but values are listed as described
60  * below and each value of a parameter is listed.
61  * <cinclude:parameters>
62  * <cinclude:parameter>
63  * <cinclude:name>parameter name</cinclude:name>
64  * <cinclude:value>parameter value</cinclude:value>
65  * </cinclude:parameter>
66  * ...
67  * <cinclude:parameter>
68  * <cinclude:name>parameter name</cinclude:name>
69  * <cinclude:value>parameter value</cinclude:value>
70  * </session:parameter>
71  * </cinclude:parameters>
72  * If a parameter has more than one value for each value a
73  * <cinclude:parameter/> block is generated.
74  * This output has the namespace of the CIncludeTransformer
75  * to use it as input for a <cinclude:includexml> command.
76  * /attributes - lists all attributes, attribute names build the elements
77  * with the values as childs
78  * /headers - lists all headers, header names build the elements
79  * with the values as text node childs
80  * /cookies ----- <cookie name="...">
81  * <comment/>
82  * <domain/>
83  * <maxAge/>
84  * <name/>
85  * <path/>
86  * <secure/>
87  * <value/>
88  * <version/>
89  * /characterEncoding
90  * /contentLength
91  * /contentType
92  * /protocol
93  * /remoteAddress
94  * /remoteHost
95  * /scheme
96  * /serverName
97  * /serverPort
98  * /method
99  * /contextPath
100  * /pathInfo
101  * /pathTranslated
102  * /remoteUser
103  * /requestedSessionId
104  * /requestURI
105  * /servletPath
106  * /isRequestedSessionIdFromCookie
107  * /isRequestedSessionIdFromCookie
108  * /isRequestedSessionIdValid
109  *
110  * The following attributes of the servlet api 2.2 are missing:
111  * - getUserPrincipal()
112  * - getLocale()
113  * - getLocales()
114  * - getAuthType()
115  *
116  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
117  * @version CVS $Id: RequestSessionContext.java 306580 2005-10-06 10:33:31Z cziegeler $
118 */

119 public final class RequestSessionContext
120 implements SessionContext {
121
122     private static final String JavaDoc PARAMETERS_ELEMENT = "cinclude:" + CIncludeTransformer.CINCLUDE_PARAMETERS_ELEMENT;
123     private static final String JavaDoc PARAMETER_ELEMENT = "cinclude:" + CIncludeTransformer.CINCLUDE_PARAMETER_ELEMENT;
124     private static final String JavaDoc NAME_ELEMENT = "cinclude:" + CIncludeTransformer.CINCLUDE_NAME_ELEMENT;
125     private static final String JavaDoc VALUE_ELEMENT = "cinclude:" + CIncludeTransformer.CINCLUDE_VALUE_ELEMENT;
126
127     /** The logger. */
128     protected Logger logger;
129
130     /** Name of this context */
131     private String JavaDoc name;
132
133     /** The current {@link org.apache.cocoon.environment.Request} */
134     transient private Request request;
135
136     /** The content of this context */
137     private Document JavaDoc contextData;
138
139     /** The XPath Processor */
140     private XPathProcessor xpathProcessor;
141
142     public RequestSessionContext(Logger logger) {
143         this.logger = logger;
144     }
145
146     /**
147      * Setup this context
148      */

149     public void setup(String JavaDoc value, String JavaDoc loadResource, String JavaDoc saveResource) {
150         this.name = value;
151     }
152
153     /**
154      * Set the Request
155      */

156     public void setup(Map JavaDoc objectModel, ServiceManager manager, XPathProcessor processor)
157     throws ProcessingException {
158         this.xpathProcessor = processor;
159         this.request = ObjectModelHelper.getRequest(objectModel);
160
161         contextData = DOMUtil.createDocument();
162         contextData.appendChild(contextData.createElementNS(null, "context"));
163
164         Element JavaDoc root = contextData.getDocumentElement();
165
166         SAXParser parser = null;
167         try {
168             parser = (SAXParser) manager.lookup( SAXParser.ROLE );
169             this.buildParameterXML(root, parser);
170         } catch (ServiceException ce) {
171             throw new ProcessingException("Unable to lookup parser.", ce);
172         } finally {
173             manager.release(parser );
174         }
175         this.buildAttributesXML(root);
176         this.buildMiscXML(root);
177         this.buildCookiesXML(root);
178         this.buildHeadersXML(root);
179     }
180
181     /**
182      * Get the name of the context
183      */

184     public String JavaDoc getName() {
185         return this.name;
186     }
187
188     /**
189      * Get the request object
190      */

191     public Request getRequest() {
192         return this.request;
193     }
194     
195     /**
196      * Build path
197      */

198     private String JavaDoc createPath(String JavaDoc path) {
199         if (path == null) path = "/";
200         if (path.startsWith("/") == false) path = "/" + path;
201         path = "/context" + path;
202         if (path.endsWith("/") == true) path = path.substring(0, path.length() - 1);
203         return path;
204     }
205
206     private Node JavaDoc createTextNode(Document JavaDoc doc, String JavaDoc value) {
207         return doc.createTextNode(value != null ? value : "");
208     }
209
210     /**
211      * Build attributes XML
212      */

213     private void buildMiscXML(Element JavaDoc root) {
214         Document JavaDoc doc = root.getOwnerDocument();
215
216         Element JavaDoc node;
217
218         node = doc.createElementNS(null, "characterEncoding");
219         node.appendChild(this.createTextNode(doc, this.request.getCharacterEncoding()));
220         root.appendChild(node);
221         node = doc.createElementNS(null, "contentLength");
222         node.appendChild(this.createTextNode(doc, "" + this.request.getContentLength()));
223         root.appendChild(node);
224         node = doc.createElementNS(null, "contentType");
225         node.appendChild(this.createTextNode(doc, this.request.getContentType()));
226         root.appendChild(node);
227         node = doc.createElementNS(null, "protocol");
228         node.appendChild(this.createTextNode(doc, this.request.getProtocol()));
229         root.appendChild(node);
230         node = doc.createElementNS(null, "remoteAddress");
231         node.appendChild(this.createTextNode(doc, this.request.getRemoteAddr()));
232         root.appendChild(node);
233         node = doc.createElementNS(null, "remoteHost");
234         node.appendChild(this.createTextNode(doc, this.request.getRemoteHost()));
235         root.appendChild(node);
236         node = doc.createElementNS(null, "scheme");
237         node.appendChild(this.createTextNode(doc, this.request.getScheme()));
238         root.appendChild(node);
239         node = doc.createElementNS(null, "serverName");
240         node.appendChild(this.createTextNode(doc, this.request.getServerName()));
241         root.appendChild(node);
242         node = doc.createElementNS(null, "serverPort");
243         node.appendChild(this.createTextNode(doc, ""+this.request.getServerPort()));
244         root.appendChild(node);
245         node = doc.createElementNS(null, "method");
246         node.appendChild(this.createTextNode(doc, this.request.getMethod()));
247         root.appendChild(node);
248         node = doc.createElementNS(null, "contextPath");
249         node.appendChild(this.createTextNode(doc, this.request.getContextPath()));
250         root.appendChild(node);
251         node = doc.createElementNS(null, "pathInfo");
252         node.appendChild(this.createTextNode(doc, this.request.getPathInfo()));
253         root.appendChild(node);
254         node = doc.createElementNS(null, "pathTranslated");
255         node.appendChild(this.createTextNode(doc, this.request.getPathTranslated()));
256         root.appendChild(node);
257         node = doc.createElementNS(null, "remoteUser");
258         node.appendChild(this.createTextNode(doc, this.request.getRemoteUser()));
259         root.appendChild(node);
260         node = doc.createElementNS(null, "requestedSessionId");
261         node.appendChild(this.createTextNode(doc, this.request.getRequestedSessionId()));
262         root.appendChild(node);
263         node = doc.createElementNS(null, "requestURI");
264         node.appendChild(this.createTextNode(doc, this.request.getRequestURI()));
265         root.appendChild(node);
266         node = doc.createElementNS(null, "servletPath");
267         node.appendChild(this.createTextNode(doc, this.request.getServletPath()));
268         root.appendChild(node);
269         node = doc.createElementNS(null, "isRequestedSessionIdFromCookie");
270         node.appendChild(doc.createTextNode(BooleanUtils.toStringTrueFalse(this.request.isRequestedSessionIdFromCookie())));
271         root.appendChild(node);
272         node = doc.createElementNS(null, "isRequestedSessionIdFromURL");
273         node.appendChild(doc.createTextNode(BooleanUtils.toStringTrueFalse(this.request.isRequestedSessionIdFromURL())));
274         root.appendChild(node);
275         node = doc.createElementNS(null, "isRequestedSessionIdValid");
276         node.appendChild(doc.createTextNode(BooleanUtils.toStringTrueFalse(this.request.isRequestedSessionIdValid())));
277         root.appendChild(node);
278     }
279
280     /**
281      * Build attributes XML
282      */

283     private void buildAttributesXML(Element JavaDoc root)
284     throws ProcessingException {
285         Document JavaDoc doc = root.getOwnerDocument();
286         Element JavaDoc attrElement = doc.createElementNS(null, "attributes");
287         String JavaDoc attrName;
288         Element JavaDoc attr;
289
290         root.appendChild(attrElement);
291         Enumeration JavaDoc all = this.request.getAttributeNames();
292         while (all.hasMoreElements() == true) {
293             attrName = (String JavaDoc) all.nextElement();
294             try {
295                 attr = doc.createElementNS(null, attrName);
296                 attrElement.appendChild(attr);
297                 DOMUtil.valueOf(attr, this.request.getAttribute(attrName));
298             } catch (DOMException JavaDoc de) {
299                 // Some request attributes have names that are invalid as element names.
300
// Example : "FOM JavaScript GLOBAL SCOPE/file://my/path/to/flow/script.js"
301
this.logger.info("RequestSessionContext: Cannot create XML element from request attribute '" + attrName + "' : " + de.getMessage());
302             }
303         }
304     }
305
306     /**
307      * Build cookies XML
308      */

309     private void buildCookiesXML(Element JavaDoc root) {
310         Document JavaDoc doc = root.getOwnerDocument();
311
312         Element JavaDoc cookiesElement = doc.createElementNS(null, "cookies");
313         root.appendChild(cookiesElement);
314
315         Cookie[] cookies = this.request.getCookies();
316         if (cookies != null) {
317             Cookie current;
318             Element JavaDoc node;
319             Element JavaDoc parent;
320             for(int i = 0; i < cookies.length; i++) {
321                 current = cookies[i];
322                 parent = doc.createElementNS(null, "cookie");
323                 parent.setAttributeNS(null, "name", current.getName());
324                 cookiesElement.appendChild(parent);
325                 node = doc.createElementNS(null, "comment");
326                 node.appendChild(this.createTextNode(doc, current.getComment()));
327                 parent.appendChild(node);
328                 node = doc.createElementNS(null, "domain");
329                 node.appendChild(this.createTextNode(doc, current.getDomain()));
330                 parent.appendChild(node);
331                 node = doc.createElementNS(null, "maxAge");
332                 node.appendChild(this.createTextNode(doc, "" + current.getMaxAge()));
333                 parent.appendChild(node);
334                 node = doc.createElementNS(null, "name");
335                 node.appendChild(this.createTextNode(doc, current.getName()));
336                 parent.appendChild(node);
337                 node = doc.createElementNS(null, "path");
338                 node.appendChild(this.createTextNode(doc, current.getPath()));
339                 parent.appendChild(node);
340                 node = doc.createElementNS(null, "secure");
341                 node.appendChild(doc.createTextNode(BooleanUtils.toStringTrueFalse(current.getSecure())));
342                 parent.appendChild(node);
343                 node = doc.createElementNS(null, "value");
344                 node.appendChild(this.createTextNode(doc, current.getValue()));
345                 parent.appendChild(node);
346                 node = doc.createElementNS(null, "version");
347                 node.appendChild(this.createTextNode(doc, "" + current.getVersion()));
348                 parent.appendChild(node);
349             }
350         }
351     }
352
353     /**
354      * Build headers XML
355      */

356     private void buildHeadersXML(Element JavaDoc root) {
357         Document JavaDoc doc = root.getOwnerDocument();
358         Element JavaDoc headersElement = doc.createElementNS(null, "headers");
359         String JavaDoc headerName;
360         Element JavaDoc header;
361
362         root.appendChild(headersElement);
363         Enumeration JavaDoc all = this.request.getHeaderNames();
364         while (all.hasMoreElements() == true) {
365             headerName = (String JavaDoc) all.nextElement();
366             try {
367                 header = doc.createElementNS(null, headerName);
368                 headersElement.appendChild(header);
369                 header.appendChild(this.createTextNode(doc, this.request.getHeader(headerName)));
370             } catch (Exception JavaDoc ignore) {
371                 // if the header name is not a valid element name, we simply ignore it
372
}
373         }
374     }
375
376     /**
377      * Build parameter XML
378      */

379     private void buildParameterXML(Element JavaDoc root, SAXParser parser) {
380         Document JavaDoc doc = root.getOwnerDocument();
381         // include all parameters
382
// process "/parameter" and "/parametervalues" at the same time
383
Element JavaDoc parameterElement = doc.createElementNS(null, "parameter");
384         Element JavaDoc parameterValuesElement = doc.createElementNS(null, "parametervalues");
385         root.appendChild(parameterElement);
386         root.appendChild(parameterValuesElement);
387         String JavaDoc parameterName = null;
388         Enumeration JavaDoc pars = this.request.getParameterNames();
389         Element JavaDoc parameter;
390         Element JavaDoc element;
391         Node JavaDoc valueNode;
392         String JavaDoc[] values;
393         String JavaDoc parValue;
394
395         element = doc.createElementNS(CIncludeTransformer.CINCLUDE_NAMESPACE_URI, PARAMETERS_ELEMENT);
396         element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:cinclude", CIncludeTransformer.CINCLUDE_NAMESPACE_URI);
397         parameterValuesElement.appendChild(element);
398         parameterValuesElement = element;
399
400         while (pars.hasMoreElements() == true) {
401             parameterName = (String JavaDoc)pars.nextElement();
402             values = this.request.getParameterValues(parameterName);
403
404             for(int i = 0; i < values.length; i++) {
405
406                 // this is a fast test, if the parameter value contains xml!
407
parValue = values[i].trim();
408                 if (parValue.length() > 0 && parValue.charAt(0) == '<') {
409                     try {
410                         valueNode = DOMUtil.getDocumentFragment(parser, new StringReader JavaDoc(parValue));
411                         valueNode = doc.importNode(valueNode, true);
412                     } catch (Exception JavaDoc noXMLException) {
413                         valueNode = doc.createTextNode(parValue);
414                     }
415                 } else {
416                     valueNode = doc.createTextNode(parValue);
417                 }
418                 // create "/parameter" entry for first value
419
if (i == 0) {
420                     try {
421                         parameter = doc.createElementNS(null, parameterName);
422                         parameter.appendChild(valueNode);
423                         parameterElement.appendChild(parameter);
424                     } catch (Exception JavaDoc local) {
425                         // the exception is ignored and only this parameters is ignored
426
}
427                 }
428
429                 try {
430                     // create "/parametervalues" entry
431
element = doc.createElementNS(CIncludeTransformer.CINCLUDE_NAMESPACE_URI, PARAMETER_ELEMENT);
432                     parameterValuesElement.appendChild(element);
433                     parameter = element;
434                     element = doc.createElementNS(CIncludeTransformer.CINCLUDE_NAMESPACE_URI, NAME_ELEMENT);
435                     parameter.appendChild(element);
436                     element.appendChild(doc.createTextNode(parameterName));
437                     element = doc.createElementNS(CIncludeTransformer.CINCLUDE_NAMESPACE_URI, VALUE_ELEMENT);
438                     parameter.appendChild(element);
439                     element.appendChild(valueNode.cloneNode(true));
440                 } catch (Exception JavaDoc local) {
441                     // the exception is ignored and only this parameters is ignored
442
}
443             }
444         }
445         // and now the query string
446
element = doc.createElementNS(null, "querystring");
447         root.appendChild(element);
448         String JavaDoc value = request.getQueryString();
449         if (value != null) {
450             element.appendChild(doc.createTextNode('?' + value));
451         }
452     }
453
454     /**
455      * Get the XML from the request object
456      */

457     public DocumentFragment JavaDoc getXML(String JavaDoc path)
458     throws ProcessingException {
459         if (path == null || path.charAt(0) != '/') {
460             throw new ProcessingException("Not a valid XPath: " + path);
461         }
462         path = this.createPath(path);
463         DocumentFragment JavaDoc result = null;
464         NodeList JavaDoc list;
465
466         try {
467             list = DOMUtil.selectNodeList(this.contextData, path, this.xpathProcessor);
468         } catch (javax.xml.transform.TransformerException JavaDoc localException) {
469             throw new ProcessingException("Exception: " + localException, localException);
470         }
471         if (list != null && list.getLength() > 0) {
472             result = DOMUtil.getOwnerDocument(contextData).createDocumentFragment();
473             for(int i = 0; i < list.getLength(); i++) {
474
475                 // the found node is either an attribute or an element
476
if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) {
477                     // if it is an attribute simple create a new text node with the value of the attribute
478
result.appendChild(DOMUtil.getOwnerDocument(contextData).createTextNode(list.item(i).getNodeValue()));
479                 } else {
480                     // now we have an element
481
// copy all children of this element in the resulting tree
482
NodeList JavaDoc childs = list.item(i).getChildNodes();
483                     if (childs != null) {
484                         for(int m = 0; m < childs.getLength(); m++) {
485                             result.appendChild(DOMUtil.getOwnerDocument(contextData).importNode(childs.item(m), true));
486                         }
487                     }
488                 }
489             }
490         }
491
492         return result;
493     }
494
495     /**
496      * Setting of xml is not possible for the request context
497      */

498     public void setXML(String JavaDoc path, DocumentFragment JavaDoc fragment)
499     throws ProcessingException {
500         throw new ProcessingException("RequestSessionContext: Setting of xml not allowed");
501     }
502
503     /**
504      * Setting of xml is not possible for the request context
505      */

506     public void setValueOfNode(String JavaDoc path, String JavaDoc value)
507     throws ProcessingException {
508         throw new ProcessingException("RequestSessionContext: Setting of xml not allowed");
509     }
510
511     /**
512      * Append a document fragment is not possible for the request context.
513      */

514     public void appendXML(String JavaDoc path, DocumentFragment JavaDoc fragment)
515     throws ProcessingException {
516         throw new ProcessingException("RequestSessionContext: Appending of xml not allowed");
517     }
518
519     /**
520      * Removing is not possible for the request context.
521      */

522     public void removeXML(String JavaDoc path)
523     throws ProcessingException {
524         throw new ProcessingException("RequestSessionContext: Removing of xml not allowed");
525     }
526
527     /**
528      * Set a context attribute. If value is null the attribute is removed.
529      */

530     public void setAttribute(String JavaDoc key, Object JavaDoc value)
531     throws ProcessingException {
532         if (value == null) {
533             this.request.removeAttribute(key);
534         } else {
535             this.request.setAttribute(key, value);
536         }
537     }
538
539     /**
540      * Get a context attribute. If the attribute is not available return null
541      */

542     public Object JavaDoc getAttribute(String JavaDoc key)
543     throws ProcessingException {
544         return this.request.getAttribute(key);
545     }
546
547     /**
548      * Get a context attribute. If the attribute is not available the defaultObject is returned
549      */

550     public Object JavaDoc getAttribute(String JavaDoc key, Object JavaDoc defaultObject)
551     throws ProcessingException {
552         Object JavaDoc obj = this.getAttribute(key);
553         return (obj != null ? obj : defaultObject);
554     }
555
556     /**
557      * Get a copy the first node specified by the path.
558      */

559     public Node JavaDoc getSingleNode(String JavaDoc path)
560     throws ProcessingException {
561         path = this.createPath(path);
562         Node JavaDoc node = null;
563
564         try {
565             node = DOMUtil.getSingleNode(this.contextData, path, this.xpathProcessor);
566         } catch (javax.xml.transform.TransformerException JavaDoc localException) {
567             throw new ProcessingException("Exception: " + localException, localException);
568         }
569         return node;
570     }
571
572     /**
573      * Get a copy all the nodes specified by the path.
574      */

575     public NodeList JavaDoc getNodeList(String JavaDoc path)
576     throws ProcessingException {
577         path = this.createPath(path);
578         NodeList JavaDoc list = null;
579
580         try {
581             list = DOMUtil.selectNodeList(this.contextData, path, this.xpathProcessor);
582         } catch (javax.xml.transform.TransformerException JavaDoc localException) {
583             throw new ProcessingException("Exception: " + localException, localException);
584         }
585         return list;
586     }
587
588     /**
589      * Set the value of a node. The node is copied before insertion.
590      */

591     public void setNode(String JavaDoc path, Node JavaDoc node)
592     throws ProcessingException {
593         throw new ProcessingException("RequestSessionContext: Setting of XML not allowed");
594     }
595
596     /**
597      * Get the value of this node. This is similiar to the xsl:value-of
598      * function. If the node does not exist, <code>null</code> is returned.
599      */

600     public String JavaDoc getValueOfNode(String JavaDoc path)
601     throws ProcessingException {
602         String JavaDoc value = null;
603         Node JavaDoc node = this.getSingleNode(path);
604         if (node != null) {
605             value = DOMUtil.getValueOfNode(node);
606         }
607
608         return value;
609     }
610
611     /**
612      * Stream the XML directly to the handler. This streams the contents of getXML()
613      * to the given handler without creating a DocumentFragment containing a copy
614      * of the data
615      */

616     public boolean streamXML(String JavaDoc path,
617                              ContentHandler JavaDoc contentHandler,
618                              LexicalHandler JavaDoc lexicalHandler)
619     throws SAXException JavaDoc, ProcessingException {
620         boolean result = false;
621         NodeList JavaDoc list;
622
623         try {
624             list = DOMUtil.selectNodeList(this.contextData, this.createPath(path), this.xpathProcessor);
625         } catch (javax.xml.transform.TransformerException JavaDoc local) {
626             throw new ProcessingException("TransformerException: " + local, local);
627         }
628         if (list != null && list.getLength() > 0) {
629             result = true;
630             for(int i = 0; i < list.getLength(); i++) {
631
632                 // the found node is either an attribute or an element
633
if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) {
634                     // if it is an attribute simple create a new text node with the value of the attribute
635
String JavaDoc value = list.item(i).getNodeValue();
636                     contentHandler.characters(value.toCharArray(), 0, value.length());
637                 } else {
638                     // now we have an element
639
// stream all children of this element to the resulting tree
640
NodeList JavaDoc childs = list.item(i).getChildNodes();
641                     if (childs != null) {
642                         for(int m = 0; m < childs.getLength(); m++) {
643                             IncludeXMLConsumer.includeNode(childs.item(m), contentHandler, lexicalHandler);
644                         }
645                     }
646                 }
647             }
648         }
649
650         return result;
651     }
652
653     /**
654      * Get the request parameter as xml
655      */

656     public DocumentFragment JavaDoc getParameterAsXML(final String JavaDoc parameterName)
657     throws ProcessingException {
658         return this.getXML("/parameter/"+parameterName);
659     }
660
661     /**
662      * Get the request parameter as a String
663      */

664     public String JavaDoc getParameter(final String JavaDoc parameterName) {
665         return this.request.getParameter(parameterName);
666     }
667
668     /**
669      * Try to load XML into the context.
670      * If the context does not provide the ability of loading,
671      * an exception is thrown.
672      */

673     public void loadXML(String JavaDoc path,
674                         SourceParameters parameters)
675     throws SAXException JavaDoc, ProcessingException, IOException JavaDoc {
676         throw new ProcessingException("The context " + this.name + " does not support loading.");
677     }
678
679     /**
680      * Try to save XML from the context.
681      * If the context does not provide the ability of saving,
682      * an exception is thrown.
683      */

684     public void saveXML(String JavaDoc path,
685                         SourceParameters parameters)
686     throws SAXException JavaDoc, ProcessingException, IOException JavaDoc {
687         throw new ProcessingException("The context " + this.name + " does not support saving.");
688     }
689
690 }
691
Popular Tags