Source Code Cross Referenced for RequestSessionContext.java in  » Content-Management-System » apache-lenya-2.0 » org » apache » cocoon » webapps » session » context » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » apache lenya 2.0 » org.apache.cocoon.webapps.session.context 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.cocoon.webapps.session.context;
018:
019:        import java.io.IOException;
020:        import java.io.StringReader;
021:        import java.util.Enumeration;
022:        import java.util.Map;
023:
024:        import org.apache.avalon.framework.logger.Logger;
025:        import org.apache.avalon.framework.service.ServiceException;
026:        import org.apache.avalon.framework.service.ServiceManager;
027:        import org.apache.cocoon.ProcessingException;
028:        import org.apache.cocoon.environment.Cookie;
029:        import org.apache.cocoon.environment.ObjectModelHelper;
030:        import org.apache.cocoon.environment.Request;
031:        import org.apache.cocoon.transformation.CIncludeTransformer;
032:        import org.apache.cocoon.xml.IncludeXMLConsumer;
033:        import org.apache.cocoon.xml.dom.DOMUtil;
034:        import org.apache.commons.lang.BooleanUtils;
035:        import org.apache.excalibur.source.SourceParameters;
036:        import org.apache.excalibur.xml.sax.SAXParser;
037:        import org.apache.excalibur.xml.xpath.XPathProcessor;
038:        import org.w3c.dom.DOMException;
039:        import org.w3c.dom.Document;
040:        import org.w3c.dom.DocumentFragment;
041:        import org.w3c.dom.Element;
042:        import org.w3c.dom.Node;
043:        import org.w3c.dom.NodeList;
044:        import org.xml.sax.ContentHandler;
045:        import org.xml.sax.SAXException;
046:        import org.xml.sax.ext.LexicalHandler;
047:
048:        /**
049:         * A SessionContext which encapsulates the current Request object.
050:         *
051:         * It is not allowed to change this context.
052:         * The following paths are valid:
053:         * /parameter                  - lists all parameters, parameter names build the
054:         *                               elements with the value of the first parameter with
055:         *                               this name as text node childs
056:         * /parameter/<parameter_name> - one text node containing the value of the first
057:         *                               parameter with this name
058:         * /querystring                - the querystring with a leading '?' or null (the querystring is only for GET)
059:         *
060:         * /parametervalues            - same as /parameter but values are listed as described
061:         *                               below and each value of a parameter is listed.
062:         *                               <cinclude:parameters>
063:         *                                      <cinclude:parameter>
064:         *                                              <cinclude:name>parameter name</cinclude:name>
065:         *                                              <cinclude:value>parameter value</cinclude:value>
066:         *                                      </cinclude:parameter>
067:         *                                       ...
068:         *                                      <cinclude:parameter>
069:         *                                              <cinclude:name>parameter name</cinclude:name>
070:         *                                              <cinclude:value>parameter value</cinclude:value>
071:         *                                      </session:parameter>
072:         *                               </cinclude:parameters>
073:         *                               If a parameter has more than one value for each value a
074:         *                               <cinclude:parameter/> block is generated.
075:         *                               This output has the namespace of the CIncludeTransformer
076:         *                               to use it as input for a <cinclude:includexml> command.
077:         * /attributes - lists all attributes, attribute names build the elements
078:         *               with the values as childs
079:         * /headers    - lists all headers, header names build the elements
080:         *               with the values as text node childs
081:         * /cookies ----- <cookie name="...">
082:         *                   <comment/>
083:         *                   <domain/>
084:         *                   <maxAge/>
085:         *                   <name/>
086:         *                   <path/>
087:         *                   <secure/>
088:         *                   <value/>
089:         *                   <version/>
090:         * /characterEncoding
091:         * /contentLength
092:         * /contentType
093:         * /protocol
094:         * /remoteAddress
095:         * /remoteHost
096:         * /scheme
097:         * /serverName
098:         * /serverPort
099:         * /method
100:         * /contextPath
101:         * /pathInfo
102:         * /pathTranslated
103:         * /remoteUser
104:         * /requestedSessionId
105:         * /requestURI
106:         * /servletPath
107:         * /isRequestedSessionIdFromCookie
108:         * /isRequestedSessionIdFromCookie
109:         * /isRequestedSessionIdValid
110:         *
111:         *  The following attributes of the servlet api 2.2 are missing:
112:         *  - getUserPrincipal()
113:         *  - getLocale()
114:         *  - getLocales()
115:         *  - getAuthType()
116:         *
117:         * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
118:         * @deprecated This block is deprecated and will be removed in future versions.
119:         * @version CVS $Id: RequestSessionContext.java 433543 2006-08-22 06:22:54Z crossley $
120:         */
121:        public final class RequestSessionContext implements  SessionContext {
122:
123:            private static final String PARAMETERS_ELEMENT = "cinclude:"
124:                    + CIncludeTransformer.CINCLUDE_PARAMETERS_ELEMENT;
125:            private static final String PARAMETER_ELEMENT = "cinclude:"
126:                    + CIncludeTransformer.CINCLUDE_PARAMETER_ELEMENT;
127:            private static final String NAME_ELEMENT = "cinclude:"
128:                    + CIncludeTransformer.CINCLUDE_NAME_ELEMENT;
129:            private static final String VALUE_ELEMENT = "cinclude:"
130:                    + CIncludeTransformer.CINCLUDE_VALUE_ELEMENT;
131:
132:            /** The logger. */
133:            protected Logger logger;
134:
135:            /** Name of this context */
136:            private String name;
137:
138:            /** The current {@link org.apache.cocoon.environment.Request} */
139:            transient private Request request;
140:
141:            /** The content of this context */
142:            private Document contextData;
143:
144:            /** The XPath Processor */
145:            private XPathProcessor xpathProcessor;
146:
147:            public RequestSessionContext(Logger logger) {
148:                this .logger = logger;
149:            }
150:
151:            /**
152:             * Setup this context
153:             */
154:            public void setup(String value, String loadResource,
155:                    String saveResource) {
156:                this .name = value;
157:            }
158:
159:            /**
160:             * Set the Request
161:             */
162:            public void setup(Map objectModel, ServiceManager manager,
163:                    XPathProcessor processor) throws ProcessingException {
164:                this .xpathProcessor = processor;
165:                this .request = ObjectModelHelper.getRequest(objectModel);
166:
167:                contextData = DOMUtil.createDocument();
168:                contextData.appendChild(contextData.createElementNS(null,
169:                        "context"));
170:
171:                Element root = contextData.getDocumentElement();
172:
173:                SAXParser parser = null;
174:                try {
175:                    parser = (SAXParser) manager.lookup(SAXParser.ROLE);
176:                    this .buildParameterXML(root, parser);
177:                } catch (ServiceException ce) {
178:                    throw new ProcessingException("Unable to lookup parser.",
179:                            ce);
180:                } finally {
181:                    manager.release(parser);
182:                }
183:                this .buildAttributesXML(root);
184:                this .buildMiscXML(root);
185:                this .buildCookiesXML(root);
186:                this .buildHeadersXML(root);
187:            }
188:
189:            /**
190:             * Get the name of the context
191:             */
192:            public String getName() {
193:                return this .name;
194:            }
195:
196:            /**
197:             * Get the request object
198:             */
199:            public Request getRequest() {
200:                return this .request;
201:            }
202:
203:            /**
204:             * Build path
205:             */
206:            private String createPath(String path) {
207:                if (path == null)
208:                    path = "/";
209:                if (path.startsWith("/") == false)
210:                    path = "/" + path;
211:                path = "/context" + path;
212:                if (path.endsWith("/") == true)
213:                    path = path.substring(0, path.length() - 1);
214:                return path;
215:            }
216:
217:            private Node createTextNode(Document doc, String value) {
218:                return doc.createTextNode(value != null ? value : "");
219:            }
220:
221:            /**
222:             * Build attributes XML
223:             */
224:            private void buildMiscXML(Element root) {
225:                Document doc = root.getOwnerDocument();
226:
227:                Element node;
228:
229:                node = doc.createElementNS(null, "characterEncoding");
230:                node.appendChild(this .createTextNode(doc, this .request
231:                        .getCharacterEncoding()));
232:                root.appendChild(node);
233:                node = doc.createElementNS(null, "contentLength");
234:                node.appendChild(this .createTextNode(doc, ""
235:                        + this .request.getContentLength()));
236:                root.appendChild(node);
237:                node = doc.createElementNS(null, "contentType");
238:                node.appendChild(this .createTextNode(doc, this .request
239:                        .getContentType()));
240:                root.appendChild(node);
241:                node = doc.createElementNS(null, "protocol");
242:                node.appendChild(this .createTextNode(doc, this .request
243:                        .getProtocol()));
244:                root.appendChild(node);
245:                node = doc.createElementNS(null, "remoteAddress");
246:                node.appendChild(this .createTextNode(doc, this .request
247:                        .getRemoteAddr()));
248:                root.appendChild(node);
249:                node = doc.createElementNS(null, "remoteHost");
250:                node.appendChild(this .createTextNode(doc, this .request
251:                        .getRemoteHost()));
252:                root.appendChild(node);
253:                node = doc.createElementNS(null, "scheme");
254:                node.appendChild(this .createTextNode(doc, this .request
255:                        .getScheme()));
256:                root.appendChild(node);
257:                node = doc.createElementNS(null, "serverName");
258:                node.appendChild(this .createTextNode(doc, this .request
259:                        .getServerName()));
260:                root.appendChild(node);
261:                node = doc.createElementNS(null, "serverPort");
262:                node.appendChild(this .createTextNode(doc, ""
263:                        + this .request.getServerPort()));
264:                root.appendChild(node);
265:                node = doc.createElementNS(null, "method");
266:                node.appendChild(this .createTextNode(doc, this .request
267:                        .getMethod()));
268:                root.appendChild(node);
269:                node = doc.createElementNS(null, "contextPath");
270:                node.appendChild(this .createTextNode(doc, this .request
271:                        .getContextPath()));
272:                root.appendChild(node);
273:                node = doc.createElementNS(null, "pathInfo");
274:                node.appendChild(this .createTextNode(doc, this .request
275:                        .getPathInfo()));
276:                root.appendChild(node);
277:                node = doc.createElementNS(null, "pathTranslated");
278:                node.appendChild(this .createTextNode(doc, this .request
279:                        .getPathTranslated()));
280:                root.appendChild(node);
281:                node = doc.createElementNS(null, "remoteUser");
282:                node.appendChild(this .createTextNode(doc, this .request
283:                        .getRemoteUser()));
284:                root.appendChild(node);
285:                node = doc.createElementNS(null, "requestedSessionId");
286:                node.appendChild(this .createTextNode(doc, this .request
287:                        .getRequestedSessionId()));
288:                root.appendChild(node);
289:                node = doc.createElementNS(null, "requestURI");
290:                node.appendChild(this .createTextNode(doc, this .request
291:                        .getRequestURI()));
292:                root.appendChild(node);
293:                node = doc.createElementNS(null, "servletPath");
294:                node.appendChild(this .createTextNode(doc, this .request
295:                        .getServletPath()));
296:                root.appendChild(node);
297:                node = doc.createElementNS(null,
298:                        "isRequestedSessionIdFromCookie");
299:                node.appendChild(doc.createTextNode(BooleanUtils
300:                        .toStringTrueFalse(this .request
301:                                .isRequestedSessionIdFromCookie())));
302:                root.appendChild(node);
303:                node = doc.createElementNS(null, "isRequestedSessionIdFromURL");
304:                node.appendChild(doc.createTextNode(BooleanUtils
305:                        .toStringTrueFalse(this .request
306:                                .isRequestedSessionIdFromURL())));
307:                root.appendChild(node);
308:                node = doc.createElementNS(null, "isRequestedSessionIdValid");
309:                node.appendChild(doc.createTextNode(BooleanUtils
310:                        .toStringTrueFalse(this .request
311:                                .isRequestedSessionIdValid())));
312:                root.appendChild(node);
313:            }
314:
315:            /**
316:             * Build attributes XML
317:             */
318:            private void buildAttributesXML(Element root)
319:                    throws ProcessingException {
320:                Document doc = root.getOwnerDocument();
321:                Element attrElement = doc.createElementNS(null, "attributes");
322:                String attrName;
323:                Element attr;
324:
325:                root.appendChild(attrElement);
326:                Enumeration all = this .request.getAttributeNames();
327:                while (all.hasMoreElements() == true) {
328:                    attrName = (String) all.nextElement();
329:                    try {
330:                        attr = doc.createElementNS(null, attrName);
331:                        attrElement.appendChild(attr);
332:                        DOMUtil.valueOf(attr, this .request
333:                                .getAttribute(attrName));
334:                    } catch (DOMException de) {
335:                        // Some request attributes have names that are invalid as element names.
336:                        // Example : "FOM JavaScript GLOBAL SCOPE/file://my/path/to/flow/script.js"
337:                        this .logger
338:                                .info("RequestSessionContext: Cannot create XML element from request attribute '"
339:                                        + attrName + "' : " + de.getMessage());
340:                    }
341:                }
342:            }
343:
344:            /**
345:             * Build cookies XML
346:             */
347:            private void buildCookiesXML(Element root) {
348:                Document doc = root.getOwnerDocument();
349:
350:                Element cookiesElement = doc.createElementNS(null, "cookies");
351:                root.appendChild(cookiesElement);
352:
353:                Cookie[] cookies = this .request.getCookies();
354:                if (cookies != null) {
355:                    Cookie current;
356:                    Element node;
357:                    Element parent;
358:                    for (int i = 0; i < cookies.length; i++) {
359:                        current = cookies[i];
360:                        parent = doc.createElementNS(null, "cookie");
361:                        parent.setAttributeNS(null, "name", current.getName());
362:                        cookiesElement.appendChild(parent);
363:                        node = doc.createElementNS(null, "comment");
364:                        node.appendChild(this .createTextNode(doc, current
365:                                .getComment()));
366:                        parent.appendChild(node);
367:                        node = doc.createElementNS(null, "domain");
368:                        node.appendChild(this .createTextNode(doc, current
369:                                .getDomain()));
370:                        parent.appendChild(node);
371:                        node = doc.createElementNS(null, "maxAge");
372:                        node.appendChild(this .createTextNode(doc, ""
373:                                + current.getMaxAge()));
374:                        parent.appendChild(node);
375:                        node = doc.createElementNS(null, "name");
376:                        node.appendChild(this .createTextNode(doc, current
377:                                .getName()));
378:                        parent.appendChild(node);
379:                        node = doc.createElementNS(null, "path");
380:                        node.appendChild(this .createTextNode(doc, current
381:                                .getPath()));
382:                        parent.appendChild(node);
383:                        node = doc.createElementNS(null, "secure");
384:                        node.appendChild(doc.createTextNode(BooleanUtils
385:                                .toStringTrueFalse(current.getSecure())));
386:                        parent.appendChild(node);
387:                        node = doc.createElementNS(null, "value");
388:                        node.appendChild(this .createTextNode(doc, current
389:                                .getValue()));
390:                        parent.appendChild(node);
391:                        node = doc.createElementNS(null, "version");
392:                        node.appendChild(this .createTextNode(doc, ""
393:                                + current.getVersion()));
394:                        parent.appendChild(node);
395:                    }
396:                }
397:            }
398:
399:            /**
400:             * Build headers XML
401:             */
402:            private void buildHeadersXML(Element root) {
403:                Document doc = root.getOwnerDocument();
404:                Element headersElement = doc.createElementNS(null, "headers");
405:                String headerName;
406:                Element header;
407:
408:                root.appendChild(headersElement);
409:                Enumeration all = this .request.getHeaderNames();
410:                while (all.hasMoreElements() == true) {
411:                    headerName = (String) all.nextElement();
412:                    try {
413:                        header = doc.createElementNS(null, headerName);
414:                        headersElement.appendChild(header);
415:                        header.appendChild(this .createTextNode(doc,
416:                                this .request.getHeader(headerName)));
417:                    } catch (Exception ignore) {
418:                        // if the header name is not a valid element name, we simply ignore it
419:                    }
420:                }
421:            }
422:
423:            /**
424:             * Build parameter XML
425:             */
426:            private void buildParameterXML(Element root, SAXParser parser) {
427:                Document doc = root.getOwnerDocument();
428:                // include all parameters
429:                // process "/parameter" and "/parametervalues" at the same time
430:                Element parameterElement = doc.createElementNS(null,
431:                        "parameter");
432:                Element parameterValuesElement = doc.createElementNS(null,
433:                        "parametervalues");
434:                root.appendChild(parameterElement);
435:                root.appendChild(parameterValuesElement);
436:                String parameterName = null;
437:                Enumeration pars = this .request.getParameterNames();
438:                Element parameter;
439:                Element element;
440:                Node valueNode;
441:                String[] values;
442:                String parValue;
443:
444:                element = doc.createElementNS(
445:                        CIncludeTransformer.CINCLUDE_NAMESPACE_URI,
446:                        PARAMETERS_ELEMENT);
447:                element.setAttributeNS("http://www.w3.org/2000/xmlns/",
448:                        "xmlns:cinclude",
449:                        CIncludeTransformer.CINCLUDE_NAMESPACE_URI);
450:                parameterValuesElement.appendChild(element);
451:                parameterValuesElement = element;
452:
453:                while (pars.hasMoreElements() == true) {
454:                    parameterName = (String) pars.nextElement();
455:                    values = this .request.getParameterValues(parameterName);
456:
457:                    for (int i = 0; i < values.length; i++) {
458:
459:                        // this is a fast test, if the parameter value contains xml!
460:                        parValue = values[i].trim();
461:                        if (parValue.length() > 0 && parValue.charAt(0) == '<') {
462:                            try {
463:                                valueNode = DOMUtil.getDocumentFragment(parser,
464:                                        new StringReader(parValue));
465:                                valueNode = doc.importNode(valueNode, true);
466:                            } catch (Exception noXMLException) {
467:                                valueNode = doc.createTextNode(parValue);
468:                            }
469:                        } else {
470:                            valueNode = doc.createTextNode(parValue);
471:                        }
472:                        // create "/parameter" entry for first value
473:                        if (i == 0) {
474:                            try {
475:                                parameter = doc.createElementNS(null,
476:                                        parameterName);
477:                                parameter.appendChild(valueNode);
478:                                parameterElement.appendChild(parameter);
479:                            } catch (Exception local) {
480:                                // the exception is ignored and only this parameters is ignored
481:                            }
482:                        }
483:
484:                        try {
485:                            // create "/parametervalues" entry
486:                            element = doc.createElementNS(
487:                                    CIncludeTransformer.CINCLUDE_NAMESPACE_URI,
488:                                    PARAMETER_ELEMENT);
489:                            parameterValuesElement.appendChild(element);
490:                            parameter = element;
491:                            element = doc.createElementNS(
492:                                    CIncludeTransformer.CINCLUDE_NAMESPACE_URI,
493:                                    NAME_ELEMENT);
494:                            parameter.appendChild(element);
495:                            element.appendChild(doc
496:                                    .createTextNode(parameterName));
497:                            element = doc.createElementNS(
498:                                    CIncludeTransformer.CINCLUDE_NAMESPACE_URI,
499:                                    VALUE_ELEMENT);
500:                            parameter.appendChild(element);
501:                            element.appendChild(valueNode.cloneNode(true));
502:                        } catch (Exception local) {
503:                            // the exception is ignored and only this parameters is ignored
504:                        }
505:                    }
506:                }
507:                // and now the query string
508:                element = doc.createElementNS(null, "querystring");
509:                root.appendChild(element);
510:                String value = request.getQueryString();
511:                if (value != null) {
512:                    element.appendChild(doc.createTextNode('?' + value));
513:                }
514:            }
515:
516:            /**
517:             * Get the XML from the request object
518:             */
519:            public DocumentFragment getXML(String path)
520:                    throws ProcessingException {
521:                if (path == null || path.charAt(0) != '/') {
522:                    throw new ProcessingException("Not a valid XPath: " + path);
523:                }
524:                path = this .createPath(path);
525:                DocumentFragment result = null;
526:                NodeList list;
527:
528:                try {
529:                    list = DOMUtil.selectNodeList(this .contextData, path,
530:                            this .xpathProcessor);
531:                } catch (javax.xml.transform.TransformerException localException) {
532:                    throw new ProcessingException("Exception: "
533:                            + localException, localException);
534:                }
535:                if (list != null && list.getLength() > 0) {
536:                    result = DOMUtil.getOwnerDocument(contextData)
537:                            .createDocumentFragment();
538:                    for (int i = 0; i < list.getLength(); i++) {
539:
540:                        // the found node is either an attribute or an element
541:                        if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) {
542:                            // if it is an attribute simple create a new text node with the value of the attribute
543:                            result.appendChild(DOMUtil.getOwnerDocument(
544:                                    contextData).createTextNode(
545:                                    list.item(i).getNodeValue()));
546:                        } else {
547:                            // now we have an element
548:                            // copy all children of this element in the resulting tree
549:                            NodeList childs = list.item(i).getChildNodes();
550:                            if (childs != null) {
551:                                for (int m = 0; m < childs.getLength(); m++) {
552:                                    result.appendChild(DOMUtil
553:                                            .getOwnerDocument(contextData)
554:                                            .importNode(childs.item(m), true));
555:                                }
556:                            }
557:                        }
558:                    }
559:                }
560:
561:                return result;
562:            }
563:
564:            /**
565:             * Setting of xml is not possible for the request context
566:             */
567:            public void setXML(String path, DocumentFragment fragment)
568:                    throws ProcessingException {
569:                throw new ProcessingException(
570:                        "RequestSessionContext: Setting of xml not allowed");
571:            }
572:
573:            /**
574:             * Setting of xml is not possible for the request context
575:             */
576:            public void setValueOfNode(String path, String value)
577:                    throws ProcessingException {
578:                throw new ProcessingException(
579:                        "RequestSessionContext: Setting of xml not allowed");
580:            }
581:
582:            /**
583:             * Append a document fragment is not possible for the request context.
584:             */
585:            public void appendXML(String path, DocumentFragment fragment)
586:                    throws ProcessingException {
587:                throw new ProcessingException(
588:                        "RequestSessionContext: Appending of xml not allowed");
589:            }
590:
591:            /**
592:             * Removing is not possible for the request context.
593:             */
594:            public void removeXML(String path) throws ProcessingException {
595:                throw new ProcessingException(
596:                        "RequestSessionContext: Removing of xml not allowed");
597:            }
598:
599:            /**
600:             * Set a context attribute. If value is null the attribute is removed.
601:             */
602:            public void setAttribute(String key, Object value)
603:                    throws ProcessingException {
604:                if (value == null) {
605:                    this .request.removeAttribute(key);
606:                } else {
607:                    this .request.setAttribute(key, value);
608:                }
609:            }
610:
611:            /**
612:             * Get a context attribute. If the attribute is not available return null
613:             */
614:            public Object getAttribute(String key) throws ProcessingException {
615:                return this .request.getAttribute(key);
616:            }
617:
618:            /**
619:             * Get a context attribute. If the attribute is not available the defaultObject is returned
620:             */
621:            public Object getAttribute(String key, Object defaultObject)
622:                    throws ProcessingException {
623:                Object obj = this .getAttribute(key);
624:                return (obj != null ? obj : defaultObject);
625:            }
626:
627:            /**
628:             * Get a copy the first node specified by the path.
629:             */
630:            public Node getSingleNode(String path) throws ProcessingException {
631:                path = this .createPath(path);
632:                Node node = null;
633:
634:                try {
635:                    node = DOMUtil.getSingleNode(this .contextData, path,
636:                            this .xpathProcessor);
637:                } catch (javax.xml.transform.TransformerException localException) {
638:                    throw new ProcessingException("Exception: "
639:                            + localException, localException);
640:                }
641:                return node;
642:            }
643:
644:            /**
645:             * Get a copy all the nodes specified by the path.
646:             */
647:            public NodeList getNodeList(String path) throws ProcessingException {
648:                path = this .createPath(path);
649:                NodeList list = null;
650:
651:                try {
652:                    list = DOMUtil.selectNodeList(this .contextData, path,
653:                            this .xpathProcessor);
654:                } catch (javax.xml.transform.TransformerException localException) {
655:                    throw new ProcessingException("Exception: "
656:                            + localException, localException);
657:                }
658:                return list;
659:            }
660:
661:            /**
662:             * Set the value of a node. The node is copied before insertion.
663:             */
664:            public void setNode(String path, Node node)
665:                    throws ProcessingException {
666:                throw new ProcessingException(
667:                        "RequestSessionContext: Setting of XML not allowed");
668:            }
669:
670:            /**
671:             * Get the value of this node. This is similiar to the xsl:value-of
672:             * function. If the node does not exist, <code>null</code> is returned.
673:             */
674:            public String getValueOfNode(String path)
675:                    throws ProcessingException {
676:                String value = null;
677:                Node node = this .getSingleNode(path);
678:                if (node != null) {
679:                    value = DOMUtil.getValueOfNode(node);
680:                }
681:
682:                return value;
683:            }
684:
685:            /**
686:             * Stream the XML directly to the handler. This streams the contents of getXML()
687:             * to the given handler without creating a DocumentFragment containing a copy
688:             * of the data
689:             */
690:            public boolean streamXML(String path,
691:                    ContentHandler contentHandler, LexicalHandler lexicalHandler)
692:                    throws SAXException, ProcessingException {
693:                boolean result = false;
694:                NodeList list;
695:
696:                try {
697:                    list = DOMUtil.selectNodeList(this .contextData, this 
698:                            .createPath(path), this .xpathProcessor);
699:                } catch (javax.xml.transform.TransformerException local) {
700:                    throw new ProcessingException("TransformerException: "
701:                            + local, local);
702:                }
703:                if (list != null && list.getLength() > 0) {
704:                    result = true;
705:                    for (int i = 0; i < list.getLength(); i++) {
706:
707:                        // the found node is either an attribute or an element
708:                        if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) {
709:                            // if it is an attribute simple create a new text node with the value of the attribute
710:                            String value = list.item(i).getNodeValue();
711:                            contentHandler.characters(value.toCharArray(), 0,
712:                                    value.length());
713:                        } else {
714:                            // now we have an element
715:                            // stream all children of this element to the resulting tree
716:                            NodeList childs = list.item(i).getChildNodes();
717:                            if (childs != null) {
718:                                for (int m = 0; m < childs.getLength(); m++) {
719:                                    IncludeXMLConsumer.includeNode(childs
720:                                            .item(m), contentHandler,
721:                                            lexicalHandler);
722:                                }
723:                            }
724:                        }
725:                    }
726:                }
727:
728:                return result;
729:            }
730:
731:            /**
732:             * Get the request parameter as xml
733:             */
734:            public DocumentFragment getParameterAsXML(final String parameterName)
735:                    throws ProcessingException {
736:                return this .getXML("/parameter/" + parameterName);
737:            }
738:
739:            /**
740:             * Get the request parameter as a String
741:             */
742:            public String getParameter(final String parameterName) {
743:                return this .request.getParameter(parameterName);
744:            }
745:
746:            /**
747:             * Try to load XML into the context.
748:             * If the context does not provide the ability of loading,
749:             * an exception is thrown.
750:             */
751:            public void loadXML(String path, SourceParameters parameters)
752:                    throws SAXException, ProcessingException, IOException {
753:                throw new ProcessingException("The context " + this .name
754:                        + " does not support loading.");
755:            }
756:
757:            /**
758:             * Try to save XML from the context.
759:             * If the context does not provide the ability of saving,
760:             * an exception is thrown.
761:             */
762:            public void saveXML(String path, SourceParameters parameters)
763:                    throws SAXException, ProcessingException, IOException {
764:                throw new ProcessingException("The context " + this .name
765:                        + " does not support saving.");
766:            }
767:
768:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.