001: /*
002:
003: ============================================================================
004: The Apache Software License, Version 1.1
005: ============================================================================
006:
007: Copyright (C) 2000 The Apache Software Foundation. All rights reserved.
008:
009: Redistribution and use in source and binary forms, with or without modifica-
010: tion, are permitted provided that the following conditions are met:
011:
012: 1. Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014:
015: 2. Redistributions in binary form must reproduce the above copyright notice,
016: this list of conditions and the following disclaimer in the documentation
017: and/or other materials provided with the distribution.
018:
019: 3. The end-user documentation included with the redistribution, if any, must
020: include the following acknowledgment: "This product includes software
021: developed by the Apache Software Foundation (http://www.apache.org/)."
022: Alternately, this acknowledgment may appear in the software itself, if
023: and wherever such third-party acknowledgments normally appear.
024:
025: 4. The names "Cocoon" and "Apache Software Foundation" must not be used to
026: endorse or promote products derived from this software without prior
027: written permission. For written permission, please contact
028: apache@apache.org.
029:
030: 5. Products derived from this software may not be called "Apache", nor may
031: "Apache" appear in their name, without prior written permission of the
032: Apache Software Foundation.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
035: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
036: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
037: APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
038: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
039: DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
040: OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
041: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: This software consists of voluntary contributions made by many individuals
046: on behalf of the Apache Software Foundation and was originally created by
047: Stefano Mazzocchi <stefano@apache.org>. For more information on the Apache
048: Software Foundation, please see <http://www.apache.org/>.
049:
050: */
051:
052: /*
053: * $Id: CXMLContentHandler.java,v 1.1 2001/12/18 10:31:31 per_nyfelt Exp $
054: *
055: * This code was part of the prove of concept of a binary representation of XML
056: * called CXML send to the Cocoon users list by Stefano Mazzocchi. It was modified
057: * to support the LexicalHandler interface.
058: */
059:
060: package org.ozoneDB.xml.util;
061:
062: import java.io.OutputStream;
063: import java.io.IOException;
064: import java.io.Serializable;
065:
066: import org.xml.sax.SAXException;
067: import org.xml.sax.ContentHandler;
068: import org.xml.sax.ext.LexicalHandler;
069: import org.xml.sax.Attributes;
070: import org.xml.sax.Locator;
071:
072: /**
073: */
074: class CXMLContentHandler implements ContentHandler, LexicalHandler,
075: Serializable {
076:
077: public final static boolean debug = false;
078:
079: public final static int START_DOCUMENT = 0;
080: public final static int END_DOCUMENT = 1;
081: public final static int START_PREFIX_MAPPING = 2;
082: public final static int END_PREFIX_MAPPING = 3;
083: public final static int START_ELEMENT = 4;
084: public final static int END_ELEMENT = 5;
085: public final static int CHARACTERS = 6;
086: public final static int IGNORABLE_WHITESPACE = 7;
087: public final static int PROCESSING_INSTRUCTION = 8;
088: public final static int COMMENT = 9;
089: public final static int START_CDATA = 10;
090: public final static int END_CDATA = 11;
091:
092: private final CompiledXMLOutputStream out;
093:
094: public CXMLContentHandler(OutputStream stream) throws IOException {
095: out = new CompiledXMLOutputStream(stream);
096: }
097:
098: public void startDocument() throws SAXException {
099: if (debug) {
100: System.out.println(this .getClass().getName()
101: + ": startDocument()");
102: }
103: try {
104: out.writeEvent(START_DOCUMENT);
105: } catch (Exception e) {
106: throw new SAXException(e);
107: }
108: }
109:
110: public void endDocument() throws SAXException {
111: if (debug) {
112: System.out.println(this .getClass().getName()
113: + ": endDocument()");
114: }
115: try {
116: out.writeEvent(END_DOCUMENT);
117: } catch (Exception e) {
118: throw new SAXException(e);
119: }
120: }
121:
122: public void startPrefixMapping(java.lang.String prefix,
123: java.lang.String uri) throws SAXException {
124: if (debug) {
125: System.out.println(this .getClass().getName()
126: + ": startPrefixMapping(...)");
127: }
128: try {
129: out.writeEvent(START_PREFIX_MAPPING);
130: out.writeString(prefix);
131: out.writeString(uri);
132: } catch (Exception e) {
133: throw new SAXException(e);
134: }
135: }
136:
137: public void endPrefixMapping(java.lang.String prefix)
138: throws SAXException {
139: if (debug) {
140: System.out.println(this .getClass().getName()
141: + ": endPrefixMapping(...)");
142: }
143: try {
144: out.writeEvent(END_PREFIX_MAPPING);
145: out.writeString(prefix);
146: } catch (Exception e) {
147: throw new SAXException(e);
148: }
149: }
150:
151: public void startElement(java.lang.String namespaceURI,
152: java.lang.String localName, java.lang.String qName,
153: Attributes atts) throws SAXException {
154: if (debug) {
155: System.out.println(this .getClass().getName()
156: + ": startElement(...)");
157: }
158: try {
159: int length = atts.getLength();
160: out.writeEvent(START_ELEMENT);
161: out.writeAttributes(length);
162: for (int i = 0; i < length; i++) {
163: out.writeString(atts.getURI(i));
164: out.writeString(atts.getLocalName(i));
165: out.writeString(atts.getQName(i));
166: out.writeString(atts.getType(i));
167:
168: // write att value as char to prevent the value from being
169: // indexed by the CompiledXMLOutputStream
170: String attValue = atts.getValue(i);
171: out.writeChars(attValue.toCharArray(), 0, attValue
172: .length());
173:
174: // out.writeString( attValue );
175: }
176: out.writeString(namespaceURI);
177: out.writeString(localName);
178: out.writeString(qName);
179: } catch (Exception e) {
180: throw new SAXException(e);
181: }
182: }
183:
184: public void endElement(java.lang.String namespaceURI,
185: java.lang.String localName, java.lang.String qName)
186: throws SAXException {
187: if (debug) {
188: System.out.println(this .getClass().getName()
189: + ": endElement(...)");
190: }
191: try {
192: out.writeEvent(END_ELEMENT);
193: out.writeString(namespaceURI);
194: out.writeString(localName);
195: out.writeString(qName);
196: } catch (Exception e) {
197: throw new SAXException(e);
198: }
199: }
200:
201: public void characters(char[] ch, int start, int length)
202: throws SAXException {
203: if (debug) {
204: System.out.println(this .getClass().getName()
205: + ": characters(...)");
206: }
207: try {
208: out.writeEvent(CHARACTERS);
209: out.writeChars(ch, start, length);
210: } catch (Exception e) {
211: throw new SAXException(e);
212: }
213: }
214:
215: public void ignorableWhitespace(char[] ch, int start, int length)
216: throws SAXException {
217: if (debug) {
218: System.out.println(this .getClass().getName()
219: + ": ignorableWhitespace(...)");
220: }
221: try {
222: out.writeEvent(IGNORABLE_WHITESPACE);
223: out.writeChars(ch, start, length);
224: } catch (Exception e) {
225: throw new SAXException(e);
226: }
227: }
228:
229: public void processingInstruction(java.lang.String target,
230: java.lang.String data) throws SAXException {
231: if (debug) {
232: System.out.println(this .getClass().getName()
233: + ": processingInstruction(...)");
234: }
235: try {
236: out.writeEvent(PROCESSING_INSTRUCTION);
237: out.writeString(target);
238: out.writeString(data);
239: } catch (Exception e) {
240: throw new SAXException(e);
241: }
242: }
243:
244: public void comment(char[] ch, int start, int length)
245: throws SAXException {
246: if (debug) {
247: System.out.println(this .getClass().getName()
248: + ": comment(...)");
249: }
250: try {
251: out.writeEvent(COMMENT);
252: out.writeChars(ch, start, length);
253: } catch (Exception e) {
254: throw new SAXException(e);
255: }
256: }
257:
258: public void startCDATA() throws SAXException {
259: if (debug) {
260: System.out.println(this .getClass().getName()
261: + ": startCDATA()");
262: }
263: try {
264: out.writeEvent(START_CDATA);
265: } catch (Exception e) {
266: throw new SAXException(e);
267: }
268: }
269:
270: public void endCDATA() throws SAXException {
271: if (debug) {
272: System.out.println(this .getClass().getName()
273: + ": endCDATA()");
274: }
275: try {
276: out.writeEvent(END_CDATA);
277: } catch (Exception e) {
278: throw new SAXException(e);
279: }
280: }
281:
282: public void startDTD(String name, String publicId, String systemId)
283: throws SAXException {
284: if (debug) {
285: System.out.println(this .getClass().getName()
286: + ": startDTD()");
287: }
288: //FIXME(?)
289: //not handled
290: }
291:
292: public void endDTD() throws SAXException {
293: if (debug) {
294: System.out
295: .println(this .getClass().getName() + ": endDTD()");
296: }
297: //FIXME(?)
298: //not handled
299: }
300:
301: public void startEntity(String name) throws SAXException {
302: if (debug) {
303: System.out.println(this .getClass().getName()
304: + ": startEntity()");
305: }
306: //FIXME(?)
307: //not handled
308: }
309:
310: public void endEntity(String name) throws SAXException {
311: if (debug) {
312: System.out.println(this .getClass().getName()
313: + ": endEntity()");
314: }
315: //FIXME(?)
316: //not handled
317: }
318:
319: public void setDocumentLocator(Locator locator) {
320: // ignore.
321: }
322:
323: public void skippedEntity(java.lang.String name)
324: throws SAXException {
325: if (debug) {
326: System.out.println(this .getClass().getName()
327: + ": skippedEntity(...)[ignored]");
328: }
329: // ignore.
330: }
331: }
|