AltInputSource, alt2xml.sh, první fungující verze .properties readeru
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 07 Jun 2014 15:42:48 +0200
changeset 2223a12c58b57c
parent 21 ac4e617f44e7
child 23 6ccf0c9b34dc
AltInputSource, alt2xml.sh, první fungující verze .properties readeru
java/alt2xml-in-properties/src/cz/frantovo/alt2xml/in/properties/Reader.java
java/alt2xml-lib/src/cz/frantovo/alt2xml/AltInputSource.java
java/alt2xml-lib/src/cz/frantovo/alt2xml/SuperReader.java
scripts/alt2xml.sh
     1.1 --- a/java/alt2xml-in-properties/src/cz/frantovo/alt2xml/in/properties/Reader.java	Sat Jun 07 15:03:52 2014 +0200
     1.2 +++ b/java/alt2xml-in-properties/src/cz/frantovo/alt2xml/in/properties/Reader.java	Sat Jun 07 15:42:48 2014 +0200
     1.3 @@ -51,7 +51,7 @@
     1.4  			String value = (String) e.getValue();
     1.5  
     1.6  			AttributesImpl attributes = new AttributesImpl();
     1.7 -			attributes.setAttribute(0, null, "name", "name", "xs:string", key);
     1.8 +			attributes.addAttribute(null, "name", "name", "xs:string", key);
     1.9  
    1.10  			contentHandler.startElement(null, null, "property", attributes);
    1.11  			contentHandler.characters(value.toCharArray(), 0, value.length());
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/java/alt2xml-lib/src/cz/frantovo/alt2xml/AltInputSource.java	Sat Jun 07 15:42:48 2014 +0200
     2.3 @@ -0,0 +1,127 @@
     2.4 +/**
     2.5 + * Alt2XML
     2.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, either version 3 of the License, or
    2.11 + * (at your option) any later version.
    2.12 + *
    2.13 + * This program is distributed in the hope that it will be useful,
    2.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    2.16 + * GNU General Public License for more details.
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License
    2.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    2.20 + */
    2.21 +package cz.frantovo.alt2xml;
    2.22 +
    2.23 +import java.io.IOException;
    2.24 +import java.io.InputStream;
    2.25 +import java.io.InputStreamReader;
    2.26 +import java.io.Reader;
    2.27 +import java.io.UnsupportedEncodingException;
    2.28 +import java.net.MalformedURLException;
    2.29 +import java.net.URL;
    2.30 +import java.util.logging.Level;
    2.31 +import java.util.logging.Logger;
    2.32 +import org.xml.sax.EntityResolver;
    2.33 +import org.xml.sax.InputSource;
    2.34 +import org.xml.sax.SAXException;
    2.35 +
    2.36 +/**
    2.37 + *
    2.38 + * @author Ing. František Kučera (frantovo.cz)
    2.39 + */
    2.40 +public class AltInputSource extends InputSource {
    2.41 +
    2.42 +	private final EntityResolver entityResolver;
    2.43 +	private final InputSource originalSource;
    2.44 +
    2.45 +	private static final Logger log = Logger.getLogger(AltInputSource.class.getName());
    2.46 +
    2.47 +	public AltInputSource(EntityResolver entityResolver, InputSource originalSource) {
    2.48 +		this.entityResolver = entityResolver;
    2.49 +		this.originalSource = originalSource;
    2.50 +	}
    2.51 +
    2.52 +	@Override
    2.53 +	public InputStream getByteStream() {
    2.54 +		InputStream originalStream = originalSource.getByteStream();
    2.55 +
    2.56 +		if (originalStream == null) {
    2.57 +
    2.58 +			InputSource source = originalSource;
    2.59 +
    2.60 +			if (entityResolver != null) {
    2.61 +				try {
    2.62 +					InputSource resolvedSource = entityResolver.resolveEntity(originalSource.getSystemId(), originalSource.getSystemId());
    2.63 +					if (resolvedSource != null) {
    2.64 +						source = resolvedSource;
    2.65 +					}
    2.66 +				} catch (IOException | SAXException e) {
    2.67 +					log.log(Level.WARNING, "Error while resolving InputSource – publicId: " + originalSource.getPublicId() + " systemId: " + originalSource.getSystemId(), e);
    2.68 +				}
    2.69 +			}
    2.70 +
    2.71 +			InputStream resolvedStream = source.getByteStream();
    2.72 +
    2.73 +			if (resolvedStream == null) {
    2.74 +
    2.75 +				try {
    2.76 +					URL url = new URL(source.getSystemId());
    2.77 +					return url.openStream();
    2.78 +				} catch (MalformedURLException e) {
    2.79 +					log.log(Level.WARNING, "Invalid SystemId URL syntax: " + originalSource.getSystemId(), e);
    2.80 +					return null;
    2.81 +				} catch (IOException e) {
    2.82 +					log.log(Level.WARNING, "Unable to open stream for systemId: " + originalSource.getSystemId(), e);
    2.83 +					return null;
    2.84 +				}
    2.85 +			} else {
    2.86 +				return resolvedStream;
    2.87 +			}
    2.88 +
    2.89 +		} else {
    2.90 +			return originalStream;
    2.91 +		}
    2.92 +	}
    2.93 +
    2.94 +	@Override
    2.95 +	public Reader getCharacterStream() {
    2.96 +		Reader originalStream = originalSource.getCharacterStream();
    2.97 +
    2.98 +		if (originalStream == null) {
    2.99 +			String encoding = getEncoding();
   2.100 +
   2.101 +			if (encoding == null) {
   2.102 +				return new InputStreamReader(getByteStream());
   2.103 +			} else {
   2.104 +				try {
   2.105 +					return new InputStreamReader(getByteStream(), encoding);
   2.106 +				} catch (UnsupportedEncodingException e) {
   2.107 +					log.log(Level.WARNING, "Invalid encoding: " + encoding, e);
   2.108 +					return null;
   2.109 +				}
   2.110 +			}
   2.111 +		} else {
   2.112 +			return originalStream;
   2.113 +		}
   2.114 +	}
   2.115 +
   2.116 +	@Override
   2.117 +	public String getEncoding() {
   2.118 +		return originalSource.getEncoding();
   2.119 +	}
   2.120 +
   2.121 +	@Override
   2.122 +	public String getPublicId() {
   2.123 +		return originalSource.getPublicId();
   2.124 +	}
   2.125 +
   2.126 +	@Override
   2.127 +	public String getSystemId() {
   2.128 +		return originalSource.getSystemId();
   2.129 +	}
   2.130 +}
     3.1 --- a/java/alt2xml-lib/src/cz/frantovo/alt2xml/SuperReader.java	Sat Jun 07 15:03:52 2014 +0200
     3.2 +++ b/java/alt2xml-lib/src/cz/frantovo/alt2xml/SuperReader.java	Sat Jun 07 15:42:48 2014 +0200
     3.3 @@ -18,9 +18,9 @@
     3.4  package cz.frantovo.alt2xml;
     3.5  
     3.6  import java.io.IOException;
     3.7 -import java.net.URI;
     3.8  import java.util.HashMap;
     3.9  import java.util.Map;
    3.10 +import java.util.Map.Entry;
    3.11  import org.xml.sax.ContentHandler;
    3.12  import org.xml.sax.DTDHandler;
    3.13  import org.xml.sax.EntityResolver;
    3.14 @@ -41,7 +41,8 @@
    3.15  	private ErrorHandler errorHandler;
    3.16  	private DTDHandler dtdHandler;
    3.17  	private EntityResolver entityResolver;
    3.18 -	private Map<String, Object> properties = new HashMap<>();
    3.19 +	private final Map<String, Object> properties = new HashMap<>();
    3.20 +	private final Map<String, Boolean> features = new HashMap<>();
    3.21  	private final ReaderFinder readerFinder;
    3.22  
    3.23  	public SuperReader(ReaderFinder readerFinder) {
    3.24 @@ -51,9 +52,25 @@
    3.25  	@Override
    3.26  	public void parse(InputSource input) throws IOException, SAXException {
    3.27  
    3.28 +		input = new AltInputSource(entityResolver, input);
    3.29 +
    3.30  		System.err.println("SystemId: " + input.getSystemId());
    3.31 -		
    3.32 +
    3.33  		XMLReader reader = readerFinder.findReader(input.getSystemId());
    3.34 +
    3.35 +		reader.setContentHandler(contentHandler);
    3.36 +		reader.setDTDHandler(dtdHandler);
    3.37 +		reader.setEntityResolver(entityResolver);
    3.38 +		reader.setErrorHandler(errorHandler);
    3.39 +
    3.40 +		for (Entry<String, Object> e : properties.entrySet()) {
    3.41 +			reader.setProperty(e.getKey(), e.getValue());
    3.42 +		}
    3.43 +
    3.44 +		for (Entry<String, Boolean> e : features.entrySet()) {
    3.45 +			reader.setFeature(e.getKey(), e.getValue());
    3.46 +		}
    3.47 +
    3.48  		reader.parse(input);
    3.49  	}
    3.50  
    3.51 @@ -70,12 +87,15 @@
    3.52  		 * the http://xml.org/sax/features/namespaces
    3.53  		 * and the http://xml.org/sax/features/namespace-prefixes feature names.
    3.54  		 */
    3.55 -		throw new SAXNotSupportedException("Zatím není podporováno.");
    3.56 +		return features.get(name);
    3.57  	}
    3.58  
    3.59  	@Override
    3.60  	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    3.61 -		throw new SAXNotSupportedException("Zatím není podporováno.");
    3.62 +		/**
    3.63 +		 * TODO: filtrovat – povolit jen náš jmenný prostor
    3.64 +		 */
    3.65 +		features.put(name, value);
    3.66  	}
    3.67  
    3.68  	@Override
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/scripts/alt2xml.sh	Sat Jun 07 15:42:48 2014 +0200
     4.3 @@ -0,0 +1,21 @@
     4.4 +#!/bin/bash
     4.5 +
     4.6 +DIR="`dirname $0`/..";
     4.7 +
     4.8 +STANDARD_JARS=(
     4.9 +	"$DIR/java/alt2xml-cli/dist/alt2xml-cli.jar"
    4.10 +	"$DIR/java/alt2xml-lib/dist/alt2xml-lib.jar"
    4.11 +);
    4.12 +
    4.13 +PLUGINS=(
    4.14 +	"$DIR/java/alt2xml-in-properties/dist/alt2xml-in-properties.jar"
    4.15 +);
    4.16 +
    4.17 +for e in "${STANDARD_JARS[@]}" "${PLUGINS[@]}"; do
    4.18 +	CLASS_PATH="$CLASS_PATH:$e";
    4.19 +done
    4.20 +
    4.21 +MAIN_CLASS="cz.frantovo.alt2xml.cli.CLI";
    4.22 +
    4.23 +java -cp "$CLASS_PATH" $MAIN_CLASS "$@"
    4.24 +