java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/JsonSimpleContentHandler.java
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.in.json;
19 import java.io.IOException;
20 import java.util.Stack;
21 import org.json.simple.parser.ParseException;
22 import org.xml.sax.ContentHandler;
23 import org.xml.sax.SAXException;
27 * @author Ing. František Kučera (frantovo.cz)
29 public class JsonSimpleContentHandler implements org.json.simple.parser.ContentHandler {
31 /** Sem vypisujeme XML události */
32 private final ContentHandler saxVýstup;
33 /** Musíme si pamatovat polohu v XML stromu, abychom věděli, kterou značku kdy uzavřít */
34 private final Stack<String> poloha = new Stack<>();
36 * Po textových uzlech vkládáme konce elementů rovnou,
37 * ale pokud jeden element končí hned po jiném,
38 * vložíme mezi ně ještě konec řádku a odsazení.
40 private boolean zalomitŘádek = false;
42 public JsonSimpleContentHandler(ContentHandler saxVýstup) {
43 this.saxVýstup = saxVýstup;
46 private void začniElement(String název) throws IOException {
49 saxVýstup.startElement(null, null, název, null);
51 } catch (SAXException e) {
52 throw new IOException("Chyba při začátku elementu.", e);
56 private void ukončiElement() throws IOException {
58 String značka = poloha.pop();
63 saxVýstup.endElement(null, null, značka);
64 } catch (SAXException e) {
65 throw new IOException("Chyba při ukončování elementu.", e);
69 private void vložOdsazení() throws IOException {
71 * TODO: ignorableWhitespace() ?
74 for (int i = 0; i < poloha.size(); i++) {
79 private void vložText(String text) throws IOException {
81 saxVýstup.characters(text.toCharArray(), 0, text.length());
82 } catch (SAXException e) {
83 throw new IOException("Chyba při vkládání textu.", e);
88 public void startJSON() throws ParseException, IOException {
90 saxVýstup.startDocument();
91 začniElement("objekt");
92 } catch (SAXException e) {
93 throw new IOException("Chyba při začátku dokumentu.", e);
98 public void endJSON() throws ParseException, IOException {
102 saxVýstup.endDocument();
103 } catch (SAXException e) {
104 throw new IOException(e);
109 public boolean startObject() throws ParseException, IOException {
111 * TODO: zachovat hranice mezi objekty, které jsou prvkem pole
113 * { "type": "home", "number": "212 555-1234" },
114 * { "type": "fax", "number": "646 555-4567" }
119 // System.err.println("startObject");
124 public boolean endObject() throws ParseException, IOException {
125 // System.err.println("endObject");
130 public boolean startObjectEntry(String key) throws ParseException, IOException {
136 public boolean endObjectEntry() throws ParseException, IOException {
138 // System.err.println("endObjectEntry");
143 public boolean startArray() throws ParseException, IOException {
144 // System.err.println("startArray");
149 public boolean endArray() throws ParseException, IOException {
150 // System.err.println("endArray");
155 public boolean primitive(Object value) throws ParseException, IOException {
156 vložText(String.valueOf(value));
157 zalomitŘádek = false;