java/alt2xml/src/cz/frantovo/alt2xml/EchoContentHandler.java
changeset 2 be5bfbe1f0cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/alt2xml/src/cz/frantovo/alt2xml/EchoContentHandler.java	Mon Jan 02 20:15:52 2012 +0100
     1.3 @@ -0,0 +1,83 @@
     1.4 +package cz.frantovo.alt2xml;
     1.5 +
     1.6 +import javax.xml.stream.XMLStreamException;
     1.7 +import javax.xml.stream.XMLStreamWriter;
     1.8 +import org.xml.sax.Attributes;
     1.9 +import org.xml.sax.SAXException;
    1.10 +import org.xml.sax.helpers.DefaultHandler;
    1.11 +
    1.12 +/**
    1.13 + * Slouží k převodu právě parsovaného XML zpět na XML.
    1.14 + * Určen pro testování a ladění a pro použití s neobvyklými „XML“ parsery,
    1.15 + * které nečtou XML ale jiný jazyk (např. JSON, INI atd.), ale používají stejné rozhraní (SAX).
    1.16 + * 
    1.17 + * TODO: další typy uzlů a jmenné prostory.
    1.18 + * @author fiki
    1.19 + */
    1.20 +public class EchoContentHandler extends DefaultHandler {
    1.21 +
    1.22 +	private XMLStreamWriter w;
    1.23 +
    1.24 +	/**
    1.25 +	 * @param writer kam se bude vypisovat XML.
    1.26 +	 */
    1.27 +	public EchoContentHandler(XMLStreamWriter writer) {
    1.28 +		w = writer;
    1.29 +	}
    1.30 +
    1.31 +	@Override
    1.32 +	public void startDocument() throws SAXException {
    1.33 +		try {
    1.34 +			w.writeStartDocument();
    1.35 +		} catch (XMLStreamException e) {
    1.36 +			throw new SAXException(e);
    1.37 +		}
    1.38 +	}
    1.39 +
    1.40 +	@Override
    1.41 +	public void endDocument() throws SAXException {
    1.42 +		try {
    1.43 +			w.writeEndDocument();
    1.44 +			w.close();
    1.45 +		} catch (XMLStreamException e) {
    1.46 +			throw new SAXException(e);
    1.47 +		}
    1.48 +	}
    1.49 +
    1.50 +	@Override
    1.51 +	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    1.52 +		try {
    1.53 +			w.writeStartElement(qName);
    1.54 +
    1.55 +			if (attributes != null) {
    1.56 +				for (int i = 0; i < attributes.getLength(); i++) {
    1.57 +					w.writeAttribute(attributes.getQName(i), attributes.getValue(i));
    1.58 +				}
    1.59 +			}
    1.60 +
    1.61 +			w.flush();
    1.62 +		} catch (XMLStreamException e) {
    1.63 +			throw new SAXException(e);
    1.64 +		}
    1.65 +	}
    1.66 +
    1.67 +	@Override
    1.68 +	public void endElement(String uri, String localName, String qName) throws SAXException {
    1.69 +		try {
    1.70 +			w.writeEndElement();
    1.71 +			w.flush();
    1.72 +		} catch (XMLStreamException e) {
    1.73 +			throw new SAXException(e);
    1.74 +		}
    1.75 +	}
    1.76 +
    1.77 +	@Override
    1.78 +	public void characters(char[] ch, int start, int length) throws SAXException {
    1.79 +		try {
    1.80 +			w.writeCharacters(ch, start, length);
    1.81 +			w.flush();
    1.82 +		} catch (XMLStreamException e) {
    1.83 +			throw new SAXException(e);
    1.84 +		}
    1.85 +	}
    1.86 +}