# HG changeset patch
# User František Kučera <franta-hg@frantovo.cz>
# Date 1402130311 -7200
# Node ID b890783e4043067db2030c5030cbe6fb8fe9c539
# Parent  471c0cda94c309f00756a5082d616e2cb3f34b96
META-INF/services, Alt2XmlReaderFactory, ReaderFinder

diff -r 471c0cda94c3 -r b890783e4043 java/alt2xml-bin/src/cz/frantovo/alt2xml/ReaderFinder.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/alt2xml-bin/src/cz/frantovo/alt2xml/ReaderFinder.java	Sat Jun 07 10:38:31 2014 +0200
@@ -0,0 +1,20 @@
+package cz.frantovo.alt2xml;
+
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+
+/**
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public interface ReaderFinder {
+
+	/**
+	 *
+	 * @param systemId systemId of the document which should be parsed
+	 * @return XMLReader appropriate for this document
+	 * @throws org.xml.sax.SAXException if we have no reader for given systemId
+	 */
+	public XMLReader findReader(String systemId) throws SAXException;
+
+}
diff -r 471c0cda94c3 -r b890783e4043 java/alt2xml-bin/src/cz/frantovo/alt2xml/SAXTovarna.java
--- a/java/alt2xml-bin/src/cz/frantovo/alt2xml/SAXTovarna.java	Thu Jun 05 23:45:24 2014 +0200
+++ b/java/alt2xml-bin/src/cz/frantovo/alt2xml/SAXTovarna.java	Sat Jun 07 10:38:31 2014 +0200
@@ -18,6 +18,9 @@
 package cz.frantovo.alt2xml;
 
 import cz.frantovo.alt2xml.vstup.SuperReader;
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.ServiceLoader;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
@@ -31,11 +34,31 @@
  *
  * @author fiki
  */
-public class SAXTovarna extends SAXParserFactory {
+public class SAXTovarna extends SAXParserFactory implements ReaderFinder {
+
+	private final Deque<Alt2XmlReaderFactory> readerFactories = new LinkedList();
+
+	public SAXTovarna() {
+		super();
+		for (Alt2XmlReaderFactory f : ServiceLoader.load(Alt2XmlReaderFactory.class)) {
+			readerFactories.add(f);
+		}
+
+	}
+
+	@Override
+	public XMLReader findReader(String systemId) throws SAXException {
+		for (Alt2XmlReaderFactory f : readerFactories) {
+			if (f.canRead(systemId)) {
+				return f.getReader();
+			}
+		}
+		throw new SAXException("Iterated over " + readerFactories.size() + " and was unable to find XMLReader for SystemId: " + systemId);
+	}
 
 	@Override
 	public SAXParser newSAXParser() throws ParserConfigurationException, SAXException {
-		return new AltSAXParser(new SuperReader());
+		return new AltSAXParser(new SuperReader(this));
 	}
 
 	@Override
@@ -87,4 +110,4 @@
 			return xmlReader.getProperty(name);
 		}
 	}
-}
\ No newline at end of file
+}
diff -r 471c0cda94c3 -r b890783e4043 java/alt2xml-bin/src/cz/frantovo/alt2xml/vstup/SuperReader.java
--- a/java/alt2xml-bin/src/cz/frantovo/alt2xml/vstup/SuperReader.java	Thu Jun 05 23:45:24 2014 +0200
+++ b/java/alt2xml-bin/src/cz/frantovo/alt2xml/vstup/SuperReader.java	Sat Jun 07 10:38:31 2014 +0200
@@ -17,12 +17,11 @@
  */
 package cz.frantovo.alt2xml.vstup;
 
+import cz.frantovo.alt2xml.ReaderFinder;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.HashMap;
 import java.util.Map;
-//import org.json.simple.parser.JSONParser;
-//import org.json.simple.parser.ParseException;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.DTDHandler;
 import org.xml.sax.EntityResolver;
@@ -44,7 +43,12 @@
 	private DTDHandler dtdHandler;
 	private EntityResolver entityResolver;
 	private Map<String, Object> konfigurace = new HashMap<>();
