diff -r f1e91d4ac35c -r 058c1c39251e java/alt2xml-out-xml/src/cz/frantovo/alt2xml/out/xml/XMLHandler.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/alt2xml-out-xml/src/cz/frantovo/alt2xml/out/xml/XMLHandler.java Sun Jun 15 14:50:49 2014 +0200 @@ -0,0 +1,145 @@ +/** + * Alt2XML + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.alt2xml.out.xml; + +import java.util.logging.Logger; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +/** + * Slouží k převodu právě parsovaného XML zpět na XML. + * Určen pro testování a ladění a pro použití s neobvyklými „XML“ parsery, + * které nečtou XML ale jiný jazyk (např. JSON, INI atd.), ale používají stejné rozhraní (SAX). + * + * TODO: další typy uzlů a jmenné prostory. + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class XMLHandler extends DefaultHandler { + + private static final Logger log = Logger.getLogger(XMLHandler.class.getName()); + private XMLStreamWriter w; + + public XMLHandler(XMLStreamWriter writer) { + w = writer; + } + + @Override + public void startDocument() throws SAXException { + try { + w.writeStartDocument(); + } catch (XMLStreamException e) { + throw new SAXException(e); + } + } + + @Override + public void endDocument() throws SAXException { + try { + w.writeEndDocument(); + w.close(); + } catch (XMLStreamException e) { + throw new SAXException(e); + } + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + try { + w.writeStartElement(qName); + + if (attributes != null) { + for (int i = 0; i < attributes.getLength(); i++) { + w.writeAttribute(attributes.getQName(i), attributes.getValue(i)); + } + } + + w.flush(); + } catch (XMLStreamException e) { + throw new SAXException(e); + } + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + try { + w.writeEndElement(); + w.flush(); + } catch (XMLStreamException e) { + throw new SAXException(e); + } + } + + @Override + public void characters(char[] ch, int start, int length) throws SAXException { + try { + w.writeCharacters(ch, start, length); + w.flush(); + } catch (XMLStreamException e) { + throw new SAXException(e); + } + } + + /** + * LexicalHandler methods + * + * @Override + * public void startDTD(String name, String publicId, String systemId) throws SAXException { + * log.log(Level.WARNING, "Start of DTD: {0} | {1} | {2}", new Object[]{name, publicId, + * systemId}); + * } + * + * @Override + * public void endDTD() throws SAXException { + * log.log(Level.WARNING, "End of DTD"); + * } + * + * @Override + * public void startEntity(String name) throws SAXException { + * log.log(Level.WARNING, "Start of Entity: {0}", name); + * } + * + * @Override + * public void endEntity(String name) throws SAXException { + * log.log(Level.WARNING, "End of Entity: {0}", name); + * } + * + * @Override + * public void startCDATA() throws SAXException { + * log.log(Level.WARNING, "Start of CDATA"); + * } + * + * @Override + * public void endCDATA() throws SAXException { + * log.log(Level.WARNING, "End of CDATA"); + * } + * + * @Override + * public void comment(char[] ch, int start, int length) throws SAXException { + * try { + * w.writeComment(new String(ch, start, length)); + * w.flush(); + * } catch (XMLStreamException e) { + * throw new SAXException(e); + * } + * } + */ +}