java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/EchoContentHandler.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 08 Jun 2014 15:11:01 +0200
changeset 40 4afb00b7b1a9
parent 39 java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/EchoContentHandler.java@a5020340362b
permissions -rw-r--r--
move EchoContentHandler to alt2xml-lib-output
     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.out;
    19 
    20 import java.util.logging.Logger;
    21 import javax.xml.stream.XMLStreamException;
    22 import javax.xml.stream.XMLStreamWriter;
    23 import org.xml.sax.Attributes;
    24 import org.xml.sax.SAXException;
    25 import org.xml.sax.helpers.DefaultHandler;
    26 
    27 /**
    28  * Slouží k převodu právě parsovaného XML zpět na XML.
    29  * Určen pro testování a ladění a pro použití s neobvyklými „XML“ parsery,
    30  * které nečtou XML ale jiný jazyk (např. JSON, INI atd.), ale používají stejné rozhraní (SAX).
    31  *
    32  * TODO: další typy uzlů a jmenné prostory.
    33  *
    34  * @author Ing. František Kučera (frantovo.cz)
    35  */
    36 public class EchoContentHandler extends DefaultHandler {
    37 
    38 	private static final Logger log = Logger.getLogger(EchoContentHandler.class.getName());
    39 	private XMLStreamWriter w;
    40 
    41 	/**
    42 	 * @param writer kam se bude vypisovat XML.
    43 	 */
    44 	public EchoContentHandler(XMLStreamWriter writer) {
    45 		w = writer;
    46 	}
    47 
    48 	@Override
    49 	public void startDocument() throws SAXException {
    50 		try {
    51 			w.writeStartDocument();
    52 		} catch (XMLStreamException e) {
    53 			throw new SAXException(e);
    54 		}
    55 	}
    56 
    57 	@Override
    58 	public void endDocument() throws SAXException {
    59 		try {
    60 			w.writeEndDocument();
    61 			w.close();
    62 		} catch (XMLStreamException e) {
    63 			throw new SAXException(e);
    64 		}
    65 	}
    66 
    67 	@Override
    68 	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    69 		try {
    70 			w.writeStartElement(qName);
    71 
    72 			if (attributes != null) {
    73 				for (int i = 0; i < attributes.getLength(); i++) {
    74 					w.writeAttribute(attributes.getQName(i), attributes.getValue(i));
    75 				}
    76 			}
    77 
    78 			w.flush();
    79 		} catch (XMLStreamException e) {
    80 			throw new SAXException(e);
    81 		}
    82 	}
    83 
    84 	@Override
    85 	public void endElement(String uri, String localName, String qName) throws SAXException {
    86 		try {
    87 			w.writeEndElement();
    88 			w.flush();
    89 		} catch (XMLStreamException e) {
    90 			throw new SAXException(e);
    91 		}
    92 	}
    93 
    94 	@Override
    95 	public void characters(char[] ch, int start, int length) throws SAXException {
    96 		try {
    97 			w.writeCharacters(ch, start, length);
    98 			w.flush();
    99 		} catch (XMLStreamException e) {
   100 			throw new SAXException(e);
   101 		}
   102 	}
   103 
   104 	/**
   105 	 * LexicalHandler methods
   106 	 *
   107 	 * @Override
   108 	 * public void startDTD(String name, String publicId, String systemId) throws SAXException {
   109 	 * log.log(Level.WARNING, "Start of DTD: {0} | {1} | {2}", new Object[]{name, publicId,
   110 	 * systemId});
   111 	 * }
   112 	 *
   113 	 * @Override
   114 	 * public void endDTD() throws SAXException {
   115 	 * log.log(Level.WARNING, "End of DTD");
   116 	 * }
   117 	 *
   118 	 * @Override
   119 	 * public void startEntity(String name) throws SAXException {
   120 	 * log.log(Level.WARNING, "Start of Entity: {0}", name);
   121 	 * }
   122 	 *
   123 	 * @Override
   124 	 * public void endEntity(String name) throws SAXException {
   125 	 * log.log(Level.WARNING, "End of Entity: {0}", name);
   126 	 * }
   127 	 *
   128 	 * @Override
   129 	 * public void startCDATA() throws SAXException {
   130 	 * log.log(Level.WARNING, "Start of CDATA");
   131 	 * }
   132 	 *
   133 	 * @Override
   134 	 * public void endCDATA() throws SAXException {
   135 	 * log.log(Level.WARNING, "End of CDATA");
   136 	 * }
   137 	 *
   138 	 * @Override
   139 	 * public void comment(char[] ch, int start, int length) throws SAXException {
   140 	 * try {
   141 	 * w.writeComment(new String(ch, start, length));
   142 	 * w.flush();
   143 	 * } catch (XMLStreamException e) {
   144 	 * throw new SAXException(e);
   145 	 * }
   146 	 * }
   147 	 */
   148 }