-	
+	private final ReaderFinder readerFinder;
+
+	public SuperReader(ReaderFinder readerFinder) {
+		this.readerFinder = readerFinder;
+	}
+
 	@Override
 	public void parse(InputSource input) throws IOException, SAXException {
 		/**
@@ -55,12 +59,12 @@
 		//JsonSimpleContentHandler handler = new JsonSimpleContentHandler(contentHandler);
 
 		/*
-		try {
-			p.parse(vstup, handler);
-		} catch (ParseException e) {
-			throw new SAXException("Chyba při načítání JSONu", e);
-		}
-		*/
+		 try {
+		 p.parse(vstup, handler);
+		 } catch (ParseException e) {
+		 throw new SAXException("Chyba při načítání JSONu", e);
+		 }
+		 */
 	}
 
 	@Override
@@ -71,9 +75,9 @@
 	@Override
 	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
 		/**
-		 * TODO: 
-		 * All XMLReaders are required to recognize 
-		 * the http://xml.org/sax/features/namespaces 
+		 * TODO:
+		 * All XMLReaders are required to recognize
+		 * 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.");
diff -r 471c0cda94c3 -r b890783e4043 java/alt2xml-in-json/config/META-INF/services/cz.frantovo.alt2xml.Alt2XmlReaderFactory
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/alt2xml-in-json/config/META-INF/services/cz.frantovo.alt2xml.Alt2XmlReaderFactory	Sat Jun 07 10:38:31 2014 +0200
@@ -0,0 +1,1 @@
+cz.frantovo.alt2xml.in.json.ReaderFactory
diff -r 471c0cda94c3 -r b890783e4043 java/alt2xml-in-json/nbproject/build-impl.xml
--- a/java/alt2xml-in-json/nbproject/build-impl.xml	Thu Jun 05 23:45:24 2014 +0200
+++ b/java/alt2xml-in-json/nbproject/build-impl.xml	Sat Jun 07 10:38:31 2014 +0200
@@ -127,6 +127,7 @@
         </condition>
         <condition property="have.sources">
             <or>
+                <available file="${src.config.dir}"/>
                 <available file="${src.dir}"/>
             </or>
         </condition>
@@ -223,6 +224,7 @@
         <!-- You can override this target in the ../build.xml file. -->
     </target>
     <target depends="-pre-init,-init-private,-init-user,-init-project,-do-init" name="-init-check">
+        <fail unless="src.config.dir">Must set src.config.dir</fail>
         <fail unless="src.dir">Must set src.dir</fail>
         <fail unless="test.src.dir">Must set test.src.dir</fail>
         <fail unless="build.dir">Must set build.dir</fail>
@@ -245,7 +247,7 @@
     </target>
     <target depends="-init-ap-cmdline-properties" if="ap.supported.internal" name="-init-macrodef-javac-with-processors">
         <macrodef name="javac" uri="http://www.netbeans.org/ns/j2se-project/3">
-            <attribute default="${src.dir}" name="srcdir"/>
+            <attribute default="${src.config.dir}:${src.dir}" name="srcdir"/>
             <attribute default="${build.classes.dir}" name="destdir"/>
             <attribute default="${javac.classpath}" name="classpath"/>
             <attribute default="${javac.processorpath}" name="processorpath"/>
@@ -286,7 +288,7 @@
     </target>
     <target depends="-init-ap-cmdline-properties" name="-init-macrodef-javac-without-processors" unless="ap.supported.internal">
         <macrodef name="javac" uri="http://www.netbeans.org/ns/j2se-project/3">
-            <attribute default="${src.dir}" name="srcdir"/>
+            <attribute default="${src.config.dir}:${src.dir}" name="srcdir"/>
             <attribute default="${build.classes.dir}" name="destdir"/>
             <attribute default="${javac.classpath}" name="classpath"/>
             <attribute default="${javac.processorpath}" name="processorpath"/>
@@ -319,7 +321,7 @@
     </target>
     <target depends="-init-macrodef-javac-with-processors,-init-macrodef-javac-without-processors" name="-init-macrodef-javac">
         <macrodef name="depend" uri="http://www.netbeans.org/ns/j2se-project/3">
-            <attribute default="${src.dir}" name="srcdir"/>
+            <attribute default="${src.config.dir}:${src.dir}" name="srcdir"/>
             <attribute default="${build.classes.dir}" name="destdir"/>
             <attribute default="${javac.classpath}" name="classpath"/>
             <sequential>
@@ -925,11 +927,12 @@
                 <include name="*"/>
             </dirset>
         </pathconvert>
-        <j2seproject3:depend srcdir="${src.dir}:${build.generated.subdirs}"/>
+        <j2seproject3:depend srcdir="${src.config.dir}:${src.dir}:${build.generated.subdirs}"/>
     </target>
     <target depends="init,deps-jar,-pre-pre-compile,-pre-compile, -copy-persistence-xml,-compile-depend" if="have.sources" name="-do-compile">
         <j2seproject3:javac gensrcdir="${build.generated.sources.dir}"/>
         <copy todir="${build.classes.dir}">
+            <fileset dir="${src.config.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
             <fileset dir="${src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
         </copy>
     </target>
@@ -951,7 +954,7 @@
     <target depends="init,deps-jar,-pre-pre-compile" name="-do-compile-single">
         <fail unless="javac.includes">Must select some files in the IDE or set javac.includes</fail>
         <j2seproject3:force-recompile/>
-        <j2seproject3:javac excludes="" gensrcdir="${build.generated.sources.dir}" includes="${javac.includes}" sourcepath="${src.dir}"/>
+        <j2seproject3:javac excludes="" gensrcdir="${build.generated.sources.dir}" includes="${javac.includes}" sourcepath="${src.config.dir}:${src.dir}"/>
     </target>
     <target name="-post-compile-single">
         <!-- Empty placeholder for easier customization. -->
@@ -1217,6 +1220,9 @@
             <classpath>
                 <path path="${javac.classpath}"/>
             </classpath>
+            <fileset dir="${src.config.dir}" excludes="${bug5101868workaround},${excludes}" includes="${includes}">
+                <filename name="**/*.java"/>
+            </fileset>
             <fileset dir="${src.dir}" excludes="${bug5101868workaround},${excludes}" includes="${includes}">
                 <filename name="**/*.java"/>
             </fileset>
