MyNSContentHandler.java revision 687:e7736286abe1
1/*
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.xml.sax.ptests;
24
25import org.xml.sax.helpers.DefaultHandler;
26import org.xml.sax.helpers.LocatorImpl;
27import org.xml.sax.Locator;
28import org.xml.sax.Attributes;
29import java.io.BufferedWriter;
30import java.io.IOException;
31import java.io.FileWriter;
32import org.xml.sax.SAXException;
33
34class MyNSContentHandler extends DefaultHandler implements AutoCloseable{
35    /**
36     * Prefix for written string.
37     */
38    private final static String WRITE_ERROR = "bWrite error";
39
40    /**
41     * FileWriter to write output file.
42     */
43    private final BufferedWriter bWriter;
44
45    /**
46     * Default locator.
47     */
48    Locator locator = new LocatorImpl();
49
50    /**
51     * Initiate FileWrite.
52     * @param outputFileName file name of output file.
53     * @throws SAXException when open output file failed.
54     */
55    public MyNSContentHandler(String outputFileName) throws SAXException {
56        try {
57            bWriter = new BufferedWriter(new FileWriter(outputFileName));
58        } catch (IOException ex) {
59            throw new SAXException(ex);
60        }
61    }
62
63    /**
64     * Write characters tag along with content of characters when meet
65     * characters event.
66     * @throws IOException error happen when writing file.
67     */
68    @Override
69    public void characters(char[] ch, int start, int length)
70            throws SAXException {
71        String s = new String(ch, start, length);
72        println("characters...length is:" + s.length() + "\n"
73                + "<" + s + ">");
74    }
75
76    /**
77     * Write endDocument tag then flush the content and close the file when meet
78     * endDocument event.
79     * @throws IOException error happen when writing file or closing file.
80     */
81    @Override
82    public void endDocument() throws SAXException {
83        try {
84            println("endDocument...");
85            bWriter.flush();
86            bWriter.close();
87        } catch (IOException ex) {
88            throw new SAXException(WRITE_ERROR, ex);
89        }
90    }
91
92    /**
93     * Write endElement tag with namespaceURI, localName, qName to the file when
94     * meet endElement event.
95     * @throws IOException error happen when writing file.
96     */
97    @Override
98    public void endElement(String namespaceURI, String localName, String qName)
99            throws SAXException {
100        println("endElement...\n" + "namespaceURI: <" + namespaceURI
101                + "> localName: <" + localName + "> qName: <" + qName + ">");
102    }
103
104    /**
105     * Write endPrefixMapping tag along with prefix to the file when meet
106     * endPrefixMapping event.
107     * @throws IOException error happen when writing file.
108     */
109    @Override
110    public void endPrefixMapping(String prefix) throws SAXException {
111        println("endPrefixMapping...\n" + "prefix: <" + prefix + ">");
112    }
113
114    /**
115     * Write ignorableWhitespace tag along with white spaces when meet
116     * ignorableWhitespace event.
117     * @throws IOException error happen when writing file.
118     */
119    @Override
120    public void ignorableWhitespace(char[] ch, int start, int length)
121            throws SAXException {
122        String s = new String(ch, start, length);
123        println("ignorableWhitespace...\n" + s
124                + " ignorable white space string length: " + s.length());
125    }
126
127    /**
128     * Write processingInstruction tag along with target name and target data
129     * when meet processingInstruction event.
130     * @throws IOException error happen when writing file.
131     */
132    @Override
133    public void processingInstruction(String target, String data)
134            throws SAXException {
135        println("processingInstruction...target:<" + target
136                + "> data: <" + data + ">");
137    }
138
139    /**
140     * Write setDocumentLocator tag when meet setDocumentLocator event.
141     */
142    @Override
143    public void setDocumentLocator(Locator locator) {
144        try {
145            this.locator = locator;
146            println("setDocumentLocator...");
147        } catch (SAXException ex) {
148            System.err.println(WRITE_ERROR + ex);
149        }
150    }
151
152    /**
153     * Write skippedEntity tag along with entity name when meet skippedEntity
154     * event.
155     * @throws IOException error happen when writing file.
156     */
157    @Override
158    public void skippedEntity(String name) throws SAXException {
159        println("skippedEntity...\n" + "name: <" + name + ">");
160    }
161
162    /**
163     * Write startDocument tag when meet startDocument event.
164     * @throws IOException error happen when writing file.
165     */
166    @Override
167    public void startDocument() throws SAXException {
168        println("startDocument...");
169    }
170
171    /**
172     * Write startElement tag along with namespaceURI, localName, qName, number
173     * of attributes and line number when meet startElement event.
174     * @throws IOException error happen when writing file.
175     */
176    @Override
177    public void startElement(String namespaceURI, String localName,
178            String qName, Attributes atts) throws SAXException {
179        println("startElement...\n" + "namespaceURI: <" + namespaceURI
180                + "> localName: <" + localName + "> qName: <" + qName
181                + "> Number of Attributes: <" + atts.getLength()
182                + "> Line# <" + locator.getLineNumber() + ">");
183    }
184
185    /**
186     * Write startPrefixMapping tag along with prefix and uri when meet
187     * startPrefixMapping event.
188     * @throws IOException error happen when writing file.
189     */
190    @Override
191    public void startPrefixMapping(String prefix, String uri)
192            throws SAXException {
193        println("startPrefixMapping...\n" + "prefix: <" + prefix
194                + "> uri: <" + uri + ">");
195    }
196    /**
197     * Write outString to output file.
198     * @param outString string to be written.
199     * @throws SAXException
200     */
201    private void println(String outString) throws SAXException {
202        try {
203            bWriter.write( outString, 0, outString.length());
204            bWriter.newLine();
205        } catch (IOException ex) {
206            throw new SAXException(WRITE_ERROR, ex);
207        }
208    }
209
210    /**
211     * Close writer if it's initiated.
212     * @throws IOException if any I/O error when close writer.
213     */
214    @Override
215    public void close() throws IOException {
216        if (bWriter != null)
217            bWriter.close();
218    }
219}
220