# HG changeset patch # User František Kučera # Date 1410980432 -7200 # Node ID fadfde5b3e5561848bea128ff3ea9e0768f262ae # Parent 944a81a54bb93b2bcf8895efbf5fa9c3cd20acbe cli, lib-input: propagate features and properties to parser/reader diff -r 944a81a54bb9 -r fadfde5b3e55 java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java --- a/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java Wed Sep 17 19:13:47 2014 +0200 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java Wed Sep 17 21:00:32 2014 +0200 @@ -23,6 +23,8 @@ import cz.frantovo.alt2xml.out.OutputActionException; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.LogRecord; @@ -31,6 +33,8 @@ import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; /** * @@ -58,7 +62,7 @@ try { CLIParser cliParser = new CLIParser(); CLIOptions cliOptions = cliParser.parseOptions(args); - + cliOptions.validate(); Map actionFactories = new HashMap<>(); @@ -79,14 +83,17 @@ } else { ActionContext actionContext = new ActionContext(cliOptions.getOutputStream()); - + actionContext.setActionProperties(cliOptions.getActionProperties()); actionContext.setActionData(cliOptions.getActionData()); - + Action action = actionFactory.getAction(actionContext); SAXParserFactory t = SAXParserFactory.newInstance(); + parametrizeParserFactory(t, cliOptions.getReaderFeatures()); + SAXParser p = t.newSAXParser(); + paramtrizeParser(p, cliOptions.getReaderProperties()); action.run(p, cliOptions.getInputSource()); } @@ -115,4 +122,16 @@ System.exit(exitCode); } + + private static void parametrizeParserFactory(SAXParserFactory factory, Map features) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException { + for (Entry feature : features.entrySet()) { + factory.setFeature(feature.getKey(), feature.getValue()); + } + } + + private static void paramtrizeParser(SAXParser parser, Properties readerProperties) throws SAXNotRecognizedException, SAXNotSupportedException { + for (String name : readerProperties.stringPropertyNames()) { + parser.setProperty(name, readerProperties.getProperty(name)); + } + } } diff -r 944a81a54bb9 -r fadfde5b3e55 java/alt2xml-lib-input/src/cz/frantovo/alt2xml/ParserFactory.java --- a/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/ParserFactory.java Wed Sep 17 19:13:47 2014 +0200 +++ b/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/ParserFactory.java Wed Sep 17 21:00:32 2014 +0200 @@ -18,7 +18,10 @@ package cz.frantovo.alt2xml; import java.util.Deque; +import java.util.HashMap; import java.util.LinkedList; +import java.util.Map; +import java.util.Map.Entry; import java.util.ServiceLoader; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; @@ -47,6 +50,8 @@ */ public static final String DEFAULT_FACTORY_PROPERTY = "cz.frantovo.alt2xml.fallback.javax.xml.parsers.SAXParserFactory"; + private final Map features = new HashMap<>(); + public ParserFactory() { super(); for (Alt2XmlReaderFactory f : ServiceLoader.load(Alt2XmlReaderFactory.class)) { @@ -112,19 +117,20 @@ @Override public SAXParser newSAXParser() throws ParserConfigurationException, SAXException { - return new AltSAXParser(new SuperReader(this)); + SuperReader r = new SuperReader(this); + for (Entry f : features.entrySet()) { + r.setFeature(f.getKey(), f.getValue()); + } + return new AltSAXParser(r); } @Override public void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException { - /** - * TODO: feature for disabling default/fallback SAXParserFactory - */ - throw new SAXNotSupportedException("Zatím není podporováno."); + features.put(name, value); } @Override public boolean getFeature(String name) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException { - throw new SAXNotSupportedException("Zatím není podporováno."); + return features.get(name); } }