001    package org.maltparser.core.feature.spec;
002    import java.util.Iterator;
003    import java.util.LinkedHashMap;
004    
005    import org.maltparser.core.exception.MaltChainedException;
006    
007    /**
008    *
009    *
010    * @author Johan Hall
011    */
012    public class SpecificationModel implements Iterable<SpecificationSubModel> {
013            private String specModelName;
014            private LinkedHashMap<String, SpecificationSubModel> subModelMap;
015            
016            public SpecificationModel() throws MaltChainedException {
017                    this(null);
018            }
019            
020            public SpecificationModel(String specModelName) throws MaltChainedException {
021                    setSpecModelName(specModelName);
022                    subModelMap = new LinkedHashMap<String, SpecificationSubModel>();
023            }
024            
025            public void add(String featureSpec) throws MaltChainedException {
026                    this.add("MAIN", featureSpec);
027            }
028            
029            public void add(String subModelName, String featureSpec) throws MaltChainedException {
030                    if (subModelName == null || subModelName.length() < 1  || subModelName.toUpperCase().equals("MAIN")) {
031                            if (!subModelMap.containsKey("MAIN")) {
032                                    subModelMap.put("MAIN", new SpecificationSubModel("MAIN"));
033                            }
034                            subModelMap.get("MAIN").add(featureSpec);
035                    } else {
036                            if (!subModelMap.containsKey(subModelName.toUpperCase())) {
037                                    subModelMap.put(subModelName.toUpperCase(), new SpecificationSubModel(subModelName.toUpperCase()));
038                            }
039                            subModelMap.get(subModelName.toUpperCase()).add(featureSpec);
040                    }
041            }
042    
043            public String getSpecModelName() {
044                    return specModelName;
045            }
046    
047            public void setSpecModelName(String specModelName) {
048                    this.specModelName = specModelName;
049            }
050            
051            public Iterator<SpecificationSubModel> iterator() {
052                    return subModelMap.values().iterator();
053            }
054            
055            public int size() {
056                    return subModelMap.size();
057            }
058            
059            public SpecificationSubModel getSpecSubModel(String subModelName) {
060                    return subModelMap.get(subModelName);
061            }
062            
063            public String toString() {
064                    StringBuilder sb = new StringBuilder();
065    
066                    for (SpecificationSubModel subModel : this) {
067                            if (subModel.size() > 0) { 
068                                    if (subModelMap.size() != 1 || subModel.getSubModelName().equalsIgnoreCase("MAIN")) {
069                                            sb.append(subModel.getSubModelName());
070                                            sb.append('\n');
071                                    }
072                                    sb.append(subModel.toString());
073                            }
074                    }
075                    return sb.toString();
076            }
077    }