KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > io > SAXContentHandler


1 /*
2  * Copyright 2001-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.commons.betwixt.io;
17
18 import java.io.IOException JavaDoc;
19 import java.io.Writer JavaDoc;
20
21 import org.xml.sax.Attributes JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23 import org.xml.sax.helpers.DefaultHandler JavaDoc;
24
25 /**
26  * Simple SAXContentHandler to test the SAXBeanWriter
27  *
28  * @author <a HREF="mailto:martin@mvdb.net">Martin van den Bemt</a>
29  * @version $Id: SAXContentHandler.java,v 1.4 2004/02/28 13:38:35 yoavs Exp $
30  */

31 public class SAXContentHandler extends DefaultHandler JavaDoc {
32     
33     private Writer JavaDoc out;
34     /**
35      * Constructor for SAXContentHandler.
36      */

37     public SAXContentHandler(Writer JavaDoc out) {
38         this.out = out;
39     }
40
41     /**
42      * @see org.xml.sax.ContentHandler#characters(char[], int, int)
43      */

44     public void characters(char[] ch, int start, int length)
45         throws SAXException JavaDoc
46     {
47         try {
48             out.write(" "+new String JavaDoc(ch, start, length)+"\n");
49         }catch(IOException JavaDoc ioe) {
50         }
51     }
52
53     /**
54      * @see org.xml.sax.ContentHandler#endElement(String, String, String)
55      */

56     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
57         throws SAXException JavaDoc
58     {
59         try {
60             out.write("</"+qName+">\n");
61         }catch (IOException JavaDoc e) {
62         }
63     }
64
65     /**
66      * @see org.xml.sax.ContentHandler#startDocument()
67      */

68     public void startDocument() throws SAXException JavaDoc
69     {
70         try {
71             out.write("<?xml version=\"1.0\"?>\n");
72         }catch (IOException JavaDoc e){
73         }
74     }
75
76     /**
77      * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
78      */

79     public void startElement(
80         String JavaDoc namespaceURI,
81         String JavaDoc localName,
82         String JavaDoc qName,
83         Attributes JavaDoc atts)
84         throws SAXException JavaDoc
85     {
86         try {
87             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
88             sb.append("<"+qName);
89             for (int i=0; i < atts.getLength();i++)
90             {
91                 sb.append(" "+atts.getQName(i));
92                 sb.append("=\"");
93                 sb.append(atts.getValue(i));
94                 sb.append("\"");
95             }
96             sb.append(">\n");
97             out.write(sb.toString());
98         } catch (IOException JavaDoc e) {
99         }
100     }
101
102 }
103
Popular Tags