1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/EchoContentHandler.java Sat Jun 07 10:49:42 2014 +0200
1.3 @@ -0,0 +1,100 @@
1.4 +/**
1.5 + * Alt2XML
1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +package cz.frantovo.alt2xml.cli;
1.22 +
1.23 +import javax.xml.stream.XMLStreamException;
1.24 +import javax.xml.stream.XMLStreamWriter;
1.25 +import org.xml.sax.Attributes;
1.26 +import org.xml.sax.SAXException;
1.27 +import org.xml.sax.helpers.DefaultHandler;
1.28 +
1.29 +/**
1.30 + * Slouží k převodu právě parsovaného XML zpět na XML.
1.31 + * Určen pro testování a ladění a pro použití s neobvyklými „XML“ parsery,
1.32 + * které nečtou XML ale jiný jazyk (např. JSON, INI atd.), ale používají stejné rozhraní (SAX).
1.33 + *
1.34 + * TODO: další typy uzlů a jmenné prostory.
1.35 + * @author fiki
1.36 + */
1.37 +public class EchoContentHandler extends DefaultHandler {
1.38 +
1.39 + private XMLStreamWriter w;
1.40 +
1.41 + /**
1.42 + * @param writer kam se bude vypisovat XML.
1.43 + */
1.44 + public EchoContentHandler(XMLStreamWriter writer) {
1.45 + w = writer;
1.46 + }
1.47 +
1.48 + @Override
1.49 + public void startDocument() throws SAXException {
1.50 + try {
1.51 + w.writeStartDocument();
1.52 + } catch (XMLStreamException e) {
1.53 + throw new SAXException(e);
1.54 + }
1.55 + }
1.56 +
1.57 + @Override
1.58 + public void endDocument() throws SAXException {
1.59 + try {
1.60 + w.writeEndDocument();
1.61 + w.close();
1.62 + } catch (XMLStreamException e) {
1.63 + throw new SAXException(e);
1.64 + }
1.65 + }
1.66 +
1.67 + @Override
1.68 + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
1.69 + try {
1.70 + w.writeStartElement(qName);
1.71 +
1.72 + if (attributes != null) {
1.73 + for (int i = 0; i < attributes.getLength(); i++) {
1.74 + w.writeAttribute(attributes.getQName(i), attributes.getValue(i));
1.75 + }
1.76 + }
1.77 +
1.78 + w.flush();
1.79 + } catch (XMLStreamException e) {
1.80 + throw new SAXException(e);
1.81 + }
1.82 + }
1.83 +
1.84 + @Override
1.85 + public void endElement(String uri, String localName, String qName) throws SAXException {
1.86 + try {
1.87 + w.writeEndElement();
1.88 + w.flush();
1.89 + } catch (XMLStreamException e) {
1.90 + throw new SAXException(e);
1.91 + }
1.92 + }
1.93 +
1.94 + @Override
1.95 + public void characters(char[] ch, int start, int length) throws SAXException {
1.96 + try {
1.97 + w.writeCharacters(ch, start, length);
1.98 + w.flush();
1.99 + } catch (XMLStreamException e) {
1.100 + throw new SAXException(e);
1.101 + }
1.102 + }
1.103 +}