java/alt2xml/src/cz/frantovo/alt2xml/JsonSimpleContentHandler.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 02 Jan 2012 20:15:52 +0100
changeset 2 be5bfbe1f0cd
permissions -rw-r--r--
První nástřel – trochu už to funguje, převádí JSON na XML.
franta-hg@2
     1
package cz.frantovo.alt2xml;
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@2
    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@2
    19
franta-hg@2
    20
	public JsonSimpleContentHandler(ContentHandler saxVýstup) {
franta-hg@2
    21
		this.saxVýstup = saxVýstup;
franta-hg@2
    22
	}
franta-hg@2
    23
franta-hg@2
    24
	@Override
franta-hg@2
    25
	public void startJSON() throws ParseException, IOException {
franta-hg@2
    26
		try {
franta-hg@2
    27
			saxVýstup.startDocument();
franta-hg@2
    28
			saxVýstup.startElement(null, null, "json", null);
franta-hg@2
    29
		} catch (SAXException e) {
franta-hg@2
    30
			throw new IOException(e);
franta-hg@2
    31
		}
franta-hg@2
    32
	}
franta-hg@2
    33
franta-hg@2
    34
	@Override
franta-hg@2
    35
	public void endJSON() throws ParseException, IOException {
franta-hg@2
    36
		try {
franta-hg@2
    37
			saxVýstup.endElement(null, null, "json");
franta-hg@2
    38
			saxVýstup.endDocument();
franta-hg@2
    39
		} catch (SAXException e) {
franta-hg@2
    40
			throw new IOException(e);
franta-hg@2
    41
		}
franta-hg@2
    42
	}
franta-hg@2
    43
franta-hg@2
    44
	@Override
franta-hg@2
    45
	public boolean startObject() throws ParseException, IOException {
franta-hg@2
    46
		// System.err.println("startObject");
franta-hg@2
    47
		return true;
franta-hg@2
    48
	}
franta-hg@2
    49
franta-hg@2
    50
	@Override
franta-hg@2
    51
	public boolean endObject() throws ParseException, IOException {
franta-hg@2
    52
		// System.err.println("endObject");
franta-hg@2
    53
		return true;
franta-hg@2
    54
	}
franta-hg@2
    55
franta-hg@2
    56
	@Override
franta-hg@2
    57
	public boolean startObjectEntry(String key) throws ParseException, IOException {
franta-hg@2
    58
		try {
franta-hg@2
    59
			saxVýstup.startElement(null, null, key, null);
franta-hg@2
    60
			poloha.push(key);
franta-hg@2
    61
		} catch (SAXException e) {
franta-hg@2
    62
			throw new IOException(e);
franta-hg@2
    63
		}
franta-hg@2
    64
		// System.err.println("startObjectEntry: " + key);
franta-hg@2
    65
		return true;
franta-hg@2
    66
	}
franta-hg@2
    67
franta-hg@2
    68
	@Override
franta-hg@2
    69
	public boolean endObjectEntry() throws ParseException, IOException {
franta-hg@2
    70
		try {
franta-hg@2
    71
			String značka = poloha.pop();
franta-hg@2
    72
			saxVýstup.endElement(null, null, značka);
franta-hg@2
    73
		} catch (SAXException e) {
franta-hg@2
    74
			throw new IOException(e);
franta-hg@2
    75
		}
franta-hg@2
    76
		// System.err.println("endObjectEntry");
franta-hg@2
    77
		return true;
franta-hg@2
    78
	}
franta-hg@2
    79
franta-hg@2
    80
	@Override
franta-hg@2
    81
	public boolean startArray() throws ParseException, IOException {
franta-hg@2
    82
		// System.err.println("startArray");
franta-hg@2
    83
		return true;
franta-hg@2
    84
	}
franta-hg@2
    85
franta-hg@2
    86
	@Override
franta-hg@2
    87
	public boolean endArray() throws ParseException, IOException {
franta-hg@2
    88
		// System.err.println("endArray");
franta-hg@2
    89
		return true;
franta-hg@2
    90
	}
franta-hg@2
    91
franta-hg@2
    92
	@Override
franta-hg@2
    93
	public boolean primitive(Object value) throws ParseException, IOException {
franta-hg@2
    94
		try {
franta-hg@2
    95
			String hodnota = String.valueOf(value);
franta-hg@2
    96
			saxVýstup.characters(hodnota.toCharArray(), 0, hodnota.length());
franta-hg@2
    97
		} catch (SAXException e) {
franta-hg@2
    98
			throw new IOException(e);
franta-hg@2
    99
		}
franta-hg@2
   100
		// System.err.println("primitive: " + value);
franta-hg@2
   101
		return true;
franta-hg@2
   102
	}
franta-hg@2
   103
}