KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > util > CXMLContentHandler


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6  
7  Copyright (C) 2000 The Apache Software Foundation. All rights reserved.
8  
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11  
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14  
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18  
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24  
25  4. The names "Cocoon" and "Apache Software Foundation" must not be used to
26     endorse or promote products derived from this software without prior
27     written permission. For written permission, please contact
28     apache@apache.org.
29  
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33  
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation and was originally created by
47  Stefano Mazzocchi <stefano@apache.org>. For more information on the Apache
48  Software Foundation, please see <http://www.apache.org/>.
49  
50 */

51
52 /*
53  * $Id: CXMLContentHandler.java,v 1.8 2001/01/11 10:02:53 conny Exp $
54  *
55  * This code was part of the prove of concept of a binary representation of XML
56  * called CXML send to the Cocoon users list by Stefano Mazzocchi. It was modified
57  * to support the LexicalHandler interface.
58  */

59
60 package org.ozoneDB.xml.util;
61
62 import java.io.OutputStream JavaDoc;
63 import java.io.IOException JavaDoc;
64 import java.io.Serializable JavaDoc;
65
66 import org.xml.sax.SAXException JavaDoc;
67 import org.xml.sax.ContentHandler JavaDoc;
68 import org.xml.sax.ext.LexicalHandler JavaDoc;
69 import org.xml.sax.Attributes JavaDoc;
70 import org.xml.sax.Locator JavaDoc;
71
72
73 /**
74  */

