Skip to content
This repository has been archived by the owner on Jul 16, 2022. It is now read-only.

Commit

Permalink
Merge branch 'main' into topic/miurahr/preference-appearance
Browse files Browse the repository at this point in the history
  • Loading branch information
miurahr authored Mar 19, 2022
2 parents 2dd724a + e0a10e7 commit eb1a82f
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 137 deletions.
47 changes: 42 additions & 5 deletions src/main/java/io/github/eb4j/ebview/dictionary/LingvoDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@
import io.github.eb4j.dsl.visitor.HtmlDslVisitor;
import io.github.eb4j.ebview.data.DictionaryEntry;
import io.github.eb4j.ebview.data.IDictionary;
import io.github.eb4j.ebview.utils.Platform;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -46,9 +54,7 @@ public boolean isSupportedFile(final File file) {

@Override
public Set<IDictionary> loadDict(final File file) throws Exception {
Set<IDictionary> result = new HashSet<>();
result.add(new LingvoDSLDictionary(file));
return result;
return Collections.singleton(new LingvoDSLDictionary(file));
}

/**
Expand All @@ -62,10 +68,41 @@ public static class LingvoDSLDictionary implements IDictionary {
private final HtmlDslVisitor htmlVisitor;

public LingvoDSLDictionary(final File file) throws Exception {
dictionary = DslDictionary.loadDictionary(file);
Path dictPath = Paths.get(file.toURI());
Path cachePath = getDictCachePath(dictPath);
dictionary = DslDictionary.loadDictionary(dictPath, cachePath, true);
htmlVisitor = new HtmlDslVisitor(file.getParent());
}

private Path enforceCachePath(Path cachePath) {
if (!cachePath.getParent().toFile().exists()) {
boolean result = cachePath.getParent().toFile().mkdirs();
if (!result) {
return null;
}
}
return cachePath;
}

private Path getDictCachePath(Path dictpath) {
String cacheDir = Platform.getCacheDir();
if (StringUtils.isEmpty(cacheDir)) {
return null;
}
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(dictpath.toString().getBytes(StandardCharsets.UTF_8));
String hash = String.format("%020x", new BigInteger(1, digest.digest()));
Path filename = dictpath.getFileName();
if (filename == null) {
return null;
}
return enforceCachePath(Paths.get(cacheDir, hash).resolve(filename + ".idx"));
} catch (NoSuchAlgorithmException ex) {
return null;
}
}

@Override
public String getDictionaryName() {
return dictionary.getDictionaryName();
Expand Down
181 changes: 181 additions & 0 deletions src/main/java/io/github/eb4j/ebview/utils/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,188 @@

package io.github.eb4j.ebview.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public final class Platform {

static final Logger LOG = LoggerFactory.getLogger(Platform.class.getName());
/**
* Configuration directory on Windows platforms
*/
private static final String WINDOWS_CONFIG_DIR = "\\ebviewer\\";
private static final String WINDOWS_CACHE_DIR = "\\wbviewer\\";
/**
* Configuration directory on UNIX platforms
*/
private static final String UNIX_CONFIG_DIR = "/.config/ebviewer/";
private static final String UNIX_CACHE_DIR = "/.cache/ebviewer/";
/**
* Configuration directory on Mac OS X
*/
private static final String OSX_CONFIG_DIR = "/Library/Preferences/ebviewer/";
private static final String OSX_CACHE_DIR = "/Library/Caches/ebviewer/";
/**
* Contains the location of the directory containing the configuration
* files.
*/
private static String configDir = null;
/**
* Contains the location of the directory containing caches.
*/
private static String cacheDir = null;

private final static String NOCACHE = "-no-cache-";

/**
* Returns the location of the configuration directory, depending on the
* user's platform. Also creates the configuration directory, if necessary.
* If any problems occur while the location of the configuration directory
* is being determined, an empty string will be returned, resulting in the
* current working directory being used.
*
* <ul><li>Windows XP: &lt;Documents and Settings>\&lt;User name>\Application Data\OmegaT
* <li>Windows Vista: User\&lt;User name>\AppData\Roaming
* <li>Linux: ~/.config/ebviewer
* <li>Solaris/SunOS: ~/.ebviewer
* <li>FreeBSD: ~/.ebviewer
* <li>Mac OS X: ~/Library/Preferences/ebviewer
* <li>Other: User home directory
* </ul>
*
* @return The full path of the directory containing the configuration files, including trailing path separator.
*/
public static String getConfigDir() {
// if the configuration directory has already been determined, return it
if (configDir != null) {
return configDir;
}

OsType os = getOsType(); // name of operating system
String home; // user home directory

// get os and user home properties
try {
// get the user's home directory
home = System.getProperty("user.home");
} catch (SecurityException e) {
// access to the os/user home properties is restricted,
// the location of the config dir cannot be determined,
// set the config dir to the current working dir
configDir = new File(".").getAbsolutePath() + File.separator;

// log the exception, only do this after the config dir
// has been set to the current working dir, otherwise
// the log method will probably fail
LOG.error(e.toString());

return configDir;
}

// if os or user home is null or empty, we cannot reliably determine
// the config dir, so we use the current working dir (= empty string)
if (os == null || StringUtils.isEmpty(home)) {
// set the config dir to the current working dir
configDir = new File(".").getAbsolutePath() + File.separator;
return configDir;
}

if (isWindows()) {
String appData = new File(home, "AppData\\Roaming").getAbsolutePath();
if (!StringUtils.isEmpty(appData)) {
configDir = appData + WINDOWS_CONFIG_DIR;
} else {
configDir = home + WINDOWS_CONFIG_DIR;
}
} else if (isLinux() || os == OsType.OTHER) {
configDir = home + UNIX_CONFIG_DIR;
} else if (isMacOSX()) {
configDir = home + OSX_CONFIG_DIR;
} else {
configDir = home + File.separator;
}

// create the path to the configuration dir, if necessary
if (!configDir.isEmpty()) {
try {
// check if the dir exists
File dir = new File(configDir);
if (!dir.exists()) {
// create the dir
boolean created = dir.mkdirs();

// if the dir could not be created,
// set the config dir to the current working dir
if (!created) {
configDir = new File(".").getAbsolutePath() + File.separator;
}
}
} catch (SecurityException e) {
// the system doesn't want us to write where we want to write
// reset the config dir to the current working dir
configDir = new File(".").getAbsolutePath() + File.separator;

// log the exception, but only after the config dir has been
// reset
LOG.error(e.toString());
}
}

// we should have a correct, existing config dir now
return configDir;
}

public static String getCacheDir() {
// if the configuration directory has already been determined, return it
if (cacheDir != null) {
if (cacheDir.equals(NOCACHE)) {
return null;
}
return cacheDir;
}

String home;
try {
home = System.getProperty("user.home");
} catch (SecurityException e) {
// access to the os/user home properties is restricted,
cacheDir = NOCACHE;
return cacheDir;
}
if (isWindows()) {
String appData = new File(home, "AppData\\LocalLow").getAbsolutePath();
if (!StringUtils.isEmpty(appData)) {
cacheDir = appData + WINDOWS_CACHE_DIR;
} else {
cacheDir = home + WINDOWS_CACHE_DIR;
}
} else if (isLinux() || getOsType() == OsType.OTHER) {
cacheDir = home + UNIX_CACHE_DIR;
} else if (isMacOSX()) {
cacheDir = home + OSX_CACHE_DIR;
} else {
cacheDir = NOCACHE;
}

if (!cacheDir.equals(NOCACHE)) {
try {
File dir = new File(cacheDir);
if (!dir.exists()) {
boolean created = dir.mkdirs();
if (!created) {
cacheDir = NOCACHE;
}
}
} catch (SecurityException e) {
cacheDir = NOCACHE;
}
}
return cacheDir;
}

public enum OsType {
// os.arch=amd64, os.name=Linux, os.version=3.0.0-12-generic
LINUX64,
Expand Down
Loading

0 comments on commit eb1a82f

Please sign in to comment.