KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > generation > SessionAttributeGenerator


1 /*
2  * Copyright 1999-2005 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.generation;
17
18 import org.apache.avalon.framework.parameters.Parameters;
19 import org.apache.cocoon.ProcessingException;
20 import org.apache.cocoon.environment.ObjectModelHelper;
21 import org.apache.cocoon.environment.Request;
22 import org.apache.cocoon.environment.Session;
23 import org.apache.cocoon.environment.SourceResolver;
24 import org.apache.cocoon.xml.XMLUtils;
25 import org.apache.cocoon.xml.IncludeXMLConsumer;
26 import org.apache.excalibur.xml.sax.XMLizable;
27 import org.w3c.dom.Node JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29
30 import java.io.IOException JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * @cocoon.sitemap.component.documentation
35  * Generates a document from a session attribute.
36  *
37  * @cocoon.sitemap.component.name sessionattribute
38  * @cocoon.sitemap.component.label content
39  * @cocoon.sitemap.component.logger sitemap.generator.sessionattribute
40  *
41  * Generates a document from a session attribute. The attribute may be a DOM
42  * node, an <code>XMLizable</code>, or any other object, and is streamed using
43  * the same rules as for &lt;xsp:expr&gt; in XSPs (see {@link
44  * org.apache.cocoon.components.language.markup.xsp.XSPObjectHelper}).
45  * <p>
46  * Name of the session attribute is specified using src attribute of the generate
47  * tag, or, if no src tag present, using attr-name parameter.
48  * <p>
49  * This generator has 2 parameters:
50  * <ul>
51  * <li><code>attr-name</code> : the session attribute name (mandatory if no src
52  * attribute specified).
53  * </li>
54  * <li><code>root-element</code> (optional) : the name of the root element of the
55  * produced document. This parameter is optional if the session attribute is
56  * a DOM or an <code>XMLizable</code>.
57  * </li>
58  * </ul>
59  * <p>
60  * Example usage :
61  * <pre>
62  * &lt;map:generator name="session-attr" logger="sitemap.generator.session-attr"
63  * SRC="org.apache.cocoon.generation.SessionAttributeGenerator"/&gt;
64  * ...
65  * &lt;map:generate type="session-attr"&gt;
66  * &lt;map:parameter name="attr-name" value="myAttribute"/&gt;
67  * &lt;map:parameter name="root-element" value="root"/&gt;
68  * &lt;/map:generate&gt;
69  * </pre>
70  *
71  * @see org.apache.cocoon.transformation.ReadDOMSessionTransformer
72  * @see org.apache.cocoon.transformation.WriteDOMSessionTransformer
73  * @author <a HREF="mailto:cedric.damioli@anyware-tech.com">C&eacute;dric Damioli</a>
74  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
75  * @version $Id: SessionAttributeGenerator.java 267195 2005-09-02 12:45:49Z pier $
76  */

77 public class SessionAttributeGenerator extends AbstractGenerator {
78
79     public static final String JavaDoc ATTR_NAME = "attr-name";
80     public static final String JavaDoc ELEMENT_NAME = "root-element";
81
82     /** The object to generate */
83     private Object JavaDoc attrObject;
84
85     /** The element name */
86     private String JavaDoc elementName;
87
88     /**
89      * Setup the file generator :try to retrieve the session attribute given as sitemap parameter
90      */

91     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters par)
92       throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
93
94         super.setup(resolver, objectModel, src, par);
95
96         // Get the element name (can be null if the object is a DOM or an XMLizable)
97
this.elementName = par.getParameter(ELEMENT_NAME, null);
98
99         // Get the attribute name
100
String JavaDoc attrName = par.getParameter(ATTR_NAME, src);
101         if (attrName == null) {
102             String JavaDoc msg = "SessionAttributeGenerator needs an attribute name !";
103             getLogger().error(msg);
104             throw new ProcessingException(msg);
105         }
106
107         // Get the object to stream
108
Request request = ObjectModelHelper.getRequest(objectModel);
109         Session session = request.getSession(false);
110         if (session != null) {
111             this.attrObject = session.getAttribute(attrName);
112         }
113
114         // Controls
115
if (this.attrObject == null) {
116             if (this.elementName == null) {
117                 // Can't generate nothing...
118
String JavaDoc msg = "Session attribute '" + attrName + "' doesn't exist";
119                 getLogger().error(msg);
120                 throw new ProcessingException(msg);
121             } else {
122                 if (getLogger().isDebugEnabled()) {
123                     getLogger().debug("Session attribute '" + attrName +
124                         "' doesn't exist : will generate a single '" + this.elementName +
125                         "' element.");
126                 }
127             }
128         } else {
129             // Need an element name for non-xml objects
130
if (this.elementName == null &&
131                 ! (this.attrObject instanceof XMLizable) &&
132                 ! (this.attrObject instanceof Node JavaDoc)) {
133
134                 String JavaDoc msg = "Session attribute '" + attrName + "' needs an enclosing element : class is " +
135                     this.attrObject.getClass().getName();
136
137                 getLogger().warn(msg);
138                 throw new ProcessingException(msg);
139             }
140         }
141     }
142
143     /**
144      * Generate XML data
145      */

146     public void generate()
147     throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
148         xmlConsumer.startDocument();
149
150         if (this.elementName != null) {
151             xmlConsumer.startElement("", this.elementName, this.elementName, XMLUtils.EMPTY_ATTRIBUTES);
152             XMLUtils.valueOf(new IncludeXMLConsumer(xmlConsumer), this.attrObject);
153             xmlConsumer.endElement("", this.elementName, this.elementName);
154         } else {
155             XMLUtils.valueOf(new IncludeXMLConsumer(xmlConsumer), this.attrObject);
156         }
157
158         xmlConsumer.endDocument();
159     }
160 }
161
Popular Tags