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.
     1 package cz.frantovo.alt2xml;
     2 
     3 import java.io.IOException;
     4 import java.util.Stack;
     5 import org.json.simple.parser.ParseException;
     6 import org.xml.sax.ContentHandler;
     7 import org.xml.sax.SAXException;
     8 
     9 /**
    10 ) *
    11  * @author fiki
    12  */
    13 public class JsonSimpleContentHandler implements org.json.simple.parser.ContentHandler {
    14 
    15 	/** Sem vypisujeme XML události */
    16 	private ContentHandler saxVýstup;
    17 	/** Musíme si pamatovat polohu v XML stromu, abychom věděli, kterou značku kdy uzavřít */
    18 	private Stack<String> poloha = new Stack<>();
    19 
    20 	public JsonSimpleContentHandler(ContentHandler saxVýstup) {
    21 		this.saxVýstup = saxVýstup;
    22 	}
    23 
    24 	@Override
    25 	public void startJSON() throws ParseException, IOException {
    26 		try {
    27 			saxVýstup.startDocument();
    28 			saxVýstup.startElement(null, null, "json", null);
    29 		} catch (SAXException e) {
    30 			throw new IOException(e);
    31 		}
    32 	}
    33 
    34 	@Override
    35 	public void endJSON() throws ParseException, IOException {
    36 		try {
    37 			saxVýstup.endElement(null, null, "json");
    38 			saxVýstup.endDocument();
    39 		} catch (SAXException e) {
    40 			throw new IOException(e);
    41 		}
    42 	}
    43 
    44 	@Override
    45 	public boolean startObject() throws ParseException, IOException {
    46 		// System.err.println("startObject");
    47 		return true;
    48 	}
    49 
    50 	@Override
    51 	public boolean endObject() throws ParseException, IOException {
    52 		// System.err.println("endObject");
    53 		return true;
    54 	}
    55 
    56 	@Override
    57 	public boolean startObjectEntry(String key) throws ParseException, IOException {
    58 		try {
    59 			saxVýstup.startElement(null, null, key, null);
    60 			poloha.push(key);
    61 		} catch (SAXException e) {
    62 			throw new IOException(e);
    63 		}
    64 		// System.err.println("startObjectEntry: " + key);
    65 		return true;
    66 	}
    67 
    68 	@Override
    69 	public boolean endObjectEntry() throws ParseException, IOException {
    70 		try {
    71 			String značka = poloha.pop();
    72 			saxVýstup.endElement(null, null, značka);
    73 		} catch (SAXException e) {
    74 			throw new IOException(e);
    75 		}
    76 		// System.err.println("endObjectEntry");
    77 		return true;
    78 	}
    79 
    80 	@Override
    81 	public boolean startArray() throws ParseException, IOException {
    82 		// System.err.println("startArray");
    83 		return true;
    84 	}
    85 
    86 	@Override
    87 	public boolean endArray() throws ParseException, IOException {
    88 		// System.err.println("endArray");
    89 		return true;
    90 	}
    91 
    92 	@Override
    93 	public boolean primitive(Object value) throws ParseException, IOException {
    94 		try {
    95 			String hodnota = String.valueOf(value);
    96 			saxVýstup.characters(hodnota.toCharArray(), 0, hodnota.length());
    97 		} catch (SAXException e) {
    98 			throw new IOException(e);
    99 		}
   100 		// System.err.println("primitive: " + value);
   101 		return true;
   102 	}
   103 }