KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > session > transformation > SessionPreTransformer


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.webapps.session.transformation;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.cocoon.ProcessingException;
23 import org.apache.cocoon.environment.SourceResolver;
24 import org.apache.cocoon.webapps.session.SessionConstants;
25 import org.apache.excalibur.source.SourceParameters;
26 import org.xml.sax.Attributes JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28
29 /**
30  * This is the session pre transformer. It does all the getting
31  * and creation commands. This transformer should be the first in the
32  * pipeline.
33  *
34  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
35  * @version CVS $Id: SessionPreTransformer.java 47045 2004-09-22 11:56:13Z vgritsenko $
36 */

37 public class SessionPreTransformer
38         extends AbstractSessionTransformer {
39
40     /*
41      * The XML commands
42      */

43     public static final String JavaDoc CREATECONTEXT_ELEMENT = "createcontext";
44     public static final String JavaDoc CREATECONTEXT_NAME_ATTRIBUTE = "name";
45     public static final String JavaDoc CREATECONTEXT_SAVE_ATTRIBUTE = "save"; // optional
46
public static final String JavaDoc CREATECONTEXT_LOAD_ATTRIBUTE = "load"; // optional
47

48     public static final String JavaDoc GETXML_ELEMENT = "getxml";
49     public static final String JavaDoc GETXML_CONTEXT_ATTRIBUTE = "context";
50     public static final String JavaDoc GETXML_PATH_ATTRIBUTE = "path";
51
52     public static final String JavaDoc LOADCONTEXT_ELEMENT = "loadxml";
53     public static final String JavaDoc LOADCONTEXT_CONTEXT_ATTRIBUTE = "context";
54     public static final String JavaDoc LOADCONTEXT_PATH_ATTRIBUTE = "path"; // optional
55

56     /** The contexturl element.
57      */

58     public static final String JavaDoc CONTEXT_URL_ELEMENT = "contexturl";
59
60     /** Are we inside a getxml? */
61     protected int processingGetXML;
62
63     public SessionPreTransformer() {
64         this.defaultNamespaceURI = SessionConstants.SESSION_NAMESPACE_URI;
65     }
66
67     /**
68      * Setup the next round.
69      * The instance variables are initialised.
70      * @param resolver The current SourceResolver
71      * @param objectModel The objectModel of the environment.
72      * @param src The value of the src attribute in the sitemap.
73      * @param par The parameters from the sitemap.
74      */

75     public void setup(SourceResolver resolver,
76                       Map JavaDoc objectModel,
77                       String JavaDoc src,
78                       Parameters par)
79     throws ProcessingException,
80            SAXException JavaDoc,
81            IOException JavaDoc {
82         super.setup(resolver, objectModel, src, par);
83         this.processingGetXML = 0;
84     }
85
86     /**
87      * Process the SAX event.
88      * The namespace of the event is checked. If it is the defined namespace
89      * for this transformer the endTransformingElement() hook is called.
90      */

91     public void endElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw) throws SAXException JavaDoc {
92         super.endElement(uri, name, raw);
93         if (namespaceURI.equals(uri)
94             && this.processingGetXML > 0
95             && name.equals(GETXML_ELEMENT)) {
96             this.processingGetXML--;
97             this.ignoreEventsCount--;
98             this.ignoreHooksCount--;
99         }
100     }
101
102     /**
103      * Process the SAX event.
104      * The namespace of the event is checked. If it is the defined namespace
105      * for this transformer the endTransformingElement() hook is called.
106      */

107     public void startElement(String JavaDoc uri,
108                              String JavaDoc name,
109                              String JavaDoc raw,
110                              Attributes JavaDoc attr)
111     throws SAXException JavaDoc {
112         if (namespaceURI.equals(uri)
113             && this.processingGetXML > 0
114             && name.equals(GETXML_ELEMENT)) {
115             this.processingGetXML++;
116             this.ignoreEventsCount++;
117             this.ignoreHooksCount++;
118         }
119         super.startElement(uri, name, raw, attr);
120     }
121
122     /**
123      * This is the real implementation of the startElement event for the transformer
124      * The event is checked for a valid element and the corresponding command
125      * is executed.
126      */

