java/alt2xml/src/cz/frantovo/alt2xml/vstup/JsonSimpleContentHandler.java
changeset 3 6c608fd8c019
parent 2 be5bfbe1f0cd
child 4 e2b2f34cdb50
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/alt2xml/src/cz/frantovo/alt2xml/vstup/JsonSimpleContentHandler.java	Tue Jan 03 12:55:38 2012 +0100
     1.3 @@ -0,0 +1,135 @@
     1.4 +package cz.frantovo.alt2xml.vstup;
     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 +	 * Po textových uzlech vkládáme konce elementů rovnou,
    1.24 +	 * ale pokud jeden element končí hned po jiném, 
    1.25 +	 * vložíme mezi ně ještě konec řádku a odsazení.
    1.26 +	 */
    1.27 +	private boolean zalomitŘádek = false;
    1.28 +
    1.29 +	public JsonSimpleContentHandler(ContentHandler saxVýstup) {
    1.30 +		this.saxVýstup = saxVýstup;
    1.31 +	}
    1.32 +
    1.33 +	private void začniElement(String název) throws IOException {
    1.34 +		try {
    1.35 +			vložOdsazení();
    1.36 +			saxVýstup.startElement(null, null, název, null);
    1.37 +			poloha.push(název);
    1.38 +		} catch (SAXException e) {
    1.39 +			throw new IOException("Chyba při začátku elementu.", e);
    1.40 +		}
    1.41 +	}
    1.42 +
    1.43 +	private void ukončiElement() throws IOException {
    1.44 +		try {
    1.45 +			String značka = poloha.pop();
    1.46 +			if (zalomitŘádek) {
    1.47 +				vložOdsazení();
    1.48 +			}
    1.49 +			zalomitŘádek = true;
    1.50 +			saxVýstup.endElement(null, null, značka);
    1.51 +		} catch (SAXException e) {
    1.52 +			throw new IOException("Chyba při ukončování elementu.", e);
    1.53 +		}
    1.54 +	}
    1.55 +
    1.56 +	private void vložOdsazení() throws IOException {
    1.57 +		/**
    1.58 +		 * TODO: ignorableWhitespace() ?
    1.59 +		 */
    1.60 +		vložText("\n");
    1.61 +		for (int i = 0; i < poloha.size(); i++) {
    1.62 +			vložText("\t");
    1.63 +		}
    1.64 +	}
    1.65 +
    1.66 +	private void vložText(String text) throws IOException {
    1.67 +		try {
    1.68 +			saxVýstup.characters(text.toCharArray(), 0, text.length());
    1.69 +		} catch (SAXException e) {
    1.70 +			throw new IOException("Chyba při vkládání textu.", e);
    1.71 +		}
    1.72 +	}
    1.73 +
    1.74 +	@Override
    1.75 +	public void startJSON() throws ParseException, IOException {
    1.76 +		try {
    1.77 +			saxVýstup.startDocument();
    1.78 +			začniElement("objekt");
    1.79 +		} catch (SAXException e) {
    1.80 +			throw new IOException("Chyba při začátku dokumentu.", e);
    1.81 +		}
    1.82 +	}
    1.83 +
    1.84 +	@Override
    1.85 +	public void endJSON() throws ParseException, IOException {
    1.86 +		try {
    1.87 +			ukončiElement();
    1.88 +			vložText("\n");
    1.89 +			saxVýstup.endDocument();
    1.90 +		} catch (SAXException e) {
    1.91 +			throw new IOException(e);
    1.92 +		}
    1.93 +	}
    1.94 +
    1.95 +	@Override
    1.96 +	public boolean startObject() throws ParseException, IOException {
    1.97 +		// System.err.println("startObject");
    1.98 +		return true;
    1.99 +	}
   1.100 +
   1.101 +	@Override
   1.102 +	public boolean endObject() throws ParseException, IOException {
   1.103 +		// System.err.println("endObject");
   1.104 +		return true;
   1.105 +	}
   1.106 +
   1.107 +	@Override
   1.108 +	public boolean startObjectEntry(String key) throws ParseException, IOException {
   1.109 +		začniElement(key);
   1.110 +		return true;
   1.111 +	}
   1.112 +
   1.113 +	@Override
   1.114 +	public boolean endObjectEntry() throws ParseException, IOException {
   1.115 +		ukončiElement();
   1.116 +		// System.err.println("endObjectEntry");
   1.117 +		return true;
   1.118 +	}
   1.119 +
   1.120 +	@Override
   1.121 +	public boolean startArray() throws ParseException, IOException {
   1.122 +		// System.err.println("startArray");
   1.123 +		return true;
   1.124 +	}
   1.125 +
   1.126 +	@Override
   1.127 +	public boolean endArray() throws ParseException, IOException {
   1.128 +		// System.err.println("endArray");
   1.129 +		return true;
   1.130 +	}
   1.131 +
   1.132 +	@Override
   1.133 +	public boolean primitive(Object value) throws ParseException, IOException {
   1.134 +		vložText(String.valueOf(value));
   1.135 +		zalomitŘádek = false;
   1.136 +		return true;
   1.137 +	}
   1.138 +}