    001    package org.maltparser.core.config.version;
    002
    003    import java.io.File;
    004    import java.util.Date;
    005    import java.util.jar.JarEntry;
    006
    007    import org.maltparser.core.helper.SystemInfo;
    008    import org.maltparser.core.helper.SystemLogger;
    009
    010    public class Versioning {
    011            private String maltParserVersion;
    012            private String parserModelVersion;
    013            private File mcoPath;
    014            private String configName;
    015            private String newConfigName;
    016            private String configType;
    017
    018            private String featureModelXML;
    019            private String inputFormatXML;
    020
    021            public static String[] availableVersions = {"1.0.0", "1.0.1", "1.0.2", "1.0.3", "1.1", "1.2", "1.3", "1.3.1", "1.4", "1.4.1"};
    022            public static boolean[] supportVersions = {false, false, false, false, false, false, true, true, true};
    023
    024            public Versioning(String configName, String configType, File mcoPath, String parserModelVersion) {
    025                    setConfigName(configName);
    026                    setConfigType(configType);
    027                    setMcoPath(mcoPath);
    028                    setMaltParserVersion(SystemInfo.getVersion());
    029                    setParserModelVersion(parserModelVersion);
    030                    setNewConfigName(configName+"."+maltParserVersion);
    031            }
    032
    033            public JarEntry getJarEntry(JarEntry in) {
    034                    if (maltParserVersion.equals(parserModelVersion)) {
    035                            return in;
    036                    }
    037                    String entryName = in.getName().replace(configName+File.separator, newConfigName+File.separator);
    038                    if (entryName.endsWith(".info")) {
    039                            return new JarEntry(entryName.replace(File.separator+configName+"_", File.separator+newConfigName+"_"));
    040                    }
    041                    return new JarEntry(entryName);
    042            }
    043
    044            public boolean hasChanges(JarEntry in, JarEntry out) {
    045                    if (maltParserVersion.equals(parserModelVersion)) {
    046                            return false;
    047                    }
    048                    if (in.getName().endsWith(".info") || in.getName().endsWith(".sop")) {
    049                            return true;
    050                    }
    051                    return false;
    052            }
    053
    054            public String modifyJarEntry(JarEntry in, JarEntry out, StringBuilder sb) {
    055                    if (maltParserVersion.equals(parserModelVersion)) {
    056                            return sb.toString();
    057                    }
    058                    if (in.getName().endsWith(".info")) {
    059                            final StringBuilder outString = new StringBuilder();
    060                            String[] lines = sb.toString().split("\\n");
    061                            for (int i = 0; i < lines.length; i++) {
    062                                    if (lines[i].startsWith("Configuration name:")) {
    063                                            outString.append("Configuration name:   ");
    064                                            outString.append(configName);
    065                                            outString.append('.');
    066                                            outString.append(maltParserVersion);
    067                                            outString.append('\n');
    068                                    } else if (lines[i].startsWith("Created:")) {
    069                                            outString.append(lines[i]);
    070                                            outString.append('\n');
    071                                            outString.append("Converted:            ");
    072                                            outString.append(new Date(System.currentTimeMillis()));
    073                                            outString.append('\n');
    074                                    } else if (lines[i].startsWith("Version:")) {
    075                                            outString.append("Version:                       ");
    076                                            outString.append(maltParserVersion);
    077                                            outString.append('\n');
    078                                            outString.append("Created by:                    ");
    079                                            outString.append(parserModelVersion);
    080                                            outString.append('\n');
    081                                    } else if (lines[i].startsWith("  name (  -c)                           ")) {
    082                                            outString.append("  name (  -c)                           ");
    083                                            outString.append(newConfigName);
    084                                            outString.append('\n');
    085                                    } else if (lines[i].startsWith("  format ( -if)                         /appdata/dataformat/")) {
    086                                            outString.append("  format ( -if)                         ");
    087                                            int index = lines[i].lastIndexOf("/");
    088                                            outString.append(lines[i].substring(index + 1));
    089                                            outString.append('\n');
    090                                    } else if (lines[i].startsWith("  format ( -of)                         /appdata/dataformat/")) {
    091                                            outString.append("  format ( -of)                         ");
    092                                            int index = lines[i].lastIndexOf("/");
    093                                            outString.append(lines[i].substring(index + 1));
    094                                            outString.append('\n');
    095                                    } else if (lines[i].startsWith("--guide-features (  -F)                 /appdata/features/")) {
    096                                            outString.append("--guide-features (  -F)                 ");
    097                                            int index = lines[i].lastIndexOf("/");
    098                                            outString.append(lines[i].substring(index + 1));
    099                                            outString.append('\n');
    100                                    } else {
    101                                            outString.append(lines[i]);
    102                                            outString.append('\n');
    103                                    }
    104                            }
    105                            return outString.toString();
    106                    } else if (in.getName().endsWith(".sop")) {
    107                            final StringBuilder outString = new StringBuilder();
    108                            String[] lines = sb.toString().split("\\n");
    109                            for (int i = 0; i < lines.length; i++) {
    110                                    int index = lines[i].indexOf('\t');
    111                                    int container = 0;
    112                                    if (index > -1) {
    113                                            container = Integer.parseInt(lines[i].substring(0,index));
    114                                    }
    115                                    if (lines[i].startsWith(container+"\tguide\tfeatures")) {
    116                                            int tabIndex = lines[i].lastIndexOf('\t');
    117                                            if (lines[i].substring(tabIndex+1).startsWith("/appdata/features/")) {
    118                                                    int slashIndex = lines[i].lastIndexOf("/");
    119                                                    String xmlFile = lines[i].substring(slashIndex+1);
    120                                                    String path = lines[i].substring(tabIndex+1, slashIndex);
    121                                                    setFeatureModelXML(path + "/libsvm/" +  xmlFile);
    122                                                    outString.append(container);
    123                                                    outString.append("\tguide\tfeatures\t");
    124                                                    outString.append(xmlFile);
    125                                                    outString.append('\n');
    126                                            } else {
    127                                                    outString.append(lines[i]);
    128                                                    outString.append('\n');
    129                                            }
    130                                    } else if (lines[i].startsWith(container+"\tinput\tformat")) {
    131                                            int tabIndex = lines[i].lastIndexOf('\t');
    132                                            if (lines[i].substring(tabIndex+1).startsWith("/appdata/dataformat/")) {
    133                                                    int slashIndex = lines[i].lastIndexOf("/");
    134                                                    String xmlFile = lines[i].substring(slashIndex+1);
    135                                                    String path = lines[i].substring(tabIndex+1, slashIndex);
    136                                                    setInputFormatXML(path + "/" +  xmlFile);
    137                                                    outString.append(container);
    138                                                    outString.append("\tinput\tformat\t");
    139                                                    outString.append(xmlFile);
    140                                                    outString.append('\n');
    141                                            } else {
    142                                                    outString.append(lines[i]);
    143                                                    outString.append('\n');
    144                                            }
    145                                    } else if (earlierVersion("1.3")) {
    146                                            if (lines[i].startsWith(container+"\tnivre\tpost_processing")) {
    147                                            } else if (lines[i].startsWith(container+"\tmalt0.4\tbehavior")) {
    148                                                    if (lines[i].endsWith("true")) {
    149                                                            SystemLogger.logger().info("MaltParser "+maltParserVersion+" doesn't support MaltParser 0.4 emulation.");
    150                                                    }
    151                                            } else if (lines[i].startsWith(container+"\tsinglemalt\tparsing_algorithm")) {
    152                                                    outString.append(container);
    153                                                    outString.append("\tsinglemalt\tparsing_algorithm\t");
    154                                                    if (lines[i].endsWith("NivreStandard")) {
    155                                                            outString.append("class org.maltparser.parser.algorithm.nivre.NivreArcStandardFactory");
    156                                                    } else if (lines[i].endsWith("NivreEager")) {
    157                                                            outString.append("class org.maltparser.parser.algorithm.nivre.NivreArcEagerFactory");
    158                                                    } else if (lines[i].endsWith("CovingtonNonProjective")) {
    159                                                            outString.append("class org.maltparser.parser.algorithm.covington.CovingtonNonProjFactory");
    160                                                    } else if (lines[i].endsWith("CovingtonProjective")) {
    161                                                            outString.append("class org.maltparser.parser.algorithm.covington.CovingtonProjFactory");
    162                                                    }
    163                                                    outString.append('\n');
    164                                            }
    165                                    } else {
    166                                            outString.append(lines[i]);
    167                                            outString.append('\n');
    168                                    }
    169                            }
    170                            return outString.toString();
    171                    }
    172                    return sb.toString();
    173            }
    174
    175
    176            public boolean earlierVersion(String version) {
    177                    boolean e = false;
    178                    for (int i = 0; i < availableVersions.length; i++) {
    179                            if (availableVersions[i].equals(version)) {
    180                                    break;
    181                            } else if (availableVersions[i].equals(parserModelVersion)) {
    182                                    e = true;
    183                            }
    184                    }
    185                    return e;
    186            }
    187
    188            public boolean support(String version) {
    189                    for (int i = 0; i < availableVersions.length; i++) {
    190                            if (availableVersions[i].equals(version)) {
    191                                    return supportVersions[i];
    192                            }
    193                    }
    194                    return false;
    195            }
    196
    197            public String getFeatureModelXML() {
    198                    return featureModelXML;
    199            }
    200
    201            public void setFeatureModelXML(String featureModelXML) {
    202                    this.featureModelXML = featureModelXML;
    203            }
    204
    205            public String getInputFormatXML() {
    206                    return inputFormatXML;
    207            }
    208
    209            public void setInputFormatXML(String inputFormatXML) {
    210                    this.inputFormatXML = inputFormatXML;
    211            }
    212
    213            public String getNewConfigName() {
    214                    return newConfigName;
    215            }
    216
    217            public void setNewConfigName(String newConfigName) {
    218                    this.newConfigName = newConfigName;
    219            }
    220
    221            public String getConfigName() {
    222                    return configName;
    223            }
    224
    225            public void setConfigName(String configName) {
    226                    this.configName = configName;
    227            }
    228
    229            public String getConfigType() {
    230                    return configType;
    231            }
    232
    233            public void setConfigType(String configType) {
    234                    this.configType = configType;
    235            }
    236
    237            public File getMcoPath() {
    238                    return mcoPath;
    239            }
    240
    241            public void setMcoPath(File mcoPath) {
    242                    this.mcoPath = mcoPath;
    243            }
    244
    245            public String getMaltParserVersion() {
    246                    return maltParserVersion;
    247            }
    248
    249            public void setMaltParserVersion(String maltParserVersion) {
    250                    this.maltParserVersion = maltParserVersion;
    251            }
    252
    253            public String getParserModelVersion() {
    254                    return parserModelVersion;
    255            }
    256
    257            public void setParserModelVersion(String parserModelVersion) {
    258                    this.parserModelVersion = parserModelVersion;
    259            }
    260    }