JavaDoc: @author Ing. František Kučera (frantovo.cz)
3 * Copyright © 2014 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package cz.frantovo.alt2xml.cli;
20 import javax.xml.stream.XMLStreamException;
21 import javax.xml.stream.XMLStreamWriter;
22 import org.xml.sax.Attributes;
23 import org.xml.sax.SAXException;
24 import org.xml.sax.helpers.DefaultHandler;
27 * Slouží k převodu právě parsovaného XML zpět na XML.
28 * Určen pro testování a ladění a pro použití s neobvyklými „XML“ parsery,
29 * které nečtou XML ale jiný jazyk (např. JSON, INI atd.), ale používají stejné rozhraní (SAX).
31 * TODO: další typy uzlů a jmenné prostory.
33 * @author Ing. František Kučera (frantovo.cz)
35 public class EchoContentHandler extends DefaultHandler {
37 private XMLStreamWriter w;
40 * @param writer kam se bude vypisovat XML.
42 public EchoContentHandler(XMLStreamWriter writer) {
47 public void startDocument() throws SAXException {
49 w.writeStartDocument();
50 } catch (XMLStreamException e) {
51 throw new SAXException(e);
56 public void endDocument() throws SAXException {
60 } catch (XMLStreamException e) {
61 throw new SAXException(e);
66 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
68 w.writeStartElement(qName);
70 if (attributes != null) {
71 for (int i = 0; i < attributes.getLength(); i++) {
72 w.writeAttribute(attributes.getQName(i), attributes.getValue(i));
77 } catch (XMLStreamException e) {
78 throw new SAXException(e);
83 public void endElement(String uri, String localName, String qName) throws SAXException {
87 } catch (XMLStreamException e) {
88 throw new SAXException(e);
93 public void characters(char[] ch, int start, int length) throws SAXException {
95 w.writeCharacters(ch, start, length);
97 } catch (XMLStreamException e) {
98 throw new SAXException(e);