3 * Copyright © 2014 František Kučera (frantovo.cz)
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.
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.
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/>.
17 package cz.frantovo.alt2xml.out.xml;
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;
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).
31 * TODO: další typy uzlů a jmenné prostory.
33 * @author Ing. František Kučera (frantovo.cz)
35 public class XMLHandler extends DefaultHandler {
37 private static final Logger log = Logger.getLogger(XMLHandler.class.getName());
38 private XMLStreamWriter w;
40 public XMLHandler(XMLStreamWriter writer) {
45 public void startDocument() throws SAXException {
47 w.writeStartDocument();
48 } catch (XMLStreamException e) {
49 throw new SAXException(e);
54 public void endDocument() throws SAXException {
58 } catch (XMLStreamException e) {
59 throw new SAXException(e);
64 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
66 w.writeStartElement(qName);
68 if (attributes != null) {
69 for (int i = 0; i < attributes.getLength(); i++) {
70 w.writeAttribute(attributes.getQName(i), attributes.getValue(i));
75 } catch (XMLStreamException e) {
76 throw new SAXException(e);
81 public void endElement(String uri, String localName, String qName) throws SAXException {
85 } catch (XMLStreamException e) {
86 throw new SAXException(e);
91 public void characters(char[] ch, int start, int length) throws SAXException {
93 w.writeCharacters(ch, start, length);
95 } catch (XMLStreamException e) {
96 throw new SAXException(e);
101 * LexicalHandler methods
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,
110 * public void endDTD() throws SAXException {
111 * log.log(Level.WARNING, "End of DTD");
115 * public void startEntity(String name) throws SAXException {
116 * log.log(Level.WARNING, "Start of Entity: {0}", name);
120 * public void endEntity(String name) throws SAXException {
121 * log.log(Level.WARNING, "End of Entity: {0}", name);
125 * public void startCDATA() throws SAXException {
126 * log.log(Level.WARNING, "Start of CDATA");
130 * public void endCDATA() throws SAXException {
131 * log.log(Level.WARNING, "End of CDATA");
135 * public void comment(char[] ch, int start, int length) throws SAXException {
137 * w.writeComment(new String(ch, start, length));
139 * } catch (XMLStreamException e) {
140 * throw new SAXException(e);