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