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