META-INF/services, Alt2XmlReaderFactory, ReaderFinder
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 07 Jun 2014 10:38:31 +0200
changeset 15b890783e4043
parent 14 471c0cda94c3
child 16 b2fbb3570ae1
META-INF/services, Alt2XmlReaderFactory, ReaderFinder
java/alt2xml-bin/src/cz/frantovo/alt2xml/ReaderFinder.java
java/alt2xml-bin/src/cz/frantovo/alt2xml/SAXTovarna.java
java/alt2xml-bin/src/cz/frantovo/alt2xml/vstup/SuperReader.java
java/alt2xml-in-json/config/META-INF/services/cz.frantovo.alt2xml.Alt2XmlReaderFactory
java/alt2xml-in-json/nbproject/build-impl.xml
java/alt2xml-in-json/nbproject/genfiles.properties
java/alt2xml-in-json/nbproject/project.properties
java/alt2xml-in-json/nbproject/project.xml
java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/ReaderFactory.java
java/alt2xml-lib/src/cz/frantovo/alt2xml/Alt2XmlRegexReaderFactory.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/alt2xml-bin/src/cz/frantovo/alt2xml/ReaderFinder.java	Sat Jun 07 10:38:31 2014 +0200
     1.3 @@ -0,0 +1,20 @@
     1.4 +package cz.frantovo.alt2xml;
     1.5 +
     1.6 +import org.xml.sax.SAXException;
     1.7 +import org.xml.sax.XMLReader;
     1.8 +
     1.9 +/**
    1.10 + *
    1.11 + * @author Ing. František Kučera (frantovo.cz)
    1.12 + */
    1.13 +public interface ReaderFinder {
    1.14 +
    1.15 +	/**
    1.16 +	 *
    1.17 +	 * @param systemId systemId of the document which should be parsed
    1.18 +	 * @return XMLReader appropriate for this document
    1.19 +	 * @throws org.xml.sax.SAXException if we have no reader for given systemId
    1.20 +	 */
    1.21 +	public XMLReader findReader(String systemId) throws SAXException;
    1.22 +
    1.23 +}
     2.1 --- a/java/alt2xml-bin/src/cz/frantovo/alt2xml/SAXTovarna.java	Thu Jun 05 23:45:24 2014 +0200
     2.2 +++ b/java/alt2xml-bin/src/cz/frantovo/alt2xml/SAXTovarna.java	Sat Jun 07 10:38:31 2014 +0200
     2.3 @@ -18,6 +18,9 @@
     2.4  package cz.frantovo.alt2xml;
     2.5  
     2.6  import cz.frantovo.alt2xml.vstup.SuperReader;
     2.7 +import java.util.Deque;
     2.8 +import java.util.LinkedList;
     2.9 +import java.util.ServiceLoader;
    2.10  import javax.xml.parsers.ParserConfigurationException;
    2.11  import javax.xml.parsers.SAXParser;
    2.12  import javax.xml.parsers.SAXParserFactory;
    2.13 @@ -31,11 +34,31 @@
    2.14   *
    2.15   * @author fiki
    2.16   */
    2.17 -public class SAXTovarna extends SAXParserFactory {
    2.18 +public class SAXTovarna extends SAXParserFactory implements ReaderFinder {
    2.19 +
    2.20 +	private final Deque<Alt2XmlReaderFactory> readerFactories = new LinkedList();
    2.21 +
    2.22 +	public SAXTovarna() {
    2.23 +		super();
    2.24 +		for (Alt2XmlReaderFactory f : ServiceLoader.load(Alt2XmlReaderFactory.class)) {
    2.25 +			readerFactories.add(f);
    2.26 +		}
    2.27 +
    2.28 +	}
    2.29 +
    2.30 +	@Override
    2.31 +	public XMLReader findReader(String systemId) throws SAXException {
    2.32 +		for (Alt2XmlReaderFactory f : readerFactories) {
    2.33 +			if (f.canRead(systemId)) {
    2.34 +				return f.getReader();
    2.35 +			}
    2.36 +		}
    2.37 +		throw new SAXException("Iterated over " + readerFactories.size() + " and was unable to find XMLReader for SystemId: " + systemId);
    2.38 +	}
    2.39  
    2.40  	@Override
    2.41  	public SAXParser newSAXParser() throws ParserConfigurationException, SAXException {
    2.42 -		return new AltSAXParser(new SuperReader());
    2.43 +		return new AltSAXParser(new SuperReader(this));
    2.44  	}
    2.45  
    2.46  	@Override
    2.47 @@ -87,4 +110,4 @@
    2.48  			return xmlReader.getProperty(name);
    2.49  		}
    2.50  	}
    2.51 -}
    2.52 \ No newline at end of file
    2.53 +}
     3.1 --- a/java/alt2xml-bin/src/cz/frantovo/alt2xml/vstup/SuperReader.java	Thu Jun 05 23:45:24 2014 +0200
     3.2 +++ b/java/alt2xml-bin/src/cz/frantovo/alt2xml/vstup/SuperReader.java	Sat Jun 07 10:38:31 2014 +0200
     3.3 @@ -17,12 +17,11 @@
     3.4   */
     3.5  package cz.frantovo.alt2xml.vstup;
     3.6  
     3.7 +import cz.frantovo.alt2xml.ReaderFinder;
     3.8  import java.io.IOException;
     3.9  import java.io.InputStreamReader;
    3.10  import java.util.HashMap;
    3.11  import java.util.Map;
    3.12 -//import org.json.simple.parser.JSONParser;
    3.13 -//import org.json.simple.parser.ParseException;
    3.14  import org.xml.sax.ContentHandler;
    3.15  import org.xml.sax.DTDHandler;
    3.16  import org.xml.sax.EntityResolver;
    3.17 @@ -44,7 +43,12 @@
    3.18  	private DTDHandler dtdHandler;
    3.19  	private EntityResolver entityResolver;
    3.20  	private Map<String, Object> konfigurace = new HashMap<>();
    3.21 -	
    3.22 +	private final ReaderFinder readerFinder;
    3.23 +
    3.24 +	public SuperReader(ReaderFinder readerFinder) {
    3.25 +		this.readerFinder = readerFinder;
    3.26 +	}
    3.27 +
    3.28  	@Override
    3.29  	public void parse(InputSource input) throws IOException, SAXException {
    3.30  		/**
    3.31 @@ -55,12 +59,12 @@
    3.32  		//JsonSimpleContentHandler handler = new JsonSimpleContentHandler(contentHandler);
    3.33  
    3.34  		/*
    3.35 -		try {
    3.36 -			p.parse(vstup, handler);
    3.37 -		} catch (ParseException e) {
    3.38 -			throw new SAXException("Chyba při načítání JSONu", e);
    3.39 -		}
    3.40 -		*/
    3.41 +		 try {
    3.42 +		 p.parse(vstup, handler);
    3.43 +		 } catch (ParseException e) {
    3.44 +		 throw new SAXException("Chyba při načítání JSONu", e);
    3.45 +		 }
    3.46 +		 */
    3.47  	}
    3.48  
    3.49  	@Override
    3.50 @@ -71,9 +75,9 @@
    3.51  	@Override
    3.52  	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    3.53  		/**
    3.54 -		 * TODO: 
    3.55 -		 * All XMLReaders are required to recognize 
    3.56 -		 * the http://xml.org/sax/features/namespaces 
    3.57 +		 * TODO:
    3.58 +		 * All XMLReaders are required to recognize
    3.59 +		 * the http://xml.org/sax/features/namespaces
    3.60  		 * and the http://xml.org/sax/features/namespace-prefixes feature names.
    3.61  		 */
    3.62  		throw new SAXNotSupportedException("Zatím není podporováno.");
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/java/alt2xml-in-json/config/META-INF/services/cz.frantovo.alt2xml.Alt2XmlReaderFactory	Sat Jun 07 10:38:31 2014 +0200
     4.3 @@ -0,0 +1,1 @@
     4.4 +cz.frantovo.alt2xml.in.json.ReaderFactory
     5.1 --- a/java/alt2xml-in-json/nbproject/build-impl.xml	Thu Jun 05 23:45:24 2014 +0200
     5.2 +++ b/java/alt2xml-in-json/nbproject/build-impl.xml	Sat Jun 07 10:38:31 2014 +0200
     5.3 @@ -127,6 +127,7 @@
     5.4          </condition>
     5.5          <condition property="have.sources">
     5.6              <or>
     5.7 +                <available file="${src.config.dir}"/>
     5.8                  <available file="${src.dir}"/>
     5.9              </or>
    5.10          </condition>
    5.11 @@ -223,6 +224,7 @@
    5.12          <!-- You can override this target in the ../build.xml file. -->
    5.13      </target>
    5.14      <target depends="-pre-init,-init-private,-init-user,-init-project,-do-init" name="-init-check">
    5.15 +        <fail unless="src.config.dir">Must set src.config.dir</fail>
    5.16          <fail unless="src.dir">Must set src.dir</fail>
    5.17          <fail unless="test.src.dir">Must set test.src.dir</fail>
    5.18          <fail unless="build.dir">Must set build.dir</fail>
    5.19 @@ -245,7 +247,7 @@
    5.20      </target>
    5.21      <target depends="-init-ap-cmdline-properties" if="ap.supported.internal" name="-init-macrodef-javac-with-processors">
    5.22          <macrodef name="javac" uri="http://www.netbeans.org/ns/j2se-project/3">
    5.23 -            <attribute default="${src.dir}" name="srcdir"/>
    5.24 +            <attribute default="${src.config.dir}:${src.dir}" name="srcdir"/>
    5.25              <attribute default="${build.classes.dir}" name="destdir"/>
    5.26              <attribute default="${javac.classpath}" name="classpath"/>
    5.27              <attribute default="${javac.processorpath}" name="processorpath"/>
    5.28 @@ -286,7 +288,7 @@
    5.29      </target>
    5.30      <target depends="-init-ap-cmdline-properties" name="-init-macrodef-javac-without-processors" unless="ap.supported.internal">
    5.31          <macrodef name="javac" uri="http://www.netbeans.org/ns/j2se-project/3">
    5.32 -            <attribute default="${src.dir}" name="srcdir"/>
    5.33 +            <attribute default="${src.config.dir}:${src.dir}" name="srcdir"/>
    5.34              <attribute default="${build.classes.dir}" name="destdir"/>
    5.35              <attribute default="${javac.classpath}" name="classpath"/>
    5.36              <attribute default="${javac.processorpath}" name="processorpath"/>
    5.37 @@ -319,7 +321,7 @@
    5.38      </target>
    5.39      <target depends="-init-macrodef-javac-with-processors,-init-macrodef-javac-without-processors" name="-init-macrodef-javac">
    5.40          <macrodef name="depend" uri="http://www.netbeans.org/ns/j2se-project/3">
    5.41 -            <attribute default="${src.dir}" name="srcdir"/>
    5.42 +            <attribute default="${src.config.dir}:${src.dir}" name="srcdir"/>
    5.43              <attribute default="${build.classes.dir}" name="destdir"/>
    5.44              <attribute default="${javac.classpath}" name="classpath"/>
    5.45              <sequential>
    5.46 @@ -925,11 +927,12 @@
    5.47                  <include name="*"/>
    5.48              </dirset>
    5.49          </pathconvert>
    5.50 -        <j2seproject3:depend srcdir="${src.dir}:${build.generated.subdirs}"/>
    5.51 +        <j2seproject3:depend srcdir="${src.config.dir}:${src.dir}:${build.generated.subdirs}"/>
    5.52      </target>
    5.53      <target depends="init,deps-jar,-pre-pre-compile,-pre-compile, -copy-persistence-xml,-compile-depend" if="have.sources" name="-do-compile">
    5.54          <j2seproject3:javac gensrcdir="${build.generated.sources.dir}"/>
    5.55          <copy todir="${build.classes.dir}">
    5.56 +            <fileset dir="${src.config.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
    5.57              <fileset dir="${src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
    5.58          </copy>
    5.59      </target>
    5.60 @@ -951,7 +954,7 @@
    5.61      <target depends="init,deps-jar,-pre-pre-compile" name="-do-compile-single">
    5.62          <fail unless="javac.includes">Must select some files in the IDE or set javac.includes</fail>
    5.63          <j2seproject3:force-recompile/>
    5.64 -        <j2seproject3:javac excludes="" gensrcdir="${build.generated.sources.dir}" includes="${javac.includes}" sourcepath="${src.dir}"/>
    5.65 +        <j2seproject3:javac excludes="" gensrcdir="${build.generated.sources.dir}" includes="${javac.includes}" sourcepath="${src.config.dir}:${src.dir}"/>
    5.66      </target>
    5.67      <target name="-post-compile-single">
    5.68          <!-- Empty placeholder for easier customization. -->
    5.69 @@ -1217,6 +1220,9 @@
    5.70              <classpath>
    5.71                  <path path="${javac.classpath}"/>
    5.72              </classpath>
    5.73 +            <fileset dir="${src.config.dir}" excludes="${bug5101868workaround},${excludes}" includes="${includes}">
    5.74 +                <filename name="**/*.java"/>
    5.75 +            </fileset>
    5.76              <fileset dir="${src.dir}" excludes="${bug5101868workaround},${excludes}" includes="${includes}">
    5.77                  <filename name="**/*.java"/>
    5.78              </fileset>
    5.79 @@ -1227,6 +1233,9 @@
    5.80              <arg line="${javadoc.endorsed.classpath.cmd.line.arg}"/>
    5.81          </javadoc>
    5.82          <copy todir="${dist.javadoc.dir}">
    5.83 +            <fileset dir="${src.config.dir}" excludes="${excludes}" includes="${includes}">
    5.84 +                <filename name="**/doc-files/**"/>
    5.85 +            </fileset>
    5.86              <fileset dir="${src.dir}" excludes="${excludes}" includes="${includes}">
    5.87                  <filename name="**/doc-files/**"/>
    5.88              </fileset>
     6.1 --- a/java/alt2xml-in-json/nbproject/genfiles.properties	Thu Jun 05 23:45:24 2014 +0200
     6.2 +++ b/java/alt2xml-in-json/nbproject/genfiles.properties	Sat Jun 07 10:38:31 2014 +0200
     6.3 @@ -1,8 +1,8 @@
     6.4 -build.xml.data.CRC32=2f58bfe6
     6.5 +build.xml.data.CRC32=eaa6670b
     6.6  build.xml.script.CRC32=85cc1c66
     6.7  build.xml.stylesheet.CRC32=8064a381@1.74.2.48
     6.8  # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
     6.9  # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
    6.10 -nbproject/build-impl.xml.data.CRC32=2f58bfe6
    6.11 -nbproject/build-impl.xml.script.CRC32=6c4ebf7c
    6.12 +nbproject/build-impl.xml.data.CRC32=eaa6670b
    6.13 +nbproject/build-impl.xml.script.CRC32=fae42019
    6.14  nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.74.2.48
     7.1 --- a/java/alt2xml-in-json/nbproject/project.properties	Thu Jun 05 23:45:24 2014 +0200
     7.2 +++ b/java/alt2xml-in-json/nbproject/project.properties	Sat Jun 07 10:38:31 2014 +0200
     7.3 @@ -1,9 +1,10 @@
     7.4  annotation.processing.enabled=true
     7.5  annotation.processing.enabled.in.editor=false
     7.6 -annotation.processing.processor.options=
     7.7  annotation.processing.processors.list=
     7.8  annotation.processing.run.all.processors=true
     7.9  annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
    7.10 +application.title=alt2xml-in-json
    7.11 +application.vendor=fiki
    7.12  build.classes.dir=${build.dir}/classes
    7.13  build.classes.excludes=**/*.java,**/*.form
    7.14  # This directory is removed when the project is cleaned:
    7.15 @@ -26,6 +27,7 @@
    7.16  dist.dir=dist
    7.17  dist.jar=${dist.dir}/alt2xml-in-json.jar
    7.18  dist.javadoc.dir=${dist.dir}/javadoc
    7.19 +endorsed.classpath=
    7.20  excludes=
    7.21  includes=**
    7.22  jar.compress=false
    7.23 @@ -71,5 +73,6 @@
    7.24      ${javac.test.classpath}:\
    7.25      ${build.test.classes.dir}
    7.26  source.encoding=UTF-8
    7.27 +src.config.dir=config
    7.28  src.dir=src
    7.29  test.src.dir=test
     8.1 --- a/java/alt2xml-in-json/nbproject/project.xml	Thu Jun 05 23:45:24 2014 +0200
     8.2 +++ b/java/alt2xml-in-json/nbproject/project.xml	Sat Jun 07 10:38:31 2014 +0200
     8.3 @@ -5,6 +5,7 @@
     8.4          <data xmlns="http://www.netbeans.org/ns/j2se-project/3">
     8.5              <name>alt2xml-in-json</name>
     8.6              <source-roots>
     8.7 +                <root id="src.config.dir" name="Config"/>
     8.8                  <root id="src.dir"/>
     8.9              </source-roots>
    8.10              <test-roots>
     9.1 --- a/java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/ReaderFactory.java	Thu Jun 05 23:45:24 2014 +0200
     9.2 +++ b/java/alt2xml-in-json/src/cz/frantovo/alt2xml/in/json/ReaderFactory.java	Sat Jun 07 10:38:31 2014 +0200
     9.3 @@ -17,23 +17,25 @@
     9.4   */
     9.5  package cz.frantovo.alt2xml.in.json;
     9.6  
     9.7 -import cz.frantovo.alt2xml.Alt2XmlReaderFactory;
     9.8 +import cz.frantovo.alt2xml.Alt2XmlRegexReaderFactory;
     9.9 +import java.util.regex.Pattern;
    9.10  import org.xml.sax.XMLReader;
    9.11  
    9.12  /**
    9.13   *
    9.14   * @author Ing. František Kučera (frantovo.cz)
    9.15   */
    9.16 -public class ReaderFactory implements Alt2XmlReaderFactory {
    9.17 +public class ReaderFactory extends Alt2XmlRegexReaderFactory {
    9.18 +
    9.19 +	private static final Pattern pattern = Pattern.compile("^.*\\.json$");
    9.20  
    9.21  	@Override
    9.22 -	public boolean canRead(String systemId) {
    9.23 -		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    9.24 +	protected Pattern getSystemIdPattern() {
    9.25 +		return pattern;
    9.26  	}
    9.27  
    9.28  	@Override
    9.29  	public XMLReader getReader() {
    9.30 -		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    9.31 +		return new Reader();
    9.32  	}
    9.33 -
    9.34  }
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/java/alt2xml-lib/src/cz/frantovo/alt2xml/Alt2XmlRegexReaderFactory.java	Sat Jun 07 10:38:31 2014 +0200
    10.3 @@ -0,0 +1,36 @@
    10.4 +/**
    10.5 + * Alt2XML
    10.6 + * Copyright © 2014 František Kučera (frantovo.cz)
    10.7 + *
    10.8 + * This program is free software: you can redistribute it and/or modify
    10.9 + * it under the terms of the GNU General Public License as published by
   10.10 + * the Free Software Foundation, either version 3 of the License, or
   10.11 + * (at your option) any later version.
   10.12 + *
   10.13 + * This program is distributed in the hope that it will be useful,
   10.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
   10.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   10.16 + * GNU General Public License for more details.
   10.17 + *
   10.18 + * You should have received a copy of the GNU General Public License
   10.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
   10.20 + */
   10.21 +package cz.frantovo.alt2xml;
   10.22 +
   10.23 +import java.util.regex.Pattern;
   10.24 +
   10.25 +/**
   10.26 + * Factory for XMLReaders that can read documents with systemId defined by given pattern (typically
   10.27 + * file extension like .ini, .properties etc.)
   10.28 + *
   10.29 + * @author Ing. František Kučera (frantovo.cz)
   10.30 + */
   10.31 +public abstract class Alt2XmlRegexReaderFactory implements Alt2XmlReaderFactory {
   10.32 +
   10.33 +	protected abstract Pattern getSystemIdPattern();
   10.34 +
   10.35 +	@Override
   10.36 +	public boolean canRead(String systemId) {
   10.37 +		return getSystemIdPattern().matcher(systemId).matches();
   10.38 +	}
   10.39 +}