@@ -1227,6 +1233,9 @@
             <arg line="${javadoc.endorsed.classpath.cmd.line.arg}"/>
         </javadoc>
         <copy todir="${dist.javadoc.dir}">
+            <fileset dir="${src.config.dir}" excludes="${excludes}" includes="${includes}">
+                <filename name="**/doc-files/**"/>
+            </fileset>
             <fileset dir="${src.dir}" excludes="${excludes}" includes="${includes}">
                 <filename name="**/doc-files/**"/>
             </fileset>
diff -r 471c0cda94c3 -r b890783e4043 java/alt2xml-in-json/nbproject/genfiles.properties
--- a/java/alt2xml-in-json/nbproject/genfiles.properties	Thu Jun 05 23:45:24 2014 +0200
+++ b/java/alt2xml-in-json/nbproject/genfiles.properties	Sat Jun 07 10:38:31 2014 +0200
@@ -1,8 +1,8 @@
-build.xml.data.CRC32=2f58bfe6
+build.xml.data.CRC32=eaa6670b
 build.xml.script.CRC32=85cc1c66
 build.xml.stylesheet.CRC32=8064a381@1.74.2.48
 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
 # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
-nbproject/build-impl.xml.data.CRC32=2f58bfe6
-nbproject/build-impl.xml.script.CRC32=6c4ebf7c
+nbproject/build-impl.xml.data.CRC32=eaa6670b
+nbproject/build-impl.xml.script.CRC32=fae42019
 nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.74.2.48
