franta-hg@11: /** franta-hg@11: * Alt2XML franta-hg@11: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@11: * franta-hg@11: * This program is free software: you can redistribute it and/or modify franta-hg@11: * it under the terms of the GNU General Public License as published by franta-hg@11: * the Free Software Foundation, either version 3 of the License, or franta-hg@11: * (at your option) any later version. franta-hg@11: * franta-hg@11: * This program is distributed in the hope that it will be useful, franta-hg@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@11: * GNU General Public License for more details. franta-hg@11: * franta-hg@11: * You should have received a copy of the GNU General Public License franta-hg@11: * along with this program. If not, see . franta-hg@11: */ franta-hg@3: package cz.frantovo.alt2xml.výstup; franta-hg@2: franta-hg@2: import javax.xml.stream.XMLStreamException; franta-hg@2: import javax.xml.stream.XMLStreamWriter; franta-hg@2: import org.xml.sax.Attributes; franta-hg@2: import org.xml.sax.SAXException; franta-hg@2: import org.xml.sax.helpers.DefaultHandler; franta-hg@2: franta-hg@2: /** franta-hg@2: * Slouží k převodu právě parsovaného XML zpět na XML. franta-hg@2: * Určen pro testování a ladění a pro použití s neobvyklými „XML“ parsery, franta-hg@2: * které nečtou XML ale jiný jazyk (např. JSON, INI atd.), ale používají stejné rozhraní (SAX). franta-hg@2: * franta-hg@2: * TODO: další typy uzlů a jmenné prostory. franta-hg@2: * @author fiki franta-hg@2: */ franta-hg@2: public class EchoContentHandler extends DefaultHandler { franta-hg@2: franta-hg@2: private XMLStreamWriter w; franta-hg@2: franta-hg@2: /** franta-hg@2: * @param writer kam se bude vypisovat XML. franta-hg@2: */ franta-hg@2: public EchoContentHandler(XMLStreamWriter writer) { franta-hg@2: w = writer; franta-hg@2: } franta-hg@2: franta-hg@2: @Override franta-hg@2: public void startDocument() throws SAXException { franta-hg@2: try { franta-hg@2: w.writeStartDocument(); franta-hg@2: } catch (XMLStreamException e) { franta-hg@2: throw new SAXException(e); franta-hg@2: } franta-hg@2: } franta-hg@2: franta-hg@2: @Override franta-hg@2: public void endDocument() throws SAXException { franta-hg@2: try { franta-hg@2: w.writeEndDocument(); franta-hg@2: w.close(); franta-hg@2: } catch (XMLStreamException e) { franta-hg@2: throw new SAXException(e); franta-hg@2: } franta-hg@2: } franta-hg@2: franta-hg@2: @Override franta-hg@2: public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { franta-hg@2: try { franta-hg@2: w.writeStartElement(qName); franta-hg@2: franta-hg@2: if (attributes != null) { franta-hg@2: for (int i = 0; i < attributes.getLength(); i++) { franta-hg@2: w.writeAttribute(attributes.getQName(i), attributes.getValue(i)); franta-hg@2: } franta-hg@2: } franta-hg@2: franta-hg@2: w.flush(); franta-hg@2: } catch (XMLStreamException e) { franta-hg@2: throw new SAXException(e); franta-hg@2: } franta-hg@2: } franta-hg@2: franta-hg@2: @Override franta-hg@2: public void endElement(String uri, String localName, String qName) throws SAXException { franta-hg@2: try { franta-hg@2: w.writeEndElement(); franta-hg@2: w.flush(); franta-hg@2: } catch (XMLStreamException e) { franta-hg@2: throw new SAXException(e); franta-hg@2: } franta-hg@2: } franta-hg@2: franta-hg@2: @Override franta-hg@2: public void characters(char[] ch, int start, int length) throws SAXException { franta-hg@2: try { franta-hg@2: w.writeCharacters(ch, start, length); franta-hg@2: w.flush(); franta-hg@2: } catch (XMLStreamException e) { franta-hg@2: throw new SAXException(e); franta-hg@2: } franta-hg@2: } franta-hg@2: }