cli, lib-input: propagate features and properties to parser/reader
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 17 Sep 2014 21:00:32 +0200
changeset 99fadfde5b3e55
parent 98 944a81a54bb9
child 100 b50e77d23d13
cli, lib-input: propagate features and properties to parser/reader
java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java
java/alt2xml-lib-input/src/cz/frantovo/alt2xml/ParserFactory.java
     1.1 --- a/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java	Wed Sep 17 19:13:47 2014 +0200
     1.2 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java	Wed Sep 17 21:00:32 2014 +0200
     1.3 @@ -23,6 +23,8 @@
     1.4  import cz.frantovo.alt2xml.out.OutputActionException;
     1.5  import java.util.HashMap;
     1.6  import java.util.Map;
     1.7 +import java.util.Map.Entry;
     1.8 +import java.util.Properties;
     1.9  import java.util.ServiceLoader;
    1.10  import java.util.logging.Level;
    1.11  import java.util.logging.LogRecord;
    1.12 @@ -31,6 +33,8 @@
    1.13  import javax.xml.parsers.SAXParser;
    1.14  import javax.xml.parsers.SAXParserFactory;
    1.15  import org.xml.sax.SAXException;
    1.16 +import org.xml.sax.SAXNotRecognizedException;
    1.17 +import org.xml.sax.SAXNotSupportedException;
    1.18  
    1.19  /**
    1.20   *
    1.21 @@ -58,7 +62,7 @@
    1.22  		try {
    1.23  			CLIParser cliParser = new CLIParser();
    1.24  			CLIOptions cliOptions = cliParser.parseOptions(args);
    1.25 -			
    1.26 +
    1.27  			cliOptions.validate();
    1.28  
    1.29  			Map<String, ActionFactory> actionFactories = new HashMap<>();
    1.30 @@ -79,14 +83,17 @@
    1.31  			} else {
    1.32  
    1.33  				ActionContext actionContext = new ActionContext(cliOptions.getOutputStream());
    1.34 -				
    1.35 +
    1.36  				actionContext.setActionProperties(cliOptions.getActionProperties());
    1.37  				actionContext.setActionData(cliOptions.getActionData());
    1.38 -				
    1.39 +
    1.40  				Action action = actionFactory.getAction(actionContext);
    1.41  
    1.42  				SAXParserFactory t = SAXParserFactory.newInstance();
    1.43 +				parametrizeParserFactory(t, cliOptions.getReaderFeatures());
    1.44 +				
    1.45  				SAXParser p = t.newSAXParser();
    1.46 +				paramtrizeParser(p, cliOptions.getReaderProperties());
    1.47  
    1.48  				action.run(p, cliOptions.getInputSource());
    1.49  			}
    1.50 @@ -115,4 +122,16 @@
    1.51  
    1.52  		System.exit(exitCode);
    1.53  	}
    1.54 +
    1.55 +	private static void parametrizeParserFactory(SAXParserFactory factory, Map<String, Boolean> features) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
    1.56 +		for (Entry<String, Boolean> feature : features.entrySet()) {
    1.57 +			factory.setFeature(feature.getKey(), feature.getValue());
    1.58 +		}
    1.59 +	}
    1.60 +
    1.61 +	private static void paramtrizeParser(SAXParser parser, Properties readerProperties) throws SAXNotRecognizedException, SAXNotSupportedException {
    1.62 +		for (String name : readerProperties.stringPropertyNames()) {
    1.63 +			parser.setProperty(name, readerProperties.getProperty(name));
    1.64 +		}
    1.65 +	}
    1.66  }
     2.1 --- a/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/ParserFactory.java	Wed Sep 17 19:13:47 2014 +0200
     2.2 +++ b/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/ParserFactory.java	Wed Sep 17 21:00:32 2014 +0200
     2.3 @@ -18,7 +18,10 @@
     2.4  package cz.frantovo.alt2xml;
     2.5  
     2.6  import java.util.Deque;
     2.7 +import java.util.HashMap;
     2.8  import java.util.LinkedList;
     2.9 +import java.util.Map;
    2.10 +import java.util.Map.Entry;
    2.11  import java.util.ServiceLoader;
    2.12  import javax.xml.parsers.FactoryConfigurationError;
    2.13  import javax.xml.parsers.ParserConfigurationException;
    2.14 @@ -47,6 +50,8 @@
    2.15  	 */
    2.16  	public static final String DEFAULT_FACTORY_PROPERTY = "cz.frantovo.alt2xml.fallback.javax.xml.parsers.SAXParserFactory";
    2.17  
    2.18 +	private final Map<String, Boolean> features = new HashMap<>();
    2.19 +
    2.20  	public ParserFactory() {
    2.21  		super();
    2.22  		for (Alt2XmlReaderFactory f : ServiceLoader.load(Alt2XmlReaderFactory.class)) {
    2.23 @@ -112,19 +117,20 @@
    2.24  
    2.25  	@Override
    2.26  	public SAXParser newSAXParser() throws ParserConfigurationException, SAXException {
    2.27 -		return new AltSAXParser(new SuperReader(this));
    2.28 +		SuperReader r = new SuperReader(this);
    2.29 +		for (Entry<String, Boolean> f : features.entrySet()) {
    2.30 +			r.setFeature(f.getKey(), f.getValue());
    2.31 +		}
    2.32 +		return new AltSAXParser(r);
    2.33  	}
    2.34  
    2.35  	@Override
    2.36  	public void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
    2.37 -		/**
    2.38 -		 * TODO: feature for disabling default/fallback SAXParserFactory
    2.39 -		 */
    2.40 -		throw new SAXNotSupportedException("Zatím není podporováno.");
    2.41 +		features.put(name, value);
    2.42  	}
    2.43  
    2.44  	@Override
    2.45  	public boolean getFeature(String name) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
    2.46 -		throw new SAXNotSupportedException("Zatím není podporováno.");
    2.47 +		return features.get(name);
    2.48  	}
    2.49  }