KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > xml > ParamSaxBuffer


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.xml;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.io.Writer JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import org.xml.sax.ContentHandler JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.Attributes JavaDoc;
26
27 /**
28  * Modification of the SAX buffer with parameterization capabilities.
29  *
30  * Any <code>{name}</code> expression inside of the character events can be
31  * replaced by the content of another SaxBuffer if it is present in the map
32  * passed to the {@link #toSAX(ContentHandler, Map)} method.
33  *
34  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
35  * @version CVS $Id: ParamSaxBuffer.java 106183 2004-11-22 14:28:04Z bruno $
36  */

37 public class ParamSaxBuffer extends SaxBuffer {
38
39    /**
40     * If ch (in characters()) contains an unmatched '{' then
41     * we save the chars from '{' onward in previous_ch.
42     * Next call to characters() prepends the saved chars to ch before processing
43     * (and sets previous_ch to null).
44     */

45     private char[] previous_ch = null;
46
47     /**
48      * Creates empty SaxBuffer
49      */

50     public ParamSaxBuffer() {
51     }
52
53     /**
54      * Creates copy of another SaxBuffer
55      */

56     public ParamSaxBuffer(SaxBuffer saxBuffer) {
57         super(saxBuffer);
58     }
59
60     /**
61      * Parses text and extracts <code>{name}</code> parameters for later
62      * substitution.
63      */

64     public void characters(char ch[], int start, int length) throws SAXException JavaDoc {
65
66         if (previous_ch != null) {
67             // prepend char's from previous_ch to ch
68
char[] buf = new char[length + previous_ch.length];
69             System.arraycopy(previous_ch, 0, buf, 0, previous_ch.length);
70             System.arraycopy(ch, start, buf, previous_ch.length, length);
71             ch = buf;
72             start = 0;
73             length += previous_ch.length;
74             previous_ch = null;
75         }
76
77         final int end = start + length;
78         for (int i = start; i < end; i++) {
79             if (ch[i] == '{') {
80                 // Send any collected characters so far
81
if (i > start) {
82                     addBit(new Characters(ch, start, i - start));
83                 }
84
85                 // Find closing brace, and construct parameter name
86
StringBuffer JavaDoc name = new StringBuffer JavaDoc();
87                 int j = i + 1;
88                 for (; j < end; j++) {
89                     if (ch[j] == '}') {
90                         break;
91                     }
92                     name.append(ch[j]);
93                 }
94                 if (j == end) {
95                     // '{' without a closing '}'
96
// save char's from '{' in previous_ch in case the following call to characters()
97
// provides the '}'
98
previous_ch = new char[end - i];
99                     System.arraycopy(ch, i, previous_ch, 0, end - i);
100                     break;
101                 }
102                 addBit(new Parameter(name.toString()));
103
104                 // Continue processing
105
i = j;
106                 start = j + 1;
107                 continue;
108             }
109         }
110
111         // Send any tailing characters
112
if (start < end) {
113             addBit(new Characters(ch, start, end - start));
114         }
115     }
116
117     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
118         flushChars();
119         super.endElement(namespaceURI, localName, qName);
120     }
121
122     public void ignorableWhitespace(char ch[], int start, int length) throws SAXException JavaDoc {
123         flushChars();
124         super.ignorableWhitespace(ch, start, length);
125     }
126
127     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
128         flushChars();
129         super.processingInstruction(target, data);
130     }
131
132     public void startDocument() throws SAXException JavaDoc {
133         flushChars();
134         super.startDocument();
135     }
136
137     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
138         flushChars();
139         super.startElement(namespaceURI, localName, qName, atts);
140     }
141
142     public void endDocument() throws SAXException JavaDoc {
143         flushChars();
144         super.endDocument();
145     }
146
147     public void comment(char ch[], int start, int length) throws SAXException JavaDoc {
148         flushChars();
149         super.comment(ch, start, length);
150     }
151
152     public void endDTD() throws SAXException JavaDoc {
153         flushChars();
154         super.endDTD();
155     }
156
157     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
158         flushChars();
159         super.startDTD(name, publicId, systemId);
160     }
161
162     private void flushChars() {
163         // Handle saved chars (in case we had a '{' with no matching '}').
164
if (previous_ch != null) {
165             addBit(new Characters(previous_ch, 0, previous_ch.length));
166             previous_ch = null;
167         }
168     }
169
170     /**
171      * @param parameters map containing SaxBuffers
172      */

173     public void toSAX(ContentHandler JavaDoc contentHandler, Map JavaDoc parameters) throws SAXException JavaDoc {
174         for (Iterator JavaDoc i = bits(); i.hasNext();) {
175             SaxBit saxbit = (SaxBit)i.next();
176             if (saxbit instanceof Parameter) {
177                 ((Parameter)saxbit).send(contentHandler, parameters);
178             } else {
179                 saxbit.send(contentHandler);
180             }
181         }
182     }
183
184
185     final static class Parameter implements SaxBit {
186         private final String JavaDoc name;
187
188         public Parameter(String JavaDoc name) {
189             this.name = name;
190         }
191
192         public void send(ContentHandler JavaDoc contentHandler) {
193         }
194
195         public void send(ContentHandler JavaDoc contentHandler, Map JavaDoc parameters) throws SAXException JavaDoc {
196             SaxBuffer value = (SaxBuffer)parameters.get(name);
197             if (value != null) {
198                 value.toSAX(contentHandler);
199             }
200         }
201
202         public void dump(Writer JavaDoc writer) throws IOException JavaDoc {
203             writer.write("[Parameter] name=" + name);
204         }
205     }
206 }
207
Popular Tags