001    package org.maltparser.core.io.dataformat;
002    
003    import java.net.URL;
004    
005    import org.maltparser.core.exception.MaltChainedException;
006    import org.maltparser.core.helper.HashMap;
007    import org.maltparser.core.helper.URLFinder;
008    import org.maltparser.core.helper.Util;
009    import org.maltparser.core.io.dataformat.DataFormatSpecification.Dependency;
010    
011    public class DataFormatManager {
012            private DataFormatSpecification inputDataFormatSpec;
013            private DataFormatSpecification outputDataFormatSpec;
014            private final HashMap<String, DataFormatSpecification> fileNameDataFormatSpecs;
015            private final HashMap<String, DataFormatSpecification> nameDataFormatSpecs;
016            
017            public DataFormatManager(URL inputFormatUrl, URL outputFormatUrl) throws MaltChainedException {
018                    fileNameDataFormatSpecs = new HashMap<String, DataFormatSpecification>();
019                    nameDataFormatSpecs = new HashMap<String, DataFormatSpecification>();
020                    inputDataFormatSpec = loadDataFormat(inputFormatUrl);
021                    outputDataFormatSpec = loadDataFormat(outputFormatUrl);
022            }
023    
024            public DataFormatSpecification loadDataFormat(URL dataFormatUrl) throws MaltChainedException {
025                    if (dataFormatUrl == null) {
026                            return null;
027                    }
028                    DataFormatSpecification dataFormat = fileNameDataFormatSpecs.get(dataFormatUrl.toString());
029                    if (dataFormat == null) {
030                            dataFormat = new DataFormatSpecification();
031                            dataFormat.parseDataFormatXMLfile(dataFormatUrl);
032                            fileNameDataFormatSpecs.put(dataFormatUrl.toString(), dataFormat);
033                            nameDataFormatSpecs.put(dataFormat.getDataFormatName(), dataFormat);
034                            final URLFinder f = new URLFinder();
035                            
036                            for (Dependency dep : dataFormat.getDependencies()) {
037                                    loadDataFormat(f.findURLinJars(dep.getUrlString()));
038                            }
039                    }
040                    return dataFormat;
041            }
042            
043            public DataFormatSpecification getInputDataFormatSpec() {
044                    return inputDataFormatSpec;
045            }
046    
047            public DataFormatSpecification getOutputDataFormatSpec() {
048                    return outputDataFormatSpec;
049            }
050            
051            public void setInputDataFormatSpec(DataFormatSpecification inputDataFormatSpec) {
052                    this.inputDataFormatSpec = inputDataFormatSpec;
053            }
054    
055            public void setOutputDataFormatSpec(DataFormatSpecification outputDataFormatSpec) {
056                    this.outputDataFormatSpec = outputDataFormatSpec;
057            }
058    
059            public DataFormatSpecification getDataFormatSpec(String name) {
060                    return nameDataFormatSpecs.get(name);
061            }
062    }