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