java/alt2xml/src/cz/frantovo/alt2xml/vstup/SuperReader.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 03 Jan 2012 14:47:22 +0100
changeset 6 5cfb685d8eec
parent 5 c2496cf043c3
permissions -rw-r--r--
Uspořádání metod.
     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 		throw new SAXNotSupportedException("Zatím není podporováno.");
    55 	}
    56 
    57 	@Override
    58 	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    59 		throw new SAXNotSupportedException("Zatím není podporováno.");
    60 	}
    61 
    62 	@Override
    63 	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    64 		return konfigurace.get(name);
    65 	}
    66 
    67 	@Override
    68 	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
    69 		konfigurace.put(name, value);
    70 	}
    71 
    72 	@Override
    73 	public void setEntityResolver(EntityResolver entityResolver) {
    74 		this.entityResolver = entityResolver;
    75 	}
    76 
    77 	@Override
    78 	public EntityResolver getEntityResolver() {
    79 		return entityResolver;
    80 	}
    81 
    82 	@Override
    83 	public void setDTDHandler(DTDHandler dtdHandler) {
    84 		this.dtdHandler = dtdHandler;
    85 	}
    86 
    87 	@Override
    88 	public DTDHandler getDTDHandler() {
    89 		return dtdHandler;
    90 	}
    91 
    92 	@Override
    93 	public void setContentHandler(ContentHandler contentHandler) {
    94 		this.contentHandler = contentHandler;
    95 	}
    96 
    97 	@Override
    98 	public ContentHandler getContentHandler() {
    99 		return contentHandler;
   100 	}
   101 
   102 	@Override
   103 	public void setErrorHandler(ErrorHandler errorHandler) {
   104 		this.errorHandler = errorHandler;
   105 	}
   106 
   107 	@Override
   108 	public ErrorHandler getErrorHandler() {
   109 		return errorHandler;
   110 	}
   111 }