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.generation;
018:
019: import org.apache.avalon.framework.parameters.Parameters;
020: import org.apache.cocoon.ProcessingException;
021: import org.apache.cocoon.environment.ObjectModelHelper;
022: import org.apache.cocoon.environment.Request;
023: import org.apache.cocoon.environment.Session;
024: import org.apache.cocoon.environment.SourceResolver;
025: import org.apache.cocoon.xml.XMLUtils;
026: import org.apache.cocoon.xml.IncludeXMLConsumer;
027: import org.apache.excalibur.xml.sax.XMLizable;
028: import org.w3c.dom.Node;
029: import org.xml.sax.SAXException;
030:
031: import java.io.IOException;
032: import java.util.Map;
033:
034: /**
035: * @cocoon.sitemap.component.documentation
036: * Generates a document from a session attribute.
037: *
038: * @cocoon.sitemap.component.name sessionattribute
039: * @cocoon.sitemap.component.label content
040: * @cocoon.sitemap.component.logger sitemap.generator.sessionattribute
041: *
042: * Generates a document from a session attribute. The attribute may be a DOM
043: * node, an <code>XMLizable</code>, or any other object, and is streamed using
044: * the same rules as for <xsp:expr> in XSPs (see {@link
045: * org.apache.cocoon.components.language.markup.xsp.XSPObjectHelper}).
046: * <p>
047: * Name of the session attribute is specified using src attribute of the generate
048: * tag, or, if no src tag present, using attr-name parameter.
049: * <p>
050: * This generator has 2 parameters:
051: * <ul>
052: * <li><code>attr-name</code> : the session attribute name (mandatory if no src
053: * attribute specified).
054: * </li>
055: * <li><code>root-element</code> (optional) : the name of the root element of the
056: * produced document. This parameter is optional if the session attribute is
057: * a DOM or an <code>XMLizable</code>.
058: * </li>
059: * </ul>
060: * <p>
061: * Example usage :
062: * <pre>
063: * <map:generator name="session-attr" logger="sitemap.generator.session-attr"
064: * src="org.apache.cocoon.generation.SessionAttributeGenerator"/>
065: * ...
066: * <map:generate type="session-attr">
067: * <map:parameter name="attr-name" value="myAttribute"/>
068: * <map:parameter name="root-element" value="root"/>
069: * </map:generate>
070: * </pre>
071: *
072: * @see org.apache.cocoon.transformation.ReadDOMSessionTransformer
073: * @see org.apache.cocoon.transformation.WriteDOMSessionTransformer
074: * @author <a href="mailto:cedric.damioli@anyware-tech.com">Cédric Damioli</a>
075: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
076: * @version $Id: SessionAttributeGenerator.java 433543 2006-08-22 06:22:54Z crossley $
077: */
078: public class SessionAttributeGenerator extends AbstractGenerator {
079:
080: public static final String ATTR_NAME = "attr-name";
081: public static final String ELEMENT_NAME = "root-element";
082:
083: /** The object to generate */
084: private Object attrObject;
085:
086: /** The element name */
087: private String elementName;
088:
089: /**
090: * Setup the file generator :try to retrieve the session attribute given as sitemap parameter
091: */
092: public void setup(SourceResolver resolver, Map objectModel,
093: String src, Parameters par) throws ProcessingException,
094: SAXException, IOException {
095:
096: super .setup(resolver, objectModel, src, par);
097:
098: // Get the element name (can be null if the object is a DOM or an XMLizable)
099: this .elementName = par.getParameter(ELEMENT_NAME, null);
100:
101: // Get the attribute name
102: String attrName = par.getParameter(ATTR_NAME, src);
103: if (attrName == null) {
104: String msg = "SessionAttributeGenerator needs an attribute name !";
105: getLogger().error(msg);
106: throw new ProcessingException(msg);
107: }
108:
109: // Get the object to stream
110: Request request = ObjectModelHelper.getRequest(objectModel);
111: Session session = request.getSession(false);
112: if (session != null) {
113: this .attrObject = session.getAttribute(attrName);
114: }
115:
116: // Controls
117: if (this .attrObject == null) {
118: if (this .elementName == null) {
119: // Can't generate nothing...
120: String msg = "Session attribute '" + attrName
121: + "' doesn't exist";
122: getLogger().error(msg);
123: throw new ProcessingException(msg);
124: } else {
125: if (getLogger().isDebugEnabled()) {
126: getLogger()
127: .debug(
128: "Session attribute '"
129: + attrName
130: + "' doesn't exist : will generate a single '"
131: + this .elementName
132: + "' element.");
133: }
134: }
135: } else {
136: // Need an element name for non-xml objects
137: if (this .elementName == null
138: && !(this .attrObject instanceof XMLizable)
139: && !(this .attrObject instanceof Node)) {
140:
141: String msg = "Session attribute '" + attrName
142: + "' needs an enclosing element : class is "
143: + this .attrObject.getClass().getName();
144:
145: getLogger().warn(msg);
146: throw new ProcessingException(msg);
147: }
148: }
149: }
150:
151: /**
152: * Generate XML data
153: */
154: public void generate() throws IOException, SAXException,
155: ProcessingException {
156: xmlConsumer.startDocument();
157:
158: if (this .elementName != null) {
159: xmlConsumer.startElement("", this .elementName,
160: this .elementName, XMLUtils.EMPTY_ATTRIBUTES);
161: XMLUtils.valueOf(new IncludeXMLConsumer(xmlConsumer),
162: this .attrObject);
163: xmlConsumer.endElement("", this .elementName,
164: this .elementName);
165: } else {
166: XMLUtils.valueOf(new IncludeXMLConsumer(xmlConsumer),
167: this.attrObject);
168: }
169:
170: xmlConsumer.endDocument();
171: }
172: }
|