001    package org.maltparser.core.feature;
002    
003    import java.io.File;
004    import java.net.MalformedURLException;
005    import java.net.URL;
006    
007    import org.maltparser.core.config.ConfigurationDir;
008    import org.maltparser.core.config.ConfigurationRegistry;
009    import org.maltparser.core.exception.MaltChainedException;
010    import org.maltparser.core.feature.spec.SpecificationModel;
011    import org.maltparser.core.feature.spec.SpecificationModels;
012    import org.maltparser.core.feature.system.FeatureEngine;
013    
014    /**
015    *
016    *
017    * @author Johan Hall
018    */
019    public class FeatureModelManager {
020            protected SpecificationModels specModels;
021            protected FeatureEngine featureEngine;
022            protected ConfigurationDir configDirectory;
023    
024            
025            public FeatureModelManager(FeatureEngine engine, ConfigurationDir configDirectory) throws MaltChainedException {
026                    specModels = new SpecificationModels();
027                    setConfigDirectory(configDirectory);
028                    setFeatureEngine(engine);
029            }
030            
031            private URL findURL(String specModelFileName) throws MaltChainedException {
032                    URL url = null;
033                    File specFile = configDirectory.getFile(specModelFileName);
034                    if (specFile.exists()) {
035                            try {
036                                    url = new URL("file:///"+specFile.getAbsolutePath());
037                            } catch (MalformedURLException e) {
038                                    throw new MaltChainedException("Malformed URL: "+specFile, e);
039                            }
040                    } else {
041                            url = configDirectory.getConfigFileEntryURL(specModelFileName);
042                    }
043                    return url;
044            }
045            
046            public void loadSpecification(String specModelFileName) throws MaltChainedException {
047                    specModels.load(findURL(specModelFileName));
048            }
049    
050            
051            public void loadParSpecification(String specModelFileName, String markingStrategy, String coveredRoot) throws MaltChainedException {
052                    specModels.loadParReader(findURL(specModelFileName), markingStrategy, coveredRoot);
053            }
054            
055            public FeatureModel getFeatureModel(String specModelURL, int specModelUrlIndex, ConfigurationRegistry registry) throws MaltChainedException {
056                    return new FeatureModel(specModels.getSpecificationModel(findURL(specModelURL), specModelUrlIndex), registry, featureEngine);
057            }
058            
059            public FeatureModel getFeatureModel(String specModelURL, ConfigurationRegistry registry) throws MaltChainedException {
060                    return new FeatureModel(specModels.getSpecificationModel(findURL(specModelURL), 0), registry, featureEngine);
061            }
062            
063            public FeatureModel getFeatureModel(SpecificationModel specModel, ConfigurationRegistry registry) throws MaltChainedException {
064                    return new FeatureModel(specModel, registry, featureEngine);
065            }
066            
067            public SpecificationModels getSpecModels() {
068                    return specModels;
069            }
070    
071            protected void setSpecModels(SpecificationModels specModel) {
072                    this.specModels = specModel;
073            }
074            
075            public FeatureEngine getFeatureEngine() {
076                    return featureEngine;
077            }
078    
079            public void setFeatureEngine(FeatureEngine featureEngine) {
080                    this.featureEngine = featureEngine;
081            }
082    
083            public ConfigurationDir getConfigDirectory() {
084                    return configDirectory;
085            }
086    
087            public void setConfigDirectory(ConfigurationDir configDirectory) {
088                    this.configDirectory = configDirectory;
089            }
090    
091            public String toString() {
092                    return specModels.toString();
093            }
094    }