KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > ReadDOMSessionTransformer


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.transformation;
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.xml.sax.Attributes JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28
29 import java.io.IOException JavaDoc;
30 import java.util.Map JavaDoc;
31
32
33 /**
34  * @cocoon.sitemap.component.documentation
35  * With this transformer, an object that is stored in the session, can be inserted
36  * in the SAX stream at a given position, using usual <xsp:expr> rules.
37  * Object can be DOM Node, XMLizable, or any other object supported by <xsp:expr>.
38  *
39  * @cocoon.sitemap.component.name readDOMsession
40  * @cocoon.sitemap.component.logger sitemap.transformer.readDOMsession
41  *
42  * With this transformer, an object that is stored in the session, can be inserted
43  * in the SAX stream at a given position, using usual <xsp:expr> rules.
44  * Object can be DOM Node, XMLizable, or any other object supported by <xsp:expr>.
45  *
46  * Usage in sitemap:
47  * <pre>
48  * &lt;map:transform type="read-session"&gt;
49  * &lt;map:parameter name="attribute-name" value="companyInfo"/&gt;
50  * &lt;map:parameter name="trigger-element" value="company"/&gt;
51  * &lt;map:parameter name="position" value="after"/&gt;
52  * &lt;/map:transform&gt;
53  * </pre>
54  *
55  * Where:
56  * <ul>
57  * <li><b>attribute-name</b> is the name of the object in the session
58  * <li><b>trigger-element</b> is the element that we need to insert the SAX events
59  * <li><b>postion</b> is the actual place where the stream will be inserted, ie before, after or in
60  * the trigger-element
61  * </ul>
62  *
63  * @author <a HREF="mailto:sven.beauprez@the-ecorp.com">Sven Beauprez</a>
64  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
65  * @version CVS $Id: ReadDOMSessionTransformer.java 109639 2004-12-03 06:55:33Z crossley $
66  */

67 public class ReadDOMSessionTransformer extends AbstractTransformer {
68
69     public static final String JavaDoc ATTRIBUTE_NAME = "attribute-name";
70     public static final String JavaDoc TRIGGER_ELEMENT = "trigger-element";
71
72     /*
73       position where the sax events from the dom should be insterted
74       this can be: 'before', 'after' or 'in'
75     */

76     public static final String JavaDoc POSITION = "position";
77
78     Session session;
79     String JavaDoc attributeName;
80     String JavaDoc trigger;
81     String JavaDoc position;
82
83     /** BEGIN SitemapComponent methods **/
84     public void setup(SourceResolver resolver,
85                       Map JavaDoc objectModel,
86                       String JavaDoc source,
87                       Parameters parameters)
88             throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
89         Request request = ObjectModelHelper.getRequest(objectModel);
90         session = request.getSession(false);
91         if (session != null) {
92             if (getLogger().isDebugEnabled()) {
93                 getLogger().debug("Session is available. ID=" + session.getId());
94             }
95             this.attributeName = parameters.getParameter(ATTRIBUTE_NAME, null);
96             if (this.attributeName == null) {
97                 // Try old syntax
98
this.attributeName = parameters.getParameter("dom-name", null);
99             }
100
101             this.trigger = parameters.getParameter(TRIGGER_ELEMENT, null);
102             this.position = parameters.getParameter(POSITION, "in");
103             if (getLogger().isDebugEnabled()) {
104                 getLogger().debug(ATTRIBUTE_NAME + "=" + attributeName + ", "
105                         + TRIGGER_ELEMENT + "=" + trigger + ", "
106                         + POSITION + "=" + position);
107             }
108         } else {
109             getLogger().warn("No session object: Nothing to do.");
110         }
111     }
112     /** END SitemapComponent methods **/
113
114     /** BEGIN SAX ContentHandler handlers **/
115     public void startElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attributes)
116             throws SAXException JavaDoc {
117         // Start streaming after certain startelement is encountered
118
if (name.equalsIgnoreCase(trigger)) {
119             getLogger().debug("Trigger encountered");
120             if ("before".equalsIgnoreCase(position)) {
121                 stream();
122                 super.contentHandler.startElement(uri,name,raw,attributes);
123             } else if ("in".equalsIgnoreCase(position)) {
124                 super.contentHandler.startElement(uri,name,raw,attributes);
125                 stream();
126             } else if ("after".equalsIgnoreCase(position)) {
127                 super.contentHandler.startElement(uri,name,raw,attributes);
128             }
129         } else {
130             super.contentHandler.startElement(uri,name,raw,attributes);
131         }
132     }
133
134     public void endElement(String JavaDoc uri,String JavaDoc name,String JavaDoc raw)
135             throws SAXException JavaDoc {
136         super.contentHandler.endElement(uri,name,raw);
137         if (name.equalsIgnoreCase(trigger)) {
138             if ("after".equalsIgnoreCase(position)) {
139                 stream();
140             }
141         }
142     }
143     /** END SAX ContentHandler handlers **/
144
145     /** own methods **/
146     private void stream() throws SAXException JavaDoc {
147         if (attributeName != null) {
148             Object JavaDoc node = session.getAttribute(attributeName);
149             if (node != null) {
150                 getLogger().debug("Start streaming");
151                 XMLUtils.valueOf(new IncludeXMLConsumer(super.xmlConsumer), node);
152             } else {
153                 getLogger().error("No attribute " + attributeName + " in session");
154             }
155         } else {
156             getLogger().error("No "+ ATTRIBUTE_NAME + " parameter specified");
157         }
158     }
159 }
160
Popular Tags