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;
19  import java.io.Writer;
20  
21  import org.xml.sax.Attributes;
22  import org.xml.sax.SAXException;
23  import org.xml.sax.helpers.DefaultHandler;
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 {
32      
33      private Writer out;
34      /***
35       * Constructor for SAXContentHandler.
36       */
37      public SAXContentHandler(Writer 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
46      {
47          try {
48              out.write("  "+new String(ch, start, length)+"\n");
49          }catch(IOException ioe) {
50          }
51      }
52  
53      /***
54       * @see org.xml.sax.ContentHandler#endElement(String, String, String)
55       */
56      public void endElement(String namespaceURI, String localName, String qName)
57          throws SAXException
58      {
59          try {
60              out.write("</"+qName+">\n");
61          }catch (IOException e) {
62          }
63      }
64  
65      /***
66       * @see org.xml.sax.ContentHandler#startDocument()
67       */
68      public void startDocument() throws SAXException
69      {
70          try {
71              out.write("<?xml version=\"1.0\"?>\n");
72          }catch (IOException e){
73          }
74      }
75  
76      /***
77       * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
78       */
79      public void startElement(
80          String namespaceURI,
81          String localName,
82          String qName,
83          Attributes atts)
84          throws SAXException
85      {
86          try {
87              StringBuffer sb = new StringBuffer();
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 e)  {
99          }
100     }
101 
102 }