1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.betwixt.io;
18
19 import java.io.IOException;
20 import java.io.Writer;
21
22 import org.xml.sax.Attributes;
23 import org.xml.sax.SAXException;
24 import org.xml.sax.helpers.DefaultHandler;
25
26 /**
27 * Simple SAXContentHandler to test the SAXBeanWriter
28 *
29 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
30 * @version $Id: SAXContentHandler.java 438373 2006-08-30 05:17:21Z bayard $
31 */
32 public class SAXContentHandler extends DefaultHandler {
33
34 private Writer out;
35 /**
36 * Constructor for SAXContentHandler.
37 */
38 public SAXContentHandler(Writer out) {
39 this.out = out;
40 }
41
42 /**
43 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
44 */
45 public void characters(char[] ch, int start, int length)
46 throws SAXException
47 {
48 try {
49 out.write(" "+new String(ch, start, length)+"\n");
50 }catch(IOException ioe) {
51 }
52 }
53
54 /**
55 * @see org.xml.sax.ContentHandler#endElement(String, String, String)
56 */
57 public void endElement(String namespaceURI, String localName, String qName)
58 throws SAXException
59 {
60 try {
61 out.write("</"+qName+">\n");
62 }catch (IOException e) {
63 }
64 }
65
66 /**
67 * @see org.xml.sax.ContentHandler#startDocument()
68 */
69 public void startDocument() throws SAXException
70 {
71 try {
72 out.write("<?xml version=\"1.0\"?>\n");
73 }catch (IOException e){
74 }
75 }
76
77 /**
78 * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
79 */
80 public void startElement(
81 String namespaceURI,
82 String localName,
83 String qName,
84 Attributes atts)
85 throws SAXException
86 {
87 try {
88 StringBuffer sb = new StringBuffer();
89 sb.append("<"+qName);
90 for (int i=0; i < atts.getLength();i++)
91 {
92 sb.append(" "+atts.getQName(i));
93 sb.append("=\"");
94 sb.append(atts.getValue(i));
95 sb.append("\"");
96 }
97 sb.append(">\n");
98 out.write(sb.toString());
99 } catch (IOException e) {
100 }
101 }
102
103 }