AbstractAlt2XmlReader
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 07 Jun 2014 11:23:58 +0200
changeset 1764358dd0999f
parent 16 b2fbb3570ae1
child 18 3f90b37898f6
AbstractAlt2XmlReader
java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/Reader.java
java/alt2xml-lib/src/cz/frantovo/alt2xml/AbstractAlt2XmlReader.java
     1.1 --- a/java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/Reader.java	Sat Jun 07 10:49:42 2014 +0200
     1.2 +++ b/java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/Reader.java	Sat Jun 07 11:23:58 2014 +0200
     1.3 @@ -17,39 +17,22 @@
     1.4   */
     1.5  package cz.frantovo.alt2xml.in.json;
     1.6  
     1.7 +import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
     1.8  import java.io.IOException;
     1.9  import java.io.InputStreamReader;
    1.10 -import java.util.HashMap;
    1.11 -import java.util.Map;
    1.12  import org.json.simple.parser.JSONParser;
    1.13  import org.json.simple.parser.ParseException;
    1.14 -import org.xml.sax.ContentHandler;
    1.15 -import org.xml.sax.DTDHandler;
    1.16 -import org.xml.sax.EntityResolver;
    1.17 -import org.xml.sax.ErrorHandler;
    1.18  import org.xml.sax.InputSource;
    1.19  import org.xml.sax.SAXException;
    1.20 -import org.xml.sax.SAXNotRecognizedException;
    1.21 -import org.xml.sax.SAXNotSupportedException;
    1.22 -import org.xml.sax.XMLReader;
    1.23  
    1.24  /**
    1.25   *
    1.26 - * @author fiki
    1.27 + * @author Ing. František Kučera (frantovo.cz)
    1.28   */
    1.29 -public class Reader implements XMLReader {
    1.30 -
    1.31 -	private ContentHandler contentHandler;
    1.32 -	private ErrorHandler errorHandler;
    1.33 -	private DTDHandler dtdHandler;
    1.34 -	private EntityResolver entityResolver;
    1.35 -	private Map<String, Object> konfigurace = new HashMap<>();
    1.36 +public class Reader extends AbstractAlt2XmlReader {
    1.37  
    1.38  	@Override
    1.39  	public void parse(InputSource input) throws IOException, SAXException {
    1.40 -		/**
    1.41 -		 * TODO: rozpornat formát vstupu a podle toho delegovat
    1.42 -		 */
    1.43  		JSONParser p = new JSONParser();
    1.44  		InputStreamReader vstup = new InputStreamReader(input.getByteStream());
    1.45  		JsonSimpleContentHandler handler = new JsonSimpleContentHandler(contentHandler);
    1.46 @@ -57,78 +40,7 @@
    1.47  		try {
    1.48  			p.parse(vstup, handler);
    1.49  		} catch (ParseException e) {
    1.50 -			throw new SAXException("Chyba při načítání JSONu", e);
    1.51 +			throw new SAXException("Unable to parse JSON: " + input.getSystemId(), e);
    1.52  		}
    1.53  	}
    1.54 -
    1.55 -	@Override
    1.56 -	public void parse(String systemId) throws IOException, SAXException {
    1.57 -		parse(new InputSource(systemId));
    1.58 -	}
    1.59 -
    1.60 -	@Override
    1.61 -	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    1.62 -		/**
    1.63 -		 * TODO:
    1.64 -		 * All XMLReaders are required to recognize
    1.65 -		 * the http://xml.org/sax/features/namespaces
    1.66 -		 * and the http://xml.org/sax/features/namespace-prefixes feature names.
    1.67 -		 */
    1.68 -		throw new SAXNotSupportedException("Zatím není podporováno.");
    1.69 -	}
    1.70 -
    1.71 -	@Override
    1.72 -	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    1.73 -		throw new SAXNotSupportedException("Zatím není podporováno.");
    1.74 -	}
    1.75 -
    1.76 -	@Override
    1.77 -	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    1.78 -		return konfigurace.get(name);
    1.79 -	}
    1.80 -
    1.81 -	@Override
    1.82 -	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
    1.83 -		konfigurace.put(name, value);
    1.84 -	}
    1.85 -
    1.86 -	@Override
    1.87 -	public void setEntityResolver(EntityResolver entityResolver) {
    1.88 -		this.entityResolver = entityResolver;
    1.89 -	}
    1.90 -
    1.91 -	@Override
    1.92 -	public EntityResolver getEntityResolver() {
    1.93 -		return entityResolver;
    1.94 -	}
    1.95 -
    1.96 -	@Override
    1.97 -	public void setDTDHandler(DTDHandler dtdHandler) {
    1.98 -		this.dtdHandler = dtdHandler;
    1.99 -	}
   1.100 -
   1.101 -	@Override
   1.102 -	public DTDHandler getDTDHandler() {
   1.103 -		return dtdHandler;
   1.104 -	}
   1.105 -
   1.106 -	@Override
   1.107 -	public void setContentHandler(ContentHandler contentHandler) {
   1.108 -		this.contentHandler = contentHandler;
   1.109 -	}
   1.110 -
   1.111 -	@Override
   1.112 -	public ContentHandler getContentHandler() {
   1.113 -		return contentHandler;
   1.114 -	}
   1.115 -
   1.116 -	@Override
   1.117 -	public void setErrorHandler(ErrorHandler errorHandler) {
   1.118 -		this.errorHandler = errorHandler;
   1.119 -	}
   1.120 -
   1.121 -	@Override
   1.122 -	public ErrorHandler getErrorHandler() {
   1.123 -		return errorHandler;
   1.124 -	}
   1.125  }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/java/alt2xml-lib/src/cz/frantovo/alt2xml/AbstractAlt2XmlReader.java	Sat Jun 07 11:23:58 2014 +0200
     2.3 @@ -0,0 +1,116 @@
     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;
    2.22 +
    2.23 +import java.io.IOException;
    2.24 +import java.util.HashMap;
    2.25 +import java.util.Map;
    2.26 +import org.xml.sax.ContentHandler;
    2.27 +import org.xml.sax.DTDHandler;
    2.28 +import org.xml.sax.EntityResolver;
    2.29 +import org.xml.sax.ErrorHandler;
    2.30 +import org.xml.sax.InputSource;
    2.31 +import org.xml.sax.SAXException;
    2.32 +import org.xml.sax.SAXNotRecognizedException;
    2.33 +import org.xml.sax.SAXNotSupportedException;
    2.34 +import org.xml.sax.XMLReader;
    2.35 +
    2.36 +/**
    2.37 + * Recommended base class for all alternative format readers.
    2.38 + *
    2.39 + * @author Ing. František Kučera (frantovo.cz)
    2.40 + */
    2.41 +public abstract class AbstractAlt2XmlReader implements XMLReader {
    2.42 +
    2.43 +	protected ContentHandler contentHandler;
    2.44 +	protected ErrorHandler errorHandler;
    2.45 +	protected DTDHandler dtdHandler;
    2.46 +	protected EntityResolver entityResolver;
    2.47 +	protected Map<String, Object> properties = new HashMap<>();
    2.48 +
    2.49 +	@Override
    2.50 +	public void parse(String systemId) throws IOException, SAXException {
    2.51 +		parse(new InputSource(systemId));
    2.52 +	}
    2.53 +
    2.54 +	@Override
    2.55 +	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    2.56 +		/**
    2.57 +		 * TODO:
    2.58 +		 * All XMLReaders are required to recognize
    2.59 +		 * the http://xml.org/sax/features/namespaces
    2.60 +		 * and the http://xml.org/sax/features/namespace-prefixes feature names.
    2.61 +		 */
    2.62 +		throw new SAXNotSupportedException("Zatím není podporováno.");
    2.63 +	}
    2.64 +
    2.65 +	@Override
    2.66 +	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    2.67 +		throw new SAXNotSupportedException("Zatím není podporováno.");
    2.68 +	}
    2.69 +
    2.70 +	@Override
    2.71 +	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    2.72 +		return properties.get(name);
    2.73 +	}
    2.74 +
    2.75 +	@Override
    2.76 +	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
    2.77 +		properties.put(name, value);
    2.78 +	}
    2.79 +
    2.80 +	@Override
    2.81 +	public void setEntityResolver(EntityResolver entityResolver) {
    2.82 +		this.entityResolver = entityResolver;
    2.83 +	}
    2.84 +
    2.85 +	@Override
    2.86 +	public EntityResolver getEntityResolver() {
    2.87 +		return entityResolver;
    2.88 +	}
    2.89 +
    2.90 +	@Override
    2.91 +	public void setDTDHandler(DTDHandler dtdHandler) {
    2.92 +		this.dtdHandler = dtdHandler;
    2.93 +	}
    2.94 +
    2.95 +	@Override
    2.96 +	public DTDHandler getDTDHandler() {
    2.97 +		return dtdHandler;
    2.98 +	}
    2.99 +
   2.100 +	@Override
   2.101 +	public void setContentHandler(ContentHandler contentHandler) {
   2.102 +		this.contentHandler = contentHandler;
   2.103 +	}
   2.104 +
   2.105 +	@Override
   2.106 +	public ContentHandler getContentHandler() {
   2.107 +		return contentHandler;
   2.108 +	}
   2.109 +
   2.110 +	@Override
   2.111 +	public void setErrorHandler(ErrorHandler errorHandler) {
   2.112 +		this.errorHandler = errorHandler;
   2.113 +	}
   2.114 +
   2.115 +	@Override
   2.116 +	public ErrorHandler getErrorHandler() {
   2.117 +		return errorHandler;
   2.118 +	}
   2.119 +}