java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/EchoContentHandler.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 07 Jun 2014 11:27:57 +0200
changeset 19 a58dce1054af
parent 16 b2fbb3570ae1
child 39 a5020340362b
permissions -rw-r--r--
JavaDoc: @author Ing. František Kučera (frantovo.cz)
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.alt2xml.cli;
    19 
    20 import javax.xml.stream.XMLStreamException;
    21 import javax.xml.stream.XMLStreamWriter;
    22 import org.xml.sax.Attributes;
    23 import org.xml.sax.SAXException;
    24 import org.xml.sax.helpers.DefaultHandler;
    25 
    26 /**
    27  * Slouží k převodu právě parsovaného XML zpět na XML.
    28  * Určen pro testování a ladění a pro použití s neobvyklými „XML“ parsery,
    29  * které nečtou XML ale jiný jazyk (např. JSON, INI atd.), ale používají stejné rozhraní (SAX).
    30  *
    31  * TODO: další typy uzlů a jmenné prostory.
    32  *
    33  * @author Ing. František Kučera (frantovo.cz)
    34  */
    35 public class EchoContentHandler extends DefaultHandler {
    36 
    37 	private XMLStreamWriter w;
    38 
    39 	/**
    40 	 * @param writer kam se bude vypisovat XML.
    41 	 */
    42 	public EchoContentHandler(XMLStreamWriter writer) {
    43 		w = writer;
    44 	}
    45 
    46 	@Override
    47 	public void startDocument() throws SAXException {
    48 		try {
    49 			w.writeStartDocument();
    50 		} catch (XMLStreamException e) {
    51 			throw new SAXException(e);
    52 		}
    53 	}
    54 
    55 	@Override
    56 	public void endDocument() throws SAXException {
    57 		try {
    58 			w.writeEndDocument();
    59 			w.close();
    60 		} catch (XMLStreamException e) {
    61 			throw new SAXException(e);
    62 		}
    63 	}
    64 
    65 	@Override
    66 	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    67 		try {
    68 			w.writeStartElement(qName);
    69 
    70 			if (attributes != null) {
    71 				for (int i = 0; i < attributes.getLength(); i++) {
    72 					w.writeAttribute(attributes.getQName(i), attributes.getValue(i));
    73 				}
    74 			}
    75 
    76 			w.flush();
    77 		} catch (XMLStreamException e) {
    78 			throw new SAXException(e);
    79 		}
    80 	}
    81 
    82 	@Override
    83 	public void endElement(String uri, String localName, String qName) throws SAXException {
    84 		try {
    85 			w.writeEndElement();
    86 			w.flush();
    87 		} catch (XMLStreamException e) {
    88 			throw new SAXException(e);
    89 		}
    90 	}
    91 
    92 	@Override
    93 	public void characters(char[] ch, int start, int length) throws SAXException {
    94 		try {
    95 			w.writeCharacters(ch, start, length);
    96 			w.flush();
    97 		} catch (XMLStreamException e) {
    98 			throw new SAXException(e);
    99 		}
   100 	}
   101 }