java/alt2xml-out-xml/src/cz/frantovo/alt2xml/out/xml/XMLHandler.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 15 Jun 2014 14:50:49 +0200
changeset 43 058c1c39251e
parent 40 java/alt2xml-lib-output/src/cz/frantovo/alt2xml/out/EchoContentHandler.java@4afb00b7b1a9
child 111 e4900596abdb
permissions -rw-r--r--
ouptup modules framework + XML (echo) output module
     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.xml;
    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 XMLHandler extends DefaultHandler {
    37 
    38 	private static final Logger log = Logger.getLogger(XMLHandler.class.getName());
    39 	private XMLStreamWriter w;
    40 
    41 	public XMLHandler(XMLStreamWriter writer) {
    42 		w = writer;
    43 	}
    44 
    45 	@Override
    46 	public void startDocument() throws SAXException {
    47 		try {
    48 			w.writeStartDocument();
    49 		} catch (XMLStreamException e) {
    50 			throw new SAXException(e);
    51 		}
    52 	}
    53 
    54 	@Override
    55 	public void endDocument() throws SAXException {
    56 		try {
    57 			w.writeEndDocument();
    58 			w.close();
    59 		} catch (XMLStreamException e) {
    60 			throw new SAXException(e);
    61 		}
    62 	}
    63 
    64 	@Override
    65 	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    66 		try {
    67 			w.writeStartElement(qName);
    68 
    69 			if (attributes != null) {
    70 				for (int i = 0; i < attributes.getLength(); i++) {
    71 					w.writeAttribute(attributes.getQName(i), attributes.getValue(i));
    72 				}
    73 			}
    74 
    75 			w.flush();
    76 		} catch (XMLStreamException e) {
    77 			throw new SAXException(e);
    78 		}
    79 	}
    80 
    81 	@Override
    82 	public void endElement(String uri, String localName, String qName) throws SAXException {
    83 		try {
    84 			w.writeEndElement();
    85 			w.flush();
    86 		} catch (XMLStreamException e) {
    87 			throw new SAXException(e);
    88 		}
    89 	}
    90 
    91 	@Override
    92 	public void characters(char[] ch, int start, int length) throws SAXException {
    93 		try {
    94 			w.writeCharacters(ch, start, length);
    95 			w.flush();
    96 		} catch (XMLStreamException e) {
    97 			throw new SAXException(e);
    98 		}
    99 	}
   100 
   101 	/**
   102 	 * LexicalHandler methods
   103 	 *
   104 	 * @Override
   105 	 * public void startDTD(String name, String publicId, String systemId) throws SAXException {
   106 	 * log.log(Level.WARNING, "Start of DTD: {0} | {1} | {2}", new Object[]{name, publicId,
   107 	 * systemId});
   108 	 * }
   109 	 *
   110 	 * @Override
   111 	 * public void endDTD() throws SAXException {
   112 	 * log.log(Level.WARNING, "End of DTD");
   113 	 * }
   114 	 *
   115 	 * @Override
   116 	 * public void startEntity(String name) throws SAXException {
   117 	 * log.log(Level.WARNING, "Start of Entity: {0}", name);
   118 	 * }
   119 	 *
   120 	 * @Override
   121 	 * public void endEntity(String name) throws SAXException {
   122 	 * log.log(Level.WARNING, "End of Entity: {0}", name);
   123 	 * }
   124 	 *
   125 	 * @Override
   126 	 * public void startCDATA() throws SAXException {
   127 	 * log.log(Level.WARNING, "Start of CDATA");
   128 	 * }
   129 	 *
   130 	 * @Override
   131 	 * public void endCDATA() throws SAXException {
   132 	 * log.log(Level.WARNING, "End of CDATA");
   133 	 * }
   134 	 *
   135 	 * @Override
   136 	 * public void comment(char[] ch, int start, int length) throws SAXException {
   137 	 * try {
   138 	 * w.writeComment(new String(ch, start, length));
   139 	 * w.flush();
   140 	 * } catch (XMLStreamException e) {
   141 	 * throw new SAXException(e);
   142 	 * }
   143 	 * }
   144 	 */
   145 }