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@43: package cz.frantovo.alt2xml.out.xml; franta-hg@2: franta-hg@39: import java.util.logging.Logger; 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@19: * franta-hg@2: * TODO: další typy uzlů a jmenné prostory. franta-hg@19: * franta-hg@19: * @author Ing. František Kučera (frantovo.cz) franta-hg@2: */ franta-hg@43: public class XMLHandler extends DefaultHandler { franta-hg@2: franta-hg@43: private static final Logger log = Logger.getLogger(XMLHandler.class.getName()); franta-hg@2: private XMLStreamWriter w; franta-hg@2: franta-hg@43: public XMLHandler(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@39: franta-hg@39: /** franta-hg@39: * LexicalHandler methods franta-hg@39: * franta-hg@39: * @Override franta-hg@39: * public void startDTD(String name, String publicId, String systemId) throws SAXException { franta-hg@39: * log.log(Level.WARNING, "Start of DTD: {0} | {1} | {2}", new Object[]{name, publicId, franta-hg@39: * systemId}); franta-hg@39: * } franta-hg@39: * franta-hg@39: * @Override franta-hg@39: * public void endDTD() throws SAXException { franta-hg@39: * log.log(Level.WARNING, "End of DTD"); franta-hg@39: * } franta-hg@39: * franta-hg@39: * @Override franta-hg@39: * public void startEntity(String name) throws SAXException { franta-hg@39: * log.log(Level.WARNING, "Start of Entity: {0}", name); franta-hg@39: * } franta-hg@39: * franta-hg@39: * @Override franta-hg@39: * public void endEntity(String name) throws SAXException { franta-hg@39: * log.log(Level.WARNING, "End of Entity: {0}", name); franta-hg@39: * } franta-hg@39: * franta-hg@39: * @Override franta-hg@39: * public void startCDATA() throws SAXException { franta-hg@39: * log.log(Level.WARNING, "Start of CDATA"); franta-hg@39: * } franta-hg@39: * franta-hg@39: * @Override franta-hg@39: * public void endCDATA() throws SAXException { franta-hg@39: * log.log(Level.WARNING, "End of CDATA"); franta-hg@39: * } franta-hg@39: * franta-hg@39: * @Override franta-hg@39: * public void comment(char[] ch, int start, int length) throws SAXException { franta-hg@39: * try { franta-hg@39: * w.writeComment(new String(ch, start, length)); franta-hg@39: * w.flush(); franta-hg@39: * } catch (XMLStreamException e) { franta-hg@39: * throw new SAXException(e); franta-hg@39: * } franta-hg@39: * } franta-hg@39: */ franta-hg@2: }