75 class CXMLContentHandler implements ContentHandler JavaDoc, LexicalHandler JavaDoc, Serializable JavaDoc {
76     
77     public final static boolean debug = false;
78     
79     public final static int START_DOCUMENT = 0;
80     public final static int END_DOCUMENT = 1;
81     public final static int START_PREFIX_MAPPING = 2;
82     public final static int END_PREFIX_MAPPING = 3;
83     public final static int START_ELEMENT = 4;
84     public final static int END_ELEMENT = 5;
85     public final static int CHARACTERS = 6;
86     public final static int IGNORABLE_WHITESPACE = 7;
87     public final static int PROCESSING_INSTRUCTION = 8;
88     public final static int COMMENT = 9;
89     public final static int START_CDATA = 10;
90     public final static int END_CDATA = 11;
91     
92     private final CompiledXMLOutputStream out;
93     
94     
95     public CXMLContentHandler( OutputStream JavaDoc stream ) throws IOException JavaDoc{
96         out = new CompiledXMLOutputStream( stream );
97     }
98     
99     
100     public void startDocument() throws SAXException JavaDoc {
101         if (debug) {
102             System.out.println(this.getClass().getName() + ": startDocument()");
103         }
104         try {
105             out.writeEvent( START_DOCUMENT );
106         } catch (Exception JavaDoc e) {
107             throw new SAXException JavaDoc( e );
108         }
109     }
110     
111     
112     public void endDocument() throws SAXException JavaDoc {
113         if (debug) {
114             System.out.println(this.getClass().getName() + ": endDocument()");
115         }
116         try {
117             out.writeEvent( END_DOCUMENT );
118         } catch (Exception JavaDoc e) {
119             throw new SAXException JavaDoc( e );
120         }
121     }
122     
123     
124     public void startPrefixMapping( java.lang.String JavaDoc prefix, java.lang.String JavaDoc uri ) throws SAXException JavaDoc {
125         if (debug) {
126             System.out.println(this.getClass().getName() + ": startPrefixMapping(...)");
127         }
128         try {
129             out.writeEvent( START_PREFIX_MAPPING );
130             out.writeString( prefix );
131             out.writeString( uri );
132         } catch (Exception JavaDoc e) {
133             throw new SAXException JavaDoc( e );
134         }
135     }
136     
137     
138     public void endPrefixMapping( java.lang.String JavaDoc prefix ) throws SAXException JavaDoc {
139         if (debug) {
140             System.out.println(this.getClass().getName() + ": endPrefixMapping(...)");
141         }
142         try {
143             out.writeEvent( END_PREFIX_MAPPING );
144             out.writeString( prefix );
145         } catch (Exception JavaDoc e) {
146             throw new SAXException JavaDoc( e );
147         }
148     }
149     
150     
151     public void startElement( java.lang.String JavaDoc namespaceURI, java.lang.String JavaDoc localName, java.lang.String JavaDoc qName,
152             Attributes JavaDoc atts ) throws SAXException JavaDoc {
153         if (debug) {
154             System.out.println(this.getClass().getName() + ": startElement(...)");
155         }
156         try {
157             int length = atts.getLength();
158             out.writeEvent( START_ELEMENT );
159             out.writeAttributes( length );
160             for (int i = 0; i < length; i++) {
161                 out.writeString( atts.getURI( i ) );
162                 out.writeString( atts.getLocalName( i ) );
163                 out.writeString( atts.getQName( i ) );
164                 out.writeString( atts.getType( i ) );
165                 
166                 // write att value as char to prevent the value from being
167
// indexed by the CompiledXMLOutputStream
168
String JavaDoc attValue = atts.getValue( i );
169                 out.writeChars( attValue.toCharArray(), 0, attValue.length() );
170
171                // out.writeString( attValue );
172
}
173             out.writeString( namespaceURI );
174             out.writeString( localName );
175             out.writeString( qName );
176         } catch (Exception JavaDoc e) {
177             throw new SAXException JavaDoc( e );
178         }
179     }
180     
181     
182     public void endElement( java.lang.String JavaDoc namespaceURI, java.lang.String JavaDoc localName, java.lang.String JavaDoc qName )
183             throws SAXException JavaDoc {
184         if (debug) {
185             System.out.println(this.getClass().getName() + ": endElement(...)");
186         }
187         try {
188             out.writeEvent( END_ELEMENT );
189             out.writeString( namespaceURI );
190             out.writeString( localName );
191             out.writeString( qName );
192         } catch (Exception JavaDoc e) {
193             throw new SAXException JavaDoc( e );
194         }
195     }
196     
197     
198     public void characters( char[] ch, int start, int length ) throws SAXException JavaDoc {
199         if (debug) {
200             System.out.println(this.getClass().getName() + ": characters(...)");
201         }
202         try {
203             out.writeEvent( CHARACTERS );
204             out.writeChars( ch, start, length );
205         } catch (Exception JavaDoc e) {
206             throw new SAXException JavaDoc( e );
207         }
208     }
209     
210     
211     public void ignorableWhitespace( char[] ch, int start, int length ) throws SAXException JavaDoc {
212         if (debug) {
213             System.out.println(this.getClass().getName() + ": ignorableWhitespace(...)");
214         }
215         try {
216             out.writeEvent( IGNORABLE_WHITESPACE );
217             out.writeChars( ch, start, length );
218         } catch (Exception JavaDoc e) {
219             throw new SAXException JavaDoc( e );
220         }
221     }
222     
223     
224     public void processingInstruction( java.lang.String JavaDoc target, java.lang.String JavaDoc data ) throws SAXException JavaDoc {
225         if (debug) {
226             System.out.println(this.getClass().getName() + ": processingInstruction(...)");
227         }
228         try {
229             out.writeEvent( PROCESSING_INSTRUCTION );
230             out.writeString( target );
231             out.writeString( data );
232         } catch (Exception JavaDoc e) {
233             throw new SAXException JavaDoc( e );
234         }
235     }
236     
237
238     public void comment( char[] ch, int start, int length ) throws SAXException JavaDoc {
239         if (debug) {
240             System.out.println(this.getClass().getName() + ": comment(...)");
241         }
242         try {
243             out.writeEvent(COMMENT);
244             out.writeChars(ch, start, length);
245         } catch (Exception JavaDoc e) {
246             throw new SAXException JavaDoc(e);
247         }
248     }
249     
250
251     public void startCDATA() throws SAXException JavaDoc {
252         if (debug) {
253             System.out.println(this.getClass().getName() + ": startCDATA()");
254         }
255         try {
256             out.writeEvent(START_CDATA);
257         } catch (Exception JavaDoc e) {
258             throw new SAXException JavaDoc(e);
259         }
260     }
261     
262     
263     public void endCDATA() throws SAXException JavaDoc {
264         if (debug) {
265             System.out.println(this.getClass().getName() + ": endCDATA()");
266         }
267         try {
268             out.writeEvent(END_CDATA);
269         } catch (Exception JavaDoc e) {
270             throw new SAXException JavaDoc(e);
271         }
272     }
273     
274     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
275             throws SAXException JavaDoc {
276         if (debug) {
277             System.out.println(this.getClass().getName() + ": startDTD()");
278         }
279         //FIXME(?)
280
//not handled
281
}
282
283
284     public void endDTD() throws SAXException JavaDoc {
285         if (debug) {
286             System.out.println(this.getClass().getName() + ": endDTD()");
287         }
288         //FIXME(?)
289
//not handled
290
}
291
292
293     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
294         if (debug) {
295             System.out.println(this.getClass().getName() + ": startEntity()");
296         }
297         //FIXME(?)
298
//not handled
299
}
300     
301     
302     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
303         if (debug) {
304             System.out.println(this.getClass().getName() + ": endEntity()");
305         }
306         //FIXME(?)
307
//not handled
308
}
309     
310     
311     public void setDocumentLocator( Locator JavaDoc locator ) {
312     // ignore.
313
}
314     
315     
316     public void skippedEntity( java.lang.String JavaDoc name ) throws SAXException JavaDoc {
317         if (debug) {
318             System.out.println(this.getClass().getName() + ": skippedEntity(...)[ignored]");
319         }
320     // ignore.
321
}
322 }
323
Popular Tags