java/alt2xml-in-json/src/cz/frantovo/alt2xml/vstup/SuperReader.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 05 Jun 2014 15:03:50 +0200
changeset 10 f56a5369245e
parent 7 java/alt2xml-bin/src/cz/frantovo/alt2xml/vstup/SuperReader.java@9107f7df660c
permissions -rw-r--r--
rozdělení na více projektů: přesun tříd
     1 package cz.frantovo.alt2xml.vstup;
     2 
     3 import java.io.IOException;
     4 import java.io.InputStreamReader;
     5 import java.util.HashMap;
     6 import java.util.Map;
     7 import org.json.simple.parser.JSONParser;
     8 import org.json.simple.parser.ParseException;
     9 import org.xml.sax.ContentHandler;
    10 import org.xml.sax.DTDHandler;
    11 import org.xml.sax.EntityResolver;
    12 import org.xml.sax.ErrorHandler;
    13 import org.xml.sax.InputSource;
    14 import org.xml.sax.SAXException;
    15 import org.xml.sax.SAXNotRecognizedException;
    16 import org.xml.sax.SAXNotSupportedException;
    17 import org.xml.sax.XMLReader;
    18 
    19 /**
    20  *
    21  * @author fiki
    22  */
    23 public class SuperReader implements XMLReader {
    24 
    25 	private ContentHandler contentHandler;
    26 	private ErrorHandler errorHandler;
    27 	private DTDHandler dtdHandler;
    28 	private EntityResolver entityResolver;
    29 	private Map<String, Object> konfigurace = new HashMap<>();
    30 	
    31 	@Override
    32 	public void parse(InputSource input) throws IOException, SAXException {
    33 		/**
    34 		 * TODO: rozpornat formát vstupu a podle toho delegovat
    35 		 */
    36 		JSONParser p = new JSONParser();
    37 		InputStreamReader vstup = new InputStreamReader(input.getByteStream());
    38 		JsonSimpleContentHandler handler = new JsonSimpleContentHandler(contentHandler);
    39 
    40 		try {
    41 			p.parse(vstup, handler);
    42 		} catch (ParseException e) {
    43 			throw new SAXException("Chyba při načítání JSONu", e);
    44 		}
    45 	}
    46 
    47 	@Override
    48 	public void parse(String systemId) throws IOException, SAXException {
    49 		parse(new InputSource(systemId));
    50 	}
    51 
    52 	@Override
    53 	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    54 		/**
    55 		 * TODO: 
    56 		 * All XMLReaders are required to recognize 
    57 		 * the http://xml.org/sax/features/namespaces 
    58 		 * and the http://xml.org/sax/features/namespace-prefixes feature names.
    59 		 */
    60 		throw new SAXNotSupportedException("Zatím není podporováno.");
    61 	}
    62 
    63 	@Override
    64 	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    65 		throw new SAXNotSupportedException("Zatím není podporováno.");
    66 	}
    67 
    68 	@Override
    69 	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    70 		return konfigurace.get(name);
    71 	}
    72 
    73 	@Override
    74 	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
    75 		konfigurace.put(name, value);
    76 	}
    77 
    78 	@Override
    79 	public void setEntityResolver(EntityResolver entityResolver) {
    80 		this.entityResolver = entityResolver;
    81 	}
    82 
    83 	@Override
    84 	public EntityResolver getEntityResolver() {
    85 		return entityResolver;
    86 	}
    87 
    88 	@Override
    89 	public void setDTDHandler(DTDHandler dtdHandler) {
    90 		this.dtdHandler = dtdHandler;
    91 	}
    92 
    93 	@Override
    94 	public DTDHandler getDTDHandler() {
    95 		return dtdHandler;
    96 	}
    97 
    98 	@Override
    99 	public void setContentHandler(ContentHandler contentHandler) {
   100 		this.contentHandler = contentHandler;
   101 	}
   102 
   103 	@Override
   104 	public ContentHandler getContentHandler() {
   105 		return contentHandler;
   106 	}
   107 
   108 	@Override
   109 	public void setErrorHandler(ErrorHandler errorHandler) {
   110 		this.errorHandler = errorHandler;
   111 	}
   112 
   113 	@Override
   114 	public ErrorHandler getErrorHandler() {
   115 		return errorHandler;
   116 	}
   117 }