01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: /**
37: * Constructor for SAXContentHandler.
38: */
39: public SAXContentHandler(Writer out) {
40: this .out = out;
41: }
42:
43: /**
44: * @see org.xml.sax.ContentHandler#characters(char[], int, int)
45: */
46: public void characters(char[] ch, int start, int length)
47: throws SAXException {
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,
58: String qName) throws SAXException {
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: try {
70: out.write("<?xml version=\"1.0\"?>\n");
71: } catch (IOException e) {
72: }
73: }
74:
75: /**
76: * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
77: */
78: public void startElement(String namespaceURI, String localName,
79: String qName, Attributes atts) throws SAXException {
80: try {
81: StringBuffer sb = new StringBuffer();
82: sb.append("<" + qName);
83: for (int i = 0; i < atts.getLength(); i++) {
84: sb.append(" " + atts.getQName(i));
85: sb.append("=\"");
86: sb.append(atts.getValue(i));
87: sb.append("\"");
88: }
89: sb.append(">\n");
90: out.write(sb.toString());
91: } catch (IOException e) {
92: }
93: }
94:
95: }
|