# HG changeset patch
# User František Kučera <franta-hg@frantovo.cz>
# Date 1402148568 -7200
# Node ID 23a12c58b57cd6484039779bbee79722f8dcdcc2
# Parent  ac4e617f44e77f9b391bc62671ed0d2cc7b7fdf3
AltInputSource, alt2xml.sh, první fungující verze .properties readeru

diff -r ac4e617f44e7 -r 23a12c58b57c java/alt2xml-in-properties/src/cz/frantovo/alt2xml/in/properties/Reader.java
--- a/java/alt2xml-in-properties/src/cz/frantovo/alt2xml/in/properties/Reader.java	Sat Jun 07 15:03:52 2014 +0200
+++ b/java/alt2xml-in-properties/src/cz/frantovo/alt2xml/in/properties/Reader.java	Sat Jun 07 15:42:48 2014 +0200
@@ -51,7 +51,7 @@
 			String value = (String) e.getValue();
 
 			AttributesImpl attributes = new AttributesImpl();
-			attributes.setAttribute(0, null, "name", "name", "xs:string", key);
+			attributes.addAttribute(null, "name", "name", "xs:string", key);
 
 			contentHandler.startElement(null, null, "property", attributes);
 			contentHandler.characters(value.toCharArray(), 0, value.length());
