KickJava   Java API By Example, From Geeks To Geeks.

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


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.dom.DOMBuilder;
25 import org.xml.sax.Attributes JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27
28 import java.io.IOException JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32
33
34 /**
35  * @cocoon.sitemap.component.documentation
36  * Make a DOM object from SAX events and write it to the session.
37  *
38  * @cocoon.sitemap.component.name writeDOMsession
39  * @cocoon.sitemap.component.logger sitemap.transformer.writeDOMsession
40  *
41  *
42  * Make a DOM object from SAX events and write it to the session.
43  *
44  * Usage in sitemap:
45  * <map:transform type="writeDOMsession">
46  * <map:parameter name="dom-name" value="content"/>
47  * <map:parameter name="dom-root-element" value="companies"/>
48  * </map:transform>
49  *
50  * Where:
51  * dom-name is the name for the DOM object in the session
52  * dom-root-element is the trigger that will be the root element of the DOM
53  *
54  * @author <a HREF="mailto:sven.beauprez@the-ecorp.com">Sven Beauprez</a>
55  * @version CVS $Id: WriteDOMSessionTransformer.java 109639 2004-12-03 06:55:33Z crossley $
56  */

57
58 public class WriteDOMSessionTransformer
59   extends AbstractTransformer {
60
61     public static final String JavaDoc DOM_NAME = "dom-name";
62     public static final String JavaDoc DOM_ROOT_ELEMENT = "dom-root-element";
63
64     private boolean buildDom = false;
65     private boolean sessionAvailable = false;
66
67     private Session session;
68     private DOMBuilder builder;
69
70     private String JavaDoc DOMName;
71     private String JavaDoc rootElement;
72     private Map JavaDoc storedPrefixMap;
73
74     /**
75      * Recyclable
76      */

77     public void recycle() {
78         super.recycle();
79         this.session = null;
80         this.builder = null;
81         this.buildDom = false;
82         this.sessionAvailable = false;
83     }
84
85     /** BEGIN SitemapComponent methods **/
86
87     public void setup(SourceResolver resolver, Map JavaDoc objectModel,
88                       String JavaDoc source, Parameters parameters)
89     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
90       getLogger().debug("WriteSessionTransformer: setup");
91       Request request = ObjectModelHelper.getRequest(objectModel);
92       session = request.getSession(false);
93       if (session != null) {
94         DOMName = parameters.getParameter(WriteDOMSessionTransformer.DOM_NAME,null);
95         rootElement = parameters.getParameter(WriteDOMSessionTransformer.DOM_ROOT_ELEMENT,null);
96         if (DOMName!=null && rootElement!=null) {
97           //only now we know it is usefull to store something in the session
98
getLogger().debug("WriteSessionTransformer: "+WriteDOMSessionTransformer.DOM_NAME + "=" +
99                             DOMName + "; " + WriteDOMSessionTransformer.DOM_ROOT_ELEMENT + "=" +
100                             rootElement);
101           sessionAvailable = true;
102                     storedPrefixMap = new HashMap JavaDoc();
103         } else {
104           getLogger().error("WriteSessionTransformer: need " + WriteDOMSessionTransformer.DOM_NAME +
105                             " and " + WriteDOMSessionTransformer.DOM_ROOT_ELEMENT + " parameters");
106         }
107       } else {
108         getLogger().error("WriteSessionTransformer: no session object");
109       }
110     }
111
112     /** END SitemapComponent methods **/
113
114     /** BEGIN SAX ContentHandler handlers **/
115
116     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
117     throws SAXException JavaDoc {
118       super.startPrefixMapping(prefix,uri);
119       if (buildDom) {
120         builder.startPrefixMapping(prefix,uri);
121       } else {
122                 storePrefixMapping(prefix,uri);
123             }
124     }
125
126     public void startElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attributes)
127     throws SAXException JavaDoc {
128         //only build the DOM tree if session is available
129
if (name.equalsIgnoreCase(rootElement) && sessionAvailable) {
130           getLogger().debug("WriteSessionTransformer: start building DOM tree");
131           buildDom = true;
132           builder = new DOMBuilder();
133           builder.startDocument();
134                     launchStoredMappings();
135           builder.startElement(uri,name,raw,attributes);
136         } else if (buildDom) {
137           builder.startElement(uri,name,raw,attributes);
138         }
139         super.contentHandler.startElement(uri,name,raw,attributes);
140     }
141
142     public void endElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw)
143     throws SAXException JavaDoc {
144         if (name.equalsIgnoreCase(rootElement) && sessionAvailable) {
145           buildDom = false;
146           builder.endElement(uri,name,raw);
147           builder.endDocument();
148           getLogger().debug("WriteSessionTransformer: putting DOM tree in session object");
149           session.setAttribute(DOMName,builder.getDocument().getFirstChild());
150           getLogger().debug("WriteSessionTransformer: DOM tree is in session object");
151         } else if (buildDom) {
152           builder.endElement(uri,name,raw);
153         }
154         super.contentHandler.endElement(uri,name,raw);
155     }
156
157
158     public void characters(char c[], int start, int len)
159     throws SAXException JavaDoc {
160         if (buildDom) {
161           builder.characters(c,start,len);
162         }
163         super.contentHandler.characters(c,start,len);
164     }
165
166     public void startCDATA()
167     throws SAXException JavaDoc {
168       if (buildDom) {
169         builder.startCDATA();
170       }
171       super.lexicalHandler.startCDATA();
172     }
173
174     public void endCDATA()
175     throws SAXException JavaDoc {
176       if (buildDom) {
177         builder.endCDATA();
178       }
179       super.lexicalHandler.endCDATA();
180     }
181
182     /** END SAX ContentHandler handlers **/
183
184       protected void storePrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
185            storedPrefixMap.put(prefix,uri);
186     }
187
188       protected void launchStoredMappings()
189         throws SAXException JavaDoc {
190             Iterator JavaDoc it = storedPrefixMap.keySet().iterator();
191                 while(it.hasNext()) {
192                     String JavaDoc pre = (String JavaDoc)it.next();
193                     String JavaDoc uri = (String JavaDoc)storedPrefixMap.get(pre);
194                     getLogger().debug("WriteSessionTransformer: launching prefix mapping[ pre: "+pre+" uri: "+uri+" ]");
195                     builder.startPrefixMapping(pre,uri);
196                 }
197         }
198
199
200
201 }
202
Popular Tags