001 package org.maltparser.core.config; 002 003 import java.io.BufferedInputStream; 004 import java.io.BufferedOutputStream; 005 import java.io.BufferedReader; 006 import java.io.BufferedWriter; 007 import java.io.File; 008 import java.io.FileInputStream; 009 import java.io.FileNotFoundException; 010 import java.io.FileOutputStream; 011 import java.io.FileReader; 012 import java.io.FileWriter; 013 import java.io.IOException; 014 import java.io.InputStreamReader; 015 import java.io.OutputStreamWriter; 016 import java.io.UnsupportedEncodingException; 017 import java.net.URL; 018 import java.util.Date; 019 import java.util.HashSet; 020 import java.util.SortedSet; 021 import java.util.TreeSet; 022 import java.util.jar.JarEntry; 023 import java.util.jar.JarInputStream; 024 import java.util.jar.JarOutputStream; 025 026 import org.maltparser.core.exception.MaltChainedException; 027 import org.maltparser.core.helper.SystemInfo; 028 import org.maltparser.core.helper.SystemLogger; 029 import org.maltparser.core.helper.Util; 030 import org.maltparser.core.options.OptionManager; 031 032 /** 033 * This class contains methods for handle the configuration directory. 034 * 035 * @author Johan Hall 036 */ 037 public class ConfigurationDir { 038 protected static final int BUFFER = 4096; 039 protected File configDirectory; 040 protected String name; 041 protected String type; 042 protected File workingDirectory; 043 protected URL url = null; 044 protected int containerIndex; 045 protected BufferedWriter infoFile = null; 046 protected String createdByMaltParserVersion; 047 048 049 /** 050 * Creates a configuration directory from a mco-file specified by an URL. 051 * 052 * @param url an URL to a mco-file 053 * @throws MaltChainedException 054 */ 055 public ConfigurationDir(URL url) throws MaltChainedException { 056 initWorkingDirectory(); 057 setUrl(url); 058 initNameNTypeFromInfoFile(url); 059 } 060 061 /** 062 * Creates a new configuration directory or a configuration directory from a mco-file 063 * 064 * @param name the name of the configuration 065 * @param type the type of configuration 066 * @param containerIndex the container index 067 * @throws MaltChainedException 068 */ 069 public ConfigurationDir(String name, String type, int containerIndex) throws MaltChainedException { 070 setContainerIndex(containerIndex); 071 initWorkingDirectory(); 072 if (name != null && name.length() > 0 && type != null && type.length() > 0) { 073 setName(name); 074 setType(type); 075 } else { 076 throw new ConfigurationException("The configuration name is not specified. "); 077 } 078 setConfigDirectory(new File(workingDirectory.getPath()+File.separator+getName())); 079 } 080 081 /** 082 * Creates an output stream writer, where the corresponding file will be included in the configuration directory 083 * 084 * @param fileName a file name 085 * @param charSet a char set 086 * @return an output stream writer for writing to a file within the configuration directory 087 * @throws MaltChainedException 088 */ 089 public OutputStreamWriter getOutputStreamWriter(String fileName, String charSet) throws MaltChainedException { 090 try { 091 return new OutputStreamWriter(new FileOutputStream(configDirectory.getPath()+File.separator+fileName), charSet); 092 } catch (FileNotFoundException e) { 093 throw new ConfigurationException("The file '"+fileName+"' cannot be created. ", e); 094 } catch (UnsupportedEncodingException e) { 095 throw new ConfigurationException("The char set '"+charSet+"' is not supported. ", e); 096 } 097 } 098 099 /** 100 * Creates an output stream writer, where the corresponding file will be included in the 101 * configuration directory. Uses UTF-8 for character encoding. 102 * 103 * @param fileName a file name 104 * @return an output stream writer for writing to a file within the configuration directory 105 * @throws MaltChainedException 106 */ 107 public OutputStreamWriter getOutputStreamWriter(String fileName) throws MaltChainedException { 108 try { 109 return new OutputStreamWriter(new FileOutputStream(configDirectory.getPath()+File.separator+fileName), "UTF-8"); 110 } catch (FileNotFoundException e) { 111 throw new ConfigurationException("The file '"+fileName+"' cannot be created. ", e); 112 } catch (UnsupportedEncodingException e) { 113 throw new ConfigurationException("The char set 'UTF-8' is not supported. ", e); 114 } 115 } 116 117 /** 118 * Creates an input stream reader for reading a file within the configuration directory 119 * 120 * @param fileName a file name 121 * @param charSet a char set 122 * @return an input stream reader for reading a file within the configuration directory 123 * @throws MaltChainedException 124 */ 125 public InputStreamReader getInputStreamReader(String fileName, String charSet) throws MaltChainedException { 126 try { 127 return new InputStreamReader(new FileInputStream(configDirectory.getPath()+File.separator+fileName), charSet); 128 } catch (FileNotFoundException e) { 129 throw new ConfigurationException("The file '"+fileName+"' cannot be found. ", e); 130 } catch (UnsupportedEncodingException e) { 131 throw new ConfigurationException("The char set '"+charSet+"' is not supported. ", e); 132 } 133 } 134 135 /** 136 * Creates an input stream reader for reading a file within the configuration directory. 137 * Uses UTF-8 for character encoding. 138 * 139 * @param fileName a file name 140 * @return an input stream reader for reading a file within the configuration directory 141 * @throws MaltChainedException 142 */ 143 public InputStreamReader getInputStreamReader(String fileName) throws MaltChainedException { 144 try { 145 return new InputStreamReader(new FileInputStream(configDirectory.getPath()+File.separator+fileName), "UTF-8"); 146 } catch (FileNotFoundException e) { 147 throw new ConfigurationException("The file '"+fileName+"' cannot be found. ", e); 148 } catch (UnsupportedEncodingException e) { 149 throw new ConfigurationException("The char set 'UTF-8' is not supported. ", e); 150 } 151 } 152 153 /** 154 * Returns a file handler object of a file within the configuration directory 155 * 156 * @param fileName a file name 157 * @return a file handler object of a file within the configuration directory 158 * @throws MaltChainedException 159 */ 160 public File getFile(String fileName) throws MaltChainedException { 161 return new File(configDirectory.getPath()+File.separator+fileName); 162 } 163 164 /** 165 * Copies a file into the configuration directory. 166 * 167 * @param source a path to file 168 * @throws MaltChainedException 169 */ 170 public void copyToConfig(File source) throws MaltChainedException { 171 byte[] readBuffer = new byte[BUFFER]; 172 String destination = configDirectory.getPath()+File.separator+source.getName(); 173 try { 174 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source)); 175 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destination), BUFFER); 176 177 int n = 0; 178 while ((n = bis.read(readBuffer, 0, BUFFER)) != -1) { 179 bos.write(readBuffer, 0, n); 180 } 181 bos.flush(); 182 bos.close(); 183 bis.close(); 184 } catch (FileNotFoundException e) { 185 throw new ConfigurationException("The source file '"+source+"' cannot be found or the destination file '"+destination+"' cannot be created when coping the file. ", e); 186 } catch (IOException e) { 187 throw new ConfigurationException("The source file '"+source+"' cannot be copied to destination '"+destination+"'. ", e); 188 } 189 } 190 191 /** 192 * Copies a file into the configuration directory. 193 * 194 * @param fileUrl an URL to the file 195 * @throws MaltChainedException 196 */ 197 public void copyToConfig(String fileUrl) throws MaltChainedException { 198 URL url = Util.findURL(fileUrl); 199 if (url == null) { 200 throw new ConfigurationException("The file or URL '"+fileUrl+"' could not be found. "); 201 } 202 byte[] readBuffer = new byte[BUFFER]; 203 String destFileName = url.getFile(); 204 int indexSlash = destFileName.lastIndexOf('/'); 205 int indexQuery = destFileName.lastIndexOf('?'); 206 207 if (indexSlash != -1 || indexQuery != -1) { 208 if (indexSlash == -1) { 209 indexSlash = 0; 210 } 211 if (indexQuery == -1) { 212 indexQuery = destFileName.length(); 213 } 214 destFileName = destFileName.substring(indexSlash, indexQuery); 215 } 216 217 String destination = configDirectory.getPath()+File.separator+destFileName; 218 try { 219 BufferedInputStream bis = new BufferedInputStream(url.openStream()); 220 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destination), BUFFER); 221 222 int n = 0; 223 while ((n = bis.read(readBuffer, 0, BUFFER)) != -1) { 224 bos.write(readBuffer, 0, n); 225 } 226 bos.flush(); 227 bos.close(); 228 bis.close(); 229 } catch (FileNotFoundException e) { 230 throw new ConfigurationException("The destination file '"+destination+"' cannot be created when coping the file. ", e); 231 } catch (IOException e) { 232 throw new ConfigurationException("The URL '"+url+"' cannot be copied to destination '"+destination+"'. ", e); 233 } 234 } 235 236 /** 237 * Removes the configuration directory, if it exists and it contains a .info file. 238 * 239 * @throws MaltChainedException 240 */ 241 public void deleteConfigDirectory() throws MaltChainedException { 242 if (!configDirectory.exists()) { 243 return; 244 } 245 File infoFile = new File(configDirectory.getPath()+File.separator+getName()+"_"+getType()+".info"); 246 if (infoFile.exists()) { 247 deleteConfigDirectory(configDirectory); 248 } else { 249 throw new ConfigurationException("There exists a directory that is not a MaltParser configuration directory. "); 250 } 251 } 252 253 private void deleteConfigDirectory(File directory) throws MaltChainedException { 254 if (directory.exists()) { 255 File[] files = directory.listFiles(); 256 for (int i = 0; i < files.length; i++) { 257 if (files[i].isDirectory()) { 258 deleteConfigDirectory(files[i]); 259 } else { 260 files[i].delete(); 261 } 262 } 263 } else { 264 throw new ConfigurationException("The directory '"+directory.getPath()+ "' cannot be found. "); 265 } 266 directory.delete(); 267 } 268 269 /** 270 * Returns a file handler object for the configuration directory 271 * 272 * @return a file handler object for the configuration directory 273 */ 274 public File getConfigDirectory() { 275 return configDirectory; 276 } 277 278 protected void setConfigDirectory(File dir) { 279 this.configDirectory = dir; 280 } 281 282 /** 283 * Creates the configuration directory 284 * 285 * @throws MaltChainedException 286 */ 287 public void createConfigDirectory() throws MaltChainedException { 288 checkConfigDirectory(); 289 configDirectory.mkdir(); 290 createInfoFile(); 291 } 292 293 protected void checkConfigDirectory() throws MaltChainedException { 294 if (configDirectory.exists() && !configDirectory.isDirectory()) { 295 throw new ConfigurationException("The configuration directory name already exists and is not a directory. "); 296 } 297 298 if (configDirectory.exists()) { 299 deleteConfigDirectory(); 300 } 301 } 302 303 protected void createInfoFile() throws MaltChainedException { 304 infoFile = new BufferedWriter(getOutputStreamWriter(getName()+"_"+getType()+".info")); 305 try { 306 infoFile.write("CONFIGURATION\n"); 307 infoFile.write("Configuration name: "+getName()+"\n"); 308 infoFile.write("Configuration type: "+getType()+"\n"); 309 infoFile.write("Created: "+new Date(System.currentTimeMillis())+"\n"); 310 311 infoFile.write("\nSYSTEM\n"); 312 infoFile.write("Operating system architecture: "+System.getProperty("os.arch")+"\n"); 313 infoFile.write("Operating system name: "+System.getProperty("os.name")+"\n"); 314 infoFile.write("JRE vendor name: "+System.getProperty("java.vendor")+"\n"); 315 infoFile.write("JRE version number: "+System.getProperty("java.version")+"\n"); 316 317 infoFile.write("\nMALTPARSER\n"); 318 infoFile.write("Version: "+SystemInfo.getVersion()+"\n"); 319 infoFile.write("Build date: "+SystemInfo.getBuildDate()+"\n"); 320 HashSet<String> excludeGroups = new HashSet<String>(); 321 excludeGroups.add("system"); 322 infoFile.write("\nSETTINGS\n"); 323 infoFile.write(OptionManager.instance().toStringPrettyValues(containerIndex, excludeGroups)); 324 infoFile.flush(); 325 } catch (IOException e) { 326 throw new ConfigurationException("Could not create the maltparser info file. "); 327 } 328 } 329 330 /** 331 * Returns a writer to the configuration information file 332 * 333 * @return a writer to the configuration information file 334 * @throws MaltChainedException 335 */ 336 public BufferedWriter getInfoFileWriter() throws MaltChainedException { 337 return infoFile; 338 } 339 340 /** 341 * Creates the malt configuration file (.mco). This file is compressed. 342 * 343 * @throws MaltChainedException 344 */ 345 public void createConfigFile() throws MaltChainedException { 346 try { 347 JarOutputStream jos = new JarOutputStream(new FileOutputStream(workingDirectory.getPath()+File.separator+getName()+".mco")); 348 // configLogger.info("Creates configuration file '"+workingDirectory.getPath()+File.separator+getName()+".mco' ...\n"); 349 createConfigFile(configDirectory.getPath(), jos); 350 jos.close(); 351 } catch (FileNotFoundException e) { 352 throw new ConfigurationException("The maltparser configurtation file '"+workingDirectory.getPath()+File.separator+getName()+".mco"+"' cannot be found. ", e); 353 } catch (IOException e) { 354 throw new ConfigurationException("The maltparser configurtation file '"+workingDirectory.getPath()+File.separator+getName()+".mco"+"' cannot be created. ", e); 355 } 356 } 357 358 private void createConfigFile(String directory, JarOutputStream jos) throws MaltChainedException { 359 byte[] readBuffer = new byte[BUFFER]; 360 try { 361 File zipDir = new File(directory); 362 String[] dirList = zipDir.list(); 363 364 int bytesIn = 0; 365 366 for (int i = 0; i < dirList.length; i++) { 367 File f = new File(zipDir, dirList[i]); 368 if (f.isDirectory()) { 369 String filePath = f.getPath(); 370 createConfigFile(filePath, jos); 371 continue; 372 } 373 374 FileInputStream fis = new FileInputStream(f); 375 376 377 JarEntry entry = new JarEntry(f.getPath().substring(workingDirectory.getPath().length()+1)); 378 jos.putNextEntry(entry); 379 380 while ((bytesIn = fis.read(readBuffer)) != -1) { 381 jos.write(readBuffer, 0, bytesIn); 382 } 383 384 fis.close(); 385 } 386 } catch (FileNotFoundException e) { 387 throw new ConfigurationException("The directory '"+directory+"' cannot be found. ", e); 388 } catch (IOException e) { 389 throw new ConfigurationException("The directory '"+directory+"' cannot be compressed into a mco file. ", e); 390 } 391 } 392 393 protected void initNameNTypeFromInfoFile(URL url) throws MaltChainedException { 394 if (url == null) { 395 throw new ConfigurationException("The URL cannot be found. "); 396 } 397 try { 398 JarEntry je; 399 JarInputStream jis = new JarInputStream(url.openConnection().getInputStream()); 400 while ((je = jis.getNextJarEntry()) != null) { 401 String entryName = je.getName(); 402 if (entryName.endsWith(".info")) { 403 int indexUnderScore = entryName.lastIndexOf('_'); 404 int indexSeparator = entryName.lastIndexOf(File.separator); 405 if (indexSeparator == -1) { 406 indexSeparator = entryName.lastIndexOf('/'); 407 } 408 if (indexSeparator == -1) { 409 indexSeparator = entryName.lastIndexOf('\\'); 410 } 411 int indexDot = entryName.lastIndexOf('.'); 412 if (indexUnderScore == -1 || indexDot == -1) { 413 throw new ConfigurationException("Could not find the configuration name and type from the URL '"+url.toString()+"'. "); 414 } 415 setName(entryName.substring(indexSeparator+1, indexUnderScore)); 416 setType(entryName.substring(indexUnderScore+1, indexDot)); 417 setConfigDirectory(new File(workingDirectory.getPath()+File.separator+getName())); 418 jis.close(); 419 return; 420 } 421 } 422 423 } catch (IOException e) { 424 throw new ConfigurationException("Could not find the configuration name and type from the URL '"+url.toString()+"'. ", e); 425 } 426 } 427 428 /** 429 * Prints the content of the configuration information file to the system logger 430 * 431 * @throws MaltChainedException 432 */ 433 public void echoInfoFile() throws MaltChainedException { 434 checkConfigDirectory(); 435 JarInputStream jis; 436 try { 437 if (url == null) { 438 jis = new JarInputStream(new FileInputStream(workingDirectory.getPath()+File.separator+getName()+".mco")); 439 } else { 440 jis = new JarInputStream(url.openConnection().getInputStream()); 441 } 442 JarEntry je; 443 444 while ((je = jis.getNextJarEntry()) != null) { 445 String entryName = je.getName(); 446 447 if (entryName.endsWith(getName()+"_"+getType()+".info")) { 448 int c; 449 while ((c = jis.read()) != -1) { 450 SystemLogger.logger().info((char)c); 451 } 452 } 453 } 454 jis.close(); 455 } catch (FileNotFoundException e) { 456 throw new ConfigurationException("Could not print configuration information file. The configuration file '"+workingDirectory.getPath()+File.separator+getName()+".mco"+"' cannot be found. ", e); 457 } catch (IOException e) { 458 throw new ConfigurationException("Could not print configuration information file. ", e); 459 } 460 461 } 462 463 /** 464 * Unpacks the malt configuration file (.mco). 465 * 466 * @throws MaltChainedException 467 */ 468 public void unpackConfigFile() throws MaltChainedException { 469 checkConfigDirectory(); 470 JarInputStream jis; 471 try { 472 if (url == null) { 473 jis = new JarInputStream(new FileInputStream(workingDirectory.getPath()+File.separator+getName()+".mco")); 474 } else { 475 jis = new JarInputStream(url.openConnection().getInputStream()); 476 } 477 unpackConfigFile(jis); 478 jis.close(); 479 } catch (FileNotFoundException e) { 480 throw new ConfigurationException("Could not unpack configuration. The configuration file '"+workingDirectory.getPath()+File.separator+getName()+".mco"+"' cannot be found. ", e); 481 } catch (IOException e) { 482 if (configDirectory.exists()) { 483 deleteConfigDirectory(); 484 } 485 throw new ConfigurationException("Could not unpack configuration. ", e); 486 } 487 initCreatedByMaltParserVersionFromInfoFile(); 488 } 489 490 protected void unpackConfigFile(JarInputStream jis) throws MaltChainedException { 491 try { 492 JarEntry je; 493 byte[] readBuffer = new byte[BUFFER]; 494 SortedSet<String> directoryCache = new TreeSet<String>(); 495 while ((je = jis.getNextJarEntry()) != null) { 496 String entryName = je.getName(); 497 498 if (entryName.startsWith("/")) { 499 entryName = entryName.substring(1); 500 } 501 if (entryName.endsWith(File.separator)) { 502 return; 503 } 504 int index = -1; 505 if (File.separator.equals("\\")) { 506 entryName = entryName.replace('/', '\\'); 507 index = entryName.lastIndexOf("\\"); 508 } else if (File.separator.equals("/")) { 509 entryName = entryName.replace('\\', '/'); 510 index = entryName.lastIndexOf("/"); 511 } 512 if (index > 0) { 513 String dirName = entryName.substring(0, index); 514 if (!directoryCache.contains(dirName)) { 515 File directory = new File(workingDirectory.getPath()+File.separator+dirName); 516 if (!(directory.exists() && directory.isDirectory())) { 517 if (!directory.mkdirs()) { 518 throw new ConfigurationException("Unable to make directory '" + dirName +"'. "); 519 } 520 directoryCache.add(dirName); 521 } 522 } 523 } 524 BufferedOutputStream bos; 525 try { 526 bos = new BufferedOutputStream(new FileOutputStream(workingDirectory.getPath()+File.separator+entryName), BUFFER); 527 } catch (FileNotFoundException e) { 528 throw new ConfigurationException("Could not unpack configuration. The file '"+entryName+"' cannot be unpacked. ", e); 529 } 530 int n = 0; 531 while ((n = jis.read(readBuffer, 0, BUFFER)) != -1) { 532 bos.write(readBuffer, 0, n); 533 } 534 bos.flush(); 535 bos.close(); 536 } 537 } catch (IOException e) { 538 throw new ConfigurationException("Could not unpack configuration. ", e); 539 } 540 } 541 542 /** 543 * Returns the name of the configuration directory 544 * 545 * @return the name of the configuration directory 546 */ 547 public String getName() { 548 return name; 549 } 550 551 protected void setName(String name) { 552 this.name = name; 553 } 554 555 /** 556 * Returns the type of the configuration directory 557 * 558 * @return the type of the configuration directory 559 */ 560 public String getType() { 561 return type; 562 } 563 564 protected void setType(String type) { 565 this.type = type; 566 } 567 568 /** 569 * Returns a file handler object for the working directory 570 * 571 * @return a file handler object for the working directory 572 */ 573 public File getWorkingDirectory() { 574 return workingDirectory; 575 } 576 577 /** 578 * Initialize the working directory 579 * 580 * @throws MaltChainedException 581 */ 582 public void initWorkingDirectory() throws MaltChainedException { 583 try { 584 initWorkingDirectory(OptionManager.instance().getOptionValue(0, "config", "workingdir").toString()); 585 } catch (NullPointerException e) { 586 throw new ConfigurationException("The configuration cannot be found.", e); 587 } 588 } 589 590 /** 591 * Initialize the working directory according to the path. If the path is equals to "user.dir" or current directory, then the current directory 592 * will be the working directory. 593 * 594 * @param pathPrefixString the path to the working directory 595 * @throws MaltChainedException 596 */ 597 public void initWorkingDirectory(String pathPrefixString) throws MaltChainedException { 598 if (pathPrefixString == null || pathPrefixString.equalsIgnoreCase("user.dir") || pathPrefixString.equalsIgnoreCase(".")) { 599 workingDirectory = new File(System.getProperty("user.dir")); 600 } else { 601 workingDirectory = new File(pathPrefixString); 602 } 603 604 if (workingDirectory == null || !workingDirectory.isDirectory()) { 605 new ConfigurationException("The specified working directory '"+pathPrefixString+"' is not a directory. "); 606 } 607 } 608 609 /** 610 * Returns the URL to the malt configuration file (.mco) 611 * 612 * @return the URL to the malt configuration file (.mco) 613 */ 614 public URL getUrl() { 615 return url; 616 } 617 618 protected void setUrl(URL url) { 619 this.url = url; 620 } 621 622 /** 623 * Returns a reference to the configuration 624 * 625 * @return a reference to the configuration 626 */ 627 // public Configuration getConfiguration() { 628 // return configuration; 629 // } 630 631 /** 632 * Sets the reference to the configuration 633 * 634 * @param configuration a reference to a configuration 635 */ 636 // public void setConfiguration(Configuration configuration) { 637 // this.configuration = configuration; 638 // } 639 640 /** 641 * Returns the option container index 642 * 643 * @return the option container index 644 */ 645 public int getContainerIndex() { 646 return containerIndex; 647 } 648 649 /** 650 * Sets the option container index 651 * 652 * @param containerIndex a option container index 653 */ 654 public void setContainerIndex(int containerIndex) { 655 this.containerIndex = containerIndex; 656 } 657 658 /** 659 * Returns the version number of MaltParser which created the malt configuration file (.mco) 660 * 661 * @return the version number of MaltParser which created the malt configuration file (.mco) 662 */ 663 public String getCreatedByMaltParserVersion() { 664 return createdByMaltParserVersion; 665 } 666 667 /** 668 * Sets the version number of MaltParser which created the malt configuration file (.mco) 669 * 670 * @param createdByMaltParserVersion a version number of MaltParser 671 */ 672 public void setCreatedByMaltParserVersion(String createdByMaltParserVersion) { 673 this.createdByMaltParserVersion = createdByMaltParserVersion; 674 } 675 676 protected void initCreatedByMaltParserVersionFromInfoFile() throws MaltChainedException { 677 File info = new File(configDirectory.getPath()+File.separator+getName()+"_"+getType()+".info"); 678 if (!info.exists()) { 679 throw new ConfigurationException("Could not retrieve the version number of the MaltParser configuration."); 680 } 681 try { 682 BufferedReader br = new BufferedReader(new FileReader(info)); 683 String line = null; 684 while ((line = br.readLine()) != null) { 685 if (line.startsWith("Version: ")) { 686 setCreatedByMaltParserVersion(line.substring(31)); 687 break; 688 } 689 } 690 br.close(); 691 } catch (FileNotFoundException e) { 692 throw new ConfigurationException("Could not retrieve the version number of the MaltParser configuration.", e); 693 } catch (IOException e) { 694 throw new ConfigurationException("Could not retrieve the version number of the MaltParser configuration.", e); 695 } 696 checkNConvertConfigVersion(); 697 } 698 699 protected void checkNConvertConfigVersion() throws MaltChainedException { 700 if (createdByMaltParserVersion.startsWith("1.0")) { 701 SystemLogger.logger().info("Convert MaltParser configuration "); 702 SystemLogger.logger().info(createdByMaltParserVersion); 703 SystemLogger.logger().info(" to "); 704 SystemLogger.logger().info(SystemInfo.getVersion()); 705 SystemLogger.logger().info("\n"); 706 File[] configFiles = configDirectory.listFiles(); 707 for (int i = 0, n = configFiles.length; i < n; i++) { 708 if (configFiles[i].getName().endsWith(".mod")) { 709 configFiles[i].renameTo(new File(configDirectory.getPath()+File.separator+"odm0."+configFiles[i].getName())); 710 } 711 if (configFiles[i].getName().endsWith(getName()+".dsm")) { 712 configFiles[i].renameTo(new File(configDirectory.getPath()+File.separator+"odm0.dsm")); 713 } 714 if (configFiles[i].getName().equals("savedoptions.sop")) { 715 configFiles[i].renameTo(new File(configDirectory.getPath()+File.separator+"savedoptions.sop.old")); 716 } 717 if (configFiles[i].getName().equals("symboltables.sym")) { 718 configFiles[i].renameTo(new File(configDirectory.getPath()+File.separator+"symboltables.sym.old")); 719 } 720 } 721 try { 722 BufferedReader br = new BufferedReader(new FileReader(configDirectory.getPath()+File.separator+"savedoptions.sop.old")); 723 BufferedWriter bw = new BufferedWriter(new FileWriter(configDirectory.getPath()+File.separator+"savedoptions.sop")); 724 String line; 725 while ((line = br.readLine()) != null) { 726 if (line.startsWith("0\tguide\tprediction_strategy")) { 727 bw.write("0\tguide\tdecision_settings\tT.TRANS+A.DEPREL\n"); 728 } else { 729 bw.write(line); 730 bw.write('\n'); 731 } 732 } 733 br.close(); 734 bw.flush(); 735 bw.close(); 736 new File(configDirectory.getPath()+File.separator+"savedoptions.sop.old").delete(); 737 } catch (FileNotFoundException e) { 738 throw new ConfigurationException("Could convert savedoptions.sop version 1.0.4 to version 1.1. ", e); 739 } catch (IOException e) { 740 throw new ConfigurationException("Could convert savedoptions.sop version 1.0.4 to version 1.1. ", e); 741 } 742 try { 743 BufferedReader br = new BufferedReader(new FileReader(configDirectory.getPath()+File.separator+"symboltables.sym.old")); 744 BufferedWriter bw = new BufferedWriter(new FileWriter(configDirectory.getPath()+File.separator+"symboltables.sym")); 745 String line; 746 while ((line = br.readLine()) != null) { 747 if (line.startsWith("AllCombinedClassTable")) { 748 bw.write("T.TRANS+A.DEPREL\n"); 749 } else { 750 bw.write(line); 751 bw.write('\n'); 752 } 753 } 754 br.close(); 755 bw.flush(); 756 bw.close(); 757 new File(configDirectory.getPath()+File.separator+"symboltables.sym.old").delete(); 758 } catch (FileNotFoundException e) { 759 throw new ConfigurationException("Could convert symboltables.sym version 1.0.4 to version 1.1. ", e); 760 } catch (IOException e) { 761 throw new ConfigurationException("Could convert symboltables.sym version 1.0.4 to version 1.1. ", e); 762 } 763 } 764 } 765 766 /** 767 * Terminates the configuration directory 768 * 769 * @throws MaltChainedException 770 */ 771 public void terminate() throws MaltChainedException { 772 if (infoFile != null) { 773 try { 774 infoFile.flush(); 775 infoFile.close(); 776 } catch (IOException e) { 777 throw new ConfigurationException("Could not close configuration information file. ", e); 778 } 779 } 780 // configuration = null; 781 } 782 783 /* (non-Javadoc) 784 * @see java.lang.Object#finalize() 785 */ 786 protected void finalize() throws Throwable { 787 try { 788 if (infoFile != null) { 789 infoFile.flush(); 790 infoFile.close(); 791 } 792 } finally { 793 super.finalize(); 794 } 795 } 796 }