balíček pro JSON
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 05 Jun 2014 23:45:24 +0200
changeset 14471c0cda94c3
parent 13 a598b9d90144
child 15 b890783e4043
balíček pro JSON
java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/JsonSimpleContentHandler.java
java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/Reader.java
java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/ReaderFactory.java
java/alt2xml-in-json/src/cz/frantovo/alt2xml/vstup/JsonReaderFactory.java
java/alt2xml-in-json/src/cz/frantovo/alt2xml/vstup/JsonSimpleContentHandler.java
java/alt2xml-in-json/src/cz/frantovo/alt2xml/vstup/JsonXMLReader.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/JsonSimpleContentHandler.java	Thu Jun 05 23:45:24 2014 +0200
     1.3 @@ -0,0 +1,152 @@
     1.4 +/**
     1.5 + * Alt2XML
     1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package cz.frantovo.alt2xml.in.json;
    1.22 +
    1.23 +import java.io.IOException;
    1.24 +import java.util.Stack;
    1.25 +import org.json.simple.parser.ParseException;
    1.26 +import org.xml.sax.ContentHandler;
    1.27 +import org.xml.sax.SAXException;
    1.28 +
    1.29 +/**
    1.30 + *
    1.31 + * @author fiki
    1.32 + */
    1.33 +public class JsonSimpleContentHandler implements org.json.simple.parser.ContentHandler {
    1.34 +
    1.35 +	/** Sem vypisujeme XML události */
    1.36 +	private ContentHandler saxVýstup;
    1.37 +	/** Musíme si pamatovat polohu v XML stromu, abychom věděli, kterou značku kdy uzavřít */
    1.38 +	private Stack<String> poloha = new Stack<>();
    1.39 +	/**
    1.40 +	 * Po textových uzlech vkládáme konce elementů rovnou,
    1.41 +	 * ale pokud jeden element končí hned po jiném, 
    1.42 +	 * vložíme mezi ně ještě konec řádku a odsazení.
    1.43 +	 */
    1.44 +	private boolean zalomitŘádek = false;
    1.45 +
    1.46 +	public JsonSimpleContentHandler(ContentHandler saxVýstup) {
    1.47 +		this.saxVýstup = saxVýstup;
    1.48 +	}
    1.49 +
    1.50 +	private void začniElement(String název) throws IOException {
    1.51 +		try {
    1.52 +			vložOdsazení();
    1.53 +			saxVýstup.startElement(null, null, název, null);
    1.54 +			poloha.push(název);
    1.55 +		} catch (SAXException e) {
    1.56 +			throw new IOException("Chyba při začátku elementu.", e);
    1.57 +		}
    1.58 +	}
    1.59 +
    1.60 +	private void ukončiElement() throws IOException {
    1.61 +		try {
    1.62 +			String značka = poloha.pop();
    1.63 +			if (zalomitŘádek) {
    1.64 +				vložOdsazení();
    1.65 +			}
    1.66 +			zalomitŘádek = true;
    1.67 +			saxVýstup.endElement(null, null, značka);
    1.68 +		} catch (SAXException e) {
    1.69 +			throw new IOException("Chyba při ukončování elementu.", e);
    1.70 +		}
    1.71 +	}
    1.72 +
    1.73 +	private void vložOdsazení() throws IOException {
    1.74 +		/**
    1.75 +		 * TODO: ignorableWhitespace() ?
    1.76 +		 */
    1.77 +		vložText("\n");
    1.78 +		for (int i = 0; i < poloha.size(); i++) {
    1.79 +			vložText("\t");
    1.80 +		}
    1.81 +	}
    1.82 +
    1.83 +	private void vložText(String text) throws IOException {
    1.84 +		try {
    1.85 +			saxVýstup.characters(text.toCharArray(), 0, text.length());
    1.86 +		} catch (SAXException e) {
    1.87 +			throw new IOException("Chyba při vkládání textu.", e);
    1.88 +		}
    1.89 +	}
    1.90 +
    1.91 +	@Override
    1.92 +	public void startJSON() throws ParseException, IOException {
    1.93 +		try {
    1.94 +			saxVýstup.startDocument();
    1.95 +			začniElement("objekt");
    1.96 +		} catch (SAXException e) {
    1.97 +			throw new IOException("Chyba při začátku dokumentu.", e);
    1.98 +		}
    1.99 +	}
   1.100 +
   1.101 +	@Override
   1.102 +	public void endJSON() throws ParseException, IOException {
   1.103 +		try {
   1.104 +			ukončiElement();
   1.105 +			vložText("\n");
   1.106 +			saxVýstup.endDocument();
   1.107 +		} catch (SAXException e) {
   1.108 +			throw new IOException(e);
   1.109 +		}
   1.110 +	}
   1.111 +
   1.112 +	@Override
   1.113 +	public boolean startObject() throws ParseException, IOException {
   1.114 +		// System.err.println("startObject");
   1.115 +		return true;
   1.116 +	}
   1.117 +
   1.118 +	@Override
   1.119 +	public boolean endObject() throws ParseException, IOException {
   1.120 +		// System.err.println("endObject");
   1.121 +		return true;
   1.122 +	}
   1.123 +
   1.124 +	@Override
   1.125 +	public boolean startObjectEntry(String key) throws ParseException, IOException {
   1.126 +		začniElement(key);
   1.127 +		return true;
   1.128 +	}
   1.129 +
   1.130 +	@Override
   1.131 +	public boolean endObjectEntry() throws ParseException, IOException {
   1.132 +		ukončiElement();
   1.133 +		// System.err.println("endObjectEntry");
   1.134 +		return true;
   1.135 +	}
   1.136 +
   1.137 +	@Override
   1.138 +	public boolean startArray() throws ParseException, IOException {
   1.139 +		// System.err.println("startArray");
   1.140 +		return true;
   1.141 +	}
   1.142 +
   1.143 +	@Override
   1.144 +	public boolean endArray() throws ParseException, IOException {
   1.145 +		// System.err.println("endArray");
   1.146 +		return true;
   1.147 +	}
   1.148 +
   1.149 +	@Override
   1.150 +	public boolean primitive(Object value) throws ParseException, IOException {
   1.151 +		vložText(String.valueOf(value));
   1.152 +		zalomitŘádek = false;
   1.153 +		return true;
   1.154 +	}
   1.155 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/Reader.java	Thu Jun 05 23:45:24 2014 +0200
     2.3 @@ -0,0 +1,134 @@
     2.4 +/**
     2.5 + * Alt2XML
     2.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, either version 3 of the License, or
    2.11 + * (at your option) any later version.
    2.12 + *
    2.13 + * This program is distributed in the hope that it will be useful,
    2.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    2.16 + * GNU General Public License for more details.
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License
    2.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    2.20 + */
    2.21 +package cz.frantovo.alt2xml.in.json;
    2.22 +
    2.23 +import java.io.IOException;
    2.24 +import java.io.InputStreamReader;
    2.25 +import java.util.HashMap;
    2.26 +import java.util.Map;
    2.27 +import org.json.simple.parser.JSONParser;
    2.28 +import org.json.simple.parser.ParseException;
    2.29 +import org.xml.sax.ContentHandler;
    2.30 +import org.xml.sax.DTDHandler;
    2.31 +import org.xml.sax.EntityResolver;
    2.32 +import org.xml.sax.ErrorHandler;
    2.33 +import org.xml.sax.InputSource;
    2.34 +import org.xml.sax.SAXException;
    2.35 +import org.xml.sax.SAXNotRecognizedException;
    2.36 +import org.xml.sax.SAXNotSupportedException;
    2.37 +import org.xml.sax.XMLReader;
    2.38 +
    2.39 +/**
    2.40 + *
    2.41 + * @author fiki
    2.42 + */
    2.43 +public class Reader implements XMLReader {
    2.44 +
    2.45 +	private ContentHandler contentHandler;
    2.46 +	private ErrorHandler errorHandler;
    2.47 +	private DTDHandler dtdHandler;
    2.48 +	private EntityResolver entityResolver;
    2.49 +	private Map<String, Object> konfigurace = new HashMap<>();
    2.50 +
    2.51 +	@Override
    2.52 +	public void parse(InputSource input) throws IOException, SAXException {
    2.53 +		/**
    2.54 +		 * TODO: rozpornat formát vstupu a podle toho delegovat
    2.55 +		 */
    2.56 +		JSONParser p = new JSONParser();
    2.57 +		InputStreamReader vstup = new InputStreamReader(input.getByteStream());
    2.58 +		JsonSimpleContentHandler handler = new JsonSimpleContentHandler(contentHandler);
    2.59 +
    2.60 +		try {
    2.61 +			p.parse(vstup, handler);
    2.62 +		} catch (ParseException e) {
    2.63 +			throw new SAXException("Chyba při načítání JSONu", e);
    2.64 +		}
    2.65 +	}
    2.66 +
    2.67 +	@Override
    2.68 +	public void parse(String systemId) throws IOException, SAXException {
    2.69 +		parse(new InputSource(systemId));
    2.70 +	}
    2.71 +
    2.72 +	@Override
    2.73 +	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    2.74 +		/**
    2.75 +		 * TODO:
    2.76 +		 * All XMLReaders are required to recognize
    2.77 +		 * the http://xml.org/sax/features/namespaces
    2.78 +		 * and the http://xml.org/sax/features/namespace-prefixes feature names.
    2.79 +		 */
    2.80 +		throw new SAXNotSupportedException("Zatím není podporováno.");
    2.81 +	}
    2.82 +
    2.83 +	@Override
    2.84 +	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    2.85 +		throw new SAXNotSupportedException("Zatím není podporováno.");
    2.86 +	}
    2.87 +
    2.88 +	@Override
    2.89 +	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    2.90 +		return konfigurace.get(name);
    2.91 +	}
    2.92 +
    2.93 +	@Override
    2.94 +	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
    2.95 +		konfigurace.put(name, value);
    2.96 +	}
    2.97 +
    2.98 +	@Override
    2.99 +	public void setEntityResolver(EntityResolver entityResolver) {
   2.100 +		this.entityResolver = entityResolver;
   2.101 +	}
   2.102 +
   2.103 +	@Override
   2.104 +	public EntityResolver getEntityResolver() {
   2.105 +		return entityResolver;
   2.106 +	}
   2.107 +
   2.108 +	@Override
   2.109 +	public void setDTDHandler(DTDHandler dtdHandler) {
   2.110 +		this.dtdHandler = dtdHandler;
   2.111 +	}
   2.112 +
   2.113 +	@Override
   2.114 +	public DTDHandler getDTDHandler() {
   2.115 +		return dtdHandler;
   2.116 +	}
   2.117 +
   2.118 +	@Override
   2.119 +	public void setContentHandler(ContentHandler contentHandler) {
   2.120 +		this.contentHandler = contentHandler;
   2.121 +	}
   2.122 +
   2.123 +	@Override
   2.124 +	public ContentHandler getContentHandler() {
   2.125 +		return contentHandler;
   2.126 +	}
   2.127 +
   2.128 +	@Override
   2.129 +	public void setErrorHandler(ErrorHandler errorHandler) {
   2.130 +		this.errorHandler = errorHandler;
   2.131 +	}
   2.132 +
   2.133 +	@Override
   2.134 +	public ErrorHandler getErrorHandler() {
   2.135 +		return errorHandler;
   2.136 +	}
   2.137 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/ReaderFactory.java	Thu Jun 05 23:45:24 2014 +0200
     3.3 @@ -0,0 +1,39 @@
     3.4 +/**
     3.5 + * Alt2XML
     3.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, either version 3 of the License, or
    3.11 + * (at your option) any later version.
    3.12 + *
    3.13 + * This program is distributed in the hope that it will be useful,
    3.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    3.16 + * GNU General Public License for more details.
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License
    3.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    3.20 + */
    3.21 +package cz.frantovo.alt2xml.in.json;
    3.22 +
    3.23 +import cz.frantovo.alt2xml.Alt2XmlReaderFactory;
    3.24 +import org.xml.sax.XMLReader;
    3.25 +
    3.26 +/**
    3.27 + *
    3.28 + * @author Ing. František Kučera (frantovo.cz)
    3.29 + */
    3.30 +public class ReaderFactory implements Alt2XmlReaderFactory {
    3.31 +
    3.32 +	@Override
    3.33 +	public boolean canRead(String systemId) {
    3.34 +		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    3.35 +	}
    3.36 +
    3.37 +	@Override
    3.38 +	public XMLReader getReader() {
    3.39 +		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    3.40 +	}
    3.41 +
    3.42 +}
     4.1 --- a/java/alt2xml-in-json/src/cz/frantovo/alt2xml/vstup/JsonReaderFactory.java	Thu Jun 05 23:41:05 2014 +0200
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,39 +0,0 @@
     4.4 -/**
     4.5 - * Alt2XML
     4.6 - * Copyright © 2014 František Kučera (frantovo.cz)
     4.7 - *
     4.8 - * This program is free software: you can redistribute it and/or modify
     4.9 - * it under the terms of the GNU General Public License as published by
    4.10 - * the Free Software Foundation, either version 3 of the License, or
    4.11 - * (at your option) any later version.
    4.12 - *
    4.13 - * This program is distributed in the hope that it will be useful,
    4.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    4.16 - * GNU General Public License for more details.
    4.17 - *
    4.18 - * You should have received a copy of the GNU General Public License
    4.19 - * along with this program. If not, see <http://www.gnu.org/licenses/>.
    4.20 - */
    4.21 -package cz.frantovo.alt2xml.vstup;
    4.22 -
    4.23 -import cz.frantovo.alt2xml.Alt2XmlReaderFactory;
    4.24 -import org.xml.sax.XMLReader;
    4.25 -
    4.26 -/**
    4.27 - *
    4.28 - * @author Ing. František Kučera (frantovo.cz)
    4.29 - */
    4.30 -public class JsonReaderFactory implements Alt2XmlReaderFactory {
    4.31 -
    4.32 -	@Override
    4.33 -	public boolean canRead(String systemId) {
    4.34 -		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    4.35 -	}
    4.36 -
    4.37 -	@Override
    4.38 -	public XMLReader getReader() {
    4.39 -		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    4.40 -	}
    4.41 -
    4.42 -}
     5.1 --- a/java/alt2xml-in-json/src/cz/frantovo/alt2xml/vstup/JsonSimpleContentHandler.java	Thu Jun 05 23:41:05 2014 +0200
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,152 +0,0 @@
     5.4 -/**
     5.5 - * Alt2XML
     5.6 - * Copyright © 2014 František Kučera (frantovo.cz)
     5.7 - *
     5.8 - * This program is free software: you can redistribute it and/or modify
     5.9 - * it under the terms of the GNU General Public License as published by
    5.10 - * the Free Software Foundation, either version 3 of the License, or
    5.11 - * (at your option) any later version.
    5.12 - *
    5.13 - * This program is distributed in the hope that it will be useful,
    5.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    5.16 - * GNU General Public License for more details.
    5.17 - *
    5.18 - * You should have received a copy of the GNU General Public License
    5.19 - * along with this program. If not, see <http://www.gnu.org/licenses/>.
    5.20 - */
    5.21 -package cz.frantovo.alt2xml.vstup;
    5.22 -
    5.23 -import java.io.IOException;
    5.24 -import java.util.Stack;
    5.25 -import org.json.simple.parser.ParseException;
    5.26 -import org.xml.sax.ContentHandler;
    5.27 -import org.xml.sax.SAXException;
    5.28 -
    5.29 -/**
    5.30 - *
    5.31 - * @author fiki
    5.32 - */
    5.33 -public class JsonSimpleContentHandler implements org.json.simple.parser.ContentHandler {
    5.34 -
    5.35 -	/** Sem vypisujeme XML události */
    5.36 -	private ContentHandler saxVýstup;
    5.37 -	/** Musíme si pamatovat polohu v XML stromu, abychom věděli, kterou značku kdy uzavřít */
    5.38 -	private Stack<String> poloha = new Stack<>();
    5.39 -	/**
    5.40 -	 * Po textových uzlech vkládáme konce elementů rovnou,
    5.41 -	 * ale pokud jeden element končí hned po jiném, 
    5.42 -	 * vložíme mezi ně ještě konec řádku a odsazení.
    5.43 -	 */
    5.44 -	private boolean zalomitŘádek = false;
    5.45 -
    5.46 -	public JsonSimpleContentHandler(ContentHandler saxVýstup) {
    5.47 -		this.saxVýstup = saxVýstup;
    5.48 -	}
    5.49 -
    5.50 -	private void začniElement(String název) throws IOException {
    5.51 -		try {
    5.52 -			vložOdsazení();
    5.53 -			saxVýstup.startElement(null, null, název, null);
    5.54 -			poloha.push(název);
    5.55 -		} catch (SAXException e) {
    5.56 -			throw new IOException("Chyba při začátku elementu.", e);
    5.57 -		}
    5.58 -	}
    5.59 -
    5.60 -	private void ukončiElement() throws IOException {
    5.61 -		try {
    5.62 -			String značka = poloha.pop();
    5.63 -			if (zalomitŘádek) {
    5.64 -				vložOdsazení();
    5.65 -			}
    5.66 -			zalomitŘádek = true;
    5.67 -			saxVýstup.endElement(null, null, značka);
    5.68 -		} catch (SAXException e) {
    5.69 -			throw new IOException("Chyba při ukončování elementu.", e);
    5.70 -		}
    5.71 -	}
    5.72 -
    5.73 -	private void vložOdsazení() throws IOException {
    5.74 -		/**
    5.75 -		 * TODO: ignorableWhitespace() ?
    5.76 -		 */
    5.77 -		vložText("\n");
    5.78 -		for (int i = 0; i < poloha.size(); i++) {
    5.79 -			vložText("\t");
    5.80 -		}
    5.81 -	}
    5.82 -
    5.83 -	private void vložText(String text) throws IOException {
    5.84 -		try {
    5.85 -			saxVýstup.characters(text.toCharArray(), 0, text.length());
    5.86 -		} catch (SAXException e) {
    5.87 -			throw new IOException("Chyba při vkládání textu.", e);
    5.88 -		}
    5.89 -	}
    5.90 -
    5.91 -	@Override
    5.92 -	public void startJSON() throws ParseException, IOException {
    5.93 -		try {
    5.94 -			saxVýstup.startDocument();
    5.95 -			začniElement("objekt");
    5.96 -		} catch (SAXException e) {
    5.97 -			throw new IOException("Chyba při začátku dokumentu.", e);
    5.98 -		}
    5.99 -	}
   5.100 -
   5.101 -	@Override
   5.102 -	public void endJSON() throws ParseException, IOException {
   5.103 -		try {
   5.104 -			ukončiElement();
   5.105 -			vložText("\n");
   5.106 -			saxVýstup.endDocument();
   5.107 -		} catch (SAXException e) {
   5.108 -			throw new IOException(e);
   5.109 -		}
   5.110 -	}
   5.111 -
   5.112 -	@Override
   5.113 -	public boolean startObject() throws ParseException, IOException {
   5.114 -		// System.err.println("startObject");
   5.115 -		return true;
   5.116 -	}
   5.117 -
   5.118 -	@Override
   5.119 -	public boolean endObject() throws ParseException, IOException {
   5.120 -		// System.err.println("endObject");
   5.121 -		return true;
   5.122 -	}
   5.123 -
   5.124 -	@Override
   5.125 -	public boolean startObjectEntry(String key) throws ParseException, IOException {
   5.126 -		začniElement(key);
   5.127 -		return true;
   5.128 -	}
   5.129 -
   5.130 -	@Override
   5.131 -	public boolean endObjectEntry() throws ParseException, IOException {
   5.132 -		ukončiElement();
   5.133 -		// System.err.println("endObjectEntry");
   5.134 -		return true;
   5.135 -	}
   5.136 -
   5.137 -	@Override
   5.138 -	public boolean startArray() throws ParseException, IOException {
   5.139 -		// System.err.println("startArray");
   5.140 -		return true;
   5.141 -	}
   5.142 -
   5.143 -	@Override
   5.144 -	public boolean endArray() throws ParseException, IOException {
   5.145 -		// System.err.println("endArray");
   5.146 -		return true;
   5.147 -	}
   5.148 -
   5.149 -	@Override
   5.150 -	public boolean primitive(Object value) throws ParseException, IOException {
   5.151 -		vložText(String.valueOf(value));
   5.152 -		zalomitŘádek = false;
   5.153 -		return true;
   5.154 -	}
   5.155 -}
     6.1 --- a/java/alt2xml-in-json/src/cz/frantovo/alt2xml/vstup/JsonXMLReader.java	Thu Jun 05 23:41:05 2014 +0200
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,134 +0,0 @@
     6.4 -/**
     6.5 - * Alt2XML
     6.6 - * Copyright © 2014 František Kučera (frantovo.cz)
     6.7 - *
     6.8 - * This program is free software: you can redistribute it and/or modify
     6.9 - * it under the terms of the GNU General Public License as published by
    6.10 - * the Free Software Foundation, either version 3 of the License, or
    6.11 - * (at your option) any later version.
    6.12 - *
    6.13 - * This program is distributed in the hope that it will be useful,
    6.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    6.16 - * GNU General Public License for more details.
    6.17 - *
    6.18 - * You should have received a copy of the GNU General Public License
    6.19 - * along with this program. If not, see <http://www.gnu.org/licenses/>.
    6.20 - */
    6.21 -package cz.frantovo.alt2xml.vstup;
    6.22 -
    6.23 -import java.io.IOException;
    6.24 -import java.io.InputStreamReader;
    6.25 -import java.util.HashMap;
    6.26 -import java.util.Map;
    6.27 -import org.json.simple.parser.JSONParser;
    6.28 -import org.json.simple.parser.ParseException;
    6.29 -import org.xml.sax.ContentHandler;
    6.30 -import org.xml.sax.DTDHandler;
    6.31 -import org.xml.sax.EntityResolver;
    6.32 -import org.xml.sax.ErrorHandler;
    6.33 -import org.xml.sax.InputSource;
    6.34 -import org.xml.sax.SAXException;
    6.35 -import org.xml.sax.SAXNotRecognizedException;
    6.36 -import org.xml.sax.SAXNotSupportedException;
    6.37 -import org.xml.sax.XMLReader;
    6.38 -
    6.39 -/**
    6.40 - *
    6.41 - * @author fiki
    6.42 - */
    6.43 -public class JsonXMLReader implements XMLReader {
    6.44 -
    6.45 -	private ContentHandler contentHandler;
    6.46 -	private ErrorHandler errorHandler;
    6.47 -	private DTDHandler dtdHandler;
    6.48 -	private EntityResolver entityResolver;
    6.49 -	private Map<String, Object> konfigurace = new HashMap<>();
    6.50 -
    6.51 -	@Override
    6.52 -	public void parse(InputSource input) throws IOException, SAXException {
    6.53 -		/**
    6.54 -		 * TODO: rozpornat formát vstupu a podle toho delegovat
    6.55 -		 */
    6.56 -		JSONParser p = new JSONParser();
    6.57 -		InputStreamReader vstup = new InputStreamReader(input.getByteStream());
    6.58 -		JsonSimpleContentHandler handler = new JsonSimpleContentHandler(contentHandler);
    6.59 -
    6.60 -		try {
    6.61 -			p.parse(vstup, handler);
    6.62 -		} catch (ParseException e) {
    6.63 -			throw new SAXException("Chyba při načítání JSONu", e);
    6.64 -		}
    6.65 -	}
    6.66 -
    6.67 -	@Override
    6.68 -	public void parse(String systemId) throws IOException, SAXException {
    6.69 -		parse(new InputSource(systemId));
    6.70 -	}
    6.71 -
    6.72 -	@Override
    6.73 -	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    6.74 -		/**
    6.75 -		 * TODO:
    6.76 -		 * All XMLReaders are required to recognize
    6.77 -		 * the http://xml.org/sax/features/namespaces
    6.78 -		 * and the http://xml.org/sax/features/namespace-prefixes feature names.
    6.79 -		 */
    6.80 -		throw new SAXNotSupportedException("Zatím není podporováno.");
    6.81 -	}
    6.82 -
    6.83 -	@Override
    6.84 -	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    6.85 -		throw new SAXNotSupportedException("Zatím není podporováno.");
    6.86 -	}
    6.87 -
    6.88 -	@Override
    6.89 -	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    6.90 -		return konfigurace.get(name);
    6.91 -	}
    6.92 -
    6.93 -	@Override
    6.94 -	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
    6.95 -		konfigurace.put(name, value);
    6.96 -	}
    6.97 -
    6.98 -	@Override
    6.99 -	public void setEntityResolver(EntityResolver entityResolver) {
   6.100 -		this.entityResolver = entityResolver;
   6.101 -	}
   6.102 -
   6.103 -	@Override
   6.104 -	public EntityResolver getEntityResolver() {
   6.105 -		return entityResolver;
   6.106 -	}
   6.107 -
   6.108 -	@Override
   6.109 -	public void setDTDHandler(DTDHandler dtdHandler) {
   6.110 -		this.dtdHandler = dtdHandler;
   6.111 -	}
   6.112 -
   6.113 -	@Override
   6.114 -	public DTDHandler getDTDHandler() {
   6.115 -		return dtdHandler;
   6.116 -	}
   6.117 -
   6.118 -	@Override
   6.119 -	public void setContentHandler(ContentHandler contentHandler) {
   6.120 -		this.contentHandler = contentHandler;
   6.121 -	}
   6.122 -
   6.123 -	@Override
   6.124 -	public ContentHandler getContentHandler() {
   6.125 -		return contentHandler;
   6.126 -	}
   6.127 -
   6.128 -	@Override
   6.129 -	public void setErrorHandler(ErrorHandler errorHandler) {
   6.130 -		this.errorHandler = errorHandler;
   6.131 -	}
   6.132 -
   6.133 -	@Override
   6.134 -	public ErrorHandler getErrorHandler() {
   6.135 -		return errorHandler;
   6.136 -	}
   6.137 -}