java/alt2xml-out-xml/src/cz/frantovo/alt2xml/out/xml/XMLHandler.java
changeset 43 058c1c39251e
parent 40 4afb00b7b1a9
child 111 e4900596abdb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/alt2xml-out-xml/src/cz/frantovo/alt2xml/out/xml/XMLHandler.java	Sun Jun 15 14:50:49 2014 +0200
     1.3 @@ -0,0 +1,145 @@
     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.out.xml;
    1.22 +
    1.23 +import java.util.logging.Logger;
    1.24 +import javax.xml.stream.XMLStreamException;
    1.25 +import javax.xml.stream.XMLStreamWriter;
    1.26 +import org.xml.sax.Attributes;
    1.27 +import org.xml.sax.SAXException;
    1.28 +import org.xml.sax.helpers.DefaultHandler;
    1.29 +
    1.30 +/**
    1.31 + * Slouží k převodu právě parsovaného XML zpět na XML.
    1.32 + * Určen pro testování a ladění a pro použití s neobvyklými „XML“ parsery,
    1.33 + * které nečtou XML ale jiný jazyk (např. JSON, INI atd.), ale používají stejné rozhraní (SAX).
    1.34 + *
    1.35 + * TODO: další typy uzlů a jmenné prostory.
    1.36 + *
    1.37 + * @author Ing. František Kučera (frantovo.cz)
    1.38 + */
    1.39 +public class XMLHandler extends DefaultHandler {
    1.40 +
    1.41 +	private static final Logger log = Logger.getLogger(XMLHandler.class.getName());
    1.42 +	private XMLStreamWriter w;
    1.43 +
    1.44 +	public XMLHandler(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 +
   1.104 +	/**
   1.105 +	 * LexicalHandler methods
   1.106 +	 *
   1.107 +	 * @Override
   1.108 +	 * public void startDTD(String name, String publicId, String systemId) throws SAXException {
   1.109 +	 * log.log(Level.WARNING, "Start of DTD: {0} | {1} | {2}", new Object[]{name, publicId,
   1.110 +	 * systemId});
   1.111 +	 * }
   1.112 +	 *
   1.113 +	 * @Override
   1.114 +	 * public void endDTD() throws SAXException {
   1.115 +	 * log.log(Level.WARNING, "End of DTD");
   1.116 +	 * }
   1.117 +	 *
   1.118 +	 * @Override
   1.119 +	 * public void startEntity(String name) throws SAXException {
   1.120 +	 * log.log(Level.WARNING, "Start of Entity: {0}", name);
   1.121 +	 * }
   1.122 +	 *
   1.123 +	 * @Override
   1.124 +	 * public void endEntity(String name) throws SAXException {
   1.125 +	 * log.log(Level.WARNING, "End of Entity: {0}", name);
   1.126 +	 * }
   1.127 +	 *
   1.128 +	 * @Override
   1.129 +	 * public void startCDATA() throws SAXException {
   1.130 +	 * log.log(Level.WARNING, "Start of CDATA");
   1.131 +	 * }
   1.132 +	 *
   1.133 +	 * @Override
   1.134 +	 * public void endCDATA() throws SAXException {
   1.135 +	 * log.log(Level.WARNING, "End of CDATA");
   1.136 +	 * }
   1.137 +	 *
   1.138 +	 * @Override
   1.139 +	 * public void comment(char[] ch, int start, int length) throws SAXException {
   1.140 +	 * try {
   1.141 +	 * w.writeComment(new String(ch, start, length));
   1.142 +	 * w.flush();
   1.143 +	 * } catch (XMLStreamException e) {
   1.144 +	 * throw new SAXException(e);
   1.145 +	 * }
   1.146 +	 * }
   1.147 +	 */
   1.148 +}