127     public void startTransformingElement(String JavaDoc uri,
128                                        String JavaDoc name,
129                                        String JavaDoc raw,
130                                        Attributes JavaDoc attr)
131     throws ProcessingException, IOException JavaDoc, SAXException JavaDoc {
132
133         if (this.getLogger().isDebugEnabled() == true) {
134             this.getLogger().debug("BEGIN startTransformingElement uri=" + uri +
135                               ", name=" + name +
136                               ", raw=" + raw +
137                               ", attr=" + attr);
138         }
139         if (name.equals(CREATECONTEXT_ELEMENT) == true) {
140             this.getContextManager().createContext(attr.getValue(CREATECONTEXT_NAME_ATTRIBUTE),
141                                                       attr.getValue(CREATECONTEXT_LOAD_ATTRIBUTE),
142                                                       attr.getValue(CREATECONTEXT_SAVE_ATTRIBUTE));
143
144         } else if (name.equals(GETXML_ELEMENT) == true) {
145             final String JavaDoc path = attr.getValue(GETXML_PATH_ATTRIBUTE);
146             final String JavaDoc contextName = attr.getValue(GETXML_CONTEXT_ATTRIBUTE);
147
148             if (this.getSessionManager().streamContextFragment(contextName,
149                                                      path,
150                                                      this) == true) {
151                 this.ignoreEventsCount++;
152                 this.ignoreHooksCount++;
153                 this.processingGetXML++;
154             }
155
156         } else if (name.equals(LOADCONTEXT_ELEMENT) == true) {
157             this.startParametersRecording();
158             stack.push(attr.getValue(LOADCONTEXT_CONTEXT_ATTRIBUTE));
159             if (attr.getValue(LOADCONTEXT_PATH_ATTRIBUTE) != null) {
160                 stack.push(attr.getValue(LOADCONTEXT_PATH_ATTRIBUTE));
161             } else {
162                 stack.push("/");
163             }
164
165         // Element context url
166
} else if (name.equals(SessionPreTransformer.CONTEXT_URL_ELEMENT) == true) {
167             this.ignoreEventsCount++;
168
169         // DEFAULT
170
} else {
171             super.startTransformingElement(uri, name, raw, attr);
172         }
173
174         if (this.getLogger().isDebugEnabled() == true) {
175            this.getLogger().debug("END startTransformingElement");
176         }
177     }
178
179     public void endTransformingElement(String JavaDoc uri,
180                                        String JavaDoc name,
181                                        String JavaDoc raw)
182     throws ProcessingException ,IOException JavaDoc, SAXException JavaDoc {
183
184         if (this.getLogger().isDebugEnabled() == true) {
185             this.getLogger().debug("BEGIN endTransformingElement uri=" + uri + ", name=" + name + ", raw=" + raw);
186         }
187         if (name.equals(CREATECONTEXT_ELEMENT) == true) {
188             // do nothing, the context was created on the startElement event
189

190         } else if (name.equals(GETXML_ELEMENT) == true) {
191             // do nothing
192

193         // Element: loadxml
194
} else if (name.equals(LOADCONTEXT_ELEMENT) == true) {
195             String JavaDoc path = (String JavaDoc)stack.pop();
196             String JavaDoc contextName = (String JavaDoc)stack.pop();
197             SourceParameters pars = this.endParametersRecording((SourceParameters)null);
198             pars.setSingleParameterValue("contextname", contextName);
199             pars.setSingleParameterValue("path", path);
200
201             this.getContextManager().getContext(contextName).loadXML(path,
202                                                                      pars);
203         // Element context url
204
} else if (name.equals(SessionPreTransformer.CONTEXT_URL_ELEMENT) == true) {
205             this.ignoreEventsCount--;
206             String JavaDoc contextUrl = this.request.getScheme() + "://" +
207                                 this.request.getServerName() + ":" +
208                                 this.request.getServerPort() +
209                                 this.request.getContextPath();
210             this.sendTextEvent(contextUrl);
211
212
213         // DEFAULT
214
} else {
215             super.endTransformingElement(uri, name, raw);
216         }
217         if (this.getLogger().isDebugEnabled() == true) {
218             this.getLogger().debug("END endTransformingElement");
219         }
220     }
221 }
222
Popular Tags