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