java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/Reader.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 05 Jun 2014 23:45:24 +0200
changeset 14 471c0cda94c3
parent 13 java/alt2xml-in-json/src/cz/frantovo/alt2xml/vstup/JsonXMLReader.java@a598b9d90144
child 17 64358dd0999f
permissions -rw-r--r--
balíček pro JSON
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.alt2xml.in.json;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStreamReader;
    22 import java.util.HashMap;
    23 import java.util.Map;
    24 import org.json.simple.parser.JSONParser;
    25 import org.json.simple.parser.ParseException;
    26 import org.xml.sax.ContentHandler;
    27 import org.xml.sax.DTDHandler;
    28 import org.xml.sax.EntityResolver;
    29 import org.xml.sax.ErrorHandler;
    30 import org.xml.sax.InputSource;
    31 import org.xml.sax.SAXException;
    32 import org.xml.sax.SAXNotRecognizedException;
    33 import org.xml.sax.SAXNotSupportedException;
    34 import org.xml.sax.XMLReader;
    35 
    36 /**
    37  *
    38  * @author fiki
    39  */
    40 public class Reader implements XMLReader {
    41 
    42 	private ContentHandler contentHandler;
    43 	private ErrorHandler errorHandler;
    44 	private DTDHandler dtdHandler;
    45 	private EntityResolver entityResolver;
    46 	private Map<String, Object> konfigurace = new HashMap<>();
    47 
    48 	@Override
    49 	public void parse(InputSource input) throws IOException, SAXException {
    50 		/**
    51 		 * TODO: rozpornat formát vstupu a podle toho delegovat
    52 		 */
    53 		JSONParser p = new JSONParser();
    54 		InputStreamReader vstup = new InputStreamReader(input.getByteStream());
    55 		JsonSimpleContentHandler handler = new JsonSimpleContentHandler(contentHandler);
    56 
    57 		try {
    58 			p.parse(vstup, handler);
    59 		} catch (ParseException e) {
    60 			throw new SAXException("Chyba při načítání JSONu", e);
    61 		}
    62 	}
    63 
    64 	@Override
    65 	public void parse(String systemId) throws IOException, SAXException {
    66 		parse(new InputSource(systemId));
    67 	}
    68 
    69 	@Override
    70 	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    71 		/**
    72 		 * TODO:
    73 		 * All XMLReaders are required to recognize
    74 		 * the http://xml.org/sax/features/namespaces
    75 		 * and the http://xml.org/sax/features/namespace-prefixes feature names.
    76 		 */
    77 		throw new SAXNotSupportedException("Zatím není podporováno.");
    78 	}
    79 
    80 	@Override
    81 	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    82 		throw new SAXNotSupportedException("Zatím není podporováno.");
    83 	}
    84 
    85 	@Override
    86 	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    87 		return konfigurace.get(name);
    88 	}
    89 
    90 	@Override
    91 	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
    92 		konfigurace.put(name, value);
    93 	}
    94 
    95 	@Override
    96 	public void setEntityResolver(EntityResolver entityResolver) {
    97 		this.entityResolver = entityResolver;
    98 	}
    99 
   100 	@Override
   101 	public EntityResolver getEntityResolver() {
   102 		return entityResolver;
   103 	}
   104 
   105 	@Override
   106 	public void setDTDHandler(DTDHandler dtdHandler) {
   107 		this.dtdHandler = dtdHandler;
   108 	}
   109 
   110 	@Override
   111 	public DTDHandler getDTDHandler() {
   112 		return dtdHandler;
   113 	}
   114 
   115 	@Override
   116 	public void setContentHandler(ContentHandler contentHandler) {
   117 		this.contentHandler = contentHandler;
   118 	}
   119 
   120 	@Override
   121 	public ContentHandler getContentHandler() {
   122 		return contentHandler;
   123 	}
   124 
   125 	@Override
   126 	public void setErrorHandler(ErrorHandler errorHandler) {
   127 		this.errorHandler = errorHandler;
   128 	}
   129 
   130 	@Override
   131 	public ErrorHandler getErrorHandler() {
   132 		return errorHandler;
   133 	}
   134 }