Revision: 6992 http://languagetool.svn.sourceforge.net/languagetool/?rev=6992&view=rev Author: milek_pl Date: 2012-05-19 19:22:04 +0000 (Sat, 19 May 2012) Log Message: ----------- make spelling work under JNLP
Modified Paths: -------------- trunk/JLanguageTool/src/java/org/languagetool/JLanguageTool.java trunk/JLanguageTool/src/java/org/languagetool/Main.java trunk/JLanguageTool/src/java/org/languagetool/gui/Main.java trunk/JLanguageTool/src/java/org/languagetool/rules/spelling/hunspell/HunspellRule.java Modified: trunk/JLanguageTool/src/java/org/languagetool/JLanguageTool.java =================================================================== --- trunk/JLanguageTool/src/java/org/languagetool/JLanguageTool.java 2012-05-19 18:10:06 UTC (rev 6991) +++ trunk/JLanguageTool/src/java/org/languagetool/JLanguageTool.java 2012-05-19 19:22:04 UTC (rev 6992) @@ -31,6 +31,8 @@ import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; + +import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -110,7 +112,7 @@ private int sentenceCount; private boolean listUnknownWords; - private Set<String> unknownWords; + private Set<String> unknownWords; /** * Constants for correct paragraph-rule handling. @@ -130,6 +132,8 @@ ONLYNONPARA } + private static List<File> temporaryFiles = new ArrayList<File>(); + // just for testing: /* * private Rule[] allBuiltinRules = new Rule[] { new @@ -794,5 +798,24 @@ printStream.println(s); } } + + /** + * Adds a temporary file to the internal list + * @param f - the file to be added. + */ + public static void addTemporaryFile(final File f) { + temporaryFiles.add(f); + } + + /** + * Clean up all temporary files, if there are any. + */ + public static void removeTemporaryFiles() { + if (!temporaryFiles.isEmpty()) { + for (File f : temporaryFiles) { + f.delete(); + } + } + } } Modified: trunk/JLanguageTool/src/java/org/languagetool/Main.java =================================================================== --- trunk/JLanguageTool/src/java/org/languagetool/Main.java 2012-05-19 18:10:06 UTC (rev 6991) +++ trunk/JLanguageTool/src/java/org/languagetool/Main.java 2012-05-19 19:22:04 UTC (rev 6992) @@ -122,6 +122,14 @@ private void setListUnknownWords(final boolean listUnknownWords) { lt.setListUnknownWords(listUnknownWords); } + + private void cleanUp() { + lt.removeTemporaryFiles(); + if (srcLt != null) { + srcLt.removeTemporaryFiles(); + } + } + private void setProfilingMode() { profileRules = true; @@ -414,7 +422,7 @@ } else { runOnFile(file.getAbsolutePath(), encoding, listUnknown); } - } + } } /** @@ -512,6 +520,7 @@ } else { prg.runOnFile(options.getFilename(), options.getEncoding(), options.isListUnknown()); } + prg.cleanUp(); } // for language auto detect Modified: trunk/JLanguageTool/src/java/org/languagetool/gui/Main.java =================================================================== --- trunk/JLanguageTool/src/java/org/languagetool/gui/Main.java 2012-05-19 18:10:06 UTC (rev 6991) +++ trunk/JLanguageTool/src/java/org/languagetool/gui/Main.java 2012-05-19 19:22:04 UTC (rev 6992) @@ -363,6 +363,7 @@ Tools.showError(e); } frame.setVisible(false); + JLanguageTool.removeTemporaryFiles(); System.exit(0); } Modified: trunk/JLanguageTool/src/java/org/languagetool/rules/spelling/hunspell/HunspellRule.java =================================================================== --- trunk/JLanguageTool/src/java/org/languagetool/rules/spelling/hunspell/HunspellRule.java 2012-05-19 18:10:06 UTC (rev 6991) +++ trunk/JLanguageTool/src/java/org/languagetool/rules/spelling/hunspell/HunspellRule.java 2012-05-19 19:22:04 UTC (rev 6992) @@ -19,9 +19,12 @@ package org.languagetool.rules.spelling.hunspell; -import java.io.FileNotFoundException; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; -import java.io.UnsupportedEncodingException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; @@ -49,8 +52,7 @@ Hunspell.Dictionary dictionary = null; public HunspellRule(final ResourceBundle messages, final Language language) - throws FileNotFoundException, UnsupportedEncodingException, - UnsatisfiedLinkError, UnsupportedOperationException { + throws UnsatisfiedLinkError, UnsupportedOperationException, IOException { super(messages, language); super.setCategory(new Category(messages.getString("category_typo"))); @@ -59,33 +61,65 @@ // in the Language class declaration is important! // we might support country variants in the near future + final String langCountry = language.getShortName() + + "_" + + language.getCountryVariants()[0]; + final String shortDicPath = "/" + language.getShortName() + "/hunspell/" - + language.getShortName() - + "_" - + language.getCountryVariants()[0] + + langCountry + ".dic"; //set dictionary only if there are dictionary files if (JLanguageTool.getDataBroker().resourceExists(shortDicPath)) { - // FIXME: need to change behavior of hunspell library, this is a hack to - // test hunspell - String dictionaryPath = - JLanguageTool.getDataBroker().getFromResourceDirAsUrl( - shortDicPath).getPath(); - dictionaryPath = dictionaryPath.substring(0, dictionaryPath.length() - 4); - - // Note: the class will silently ignore the non-existence of - // dictionaries! - if (JLanguageTool.getDataBroker().getFromResourceDirAsUrl( - shortDicPath) != null) { - dictionary = Hunspell.getInstance().getDictionary(dictionaryPath); - } + dictionary = Hunspell.getInstance(). + getDictionary(getDictionaryPath(langCountry, shortDicPath)); + } } - + + private final String getDictionaryPath(final String dicName, + final String originalPath) throws IOException { + + URL dictURL = JLanguageTool.getDataBroker().getFromResourceDirAsUrl( + originalPath); + + String dictionaryPath = dictURL.getPath(); + + //in the webstart version, we need to copy the files outside the jar + //to the local temporary directory + if ("jar".equals(dictURL.getProtocol())) { + File tempDir = new File(System.getProperty("java.io.tmpdir")); + File temporaryFile = new File(tempDir, dicName + ".dic"); + JLanguageTool.addTemporaryFile(temporaryFile); + fileCopy(JLanguageTool.getDataBroker(). + getFromResourceDirAsStream(originalPath), temporaryFile); + temporaryFile = new File(tempDir, dicName + ".aff"); + JLanguageTool.addTemporaryFile(temporaryFile); + fileCopy(JLanguageTool.getDataBroker(). + getFromResourceDirAsStream(originalPath. + replaceFirst(".dic$", ".aff")), temporaryFile); + + dictionaryPath = tempDir.getAbsolutePath() + "/" + dicName; + } else { + dictionaryPath = dictionaryPath.substring(0, dictionaryPath.length() - 4); + } + return dictionaryPath; + } + + private void fileCopy(final InputStream in, final File targetFile) throws IOException { + OutputStream out = new FileOutputStream(targetFile); + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) > 0){ + out.write(buf, 0, len); + } + in.close(); + out.close(); + } + @Override public String getId() { return "HUNSPELL_RULE"; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Languagetool-cvs mailing list Languagetool-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/languagetool-cvs