diff -r 471c0cda94c3 -r b890783e4043 java/alt2xml-in-json/nbproject/project.properties
--- a/java/alt2xml-in-json/nbproject/project.properties	Thu Jun 05 23:45:24 2014 +0200
+++ b/java/alt2xml-in-json/nbproject/project.properties	Sat Jun 07 10:38:31 2014 +0200
@@ -1,9 +1,10 @@
 annotation.processing.enabled=true
 annotation.processing.enabled.in.editor=false
-annotation.processing.processor.options=
 annotation.processing.processors.list=
 annotation.processing.run.all.processors=true
 annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
+application.title=alt2xml-in-json
+application.vendor=fiki
 build.classes.dir=${build.dir}/classes
 build.classes.excludes=**/*.java,**/*.form
 # This directory is removed when the project is cleaned:
@@ -26,6 +27,7 @@
 dist.dir=dist
 dist.jar=${dist.dir}/alt2xml-in-json.jar
 dist.javadoc.dir=${dist.dir}/javadoc
+endorsed.classpath=
 excludes=
 includes=**
 jar.compress=false
@@ -71,5 +73,6 @@
     ${javac.test.classpath}:\
     ${build.test.classes.dir}
 source.encoding=UTF-8
+src.config.dir=config
 src.dir=src
 test.src.dir=test
diff -r 471c0cda94c3 -r b890783e4043 java/alt2xml-in-json/nbproject/project.xml
--- a/java/alt2xml-in-json/nbproject/project.xml	Thu Jun 05 23:45:24 2014 +0200
+++ b/java/alt2xml-in-json/nbproject/project.xml	Sat Jun 07 10:38:31 2014 +0200
@@ -5,6 +5,7 @@
         <data xmlns="http://www.netbeans.org/ns/j2se-project/3">
             <name>alt2xml-in-json</name>
             <source-roots>
+                <root id="src.config.dir" name="Config"/>
                 <root id="src.dir"/>
             </source-roots>
             <test-roots>
diff -r 471c0cda94c3 -r b890783e4043 java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/ReaderFactory.java
--- a/java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/ReaderFactory.java	Thu Jun 05 23:45:24 2014 +0200
+++ b/java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/ReaderFactory.java	Sat Jun 07 10:38:31 2014 +0200
@@ -17,23 +17,25 @@
  */
 package cz.frantovo.alt2xml.in.json;
 
-import cz.frantovo.alt2xml.Alt2XmlReaderFactory;
+import cz.frantovo.alt2xml.Alt2XmlRegexReaderFactory;
+import java.util.regex.Pattern;
 import org.xml.sax.XMLReader;
 
 /**
  *
  * @author Ing. František Kučera (frantovo.cz)
  */
-public class ReaderFactory implements Alt2XmlReaderFactory {
+public class ReaderFactory extends Alt2XmlRegexReaderFactory {
+
+	private static final Pattern pattern = Pattern.compile("^.*\\.json$");
 
 	@Override
-	public boolean canRead(String systemId) {
-		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+	protected Pattern getSystemIdPattern() {
+		return pattern;
 	}
 
 	@Override
 	public XMLReader getReader() {
-		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+		return new Reader();
 	}
-
 }
diff -r 471c0cda94c3 -r b890783e4043 java/alt2xml-lib/src/cz/frantovo/alt2xml/Alt2XmlRegexReaderFactory.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/alt2xml-lib/src/cz/frantovo/alt2xml/Alt2XmlRegexReaderFactory.java	Sat Jun 07 10:38:31 2014 +0200
@@ -0,0 +1,36 @@
+/**
+ * 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.util.regex.Pattern;
+
+/**
+ * Factory for XMLReaders that can read documents with systemId defined by given pattern (typically
+ * file extension like .ini, .properties etc.)
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public abstract class Alt2XmlRegexReaderFactory implements Alt2XmlReaderFactory {
+
+	protected abstract Pattern getSystemIdPattern();
+
+	@Override
+	public boolean canRead(String systemId) {
+		return getSystemIdPattern().matcher(systemId).matches();
+	}
+}