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.webapps.session.transformation;
018:
019: import java.io.IOException;
020: import java.util.Map;
021:
022: import org.apache.avalon.framework.parameters.Parameters;
023: import org.apache.cocoon.ProcessingException;
024: import org.apache.cocoon.environment.SourceResolver;
025: import org.apache.cocoon.webapps.session.SessionConstants;
026: import org.apache.excalibur.source.SourceParameters;
027: import org.xml.sax.Attributes;
028: import org.xml.sax.SAXException;
029:
030: /**
031: * This is the session pre transformer. It does all the getting
032: * and creation commands. This transformer should be the first in the
033: * pipeline.
034: *
035: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
036: * @deprecated This block is deprecated and will be removed in future versions.
037: * @version CVS $Id: SessionPreTransformer.java 433543 2006-08-22 06:22:54Z crossley $
038: */
039: public class SessionPreTransformer extends AbstractSessionTransformer {
040:
041: /*
042: * The XML commands
043: */
044: public static final String CREATECONTEXT_ELEMENT = "createcontext";
045: public static final String CREATECONTEXT_NAME_ATTRIBUTE = "name";
046: public static final String CREATECONTEXT_SAVE_ATTRIBUTE = "save"; // optional
047: public static final String CREATECONTEXT_LOAD_ATTRIBUTE = "load"; // optional
048:
049: public static final String GETXML_ELEMENT = "getxml";
050: public static final String GETXML_CONTEXT_ATTRIBUTE = "context";
051: public static final String GETXML_PATH_ATTRIBUTE = "path";
052:
053: public static final String LOADCONTEXT_ELEMENT = "loadxml";
054: public static final String LOADCONTEXT_CONTEXT_ATTRIBUTE = "context";
055: public static final String LOADCONTEXT_PATH_ATTRIBUTE = "path"; // optional
056:
057: /** The contexturl element.
058: */
059: public static final String CONTEXT_URL_ELEMENT = "contexturl";
060:
061: /** Are we inside a getxml? */
062: protected int processingGetXML;
063:
064: public SessionPreTransformer() {
065: this .defaultNamespaceURI = SessionConstants.SESSION_NAMESPACE_URI;
066: }
067:
068: /**
069: * Setup the next round.
070: * The instance variables are initialised.
071: * @param resolver The current SourceResolver
072: * @param objectModel The objectModel of the environment.
073: * @param src The value of the src attribute in the sitemap.
074: * @param par The parameters from the sitemap.
075: */
076: public void setup(SourceResolver resolver, Map objectModel,
077: String src, Parameters par) throws ProcessingException,
078: SAXException, IOException {
079: super .setup(resolver, objectModel, src, par);
080: this .processingGetXML = 0;
081: }
082:
083: /**
084: * Process the SAX event.
085: * The namespace of the event is checked. If it is the defined namespace
086: * for this transformer the endTransformingElement() hook is called.
087: */
088: public void endElement(String uri, String name, String raw)
089: throws SAXException {
090: super .endElement(uri, name, raw);
091: if (namespaceURI.equals(uri) && this .processingGetXML > 0
092: && name.equals(GETXML_ELEMENT)) {
093: this .processingGetXML--;
094: this .ignoreEventsCount--;
095: this .ignoreHooksCount--;
096: }
097: }
098:
099: /**
100: * Process the SAX event.
101: * The namespace of the event is checked. If it is the defined namespace
102: * for this transformer the endTransformingElement() hook is called.
103: */
104: public void startElement(String uri, String name, String raw,
105: Attributes attr) throws SAXException {
106: if (namespaceURI.equals(uri) && this .processingGetXML > 0
107: && name.equals(GETXML_ELEMENT)) {
108: this .processingGetXML++;
109: this .ignoreEventsCount++;
110: this .ignoreHooksCount++;
111: }
112: super .startElement(uri, name, raw, attr);
113: }
114:
115: /**
116: * This is the real implementation of the startElement event for the transformer
117: * The event is checked for a valid element and the corresponding command
118: * is executed.
119: */
120: public void startTransformingElement(String uri, String name,
121: String raw, Attributes attr) throws ProcessingException,
122: IOException, SAXException {
123:
124: if (this .getLogger().isDebugEnabled() == true) {
125: this .getLogger().debug(
126: "BEGIN startTransformingElement uri=" + uri
127: + ", name=" + name + ", raw=" + raw
128: + ", attr=" + attr);
129: }
130: if (name.equals(CREATECONTEXT_ELEMENT) == true) {
131: this .getContextManager().createContext(
132: attr.getValue(CREATECONTEXT_NAME_ATTRIBUTE),
133: attr.getValue(CREATECONTEXT_LOAD_ATTRIBUTE),
134: attr.getValue(CREATECONTEXT_SAVE_ATTRIBUTE));
135:
136: } else if (name.equals(GETXML_ELEMENT) == true) {
137: final String path = attr.getValue(GETXML_PATH_ATTRIBUTE);
138: final String contextName = attr
139: .getValue(GETXML_CONTEXT_ATTRIBUTE);
140:
141: if (this .getSessionManager().streamContextFragment(
142: contextName, path, this ) == true) {
143: this .ignoreEventsCount++;
144: this .ignoreHooksCount++;
145: this .processingGetXML++;
146: }
147:
148: } else if (name.equals(LOADCONTEXT_ELEMENT) == true) {
149: this .startParametersRecording();
150: stack.push(attr.getValue(LOADCONTEXT_CONTEXT_ATTRIBUTE));
151: if (attr.getValue(LOADCONTEXT_PATH_ATTRIBUTE) != null) {
152: stack.push(attr.getValue(LOADCONTEXT_PATH_ATTRIBUTE));
153: } else {
154: stack.push("/");
155: }
156:
157: // Element context url
158: } else if (name
159: .equals(SessionPreTransformer.CONTEXT_URL_ELEMENT) == true) {
160: this .ignoreEventsCount++;
161:
162: // DEFAULT
163: } else {
164: super .startTransformingElement(uri, name, raw, attr);
165: }
166:
167: if (this .getLogger().isDebugEnabled() == true) {
168: this .getLogger().debug("END startTransformingElement");
169: }
170: }
171:
172: public void endTransformingElement(String uri, String name,
173: String raw) throws ProcessingException, IOException,
174: SAXException {
175:
176: if (this .getLogger().isDebugEnabled() == true) {
177: this .getLogger().debug(
178: "BEGIN endTransformingElement uri=" + uri
179: + ", name=" + name + ", raw=" + raw);
180: }
181: if (name.equals(CREATECONTEXT_ELEMENT) == true) {
182: // do nothing, the context was created on the startElement event
183:
184: } else if (name.equals(GETXML_ELEMENT) == true) {
185: // do nothing
186:
187: // Element: loadxml
188: } else if (name.equals(LOADCONTEXT_ELEMENT) == true) {
189: String path = (String) stack.pop();
190: String contextName = (String) stack.pop();
191: SourceParameters pars = this
192: .endParametersRecording((SourceParameters) null);
193: pars.setSingleParameterValue("contextname", contextName);
194: pars.setSingleParameterValue("path", path);
195:
196: this .getContextManager().getContext(contextName).loadXML(
197: path, pars);
198: // Element context url
199: } else if (name
200: .equals(SessionPreTransformer.CONTEXT_URL_ELEMENT) == true) {
201: this .ignoreEventsCount--;
202: String contextUrl = this .request.getScheme() + "://"
203: + this .request.getServerName() + ":"
204: + this .request.getServerPort()
205: + this .request.getContextPath();
206: this .sendTextEvent(contextUrl);
207:
208: // DEFAULT
209: } else {
210: super .endTransformingElement(uri, name, raw);
211: }
212: if (this .getLogger().isDebugEnabled() == true) {
213: this .getLogger().debug("END endTransformingElement");
214: }
215: }
216: }
|