diff -r ac4e617f44e7 -r 23a12c58b57c java/alt2xml-lib/src/cz/frantovo/alt2xml/AltInputSource.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/alt2xml-lib/src/cz/frantovo/alt2xml/AltInputSource.java	Sat Jun 07 15:42:48 2014 +0200
@@ -0,0 +1,127 @@
+/**
+ * Alt2XML
+ * Copyright © 2014 František Kučera (frantovo.cz)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package cz.frantovo.alt2xml;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public class AltInputSource extends InputSource {
+
+	private final EntityResolver entityResolver;
+	private final InputSource originalSource;
+
+	private static final Logger log = Logger.getLogger(AltInputSource.class.getName());
+
+	public AltInputSource(EntityResolver entityResolver, InputSource originalSource) {
+		this.entityResolver = entityResolver;
+		this.originalSource = originalSource;
+	}
+
+	@Override
+	public InputStream getByteStream() {
+		InputStream originalStream = originalSource.getByteStream();
+
+		if (originalStream == null) {
+
+			InputSource source = originalSource;
+
+			if (entityResolver != null) {
+				try {
+					InputSource resolvedSource = entityResolver.resolveEntity(originalSource.getSystemId(), originalSource.getSystemId());
+					if (resolvedSource != null) {
+						source = resolvedSource;
+					}
+				} catch (IOException | SAXException e) {
+					log.log(Level.WARNING, "Error while resolving InputSource – publicId: " + originalSource.getPublicId() + " systemId: " + originalSource.getSystemId(), e);
+				}
+			}
+
+			InputStream resolvedStream = source.getByteStream();
+
+			if (resolvedStream == null) {
+
+				try {
+					URL url = new URL(source.getSystemId());
+					return url.openStream();
+				} catch (MalformedURLException e) {
+					log.log(Level.WARNING, "Invalid SystemId URL syntax: " + originalSource.getSystemId(), e);
+					return null;
+				} catch (IOException e) {
+					log.log(Level.WARNING, "Unable to open stream for systemId: " + originalSource.getSystemId(), e);
+					return null;
+				}
+			} else {
+				return resolvedStream;
+			}
+
+		} else {
+			return originalStream;
+		}
+	}
+
+	@Override
+	public Reader getCharacterStream() {
+		Reader originalStream = originalSource.getCharacterStream();
+
+		if (originalStream == null) {
+			String encoding = getEncoding();
+
+			if (encoding == null) {
+				return new InputStreamReader(getByteStream());
+			} else {
+				try {
+					return new InputStreamReader(getByteStream(), encoding);
+				} catch (UnsupportedEncodingException e) {
+					log.log(Level.WARNING, "Invalid encoding: " + encoding, e);
+					return null;
+				}
+			}
+		} else {
+			return originalStream;
+		}
+	}
+
+	@Override
+	public String getEncoding() {
+		return originalSource.getEncoding();
+	}
+
+	@Override
+	public String getPublicId() {
+		return originalSource.getPublicId();
+	}
+
+	@Override
+	public String getSystemId() {
+		return originalSource.getSystemId();
+	}
+}
diff -r ac4e617f44e7 -r 23a12c58b57c java/alt2xml-lib/src/cz/frantovo/alt2xml/SuperReader.java
--- a/java/alt2xml-lib/src/cz/frantovo/alt2xml/SuperReader.java	Sat Jun 07 15:03:52 2014 +0200
+++ b/java/alt2xml-lib/src/cz/frantovo/alt2xml/SuperReader.java	Sat Jun 07 15:42:48 2014 +0200
@@ -18,9 +18,9 @@
 package cz.frantovo.alt2xml;
 
 import java.io.IOException;
-import java.net.URI;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.DTDHandler;
 import org.xml.sax.EntityResolver;
@@ -41,7 +41,8 @@
 	private ErrorHandler errorHandler;
 	private DTDHandler dtdHandler;
 	private EntityResolver entityResolver;
-	private Map<String, Object> properties = new HashMap<>();
+	private final Map<String, Object> properties = new HashMap<>();
+	private final Map<String, Boolean> features = new HashMap<>();
 	private final ReaderFinder readerFinder;
 
 	public SuperReader(ReaderFinder readerFinder) {
@@ -51,9 +52,25 @@
 	@Override
 	public void parse(InputSource input) throws IOException, SAXException {
 
+		input = new AltInputSource(entityResolver, input);
+
 		System.err.println("SystemId: " + input.getSystemId());
-		
+
 		XMLReader reader = readerFinder.findReader(input.getSystemId());
+
+		reader.setContentHandler(contentHandler);
+		reader.setDTDHandler(dtdHandler);
+		reader.setEntityResolver(entityResolver);
+		reader.setErrorHandler(errorHandler);
+
+		for (Entry<String, Object> e : properties.entrySet()) {
+			reader.setProperty(e.getKey(), e.getValue());
+		}
+
+		for (Entry<String, Boolean> e : features.entrySet()) {
+			reader.setFeature(e.getKey(), e.getValue());
+		}
+
 		reader.parse(input);
 	}
 
@@ -70,12 +87,15 @@
 		 * the http://xml.org/sax/features/namespaces
 		 * and the http://xml.org/sax/features/namespace-prefixes feature names.
 		 */
-		throw new SAXNotSupportedException("Zatím není podporováno.");
+		return features.get(name);
 	}
 
 	@Override
 	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
-		throw new SAXNotSupportedException("Zatím není podporováno.");
+		/**
+		 * TODO: filtrovat – povolit jen náš jmenný prostor
+		 */
+		features.put(name, value);
 	}
 
 	@Override
diff -r ac4e617f44e7 -r 23a12c58b57c scripts/alt2xml.sh
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/alt2xml.sh	Sat Jun 07 15:42:48 2014 +0200
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+DIR="`dirname $0`/..";
+
+STANDARD_JARS=(
+	"$DIR/java/alt2xml-cli/dist/alt2xml-cli.jar"
+	"$DIR/java/alt2xml-lib/dist/alt2xml-lib.jar"
+);
+
+PLUGINS=(
+	"$DIR/java/alt2xml-in-properties/dist/alt2xml-in-properties.jar"
+);
+
+for e in "${STANDARD_JARS[@]}" "${PLUGINS[@]}"; do
+	CLASS_PATH="$CLASS_PATH:$e";
+done
+
+MAIN_CLASS="cz.frantovo.alt2xml.cli.CLI";
+
+java -cp "$CLASS_PATH" $MAIN_CLASS "$@"
+