java/alt2xml/src/cz/frantovo/alt2xml/JsonSimpleContentHandler.java
changeset 2 be5bfbe1f0cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/alt2xml/src/cz/frantovo/alt2xml/JsonSimpleContentHandler.java	Mon Jan 02 20:15:52 2012 +0100
     1.3 @@ -0,0 +1,103 @@
     1.4 +package cz.frantovo.alt2xml;
     1.5 +
     1.6 +import java.io.IOException;
     1.7 +import java.util.Stack;
     1.8 +import org.json.simple.parser.ParseException;
     1.9 +import org.xml.sax.ContentHandler;
    1.10 +import org.xml.sax.SAXException;
    1.11 +
    1.12 +/**
    1.13 +) *
    1.14 + * @author fiki
    1.15 + */
    1.16 +public class JsonSimpleContentHandler implements org.json.simple.parser.ContentHandler {
    1.17 +
    1.18 +	/** Sem vypisujeme XML události */
    1.19 +	private ContentHandler saxVýstup;
    1.20 +	/** Musíme si pamatovat polohu v XML stromu, abychom věděli, kterou značku kdy uzavřít */
    1.21 +	private Stack<String> poloha = new Stack<>();
    1.22 +
    1.23 +	public JsonSimpleContentHandler(ContentHandler saxVýstup) {
    1.24 +		this.saxVýstup = saxVýstup;
    1.25 +	}
    1.26 +
    1.27 +	@Override
    1.28 +	public void startJSON() throws ParseException, IOException {
    1.29 +		try {
    1.30 +			saxVýstup.startDocument();
    1.31 +			saxVýstup.startElement(null, null, "json", null);
    1.32 +		} catch (SAXException e) {
    1.33 +			throw new IOException(e);
    1.34 +		}
    1.35 +	}
    1.36 +
    1.37 +	@Override
    1.38 +	public void endJSON() throws ParseException, IOException {
    1.39 +		try {
    1.40 +			saxVýstup.endElement(null, null, "json");
    1.41 +			saxVýstup.endDocument();
    1.42 +		} catch (SAXException e) {
    1.43 +			throw new IOException(e);
    1.44 +		}
    1.45 +	}
    1.46 +
    1.47 +	@Override
    1.48 +	public boolean startObject() throws ParseException, IOException {
    1.49 +		// System.err.println("startObject");
    1.50 +		return true;
    1.51 +	}
    1.52 +
    1.53 +	@Override
    1.54 +	public boolean endObject() throws ParseException, IOException {
    1.55 +		// System.err.println("endObject");
    1.56 +		return true;
    1.57 +	}
    1.58 +
    1.59 +	@Override
    1.60 +	public boolean startObjectEntry(String key) throws ParseException, IOException {
    1.61 +		try {
    1.62 +			saxVýstup.startElement(null, null, key, null);
    1.63 +			poloha.push(key);
    1.64 +		} catch (SAXException e) {
    1.65 +			throw new IOException(e);
    1.66 +		}
    1.67 +		// System.err.println("startObjectEntry: " + key);
    1.68 +		return true;
    1.69 +	}
    1.70 +
    1.71 +	@Override
    1.72 +	public boolean endObjectEntry() throws ParseException, IOException {
    1.73 +		try {
    1.74 +			String značka = poloha.pop();
    1.75 +			saxVýstup.endElement(null, null, značka);
    1.76 +		} catch (SAXException e) {
    1.77 +			throw new IOException(e);
    1.78 +		}
    1.79 +		// System.err.println("endObjectEntry");
    1.80 +		return true;
    1.81 +	}
    1.82 +
    1.83 +	@Override
    1.84 +	public boolean startArray() throws ParseException, IOException {
    1.85 +		// System.err.println("startArray");
    1.86 +		return true;
    1.87 +	}
    1.88 +
    1.89 +	@Override
    1.90 +	public boolean endArray() throws ParseException, IOException {
    1.91 +		// System.err.println("endArray");
    1.92 +		return true;
    1.93 +	}
    1.94 +
    1.95 +	@Override
    1.96 +	public boolean primitive(Object value) throws ParseException, IOException {
    1.97 +		try {
    1.98 +			String hodnota = String.valueOf(value);
    1.99 +			saxVýstup.characters(hodnota.toCharArray(), 0, hodnota.length());
   1.100 +		} catch (SAXException e) {
   1.101 +			throw new IOException(e);
   1.102 +		}
   1.103 +		// System.err.println("primitive: " + value);
   1.104 +		return true;
   1.105 +	}
   1.106 +}