1.1 --- a/java/alt2xml-in-json/src/cz/frantovo/alt2xml/vstup/JsonSimpleContentHandler.java Thu Jun 05 23:41:05 2014 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,152 +0,0 @@
1.4 -/**
1.5 - * Alt2XML
1.6 - * Copyright © 2014 František Kučera (frantovo.cz)
1.7 - *
1.8 - * This program is free software: you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License as published by
1.10 - * the Free Software Foundation, either version 3 of the License, or
1.11 - * (at your option) any later version.
1.12 - *
1.13 - * This program is distributed in the hope that it will be useful,
1.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 - * GNU General Public License for more details.
1.17 - *
1.18 - * You should have received a copy of the GNU General Public License
1.19 - * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 - */
1.21 -package cz.frantovo.alt2xml.vstup;
1.22 -
1.23 -import java.io.IOException;
1.24 -import java.util.Stack;
1.25 -import org.json.simple.parser.ParseException;
1.26 -import org.xml.sax.ContentHandler;
1.27 -import org.xml.sax.SAXException;
1.28 -
1.29 -/**
1.30 - *
1.31 - * @author fiki
1.32 - */
1.33 -public class JsonSimpleContentHandler implements org.json.simple.parser.ContentHandler {
1.34 -
1.35 - /** Sem vypisujeme XML události */
1.36 - private ContentHandler saxVýstup;
1.37 - /** Musíme si pamatovat polohu v XML stromu, abychom věděli, kterou značku kdy uzavřít */
1.38 - private Stack<String> poloha = new Stack<>();
1.39 - /**
1.40 - * Po textových uzlech vkládáme konce elementů rovnou,
1.41 - * ale pokud jeden element končí hned po jiném,
1.42 - * vložíme mezi ně ještě konec řádku a odsazení.
1.43 - */
1.44 - private boolean zalomitŘádek = false;
1.45 -
1.46 - public JsonSimpleContentHandler(ContentHandler saxVýstup) {
1.47 - this.saxVýstup = saxVýstup;
1.48 - }
1.49 -
1.50 - private void začniElement(String název) throws IOException {
1.51 - try {
1.52 - vložOdsazení();
1.53 - saxVýstup.startElement(null, null, název, null);
1.54 - poloha.push(název);
1.55 - } catch (SAXException e) {
1.56 - throw new IOException("Chyba při začátku elementu.", e);
1.57 - }
1.58 - }
1.59 -
1.60 - private void ukončiElement() throws IOException {
1.61 - try {
1.62 - String značka = poloha.pop();
1.63 - if (zalomitŘádek) {
1.64 - vložOdsazení();
1.65 - }
1.66 - zalomitŘádek = true;
1.67 - saxVýstup.endElement(null, null, značka);
1.68 - } catch (SAXException e) {
1.69 - throw new IOException("Chyba při ukončování elementu.", e);
1.70 - }
1.71 - }
1.72 -
1.73 - private void vložOdsazení() throws IOException {
1.74 - /**
1.75 - * TODO: ignorableWhitespace() ?
1.76 - */
1.77 - vložText("\n");
1.78 - for (int i = 0; i < poloha.size(); i++) {
1.79 - vložText("\t");
1.80 - }
1.81 - }
1.82 -
1.83 - private void vložText(String text) throws IOException {
1.84 - try {
1.85 - saxVýstup.characters(text.toCharArray(), 0, text.length());
1.86 - } catch (SAXException e) {
1.87 - throw new IOException("Chyba při vkládání textu.", e);
1.88 - }
1.89 - }
1.90 -
1.91 - @Override
1.92 - public void startJSON() throws ParseException, IOException {
1.93 - try {
1.94 - saxVýstup.startDocument();
1.95 - začniElement("objekt");
1.96 - } catch (SAXException e) {
1.97 - throw new IOException("Chyba při začátku dokumentu.", e);
1.98 - }
1.99 - }
1.100 -
1.101 - @Override
1.102 - public void endJSON() throws ParseException, IOException {
1.103 - try {
1.104 - ukončiElement();
1.105 - vložText("\n");
1.106 - saxVýstup.endDocument();
1.107 - } catch (SAXException e) {
1.108 - throw new IOException(e);
1.109 - }
1.110 - }
1.111 -
1.112 - @Override
1.113 - public boolean startObject() throws ParseException, IOException {
1.114 - // System.err.println("startObject");
1.115 - return true;
1.116 - }
1.117 -
1.118 - @Override
1.119 - public boolean endObject() throws ParseException, IOException {
1.120 - // System.err.println("endObject");
1.121 - return true;
1.122 - }
1.123 -
1.124 - @Override
1.125 - public boolean startObjectEntry(String key) throws ParseException, IOException {
1.126 - začniElement(key);
1.127 - return true;
1.128 - }
1.129 -
1.130 - @Override
1.131 - public boolean endObjectEntry() throws ParseException, IOException {
1.132 - ukončiElement();
1.133 - // System.err.println("endObjectEntry");
1.134 - return true;
1.135 - }
1.136 -
1.137 - @Override
1.138 - public boolean startArray() throws ParseException, IOException {
1.139 - // System.err.println("startArray");
1.140 - return true;
1.141 - }
1.142 -
1.143 - @Override
1.144 - public boolean endArray() throws ParseException, IOException {
1.145 - // System.err.println("endArray");
1.146 - return true;
1.147 - }
1.148 -
1.149 - @Override
1.150 - public boolean primitive(Object value) throws ParseException, IOException {
1.151 - vložText(String.valueOf(value));
1.152 - zalomitŘádek = false;
1.153 - return true;
1.154 - }
1.155 -}