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@14
|
18 |
package cz.frantovo.alt2xml.in.json;
|
franta-hg@2
|
19 |
|
franta-hg@2
|
20 |
import java.io.IOException;
|
franta-hg@2
|
21 |
import java.util.Stack;
|
franta-hg@2
|
22 |
import org.json.simple.parser.ParseException;
|
franta-hg@2
|
23 |
import org.xml.sax.ContentHandler;
|
franta-hg@2
|
24 |
import org.xml.sax.SAXException;
|
franta-hg@2
|
25 |
|
franta-hg@2
|
26 |
/**
|
franta-hg@4
|
27 |
*
|
franta-hg@19
|
28 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@2
|
29 |
*/
|
franta-hg@2
|
30 |
public class JsonSimpleContentHandler implements org.json.simple.parser.ContentHandler {
|
franta-hg@2
|
31 |
|
franta-hg@2
|
32 |
/** Sem vypisujeme XML události */
|
franta-hg@20
|
33 |
private final ContentHandler saxVýstup;
|
franta-hg@2
|
34 |
/** Musíme si pamatovat polohu v XML stromu, abychom věděli, kterou značku kdy uzavřít */
|
franta-hg@20
|
35 |
private final Stack<String> poloha = new Stack<>();
|
franta-hg@3
|
36 |
/**
|
franta-hg@3
|
37 |
* Po textových uzlech vkládáme konce elementů rovnou,
|
franta-hg@3
|
38 |
* ale pokud jeden element končí hned po jiném,
|
franta-hg@3
|
39 |
* vložíme mezi ně ještě konec řádku a odsazení.
|
franta-hg@3
|
40 |
*/
|
franta-hg@3
|
41 |
private boolean zalomitŘádek = false;
|
franta-hg@2
|
42 |
|
franta-hg@2
|
43 |
public JsonSimpleContentHandler(ContentHandler saxVýstup) {
|
franta-hg@2
|
44 |
this.saxVýstup = saxVýstup;
|
franta-hg@2
|
45 |
}
|
franta-hg@2
|
46 |
|
franta-hg@3
|
47 |
private void začniElement(String název) throws IOException {
|
franta-hg@3
|
48 |
try {
|
franta-hg@3
|
49 |
vložOdsazení();
|
franta-hg@3
|
50 |
saxVýstup.startElement(null, null, název, null);
|
franta-hg@3
|
51 |
poloha.push(název);
|
franta-hg@3
|
52 |
} catch (SAXException e) {
|
franta-hg@3
|
53 |
throw new IOException("Chyba při začátku elementu.", e);
|
franta-hg@3
|
54 |
}
|
franta-hg@3
|
55 |
}
|
franta-hg@3
|
56 |
|
franta-hg@3
|
57 |
private void ukončiElement() throws IOException {
|
franta-hg@3
|
58 |
try {
|
franta-hg@3
|
59 |
String značka = poloha.pop();
|
franta-hg@3
|
60 |
if (zalomitŘádek) {
|
franta-hg@3
|
61 |
vložOdsazení();
|
franta-hg@3
|
62 |
}
|
franta-hg@3
|
63 |
zalomitŘádek = true;
|
franta-hg@3
|
64 |
saxVýstup.endElement(null, null, značka);
|
franta-hg@3
|
65 |
} catch (SAXException e) {
|
franta-hg@3
|
66 |
throw new IOException("Chyba při ukončování elementu.", e);
|
franta-hg@3
|
67 |
}
|
franta-hg@3
|
68 |
}
|
franta-hg@3
|
69 |
|
franta-hg@3
|
70 |
private void vložOdsazení() throws IOException {
|
franta-hg@3
|
71 |
/**
|
franta-hg@3
|
72 |
* TODO: ignorableWhitespace() ?
|
franta-hg@3
|
73 |
*/
|
franta-hg@3
|
74 |
vložText("\n");
|
franta-hg@3
|
75 |
for (int i = 0; i < poloha.size(); i++) {
|
franta-hg@3
|
76 |
vložText("\t");
|
franta-hg@3
|
77 |
}
|
franta-hg@3
|
78 |
}
|
franta-hg@3
|
79 |
|
franta-hg@3
|
80 |
private void vložText(String text) throws IOException {
|
franta-hg@3
|
81 |
try {
|
franta-hg@3
|
82 |
saxVýstup.characters(text.toCharArray(), 0, text.length());
|
franta-hg@3
|
83 |
} catch (SAXException e) {
|
franta-hg@3
|
84 |
throw new IOException("Chyba při vkládání textu.", e);
|
franta-hg@3
|
85 |
}
|
franta-hg@3
|
86 |
}
|
franta-hg@3
|
87 |
|
franta-hg@2
|
88 |
@Override
|
franta-hg@2
|
89 |
public void startJSON() throws ParseException, IOException {
|
franta-hg@2
|
90 |
try {
|
franta-hg@2
|
91 |
saxVýstup.startDocument();
|
franta-hg@3
|
92 |
začniElement("objekt");
|
franta-hg@2
|
93 |
} catch (SAXException e) {
|
franta-hg@3
|
94 |
throw new IOException("Chyba při začátku dokumentu.", e);
|
franta-hg@2
|
95 |
}
|
franta-hg@2
|
96 |
}
|
franta-hg@2
|
97 |
|
franta-hg@2
|
98 |
@Override
|
franta-hg@2
|
99 |
public void endJSON() throws ParseException, IOException {
|
franta-hg@2
|
100 |
try {
|
franta-hg@3
|
101 |
ukončiElement();
|
franta-hg@3
|
102 |
vložText("\n");
|
franta-hg@2
|
103 |
saxVýstup.endDocument();
|
franta-hg@2
|
104 |
} catch (SAXException e) {
|
franta-hg@2
|
105 |
throw new IOException(e);
|
franta-hg@2
|
106 |
}
|
franta-hg@2
|
107 |
}
|
franta-hg@2
|
108 |
|
franta-hg@2
|
109 |
@Override
|
franta-hg@2
|
110 |
public boolean startObject() throws ParseException, IOException {
|
franta-hg@2
|
111 |
// System.err.println("startObject");
|
franta-hg@2
|
112 |
return true;
|
franta-hg@2
|
113 |
}
|
franta-hg@2
|
114 |
|
franta-hg@2
|
115 |
@Override
|
franta-hg@2
|
116 |
public boolean endObject() throws ParseException, IOException {
|
franta-hg@2
|
117 |
// System.err.println("endObject");
|
franta-hg@2
|
118 |
return true;
|
franta-hg@2
|
119 |
}
|
franta-hg@2
|
120 |
|
franta-hg@2
|
121 |
@Override
|
franta-hg@2
|
122 |
public boolean startObjectEntry(String key) throws ParseException, IOException {
|
franta-hg@3
|
123 |
začniElement(key);
|
franta-hg@2
|
124 |
return true;
|
franta-hg@2
|
125 |
}
|
franta-hg@2
|
126 |
|
franta-hg@2
|
127 |
@Override
|
franta-hg@2
|
128 |
public boolean endObjectEntry() throws ParseException, IOException {
|
franta-hg@3
|
129 |
ukončiElement();
|
franta-hg@2
|
130 |
// System.err.println("endObjectEntry");
|
franta-hg@2
|
131 |
return true;
|
franta-hg@2
|
132 |
}
|
franta-hg@2
|
133 |
|
franta-hg@2
|
134 |
@Override
|
franta-hg@2
|
135 |
public boolean startArray() throws ParseException, IOException {
|
franta-hg@2
|
136 |
// System.err.println("startArray");
|
franta-hg@2
|
137 |
return true;
|
franta-hg@2
|
138 |
}
|
franta-hg@2
|
139 |
|
franta-hg@2
|
140 |
@Override
|
franta-hg@2
|
141 |
public boolean endArray() throws ParseException, IOException {
|
franta-hg@2
|
142 |
// System.err.println("endArray");
|
franta-hg@2
|
143 |
return true;
|
franta-hg@2
|
144 |
}
|
franta-hg@2
|
145 |
|
franta-hg@2
|
146 |
@Override
|
franta-hg@2
|
147 |
public boolean primitive(Object value) throws ParseException, IOException {
|
franta-hg@3
|
148 |
vložText(String.valueOf(value));
|
franta-hg@3
|
149 |
zalomitŘádek = false;
|
franta-hg@2
|
150 |
return true;
|
franta-hg@2
|
151 |
}
|
franta-hg@2
|
152 |
}
|