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 java.util.logging.Logger;
21 import javax.xml.stream.XMLStreamException;
22 import javax.xml.stream.XMLStreamWriter;
23 import org.xml.sax.Attributes;
24 import org.xml.sax.SAXException;
25 import org.xml.sax.helpers.DefaultHandler;
28 * Slouží k převodu právě parsovaného XML zpět na XML.
29 * Určen pro testování a ladění a pro použití s neobvyklými „XML“ parsery,
30 * které nečtou XML ale jiný jazyk (např. JSON, INI atd.), ale používají stejné rozhraní (SAX).
32 * TODO: další typy uzlů a jmenné prostory.
34 * @author Ing. František Kučera (frantovo.cz)
36 public class EchoContentHandler extends DefaultHandler {
38 private static final Logger log = Logger.getLogger(EchoContentHandler.class.getName());
39 private XMLStreamWriter w;
42 * @param writer kam se bude vypisovat XML.
44 public EchoContentHandler(XMLStreamWriter writer) {
49 public void startDocument() throws SAXException {
51 w.writeStartDocument();
52 } catch (XMLStreamException e) {
53 throw new SAXException(e);
58 public void endDocument() throws SAXException {
62 } catch (XMLStreamException e) {
63 throw new SAXException(e);
68 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
70 w.writeStartElement(qName);
72 if (attributes != null) {
73 for (int i = 0; i < attributes.getLength(); i++) {
74 w.writeAttribute(attributes.getQName(i), attributes.getValue(i));
79 } catch (XMLStreamException e) {
80 throw new SAXException(e);
85 public void endElement(String uri, String localName, String qName) throws SAXException {
89 } catch (XMLStreamException e) {
90 throw new SAXException(e);
95 public void characters(char[] ch, int start, int length) throws SAXException {
97 w.writeCharacters(ch, start, length);
99 } catch (XMLStreamException e) {
100 throw new SAXException(e);
105 * LexicalHandler methods
108 * public void startDTD(String name, String publicId, String systemId) throws SAXException {
109 * log.log(Level.WARNING, "Start of DTD: {0} | {1} | {2}", new Object[]{name, publicId,
114 * public void endDTD() throws SAXException {
115 * log.log(Level.WARNING, "End of DTD");
119 * public void startEntity(String name) throws SAXException {
120 * log.log(Level.WARNING, "Start of Entity: {0}", name);
124 * public void endEntity(String name) throws SAXException {
125 * log.log(Level.WARNING, "End of Entity: {0}", name);
129 * public void startCDATA() throws SAXException {
130 * log.log(Level.WARNING, "Start of CDATA");
134 * public void endCDATA() throws SAXException {
135 * log.log(Level.WARNING, "End of CDATA");
139 * public void comment(char[] ch, int start, int length) throws SAXException {
141 * w.writeComment(new String(ch, start, length));
143 * } catch (XMLStreamException e) {
144 * throw new SAXException(e);