1 package cz.frantovo.alt2xml.výstup;
3 import javax.xml.stream.XMLStreamException;
4 import javax.xml.stream.XMLStreamWriter;
5 import org.xml.sax.Attributes;
6 import org.xml.sax.SAXException;
7 import org.xml.sax.helpers.DefaultHandler;
10 * Slouží k převodu právě parsovaného XML zpět na XML.
11 * Určen pro testování a ladění a pro použití s neobvyklými „XML“ parsery,
12 * které nečtou XML ale jiný jazyk (např. JSON, INI atd.), ale používají stejné rozhraní (SAX).
14 * TODO: další typy uzlů a jmenné prostory.
17 public class EchoContentHandler extends DefaultHandler {
19 private XMLStreamWriter w;
22 * @param writer kam se bude vypisovat XML.
24 public EchoContentHandler(XMLStreamWriter writer) {
29 public void startDocument() throws SAXException {
31 w.writeStartDocument();
32 } catch (XMLStreamException e) {
33 throw new SAXException(e);
38 public void endDocument() throws SAXException {
42 } catch (XMLStreamException e) {
43 throw new SAXException(e);
48 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
50 w.writeStartElement(qName);
52 if (attributes != null) {
53 for (int i = 0; i < attributes.getLength(); i++) {
54 w.writeAttribute(attributes.getQName(i), attributes.getValue(i));
59 } catch (XMLStreamException e) {
60 throw new SAXException(e);
65 public void endElement(String uri, String localName, String qName) throws SAXException {
69 } catch (XMLStreamException e) {
70 throw new SAXException(e);
75 public void characters(char[] ch, int start, int length) throws SAXException {
77 w.writeCharacters(ch, start, length);
79 } catch (XMLStreamException e) {
80 throw new SAXException(e);