# HG changeset patch # User František Kučera # 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 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 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 @@ + @@ -223,6 +224,7 @@ + Must set src.config.dir Must set src.dir Must set test.src.dir Must set build.dir @@ -245,7 +247,7 @@ - + @@ -286,7 +288,7 @@ - + @@ -319,7 +321,7 @@ - + @@ -925,11 +927,12 @@ - + + @@ -951,7 +954,7 @@ Must select some files in the IDE or set javac.includes - + @@ -1217,6 +1220,9 @@ + + + @@ -1227,6 +1233,9 @@ + + + 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 @@ alt2xml-in-json + 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 . + */ +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(); + } +}