java/alt2xml-out-xml/src/cz/frantovo/alt2xml/out/xml/XMLHandler.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:56:03 +0200
changeset 111 e4900596abdb
parent 43 058c1c39251e
permissions -rw-r--r--
fix license version: GNU GPLv3
     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, version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 package cz.frantovo.alt2xml.out.xml;
    18 
    19 import java.util.logging.Logger;
    20 import javax.xml.stream.XMLStreamException;
    21 import javax.xml.stream.XMLStreamWriter;
    22 import org.xml.sax.Attributes;
    23 import org.xml.sax.SAXException;
    24 import org.xml.sax.helpers.DefaultHandler;
    25 
    26 /**
    27  * Slouží k převodu právě parsovaného XML zpět na XML.
    28  * Určen pro testování a ladění a pro použití s neobvyklými „XML“ parsery,
    29  * které nečtou XML ale jiný jazyk (např. JSON, INI atd.), ale používají stejné rozhraní (SAX).
    30  *
    31  * TODO: další typy uzlů a jmenné prostory.
    32  *
    33  * @author Ing. František Kučera (frantovo.cz)
    34  */
    35 public class XMLHandler extends DefaultHandler {
    36 
    37 	private static final Logger log = Logger.getLogger(XMLHandler.class.getName());
    38 	private XMLStreamWriter w;
    39 
    40 	public XMLHandler(XMLStreamWriter writer) {
    41 		w = writer;
    42 	}
    43 
    44 	@Override
    45 	public void startDocument() throws SAXException {
    46 		try {
    47 			w.writeStartDocument();
    48 		} catch (XMLStreamException e) {
    49 			throw new SAXException(e);
    50 		}
    51 	}
    52 
    53 	@Override
    54 	public void endDocument() throws SAXException {
    55 		try {
    56 			w.writeEndDocument();
    57 			w.close();
    58 		} catch (XMLStreamException e) {
    59 			throw new SAXException(e);
    60 		}
    61 	}
    62 
    63 	@Override
    64 	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    65 		try {
    66 			w.writeStartElement(qName);
    67 
    68 			if (attributes != null) {
    69 				for (int i = 0; i < attributes.getLength(); i++) {
    70 					w.writeAttribute(attributes.getQName(i), attributes.getValue(i));
    71 				}
    72 			}
    73 
    74 			w.flush();
    75 		} catch (XMLStreamException e) {
    76 			throw new SAXException(e);
    77 		}
    78 	}
    79 
    80 	@Override
    81 	public void endElement(String uri, String localName, String qName) throws SAXException {
    82 		try {
    83 			w.writeEndElement();
    84 			w.flush();
    85 		} catch (XMLStreamException e) {
    86 			throw new SAXException(e);
    87 		}
    88 	}
    89 
    90 	@Override
    91 	public void characters(char[] ch, int start, int length) throws SAXException {
    92 		try {
    93 			w.writeCharacters(ch, start, length);
    94 			w.flush();
    95 		} catch (XMLStreamException e) {
    96 			throw new SAXException(e);
    97 		}
    98 	}
    99 
   100 	/**
   101 	 * LexicalHandler methods
   102 	 *
   103 	 * @Override
   104 	 * public void startDTD(String name, String publicId, String systemId) throws SAXException {
   105 	 * log.log(Level.WARNING, "Start of DTD: {0} | {1} | {2}", new Object[]{name, publicId,
   106 	 * systemId});
   107 	 * }
   108 	 *
   109 	 * @Override
   110 	 * public void endDTD() throws SAXException {
   111 	 * log.log(Level.WARNING, "End of DTD");
   112 	 * }
   113 	 *
   114 	 * @Override
   115 	 * public void startEntity(String name) throws SAXException {
   116 	 * log.log(Level.WARNING, "Start of Entity: {0}", name);
   117 	 * }
   118 	 *
   119 	 * @Override
   120 	 * public void endEntity(String name) throws SAXException {
   121 	 * log.log(Level.WARNING, "End of Entity: {0}", name);
   122 	 * }
   123 	 *
   124 	 * @Override
   125 	 * public void startCDATA() throws SAXException {
   126 	 * log.log(Level.WARNING, "Start of CDATA");
   127 	 * }
   128 	 *
   129 	 * @Override
   130 	 * public void endCDATA() throws SAXException {
   131 	 * log.log(Level.WARNING, "End of CDATA");
   132 	 * }
   133 	 *
   134 	 * @Override
   135 	 * public void comment(char[] ch, int start, int length) throws SAXException {
   136 	 * try {
   137 	 * w.writeComment(new String(ch, start, length));
   138 	 * w.flush();
   139 	 * } catch (XMLStreamException e) {
   140 	 * throw new SAXException(e);
   141 	 * }
   142 	 * }
   143 	 */
   144 }