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