1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/alt2xml-bin/src/cz/frantovo/alt2xml/vstup/SuperReader.java Thu Jun 05 10:07:34 2014 +0200
1.3 @@ -0,0 +1,117 @@
1.4 +package cz.frantovo.alt2xml.vstup;
1.5 +
1.6 +import java.io.IOException;
1.7 +import java.io.InputStreamReader;
1.8 +import java.util.HashMap;
1.9 +import java.util.Map;
1.10 +import org.json.simple.parser.JSONParser;
1.11 +import org.json.simple.parser.ParseException;
1.12 +import org.xml.sax.ContentHandler;
1.13 +import org.xml.sax.DTDHandler;
1.14 +import org.xml.sax.EntityResolver;
1.15 +import org.xml.sax.ErrorHandler;
1.16 +import org.xml.sax.InputSource;
1.17 +import org.xml.sax.SAXException;
1.18 +import org.xml.sax.SAXNotRecognizedException;
1.19 +import org.xml.sax.SAXNotSupportedException;
1.20 +import org.xml.sax.XMLReader;
1.21 +
1.22 +/**
1.23 + *
1.24 + * @author fiki
1.25 + */
1.26 +public class SuperReader implements XMLReader {
1.27 +
1.28 + private ContentHandler contentHandler;
1.29 + private ErrorHandler errorHandler;
1.30 + private DTDHandler dtdHandler;
1.31 + private EntityResolver entityResolver;
1.32 + private Map<String, Object> konfigurace = new HashMap<>();
1.33 +
1.34 + @Override
1.35 + public void parse(InputSource input) throws IOException, SAXException {
1.36 + /**
1.37 + * TODO: rozpornat formát vstupu a podle toho delegovat
1.38 + */
1.39 + JSONParser p = new JSONParser();
1.40 + InputStreamReader vstup = new InputStreamReader(input.getByteStream());
1.41 + JsonSimpleContentHandler handler = new JsonSimpleContentHandler(contentHandler);
1.42 +
1.43 + try {
1.44 + p.parse(vstup, handler);
1.45 + } catch (ParseException e) {
1.46 + throw new SAXException("Chyba při načítání JSONu", e);
1.47 + }
1.48 + }
1.49 +
1.50 + @Override
1.51 + public void parse(String systemId) throws IOException, SAXException {
1.52 + parse(new InputSource(systemId));
1.53 + }
1.54 +
1.55 + @Override
1.56 + public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
1.57 + /**
1.58 + * TODO:
1.59 + * All XMLReaders are required to recognize
1.60 + * the http://xml.org/sax/features/namespaces
1.61 + * and the http://xml.org/sax/features/namespace-prefixes feature names.
1.62 + */
1.63 + throw new SAXNotSupportedException("Zatím není podporováno.");
1.64 + }
1.65 +
1.66 + @Override
1.67 + public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
1.68 + throw new SAXNotSupportedException("Zatím není podporováno.");
1.69 + }
1.70 +
1.71 + @Override
1.72 + public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
1.73 + return konfigurace.get(name);
1.74 + }
1.75 +
1.76 + @Override
1.77 + public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
1.78 + konfigurace.put(name, value);
1.79 + }
1.80 +
1.81 + @Override
1.82 + public void setEntityResolver(EntityResolver entityResolver) {
1.83 + this.entityResolver = entityResolver;
1.84 + }
1.85 +
1.86 + @Override
1.87 + public EntityResolver getEntityResolver() {
1.88 + return entityResolver;
1.89 + }
1.90 +
1.91 + @Override
1.92 + public void setDTDHandler(DTDHandler dtdHandler) {
1.93 + this.dtdHandler = dtdHandler;
1.94 + }
1.95 +
1.96 + @Override
1.97 + public DTDHandler getDTDHandler() {
1.98 + return dtdHandler;
1.99 + }
1.100 +
1.101 + @Override
1.102 + public void setContentHandler(ContentHandler contentHandler) {
1.103 + this.contentHandler = contentHandler;
1.104 + }
1.105 +
1.106 + @Override
1.107 + public ContentHandler getContentHandler() {
1.108 + return contentHandler;
1.109 + }
1.110 +
1.111 + @Override
1.112 + public void setErrorHandler(ErrorHandler errorHandler) {
1.113 + this.errorHandler = errorHandler;
1.114 + }
1.115 +
1.116 + @Override
1.117 + public ErrorHandler getErrorHandler() {
1.118 + return errorHandler;
1.119 + }
1.120 +}