KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > EnvironmentStack


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.components;
17
18 import org.apache.avalon.framework.component.ComponentManager;
19 import org.apache.cocoon.Processor;
20 import org.apache.cocoon.environment.Environment;
21 import org.apache.cocoon.xml.XMLConsumer;
22
23 import org.apache.commons.collections.ArrayStack;
24 import org.xml.sax.Attributes JavaDoc;
25 import org.xml.sax.Locator JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27
28 /**
29  * The stack for the processing environment.
30  * This is a special implementation of a stack for the handling of the
31  * cocoon protocol.
32  *
33  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
34  * @version CVS $Id: EnvironmentStack.java 189756 2005-06-09 14:22:26Z sylvain $
35  */

36 final class EnvironmentStack extends ArrayStack
37                              implements Cloneable JavaDoc {
38
39     int offset;
40
41     int getOffset() {
42         return this.offset;
43     }
44
45     void setOffset(int value) {
46         this.offset = value;
47     }
48
49     Item getCurrent() {
50         return (Item)this.get(offset);
51     }
52
53     public Object JavaDoc clone() {
54         EnvironmentStack old = (EnvironmentStack) super.clone();
55         old.offset = offset;
56         return old;
57     }
58
59     XMLConsumer getEnvironmentAwareConsumerWrapper(XMLConsumer consumer,
60                                                    int oldOffset) {
61         return new EnvironmentChanger(consumer, this, oldOffset, this.offset);
62     }
63
64     static final class Item {
65         public final Environment env;
66         public final Processor processor;
67         public final ComponentManager manager;
68         public final int offset;
69         
70         public Item(Environment env, Processor processor, ComponentManager manager, int offset) {
71             this.env = env;
72             this.processor = processor;
73             this.manager = manager;
74             this.offset = offset;
75         }
76     }
77 }
78
79
80 /**
81  * This class is an {@link XMLConsumer} that changes the current environment.
82  * When a pipeline calls an internal pipeline, two environments are
83  * established: one for the calling pipeline and one for the internal pipeline.
84  * Now, if SAX events are send from the internal pipeline, they are
85  * received by some component of the calling pipeline, so inbetween we
86  * have to change the environment forth and back.
87  */

88 final class EnvironmentChanger implements XMLConsumer {
89
90     final XMLConsumer consumer;
91     final EnvironmentStack stack;
92     final int oldOffset;
93     final int newOffset;
94
95     EnvironmentChanger(XMLConsumer consumer, EnvironmentStack es,
96                        int oldOffset, int newOffset) {
97         this.consumer = consumer;
98         this.stack = es;
99         this.oldOffset = oldOffset;
100         this.newOffset = newOffset;
101     }
102
103     public void setDocumentLocator(Locator JavaDoc locator) {
104         this.stack.setOffset(this.oldOffset);
105         this.consumer.setDocumentLocator(locator);
106         this.stack.setOffset(this.newOffset);
107     }
108
109     public void startDocument()
110     throws SAXException JavaDoc {
111         this.stack.setOffset(this.oldOffset);
112         this.consumer.startDocument();
113         this.stack.setOffset(this.newOffset);
114     }
115
116     public void endDocument()
117     throws SAXException JavaDoc {
118         this.stack.setOffset(this.oldOffset);
119         this.consumer.endDocument();
120         this.stack.setOffset(this.newOffset);
121     }
122
123     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
124     throws SAXException JavaDoc {
125         this.stack.setOffset(this.oldOffset);
126         this.consumer.startPrefixMapping(prefix, uri);
127         this.stack.setOffset(this.newOffset);
128     }
129
130     public void endPrefixMapping(String JavaDoc prefix)
131     throws SAXException JavaDoc {
132         this.stack.setOffset(this.oldOffset);
133         this.consumer.endPrefixMapping(prefix);
134         this.stack.setOffset(this.newOffset);
135     }
136
137     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a)
138     throws SAXException JavaDoc {
139         this.stack.setOffset(this.oldOffset);
140         this.consumer.startElement(uri, loc, raw, a);
141         this.stack.setOffset(this.newOffset);
142     }
143
144
145     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw)
146     throws SAXException JavaDoc {
147         this.stack.setOffset(this.oldOffset);
148         this.consumer.endElement(uri, loc, raw);
149         this.stack.setOffset(this.newOffset);
150     }
151
152     public void characters(char c[], int start, int len)
153     throws SAXException JavaDoc {
154         this.stack.setOffset(this.oldOffset);
155         this.consumer.characters(c, start, len);
156         this.stack.setOffset(this.newOffset);
157     }
158
159     public void ignorableWhitespace(char c[], int start, int len)
160     throws SAXException JavaDoc {
161         this.stack.setOffset(this.oldOffset);
162         this.consumer.ignorableWhitespace(c, start, len);
163         this.stack.setOffset(this.newOffset);
164     }
165
166     public void processingInstruction(String JavaDoc target, String JavaDoc data)
167     throws SAXException JavaDoc {
168         this.stack.setOffset(this.oldOffset);
169         this.consumer.processingInstruction(target, data);
170         this.stack.setOffset(this.newOffset);
171     }
172
173     public void skippedEntity(String JavaDoc name)
174     throws SAXException JavaDoc {
175         this.stack.setOffset(this.oldOffset);
176         this.consumer.skippedEntity(name);
177         this.stack.setOffset(this.newOffset);
178     }
179
180     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
181     throws SAXException JavaDoc {
182         this.stack.setOffset(this.oldOffset);
183         this.consumer.startDTD(name, publicId, systemId);
184         this.stack.setOffset(this.newOffset);
185     }
186
187     public void endDTD()
188     throws SAXException JavaDoc {
189         this.stack.setOffset(this.oldOffset);
190         this.consumer.endDTD();
191         this.stack.setOffset(this.newOffset);
192     }
193
194     public void startEntity(String JavaDoc name)
195     throws SAXException JavaDoc {
196         this.stack.setOffset(this.oldOffset);
197         this.consumer.startEntity(name);
198         this.stack.setOffset(this.newOffset);
199     }
200
201     public void endEntity(String JavaDoc name)
202     throws SAXException JavaDoc {
203         this.stack.setOffset(this.oldOffset);
204         this.consumer.endEntity(name);
205         this.stack.setOffset(this.newOffset);
206     }
207
208     public void startCDATA()
209     throws SAXException JavaDoc {
210         this.stack.setOffset(this.oldOffset);
211         this.consumer.startCDATA();
212         this.stack.setOffset(this.newOffset);
213     }
214
215     public void endCDATA()
216     throws SAXException JavaDoc {
217         this.stack.setOffset(this.oldOffset);
218         this.consumer.endCDATA();
219         this.stack.setOffset(this.newOffset);
220     }
221
222     public void comment(char ch[], int start, int len)
223     throws SAXException JavaDoc {
224         this.stack.setOffset(this.oldOffset);
225         this.consumer.comment(ch, start, len);
226         this.stack.setOffset(this.newOffset);
227     }
228 }
229
Popular Tags