Přehlednější struktura tříd/balíčků.
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 03 Jan 2012 12:55:38 +0100
changeset 36c608fd8c019
parent 2 be5bfbe1f0cd
child 4 e2b2f34cdb50
Přehlednější struktura tříd/balíčků.
java/alt2xml/src/cz/frantovo/alt2xml/AltParser.java
java/alt2xml/src/cz/frantovo/alt2xml/CLI.java
java/alt2xml/src/cz/frantovo/alt2xml/EchoContentHandler.java
java/alt2xml/src/cz/frantovo/alt2xml/JsonSimpleContentHandler.java
java/alt2xml/src/cz/frantovo/alt2xml/SAXTovarna.java
java/alt2xml/src/cz/frantovo/alt2xml/SuperReader.java
java/alt2xml/src/cz/frantovo/alt2xml/vstup/JsonSimpleContentHandler.java
java/alt2xml/src/cz/frantovo/alt2xml/vstup/SuperReader.java
java/alt2xml/src/cz/frantovo/alt2xml/výstup/EchoContentHandler.java
     1.1 --- a/java/alt2xml/src/cz/frantovo/alt2xml/AltParser.java	Mon Jan 02 20:15:52 2012 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,59 +0,0 @@
     1.4 -package cz.frantovo.alt2xml;
     1.5 -
     1.6 -import java.util.logging.Level;
     1.7 -import java.util.logging.Logger;
     1.8 -import javax.xml.parsers.SAXParser;
     1.9 -import org.xml.sax.Parser;
    1.10 -import org.xml.sax.SAXException;
    1.11 -import org.xml.sax.SAXNotRecognizedException;
    1.12 -import org.xml.sax.SAXNotSupportedException;
    1.13 -import org.xml.sax.XMLReader;
    1.14 -
    1.15 -/**
    1.16 - *
    1.17 - * @author fiki
    1.18 - */
    1.19 -public class AltParser extends SAXParser {
    1.20 -
    1.21 -	private static final Logger log = Logger.getLogger(AltParser.class.getName());
    1.22 -	private XMLReader superReader = new SuperReader();
    1.23 -
    1.24 -	@Override
    1.25 -	public Parser getParser() throws SAXException {
    1.26 -		// TODO: dopsat
    1.27 -		log.log(Level.FINE, "getParser");
    1.28 -		return null;
    1.29 -	}
    1.30 -
    1.31 -	@Override
    1.32 -	public XMLReader getXMLReader() throws SAXException {
    1.33 -		return superReader;
    1.34 -	}
    1.35 -
    1.36 -	@Override
    1.37 -	public boolean isNamespaceAware() {
    1.38 -		// TODO: dopsat
    1.39 -		log.log(Level.FINE, "isNamespaceAware");
    1.40 -		return false;
    1.41 -	}
    1.42 -
    1.43 -	@Override
    1.44 -	public boolean isValidating() {
    1.45 -		// TODO: dopsat
    1.46 -		log.log(Level.FINE, "isValidating");
    1.47 -		return false;
    1.48 -	}
    1.49 -
    1.50 -	@Override
    1.51 -	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
    1.52 -		// TODO: dopsat
    1.53 -		log.log(Level.FINE, "setProperty: {0} = {1}", new Object[]{name, value});
    1.54 -	}
    1.55 -
    1.56 -	@Override
    1.57 -	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    1.58 -		// TODO: dopsat
    1.59 -		log.log(Level.FINE, "getProperty: {0}", name);
    1.60 -		return null;
    1.61 -	}
    1.62 -}
     2.1 --- a/java/alt2xml/src/cz/frantovo/alt2xml/CLI.java	Mon Jan 02 20:15:52 2012 +0100
     2.2 +++ b/java/alt2xml/src/cz/frantovo/alt2xml/CLI.java	Tue Jan 03 12:55:38 2012 +0100
     2.3 @@ -1,5 +1,6 @@
     2.4  package cz.frantovo.alt2xml;
     2.5  
     2.6 +import cz.frantovo.alt2xml.výstup.EchoContentHandler;
     2.7  import java.io.InputStream;
     2.8  import java.io.OutputStream;
     2.9  import javax.xml.parsers.SAXParser;
    2.10 @@ -15,16 +16,27 @@
    2.11  public class CLI {
    2.12  
    2.13  	public static void main(String[] args) throws Exception {
    2.14 +		/**
    2.15 +		 * Použijeme standardní vstup a výstup:
    2.16 +		 */
    2.17  		InputStream vstup = System.in;
    2.18  		OutputStream výstup = System.out;
    2.19 -
    2.20 -		SAXParserFactory t = SAXParserFactory.newInstance(SAXTovarna.class.getName(), null);
    2.21 -		SAXParser p = t.newSAXParser();
    2.22 -
    2.23 +		
    2.24 +		/**
    2.25 +		 * Serializujeme data do XML.
    2.26 +		 * To normálně vůbec není potřeba – data se do tvaru proudu obsahujícího ostré závorky
    2.27 +		 * vůbec nedostanou – zpracováváme události (volání javovských metod – začátky a konce elementů atd.)
    2.28 +		 * a z nich např. deserializujeme nějaké naše objekty, provádíme nějaké akce, nebo třeba stavíme DOM.
    2.29 +		 */
    2.30  		XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
    2.31  		XMLStreamWriter w = xmlOutputFactory.createXMLStreamWriter(výstup);
    2.32  		DefaultHandler h = new EchoContentHandler(w);
    2.33 -
    2.34 +		
    2.35 +		/**
    2.36 +		 * Parsujeme JSON a děláme z něj XML:
    2.37 +		 */
    2.38 +		SAXParserFactory t = SAXParserFactory.newInstance(SAXTovarna.class.getName(), null);
    2.39 +		SAXParser p = t.newSAXParser();
    2.40  		p.parse(vstup, h);
    2.41  	}
    2.42  }
     3.1 --- a/java/alt2xml/src/cz/frantovo/alt2xml/EchoContentHandler.java	Mon Jan 02 20:15:52 2012 +0100
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,83 +0,0 @@
     3.4 -package cz.frantovo.alt2xml;
     3.5 -
     3.6 -import javax.xml.stream.XMLStreamException;
     3.7 -import javax.xml.stream.XMLStreamWriter;
     3.8 -import org.xml.sax.Attributes;
     3.9 -import org.xml.sax.SAXException;
    3.10 -import org.xml.sax.helpers.DefaultHandler;
    3.11 -
    3.12 -/**
    3.13 - * Slouží k převodu právě parsovaného XML zpět na XML.
    3.14 - * Určen pro testování a ladění a pro použití s neobvyklými „XML“ parsery,
    3.15 - * které nečtou XML ale jiný jazyk (např. JSON, INI atd.), ale používají stejné rozhraní (SAX).
    3.16 - * 
    3.17 - * TODO: další typy uzlů a jmenné prostory.
    3.18 - * @author fiki
    3.19 - */
    3.20 -public class EchoContentHandler extends DefaultHandler {
    3.21 -
    3.22 -	private XMLStreamWriter w;
    3.23 -
    3.24 -	/**
    3.25 -	 * @param writer kam se bude vypisovat XML.
    3.26 -	 */
    3.27 -	public EchoContentHandler(XMLStreamWriter writer) {
    3.28 -		w = writer;
    3.29 -	}
    3.30 -
    3.31 -	@Override
    3.32 -	public void startDocument() throws SAXException {
    3.33 -		try {
    3.34 -			w.writeStartDocument();
    3.35 -		} catch (XMLStreamException e) {
    3.36 -			throw new SAXException(e);
    3.37 -		}
    3.38 -	}
    3.39 -
    3.40 -	@Override
    3.41 -	public void endDocument() throws SAXException {
    3.42 -		try {
    3.43 -			w.writeEndDocument();
    3.44 -			w.close();
    3.45 -		} catch (XMLStreamException e) {
    3.46 -			throw new SAXException(e);
    3.47 -		}
    3.48 -	}
    3.49 -
    3.50 -	@Override
    3.51 -	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    3.52 -		try {
    3.53 -			w.writeStartElement(qName);
    3.54 -
    3.55 -			if (attributes != null) {
    3.56 -				for (int i = 0; i < attributes.getLength(); i++) {
    3.57 -					w.writeAttribute(attributes.getQName(i), attributes.getValue(i));
    3.58 -				}
    3.59 -			}
    3.60 -
    3.61 -			w.flush();
    3.62 -		} catch (XMLStreamException e) {
    3.63 -			throw new SAXException(e);
    3.64 -		}
    3.65 -	}
    3.66 -
    3.67 -	@Override
    3.68 -	public void endElement(String uri, String localName, String qName) throws SAXException {
    3.69 -		try {
    3.70 -			w.writeEndElement();
    3.71 -			w.flush();
    3.72 -		} catch (XMLStreamException e) {
    3.73 -			throw new SAXException(e);
    3.74 -		}
    3.75 -	}
    3.76 -
    3.77 -	@Override
    3.78 -	public void characters(char[] ch, int start, int length) throws SAXException {
    3.79 -		try {
    3.80 -			w.writeCharacters(ch, start, length);
    3.81 -			w.flush();
    3.82 -		} catch (XMLStreamException e) {
    3.83 -			throw new SAXException(e);
    3.84 -		}
    3.85 -	}
    3.86 -}
     4.1 --- a/java/alt2xml/src/cz/frantovo/alt2xml/JsonSimpleContentHandler.java	Mon Jan 02 20:15:52 2012 +0100
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,103 +0,0 @@
     4.4 -package cz.frantovo.alt2xml;
     4.5 -
     4.6 -import java.io.IOException;
     4.7 -import java.util.Stack;
     4.8 -import org.json.simple.parser.ParseException;
     4.9 -import org.xml.sax.ContentHandler;
    4.10 -import org.xml.sax.SAXException;
    4.11 -
    4.12 -/**
    4.13 -) *
    4.14 - * @author fiki
    4.15 - */
    4.16 -public class JsonSimpleContentHandler implements org.json.simple.parser.ContentHandler {
    4.17 -
    4.18 -	/** Sem vypisujeme XML události */
    4.19 -	private ContentHandler saxVýstup;
    4.20 -	/** Musíme si pamatovat polohu v XML stromu, abychom věděli, kterou značku kdy uzavřít */
    4.21 -	private Stack<String> poloha = new Stack<>();
    4.22 -
    4.23 -	public JsonSimpleContentHandler(ContentHandler saxVýstup) {
    4.24 -		this.saxVýstup = saxVýstup;
    4.25 -	}
    4.26 -
    4.27 -	@Override
    4.28 -	public void startJSON() throws ParseException, IOException {
    4.29 -		try {
    4.30 -			saxVýstup.startDocument();
    4.31 -			saxVýstup.startElement(null, null, "json", null);
    4.32 -		} catch (SAXException e) {
    4.33 -			throw new IOException(e);
    4.34 -		}
    4.35 -	}
    4.36 -
    4.37 -	@Override
    4.38 -	public void endJSON() throws ParseException, IOException {
    4.39 -		try {
    4.40 -			saxVýstup.endElement(null, null, "json");
    4.41 -			saxVýstup.endDocument();
    4.42 -		} catch (SAXException e) {
    4.43 -			throw new IOException(e);
    4.44 -		}
    4.45 -	}
    4.46 -
    4.47 -	@Override
    4.48 -	public boolean startObject() throws ParseException, IOException {
    4.49 -		// System.err.println("startObject");
    4.50 -		return true;
    4.51 -	}
    4.52 -
    4.53 -	@Override
    4.54 -	public boolean endObject() throws ParseException, IOException {
    4.55 -		// System.err.println("endObject");
    4.56 -		return true;
    4.57 -	}
    4.58 -
    4.59 -	@Override
    4.60 -	public boolean startObjectEntry(String key) throws ParseException, IOException {
    4.61 -		try {
    4.62 -			saxVýstup.startElement(null, null, key, null);
    4.63 -			poloha.push(key);
    4.64 -		} catch (SAXException e) {
    4.65 -			throw new IOException(e);
    4.66 -		}
    4.67 -		// System.err.println("startObjectEntry: " + key);
    4.68 -		return true;
    4.69 -	}
    4.70 -
    4.71 -	@Override
    4.72 -	public boolean endObjectEntry() throws ParseException, IOException {
    4.73 -		try {
    4.74 -			String značka = poloha.pop();
    4.75 -			saxVýstup.endElement(null, null, značka);
    4.76 -		} catch (SAXException e) {
    4.77 -			throw new IOException(e);
    4.78 -		}
    4.79 -		// System.err.println("endObjectEntry");
    4.80 -		return true;
    4.81 -	}
    4.82 -
    4.83 -	@Override
    4.84 -	public boolean startArray() throws ParseException, IOException {
    4.85 -		// System.err.println("startArray");
    4.86 -		return true;
    4.87 -	}
    4.88 -
    4.89 -	@Override
    4.90 -	public boolean endArray() throws ParseException, IOException {
    4.91 -		// System.err.println("endArray");
    4.92 -		return true;
    4.93 -	}
    4.94 -
    4.95 -	@Override
    4.96 -	public boolean primitive(Object value) throws ParseException, IOException {
    4.97 -		try {
    4.98 -			String hodnota = String.valueOf(value);
    4.99 -			saxVýstup.characters(hodnota.toCharArray(), 0, hodnota.length());
   4.100 -		} catch (SAXException e) {
   4.101 -			throw new IOException(e);
   4.102 -		}
   4.103 -		// System.err.println("primitive: " + value);
   4.104 -		return true;
   4.105 -	}
   4.106 -}
     5.1 --- a/java/alt2xml/src/cz/frantovo/alt2xml/SAXTovarna.java	Mon Jan 02 20:15:52 2012 +0100
     5.2 +++ b/java/alt2xml/src/cz/frantovo/alt2xml/SAXTovarna.java	Tue Jan 03 12:55:38 2012 +0100
     5.3 @@ -1,11 +1,16 @@
     5.4  package cz.frantovo.alt2xml;
     5.5  
     5.6 +import cz.frantovo.alt2xml.vstup.SuperReader;
     5.7 +import java.util.logging.Level;
     5.8 +import java.util.logging.Logger;
     5.9  import javax.xml.parsers.ParserConfigurationException;
    5.10  import javax.xml.parsers.SAXParser;
    5.11  import javax.xml.parsers.SAXParserFactory;
    5.12 +import org.xml.sax.Parser;
    5.13  import org.xml.sax.SAXException;
    5.14  import org.xml.sax.SAXNotRecognizedException;
    5.15  import org.xml.sax.SAXNotSupportedException;
    5.16 +import org.xml.sax.XMLReader;
    5.17  
    5.18  /**
    5.19   *
    5.20 @@ -15,7 +20,7 @@
    5.21  
    5.22  	@Override
    5.23  	public SAXParser newSAXParser() throws ParserConfigurationException, SAXException {
    5.24 -		return new AltParser();
    5.25 +		return new MůjParser(new SuperReader());
    5.26  	}
    5.27  
    5.28  	@Override
    5.29 @@ -27,5 +32,53 @@
    5.30  	public boolean getFeature(String name) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
    5.31  		throw new UnsupportedOperationException("Not supported yet.");
    5.32  	}
    5.33 -	
    5.34 -}
    5.35 +
    5.36 +	private static class MůjParser extends SAXParser {
    5.37 +
    5.38 +		private static final Logger log = Logger.getLogger(MůjParser.class.getName());
    5.39 +		private XMLReader xmlReader;
    5.40 +
    5.41 +		public MůjParser(XMLReader xmlReader) {
    5.42 +			this.xmlReader = xmlReader;
    5.43 +		}
    5.44 +
    5.45 +		@Override
    5.46 +		public Parser getParser() throws SAXException {
    5.47 +			// TODO: dopsat
    5.48 +			log.log(Level.FINE, "getParser");
    5.49 +			return null;
    5.50 +		}
    5.51 +
    5.52 +		@Override
    5.53 +		public XMLReader getXMLReader() throws SAXException {
    5.54 +			return xmlReader;
    5.55 +		}
    5.56 +
    5.57 +		@Override
    5.58 +		public boolean isNamespaceAware() {
    5.59 +			// TODO: dopsat
    5.60 +			log.log(Level.FINE, "isNamespaceAware");
    5.61 +			return false;
    5.62 +		}
    5.63 +
    5.64 +		@Override
    5.65 +		public boolean isValidating() {
    5.66 +			// TODO: dopsat
    5.67 +			log.log(Level.FINE, "isValidating");
    5.68 +			return false;
    5.69 +		}
    5.70 +
    5.71 +		@Override
    5.72 +		public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
    5.73 +			// TODO: dopsat
    5.74 +			log.log(Level.FINE, "setProperty: {0} = {1}", new Object[]{name, value});
    5.75 +		}
    5.76 +
    5.77 +		@Override
    5.78 +		public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    5.79 +			// TODO: dopsat
    5.80 +			log.log(Level.FINE, "getProperty: {0}", name);
    5.81 +			return null;
    5.82 +		}
    5.83 +	}
    5.84 +}
    5.85 \ No newline at end of file
     6.1 --- a/java/alt2xml/src/cz/frantovo/alt2xml/SuperReader.java	Mon Jan 02 20:15:52 2012 +0100
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,128 +0,0 @@
     6.4 -package cz.frantovo.alt2xml;
     6.5 -
     6.6 -import java.io.BufferedReader;
     6.7 -import java.io.IOException;
     6.8 -import java.io.InputStreamReader;
     6.9 -import java.util.logging.Level;
    6.10 -import java.util.logging.Logger;
    6.11 -import org.json.simple.parser.JSONParser;
    6.12 -import org.json.simple.parser.ParseException;
    6.13 -import org.xml.sax.ContentHandler;
    6.14 -import org.xml.sax.DTDHandler;
    6.15 -import org.xml.sax.EntityResolver;
    6.16 -import org.xml.sax.ErrorHandler;
    6.17 -import org.xml.sax.InputSource;
    6.18 -import org.xml.sax.SAXException;
    6.19 -import org.xml.sax.SAXNotRecognizedException;
    6.20 -import org.xml.sax.SAXNotSupportedException;
    6.21 -import org.xml.sax.XMLReader;
    6.22 -
    6.23 -/**
    6.24 - *
    6.25 - * @author fiki
    6.26 - */
    6.27 -public class SuperReader implements XMLReader {
    6.28 -
    6.29 -	private static final Logger log = Logger.getLogger(SuperReader.class.getName());
    6.30 -	private ContentHandler contentHandler;
    6.31 -
    6.32 -	@Override
    6.33 -	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    6.34 -		// TODO: dopsat
    6.35 -		log.log(Level.FINE, "getFeature: {0}", name);
    6.36 -		return false;
    6.37 -	}
    6.38 -
    6.39 -	@Override
    6.40 -	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    6.41 -		// TODO: dopsat
    6.42 -		log.log(Level.FINE, "setFeature: {0} = {1}", new Object[]{name, value});
    6.43 -	}
    6.44 -
    6.45 -	@Override
    6.46 -	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    6.47 -		// TODO: dopsat
    6.48 -		log.log(Level.FINE, "getProperty: {0}", name);
    6.49 -		return null;
    6.50 -	}
    6.51 -
    6.52 -	@Override
    6.53 -	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
    6.54 -		// TODO: dopsat
    6.55 -		log.log(Level.FINE, "setProperty: {0} = {1}", new Object[]{name, value});
    6.56 -	}
    6.57 -
    6.58 -	@Override
    6.59 -	public void setEntityResolver(EntityResolver resolver) {
    6.60 -		// TODO: dopsat
    6.61 -		log.log(Level.FINE, "setEntityResolver: {0}", resolver);
    6.62 -	}
    6.63 -
    6.64 -	@Override
    6.65 -	public EntityResolver getEntityResolver() {
    6.66 -		// TODO: dopsat
    6.67 -		log.log(Level.FINE, "getEntityResolver");
    6.68 -		return null;
    6.69 -	}
    6.70 -
    6.71 -	@Override
    6.72 -	public void setDTDHandler(DTDHandler handler) {
    6.73 -		// TODO: dopsat
    6.74 -		log.log(Level.FINE, "setDTDHandler: {0}", handler);
    6.75 -	}
    6.76 -
    6.77 -	@Override
    6.78 -	public DTDHandler getDTDHandler() {
    6.79 -		// TODO: dopsat
    6.80 -		log.log(Level.FINE, "getDTDHandler");
    6.81 -		return null;
    6.82 -	}
    6.83 -
    6.84 -	@Override
    6.85 -	public void setContentHandler(ContentHandler handler) {
    6.86 -		// TODO: dopsat
    6.87 -		contentHandler = handler;
    6.88 -	}
    6.89 -
    6.90 -	@Override
    6.91 -	public ContentHandler getContentHandler() {
    6.92 -		// TODO: dopsat
    6.93 -		return contentHandler;
    6.94 -	}
    6.95 -
    6.96 -	@Override
    6.97 -	public void setErrorHandler(ErrorHandler handler) {
    6.98 -		// TODO: dopsat
    6.99 -		log.log(Level.FINE, "setErrorHandler: {0}", handler);
   6.100 -	}
   6.101 -
   6.102 -	@Override
   6.103 -	public ErrorHandler getErrorHandler() {
   6.104 -		// TODO: dopsat
   6.105 -		log.log(Level.FINE, "getErrorHandler");
   6.106 -		return null;
   6.107 -	}
   6.108 -
   6.109 -	@Override
   6.110 -	public void parse(InputSource input) throws IOException, SAXException {
   6.111 -		/**
   6.112 -		 * TODO: rozpornat formát vstupu a podle toho delegovat
   6.113 -		 */
   6.114 -		
   6.115 -		JSONParser p = new JSONParser();
   6.116 -		InputStreamReader vstup = new InputStreamReader(input.getByteStream());
   6.117 -		JsonSimpleContentHandler handler = new JsonSimpleContentHandler(contentHandler);
   6.118 -
   6.119 -		try {
   6.120 -			p.parse(vstup, handler);
   6.121 -		} catch (ParseException e) {
   6.122 -			throw new SAXException("Chyba při načítání JSONu", e);
   6.123 -		}
   6.124 -	}
   6.125 -
   6.126 -	@Override
   6.127 -	public void parse(String systemId) throws IOException, SAXException {
   6.128 -		// TODO: dopsat
   6.129 -		throw new UnsupportedOperationException("Zatím není podporované.");
   6.130 -	}
   6.131 -}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/java/alt2xml/src/cz/frantovo/alt2xml/vstup/JsonSimpleContentHandler.java	Tue Jan 03 12:55:38 2012 +0100
     7.3 @@ -0,0 +1,135 @@
     7.4 +package cz.frantovo.alt2xml.vstup;
     7.5 +
     7.6 +import java.io.IOException;
     7.7 +import java.util.Stack;
     7.8 +import org.json.simple.parser.ParseException;
     7.9 +import org.xml.sax.ContentHandler;
    7.10 +import org.xml.sax.SAXException;
    7.11 +
    7.12 +/**
    7.13 +) *
    7.14 + * @author fiki
    7.15 + */
    7.16 +public class JsonSimpleContentHandler implements org.json.simple.parser.ContentHandler {
    7.17 +
    7.18 +	/** Sem vypisujeme XML události */
    7.19 +	private ContentHandler saxVýstup;
    7.20 +	/** Musíme si pamatovat polohu v XML stromu, abychom věděli, kterou značku kdy uzavřít */
    7.21 +	private Stack<String> poloha = new Stack<>();
    7.22 +	/**
    7.23 +	 * Po textových uzlech vkládáme konce elementů rovnou,
    7.24 +	 * ale pokud jeden element končí hned po jiném, 
    7.25 +	 * vložíme mezi ně ještě konec řádku a odsazení.
    7.26 +	 */
    7.27 +	private boolean zalomitŘádek = false;
    7.28 +
    7.29 +	public JsonSimpleContentHandler(ContentHandler saxVýstup) {
    7.30 +		this.saxVýstup = saxVýstup;
    7.31 +	}
    7.32 +
    7.33 +	private void začniElement(String název) throws IOException {
    7.34 +		try {
    7.35 +			vložOdsazení();
    7.36 +			saxVýstup.startElement(null, null, název, null);
    7.37 +			poloha.push(název);
    7.38 +		} catch (SAXException e) {
    7.39 +			throw new IOException("Chyba při začátku elementu.", e);
    7.40 +		}
    7.41 +	}
    7.42 +
    7.43 +	private void ukončiElement() throws IOException {
    7.44 +		try {
    7.45 +			String značka = poloha.pop();
    7.46 +			if (zalomitŘádek) {
    7.47 +				vložOdsazení();
    7.48 +			}
    7.49 +			zalomitŘádek = true;
    7.50 +			saxVýstup.endElement(null, null, značka);
    7.51 +		} catch (SAXException e) {
    7.52 +			throw new IOException("Chyba při ukončování elementu.", e);
    7.53 +		}
    7.54 +	}
    7.55 +
    7.56 +	private void vložOdsazení() throws IOException {
    7.57 +		/**
    7.58 +		 * TODO: ignorableWhitespace() ?
    7.59 +		 */
    7.60 +		vložText("\n");
    7.61 +		for (int i = 0; i < poloha.size(); i++) {
    7.62 +			vložText("\t");
    7.63 +		}
    7.64 +	}
    7.65 +
    7.66 +	private void vložText(String text) throws IOException {
    7.67 +		try {
    7.68 +			saxVýstup.characters(text.toCharArray(), 0, text.length());
    7.69 +		} catch (SAXException e) {
    7.70 +			throw new IOException("Chyba při vkládání textu.", e);
    7.71 +		}
    7.72 +	}
    7.73 +
    7.74 +	@Override
    7.75 +	public void startJSON() throws ParseException, IOException {
    7.76 +		try {
    7.77 +			saxVýstup.startDocument();
    7.78 +			začniElement("objekt");
    7.79 +		} catch (SAXException e) {
    7.80 +			throw new IOException("Chyba při začátku dokumentu.", e);
    7.81 +		}
    7.82 +	}
    7.83 +
    7.84 +	@Override
    7.85 +	public void endJSON() throws ParseException, IOException {
    7.86 +		try {
    7.87 +			ukončiElement();
    7.88 +			vložText("\n");
    7.89 +			saxVýstup.endDocument();
    7.90 +		} catch (SAXException e) {
    7.91 +			throw new IOException(e);
    7.92 +		}
    7.93 +	}
    7.94 +
    7.95 +	@Override
    7.96 +	public boolean startObject() throws ParseException, IOException {
    7.97 +		// System.err.println("startObject");
    7.98 +		return true;
    7.99 +	}
   7.100 +
   7.101 +	@Override
   7.102 +	public boolean endObject() throws ParseException, IOException {
   7.103 +		// System.err.println("endObject");
   7.104 +		return true;
   7.105 +	}
   7.106 +
   7.107 +	@Override
   7.108 +	public boolean startObjectEntry(String key) throws ParseException, IOException {
   7.109 +		začniElement(key);
   7.110 +		return true;
   7.111 +	}
   7.112 +
   7.113 +	@Override
   7.114 +	public boolean endObjectEntry() throws ParseException, IOException {
   7.115 +		ukončiElement();
   7.116 +		// System.err.println("endObjectEntry");
   7.117 +		return true;
   7.118 +	}
   7.119 +
   7.120 +	@Override
   7.121 +	public boolean startArray() throws ParseException, IOException {
   7.122 +		// System.err.println("startArray");
   7.123 +		return true;
   7.124 +	}
   7.125 +
   7.126 +	@Override
   7.127 +	public boolean endArray() throws ParseException, IOException {
   7.128 +		// System.err.println("endArray");
   7.129 +		return true;
   7.130 +	}
   7.131 +
   7.132 +	@Override
   7.133 +	public boolean primitive(Object value) throws ParseException, IOException {
   7.134 +		vložText(String.valueOf(value));
   7.135 +		zalomitŘádek = false;
   7.136 +		return true;
   7.137 +	}
   7.138 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/java/alt2xml/src/cz/frantovo/alt2xml/vstup/SuperReader.java	Tue Jan 03 12:55:38 2012 +0100
     8.3 @@ -0,0 +1,127 @@
     8.4 +package cz.frantovo.alt2xml.vstup;
     8.5 +
     8.6 +import java.io.IOException;
     8.7 +import java.io.InputStreamReader;
     8.8 +import java.util.logging.Level;
     8.9 +import java.util.logging.Logger;
    8.10 +import org.json.simple.parser.JSONParser;
    8.11 +import org.json.simple.parser.ParseException;
    8.12 +import org.xml.sax.ContentHandler;
    8.13 +import org.xml.sax.DTDHandler;
    8.14 +import org.xml.sax.EntityResolver;
    8.15 +import org.xml.sax.ErrorHandler;
    8.16 +import org.xml.sax.InputSource;
    8.17 +import org.xml.sax.SAXException;
    8.18 +import org.xml.sax.SAXNotRecognizedException;
    8.19 +import org.xml.sax.SAXNotSupportedException;
    8.20 +import org.xml.sax.XMLReader;
    8.21 +
    8.22 +/**
    8.23 + *
    8.24 + * @author fiki
    8.25 + */
    8.26 +public class SuperReader implements XMLReader {
    8.27 +
    8.28 +	private static final Logger log = Logger.getLogger(SuperReader.class.getName());
    8.29 +	private ContentHandler contentHandler;
    8.30 +
    8.31 +	@Override
    8.32 +	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    8.33 +		// TODO: dopsat
    8.34 +		log.log(Level.FINE, "getFeature: {0}", name);
    8.35 +		return false;
    8.36 +	}
    8.37 +
    8.38 +	@Override
    8.39 +	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    8.40 +		// TODO: dopsat
    8.41 +		log.log(Level.FINE, "setFeature: {0} = {1}", new Object[]{name, value});
    8.42 +	}
    8.43 +
    8.44 +	@Override
    8.45 +	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    8.46 +		// TODO: dopsat
    8.47 +		log.log(Level.FINE, "getProperty: {0}", name);
    8.48 +		return null;
    8.49 +	}
    8.50 +
    8.51 +	@Override
    8.52 +	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
    8.53 +		// TODO: dopsat
    8.54 +		log.log(Level.FINE, "setProperty: {0} = {1}", new Object[]{name, value});
    8.55 +	}
    8.56 +
    8.57 +	@Override
    8.58 +	public void setEntityResolver(EntityResolver resolver) {
    8.59 +		// TODO: dopsat
    8.60 +		log.log(Level.FINE, "setEntityResolver: {0}", resolver);
    8.61 +	}
    8.62 +
    8.63 +	@Override
    8.64 +	public EntityResolver getEntityResolver() {
    8.65 +		// TODO: dopsat
    8.66 +		log.log(Level.FINE, "getEntityResolver");
    8.67 +		return null;
    8.68 +	}
    8.69 +
    8.70 +	@Override
    8.71 +	public void setDTDHandler(DTDHandler handler) {
    8.72 +		// TODO: dopsat
    8.73 +		log.log(Level.FINE, "setDTDHandler: {0}", handler);
    8.74 +	}
    8.75 +
    8.76 +	@Override
    8.77 +	public DTDHandler getDTDHandler() {
    8.78 +		// TODO: dopsat
    8.79 +		log.log(Level.FINE, "getDTDHandler");
    8.80 +		return null;
    8.81 +	}
    8.82 +
    8.83 +	@Override
    8.84 +	public void setContentHandler(ContentHandler handler) {
    8.85 +		// TODO: dopsat
    8.86 +		contentHandler = handler;
    8.87 +	}
    8.88 +
    8.89 +	@Override
    8.90 +	public ContentHandler getContentHandler() {
    8.91 +		// TODO: dopsat
    8.92 +		return contentHandler;
    8.93 +	}
    8.94 +
    8.95 +	@Override
    8.96 +	public void setErrorHandler(ErrorHandler handler) {
    8.97 +		// TODO: dopsat
    8.98 +		log.log(Level.FINE, "setErrorHandler: {0}", handler);
    8.99 +	}
   8.100 +
   8.101 +	@Override
   8.102 +	public ErrorHandler getErrorHandler() {
   8.103 +		// TODO: dopsat
   8.104 +		log.log(Level.FINE, "getErrorHandler");
   8.105 +		return null;
   8.106 +	}
   8.107 +
   8.108 +	@Override
   8.109 +	public void parse(InputSource input) throws IOException, SAXException {
   8.110 +		/**
   8.111 +		 * TODO: rozpornat formát vstupu a podle toho delegovat
   8.112 +		 */
   8.113 +		
   8.114 +		JSONParser p = new JSONParser();
   8.115 +		InputStreamReader vstup = new InputStreamReader(input.getByteStream());
   8.116 +		JsonSimpleContentHandler handler = new JsonSimpleContentHandler(contentHandler);
   8.117 +
   8.118 +		try {
   8.119 +			p.parse(vstup, handler);
   8.120 +		} catch (ParseException e) {
   8.121 +			throw new SAXException("Chyba při načítání JSONu", e);
   8.122 +		}
   8.123 +	}
   8.124 +
   8.125 +	@Override
   8.126 +	public void parse(String systemId) throws IOException, SAXException {
   8.127 +		// TODO: dopsat
   8.128 +		throw new UnsupportedOperationException("Zatím není podporované.");
   8.129 +	}
   8.130 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/java/alt2xml/src/cz/frantovo/alt2xml/výstup/EchoContentHandler.java	Tue Jan 03 12:55:38 2012 +0100
     9.3 @@ -0,0 +1,83 @@
     9.4 +package cz.frantovo.alt2xml.výstup;
     9.5 +
     9.6 +import javax.xml.stream.XMLStreamException;
     9.7 +import javax.xml.stream.XMLStreamWriter;
     9.8 +import org.xml.sax.Attributes;
     9.9 +import org.xml.sax.SAXException;
    9.10 +import org.xml.sax.helpers.DefaultHandler;
    9.11 +
    9.12 +/**
    9.13 + * Slouží k převodu právě parsovaného XML zpět na XML.
    9.14 + * Určen pro testování a ladění a pro použití s neobvyklými „XML“ parsery,
    9.15 + * které nečtou XML ale jiný jazyk (např. JSON, INI atd.), ale používají stejné rozhraní (SAX).
    9.16 + * 
    9.17 + * TODO: další typy uzlů a jmenné prostory.
    9.18 + * @author fiki
    9.19 + */
    9.20 +public class EchoContentHandler extends DefaultHandler {
    9.21 +
    9.22 +	private XMLStreamWriter w;
    9.23 +
    9.24 +	/**
    9.25 +	 * @param writer kam se bude vypisovat XML.
    9.26 +	 */
    9.27 +	public EchoContentHandler(XMLStreamWriter writer) {
    9.28 +		w = writer;
    9.29 +	}
    9.30 +
    9.31 +	@Override
    9.32 +	public void startDocument() throws SAXException {
    9.33 +		try {
    9.34 +			w.writeStartDocument();
    9.35 +		} catch (XMLStreamException e) {
    9.36 +			throw new SAXException(e);
    9.37 +		}
    9.38 +	}
    9.39 +
    9.40 +	@Override
    9.41 +	public void endDocument() throws SAXException {
    9.42 +		try {
    9.43 +			w.writeEndDocument();
    9.44 +			w.close();
    9.45 +		} catch (XMLStreamException e) {
    9.46 +			throw new SAXException(e);
    9.47 +		}
    9.48 +	}
    9.49 +
    9.50 +	@Override
    9.51 +	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    9.52 +		try {
    9.53 +			w.writeStartElement(qName);
    9.54 +
    9.55 +			if (attributes != null) {
    9.56 +				for (int i = 0; i < attributes.getLength(); i++) {
    9.57 +					w.writeAttribute(attributes.getQName(i), attributes.getValue(i));
    9.58 +				}
    9.59 +			}
    9.60 +
    9.61 +			w.flush();
    9.62 +		} catch (XMLStreamException e) {
    9.63 +			throw new SAXException(e);
    9.64 +		}
    9.65 +	}
    9.66 +
    9.67 +	@Override
    9.68 +	public void endElement(String uri, String localName, String qName) throws SAXException {
    9.69 +		try {
    9.70 +			w.writeEndElement();
    9.71 +			w.flush();
    9.72 +		} catch (XMLStreamException e) {
    9.73 +			throw new SAXException(e);
    9.74 +		}
    9.75 +	}
    9.76 +
    9.77 +	@Override
    9.78 +	public void characters(char[] ch, int start, int length) throws SAXException {
    9.79 +		try {
    9.80 +			w.writeCharacters(ch, start, length);
    9.81 +			w.flush();
    9.82 +		} catch (XMLStreamException e) {
    9.83 +			throw new SAXException(e);
    9.84 +		}
    9.85 +	}
    9.86 +}