java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/Reader.java
changeset 14 471c0cda94c3
parent 13 a598b9d90144
child 17 64358dd0999f
     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/Reader.java	Thu Jun 05 23:45:24 2014 +0200
     1.3 @@ -0,0 +1,134 @@
     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.io.InputStreamReader;
    1.25 +import java.util.HashMap;
    1.26 +import java.util.Map;
    1.27 +import org.json.simple.parser.JSONParser;
    1.28 +import org.json.simple.parser.ParseException;
    1.29 +import org.xml.sax.ContentHandler;
    1.30 +import org.xml.sax.DTDHandler;
    1.31 +import org.xml.sax.EntityResolver;
    1.32 +import org.xml.sax.ErrorHandler;
    1.33 +import org.xml.sax.InputSource;
    1.34 +import org.xml.sax.SAXException;
    1.35 +import org.xml.sax.SAXNotRecognizedException;
    1.36 +import org.xml.sax.SAXNotSupportedException;
    1.37 +import org.xml.sax.XMLReader;
    1.38 +
    1.39 +/**
    1.40 + *
    1.41 + * @author fiki
    1.42 + */
    1.43 +public class Reader implements XMLReader {
    1.44 +
    1.45 +	private ContentHandler contentHandler;
    1.46 +	private ErrorHandler errorHandler;
    1.47 +	private DTDHandler dtdHandler;
    1.48 +	private EntityResolver entityResolver;
    1.49 +	private Map<String, Object> konfigurace = new HashMap<>();
    1.50 +
    1.51 +	@Override
    1.52 +	public void parse(InputSource input) throws IOException, SAXException {
    1.53 +		/**
    1.54 +		 * TODO: rozpornat formát vstupu a podle toho delegovat
    1.55 +		 */
    1.56 +		JSONParser p = new JSONParser();
    1.57 +		InputStreamReader vstup = new InputStreamReader(input.getByteStream());
    1.58 +		JsonSimpleContentHandler handler = new JsonSimpleContentHandler(contentHandler);
    1.59 +
    1.60 +		try {
    1.61 +			p.parse(vstup, handler);
    1.62 +		} catch (ParseException e) {
    1.63 +			throw new SAXException("Chyba při načítání JSONu", e);
    1.64 +		}
    1.65 +	}
    1.66 +
    1.67 +	@Override
    1.68 +	public void parse(String systemId) throws IOException, SAXException {
    1.69 +		parse(new InputSource(systemId));
    1.70 +	}
    1.71 +
    1.72 +	@Override
    1.73 +	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    1.74 +		/**
    1.75 +		 * TODO:
    1.76 +		 * All XMLReaders are required to recognize
    1.77 +		 * the http://xml.org/sax/features/namespaces
    1.78 +		 * and the http://xml.org/sax/features/namespace-prefixes feature names.
    1.79 +		 */
    1.80 +		throw new SAXNotSupportedException("Zatím není podporováno.");
    1.81 +	}
    1.82 +
    1.83 +	@Override
    1.84 +	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    1.85 +		throw new SAXNotSupportedException("Zatím není podporováno.");
    1.86 +	}
    1.87 +
    1.88 +	@Override
    1.89 +	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    1.90 +		return konfigurace.get(name);
    1.91 +	}
    1.92 +
    1.93 +	@Override
    1.94 +	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
    1.95 +		konfigurace.put(name, value);
    1.96 +	}
    1.97 +
    1.98 +	@Override
    1.99 +	public void setEntityResolver(EntityResolver entityResolver) {
   1.100 +		this.entityResolver = entityResolver;
   1.101 +	}
   1.102 +
   1.103 +	@Override
   1.104 +	public EntityResolver getEntityResolver() {
   1.105 +		return entityResolver;
   1.106 +	}
   1.107 +
   1.108 +	@Override
   1.109 +	public void setDTDHandler(DTDHandler dtdHandler) {
   1.110 +		this.dtdHandler = dtdHandler;
   1.111 +	}
   1.112 +
   1.113 +	@Override
   1.114 +	public DTDHandler getDTDHandler() {
   1.115 +		return dtdHandler;
   1.116 +	}
   1.117 +
   1.118 +	@Override
   1.119 +	public void setContentHandler(ContentHandler contentHandler) {
   1.120 +		this.contentHandler = contentHandler;
   1.121 +	}
   1.122 +
   1.123 +	@Override
   1.124 +	public ContentHandler getContentHandler() {
   1.125 +		return contentHandler;
   1.126 +	}
   1.127 +
   1.128 +	@Override
   1.129 +	public void setErrorHandler(ErrorHandler errorHandler) {
   1.130 +		this.errorHandler = errorHandler;
   1.131 +	}
   1.132 +
   1.133 +	@Override
   1.134 +	public ErrorHandler getErrorHandler() {
   1.135 +		return errorHandler;
   1.136 +	}
   1.137 +}