franta-hg@2: package cz.frantovo.alt2xml; franta-hg@2: franta-hg@2: import java.io.IOException; franta-hg@2: import java.util.Stack; franta-hg@2: import org.json.simple.parser.ParseException; franta-hg@2: import org.xml.sax.ContentHandler; franta-hg@2: import org.xml.sax.SAXException; franta-hg@2: franta-hg@2: /** franta-hg@2: ) * franta-hg@2: * @author fiki franta-hg@2: */ franta-hg@2: public class JsonSimpleContentHandler implements org.json.simple.parser.ContentHandler { franta-hg@2: franta-hg@2: /** Sem vypisujeme XML události */ franta-hg@2: private ContentHandler saxVýstup; franta-hg@2: /** Musíme si pamatovat polohu v XML stromu, abychom věděli, kterou značku kdy uzavřít */ franta-hg@2: private Stack poloha = new Stack<>(); franta-hg@2: franta-hg@2: public JsonSimpleContentHandler(ContentHandler saxVýstup) { franta-hg@2: this.saxVýstup = saxVýstup; franta-hg@2: } franta-hg@2: franta-hg@2: @Override franta-hg@2: public void startJSON() throws ParseException, IOException { franta-hg@2: try { franta-hg@2: saxVýstup.startDocument(); franta-hg@2: saxVýstup.startElement(null, null, "json", null); franta-hg@2: } catch (SAXException e) { franta-hg@2: throw new IOException(e); franta-hg@2: } franta-hg@2: } franta-hg@2: franta-hg@2: @Override franta-hg@2: public void endJSON() throws ParseException, IOException { franta-hg@2: try { franta-hg@2: saxVýstup.endElement(null, null, "json"); franta-hg@2: saxVýstup.endDocument(); franta-hg@2: } catch (SAXException e) { franta-hg@2: throw new IOException(e); franta-hg@2: } franta-hg@2: } franta-hg@2: franta-hg@2: @Override franta-hg@2: public boolean startObject() throws ParseException, IOException { franta-hg@2: // System.err.println("startObject"); franta-hg@2: return true; franta-hg@2: } franta-hg@2: franta-hg@2: @Override franta-hg@2: public boolean endObject() throws ParseException, IOException { franta-hg@2: // System.err.println("endObject"); franta-hg@2: return true; franta-hg@2: } franta-hg@2: franta-hg@2: @Override franta-hg@2: public boolean startObjectEntry(String key) throws ParseException, IOException { franta-hg@2: try { franta-hg@2: saxVýstup.startElement(null, null, key, null); franta-hg@2: poloha.push(key); franta-hg@2: } catch (SAXException e) { franta-hg@2: throw new IOException(e); franta-hg@2: } franta-hg@2: // System.err.println("startObjectEntry: " + key); franta-hg@2: return true; franta-hg@2: } franta-hg@2: franta-hg@2: @Override franta-hg@2: public boolean endObjectEntry() throws ParseException, IOException { franta-hg@2: try { franta-hg@2: String značka = poloha.pop(); franta-hg@2: saxVýstup.endElement(null, null, značka); franta-hg@2: } catch (SAXException e) { franta-hg@2: throw new IOException(e); franta-hg@2: } franta-hg@2: // System.err.println("endObjectEntry"); franta-hg@2: return true; franta-hg@2: } franta-hg@2: franta-hg@2: @Override franta-hg@2: public boolean startArray() throws ParseException, IOException { franta-hg@2: // System.err.println("startArray"); franta-hg@2: return true; franta-hg@2: } franta-hg@2: franta-hg@2: @Override franta-hg@2: public boolean endArray() throws ParseException, IOException { franta-hg@2: // System.err.println("endArray"); franta-hg@2: return true; franta-hg@2: } franta-hg@2: franta-hg@2: @Override franta-hg@2: public boolean primitive(Object value) throws ParseException, IOException { franta-hg@2: try { franta-hg@2: String hodnota = String.valueOf(value); franta-hg@2: saxVýstup.characters(hodnota.toCharArray(), 0, hodnota.length()); franta-hg@2: } catch (SAXException e) { franta-hg@2: throw new IOException(e); franta-hg@2: } franta-hg@2: // System.err.println("primitive: " + value); franta-hg@2: return true; franta-hg@2: } franta-hg@2: }