From: Niki Roo Date: Wed, 20 May 2020 14:39:59 +0000 (+0200) Subject: Merge branch 'subtree' X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=commitdiff_plain;h=53c2b6a134b08402e1daf3e4c84b9b888de9cc9c;hp=977f60a2bf84634a5343c8b244606ae9b4edda0b Merge branch 'subtree' --- diff --git a/Cache.java b/Cache.java new file mode 100644 index 0000000..6233082 --- /dev/null +++ b/Cache.java @@ -0,0 +1,457 @@ +package be.nikiroo.utils; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Date; + +import be.nikiroo.utils.streams.MarkableFileInputStream; + +/** + * A generic cache system, with special support for {@link URL}s. + *

+ * This cache also manages timeout information. + * + * @author niki + */ +public class Cache { + private File dir; + private long tooOldChanging; + private long tooOldStable; + private TraceHandler tracer = new TraceHandler(); + + /** + * Only for inheritance. + */ + protected Cache() { + } + + /** + * Create a new {@link Cache} object. + * + * @param dir + * the directory to use as cache + * @param hoursChanging + * the number of hours after which a cached file that is thought + * to change ~often is considered too old (or -1 for + * "never too old") + * @param hoursStable + * the number of hours after which a cached file that is thought + * to change rarely is considered too old (or -1 for + * "never too old") + * + * @throws IOException + * in case of I/O error + */ + public Cache(File dir, int hoursChanging, int hoursStable) + throws IOException { + this.dir = dir; + this.tooOldChanging = 1000L * 60 * 60 * hoursChanging; + this.tooOldStable = 1000L * 60 * 60 * hoursStable; + + if (dir != null && !dir.exists()) { + dir.mkdirs(); + } + + if (dir == null || !dir.exists()) { + throw new IOException("Cannot create the cache directory: " + + (dir == null ? "null" : dir.getAbsolutePath())); + } + } + + /** + * The traces handler for this {@link Cache}. + * + * @return the traces handler + */ + public TraceHandler getTraceHandler() { + return tracer; + } + + /** + * The traces handler for this {@link Cache}. + * + * @param tracer + * the new traces handler + */ + public void setTraceHandler(TraceHandler tracer) { + if (tracer == null) { + tracer = new TraceHandler(false, false, false); + } + + this.tracer = tracer; + } + + /** + * Check the resource to see if it is in the cache. + * + * @param uniqueID + * the resource to check + * @param allowTooOld + * allow files even if they are considered too old + * @param stable + * a stable file (that dones't change too often) -- parameter + * used to check if the file is too old to keep or not + * + * @return TRUE if it is + * + */ + public boolean check(String uniqueID, boolean allowTooOld, boolean stable) { + return check(getCached(uniqueID), allowTooOld, stable); + } + + /** + * Check the resource to see if it is in the cache. + * + * @param url + * the resource to check + * @param allowTooOld + * allow files even if they are considered too old + * @param stable + * a stable file (that dones't change too often) -- parameter + * used to check if the file is too old to keep or not + * + * @return TRUE if it is + * + */ + public boolean check(URL url, boolean allowTooOld, boolean stable) { + return check(getCached(url), allowTooOld, stable); + } + + /** + * Check the resource to see if it is in the cache. + * + * @param cached + * the resource to check + * @param allowTooOld + * allow files even if they are considered too old + * @param stable + * a stable file (that dones't change too often) -- parameter + * used to check if the file is too old to keep or not + * + * @return TRUE if it is + * + */ + private boolean check(File cached, boolean allowTooOld, boolean stable) { + if (cached.exists() && cached.isFile()) { + if (!allowTooOld && isOld(cached, stable)) { + if (!cached.delete()) { + tracer.error("Cannot delete temporary file: " + + cached.getAbsolutePath()); + } + } else { + return true; + } + } + + return false; + } + + /** + * Clean the cache (delete the cached items). + * + * @param onlyOld + * only clean the files that are considered too old for a stable + * resource + * + * @return the number of cleaned items + */ + public int clean(boolean onlyOld) { + long ms = System.currentTimeMillis(); + + tracer.trace("Cleaning cache from old files..."); + + int num = clean(onlyOld, dir, -1); + + tracer.trace(num + "cache items cleaned in " + + (System.currentTimeMillis() - ms) + " ms"); + + return num; + } + + /** + * Clean the cache (delete the cached items) in the given cache directory. + * + * @param onlyOld + * only clean the files that are considered too old for stable + * resources + * @param cacheDir + * the cache directory to clean + * @param limit + * stop after limit files deleted, or -1 for unlimited + * + * @return the number of cleaned items + */ + private int clean(boolean onlyOld, File cacheDir, int limit) { + int num = 0; + File[] files = cacheDir.listFiles(); + if (files != null) { + for (File file : files) { + if (limit >= 0 && num >= limit) { + return num; + } + + if (file.isDirectory()) { + num += clean(onlyOld, file, limit); + file.delete(); // only if empty + } else { + if (!onlyOld || isOld(file, true)) { + if (file.delete()) { + num++; + } else { + tracer.error("Cannot delete temporary file: " + + file.getAbsolutePath()); + } + } + } + } + } + + return num; + } + + /** + * Open a resource from the cache if it exists. + * + * @param uniqueID + * the unique ID + * @param allowTooOld + * allow files even if they are considered too old + * @param stable + * a stable file (that dones't change too often) -- parameter + * used to check if the file is too old to keep or not + * + * @return the opened resource if found, NULL if not + */ + public InputStream load(String uniqueID, boolean allowTooOld, boolean stable) { + return load(getCached(uniqueID), allowTooOld, stable); + } + + /** + * Open a resource from the cache if it exists. + * + * @param url + * the resource to open + * @param allowTooOld + * allow files even if they are considered too old + * @param stable + * a stable file (that doesn't change too often) -- parameter + * used to check if the file is too old to keep or not in the + * cache + * + * @return the opened resource if found, NULL if not + */ + public InputStream load(URL url, boolean allowTooOld, boolean stable) { + return load(getCached(url), allowTooOld, stable); + } + + /** + * Open a resource from the cache if it exists. + * + * @param cached + * the resource to open + * @param allowTooOld + * allow files even if they are considered too old + * @param stable + * a stable file (that dones't change too often) -- parameter + * used to check if the file is too old to keep or not + * + * @return the opened resource if found, NULL if not + */ + private InputStream load(File cached, boolean allowTooOld, boolean stable) { + if (cached.exists() && cached.isFile() + && (allowTooOld || !isOld(cached, stable))) { + try { + return new MarkableFileInputStream(cached); + } catch (FileNotFoundException e) { + return null; + } + } + + return null; + } + + /** + * Save the given resource to the cache. + * + * @param in + * the input data + * @param uniqueID + * a unique ID used to locate the cached resource + * + * @return the number of bytes written + * + * @throws IOException + * in case of I/O error + */ + public long save(InputStream in, String uniqueID) throws IOException { + File cached = getCached(uniqueID); + cached.getParentFile().mkdirs(); + return save(in, cached); + } + + /** + * Save the given resource to the cache. + * + * @param in + * the input data + * @param url + * the {@link URL} used to locate the cached resource + * + * @return the number of bytes written + * + * @throws IOException + * in case of I/O error + */ + public long save(InputStream in, URL url) throws IOException { + File cached = getCached(url); + return save(in, cached); + } + + /** + * Save the given resource to the cache. + *

+ * Will also clean the {@link Cache} from old files. + * + * @param in + * the input data + * @param cached + * the cached {@link File} to save to + * + * @return the number of bytes written + * + * @throws IOException + * in case of I/O error + */ + private long save(InputStream in, File cached) throws IOException { + // We want to force at least an immediate SAVE/LOAD to work for some + // workflows, even if we don't accept cached files (times set to "0" + // -- and not "-1" or a positive value) + clean(true, dir, 10); + cached.getParentFile().mkdirs(); // in case we deleted our own parent + long bytes = IOUtils.write(in, cached); + return bytes; + } + + /** + * Remove the given resource from the cache. + * + * @param uniqueID + * a unique ID used to locate the cached resource + * + * @return TRUE if it was removed + */ + public boolean remove(String uniqueID) { + File cached = getCached(uniqueID); + return cached.delete(); + } + + /** + * Remove the given resource from the cache. + * + * @param url + * the {@link URL} used to locate the cached resource + * + * @return TRUE if it was removed + */ + public boolean remove(URL url) { + File cached = getCached(url); + return cached.delete(); + } + + /** + * Check if the {@link File} is too old according to + * {@link Cache#tooOldChanging}. + * + * @param file + * the file to check + * @param stable + * TRUE to denote stable files, that are not supposed to change + * too often + * + * @return TRUE if it is + */ + private boolean isOld(File file, boolean stable) { + long max = tooOldChanging; + if (stable) { + max = tooOldStable; + } + + if (max < 0) { + return false; + } + + long time = new Date().getTime() - file.lastModified(); + if (time < 0) { + tracer.error("Timestamp in the future for file: " + + file.getAbsolutePath()); + } + + return time < 0 || time > max; + } + + /** + * Return the associated cache {@link File} from this {@link URL}. + * + * @param url + * the {@link URL} + * + * @return the cached {@link File} version of this {@link URL} + */ + private File getCached(URL url) { + File subdir; + + String name = url.getHost(); + if (name == null || name.isEmpty()) { + // File + File file = new File(url.getFile()); + if (file.getParent() == null) { + subdir = new File("+"); + } else { + subdir = new File(file.getParent().replace("..", "__")); + } + subdir = new File(dir, allowedChars(subdir.getPath())); + name = allowedChars(url.getFile()); + } else { + // URL + File subsubDir = new File(dir, allowedChars(url.getHost())); + subdir = new File(subsubDir, "_" + allowedChars(url.getPath())); + name = allowedChars("_" + url.getQuery()); + } + + File cacheFile = new File(subdir, name); + subdir.mkdirs(); + + return cacheFile; + } + + /** + * Get the basic cache resource file corresponding to this unique ID. + *

+ * Note that you may need to add a sub-directory in some cases. + * + * @param uniqueID + * the id + * + * @return the cached version if present, NULL if not + */ + private File getCached(String uniqueID) { + File file = new File(dir, allowedChars(uniqueID)); + File subdir = new File(file.getParentFile(), "_"); + return new File(subdir, file.getName()); + } + + /** + * Replace not allowed chars (in a {@link File}) by "_". + * + * @param raw + * the raw {@link String} + * + * @return the sanitised {@link String} + */ + private String allowedChars(String raw) { + return raw.replace('/', '_').replace(':', '_').replace("\\", "_"); + } +} \ No newline at end of file diff --git a/CacheMemory.java b/CacheMemory.java new file mode 100644 index 0000000..de4fae3 --- /dev/null +++ b/CacheMemory.java @@ -0,0 +1,124 @@ +package be.nikiroo.utils; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +/** + * A memory only version of {@link Cache}. + * + * @author niki + */ +public class CacheMemory extends Cache { + private Map data; + + /** + * Create a new {@link CacheMemory}. + */ + public CacheMemory() { + data = new HashMap(); + } + + @Override + public boolean check(String uniqueID, boolean allowTooOld, boolean stable) { + return data.containsKey(getKey(uniqueID)); + } + + @Override + public boolean check(URL url, boolean allowTooOld, boolean stable) { + return data.containsKey(getKey(url)); + } + + @Override + public int clean(boolean onlyOld) { + int cleaned = 0; + if (!onlyOld) { + cleaned = data.size(); + data.clear(); + } + + return cleaned; + } + + @Override + public InputStream load(String uniqueID, boolean allowTooOld, boolean stable) { + if (check(uniqueID, allowTooOld, stable)) { + return load(getKey(uniqueID)); + } + + return null; + } + + @Override + public InputStream load(URL url, boolean allowTooOld, boolean stable) { + if (check(url, allowTooOld, stable)) { + return load(getKey(url)); + } + + return null; + } + + @Override + public boolean remove(String uniqueID) { + return data.remove(getKey(uniqueID)) != null; + } + + @Override + public boolean remove(URL url) { + return data.remove(getKey(url)) != null; + } + + @Override + public long save(InputStream in, String uniqueID) throws IOException { + byte[] bytes = IOUtils.toByteArray(in); + data.put(getKey(uniqueID), bytes); + return bytes.length; + } + + @Override + public long save(InputStream in, URL url) throws IOException { + byte[] bytes = IOUtils.toByteArray(in); + data.put(getKey(url), bytes); + return bytes.length; + } + + /** + * Return a key mapping to the given unique ID. + * + * @param uniqueID the unique ID + * + * @return the key + */ + private String getKey(String uniqueID) { + return "UID:" + uniqueID; + } + + /** + * Return a key mapping to the given urm. + * + * @param url the url + * + * @return the key + */ + private String getKey(URL url) { + return "URL:" + url.toString(); + } + + /** + * Load the given key. + * + * @param key the key to load + * @return the loaded data + */ + private InputStream load(String key) { + byte[] data = this.data.get(key); + if (data != null) { + return new ByteArrayInputStream(data); + } + + return null; + } +} diff --git a/CookieUtils.java b/CookieUtils.java new file mode 100644 index 0000000..8d307a2 --- /dev/null +++ b/CookieUtils.java @@ -0,0 +1,62 @@ +package be.nikiroo.utils; + +import java.util.Date; + +/** + * Some utilities for cookie management. + * + * @author niki + */ +public class CookieUtils { + /** + * The number of seconds for the period (we accept the current or the + * previous period as valid for a cookie, via "offset"). + */ + static public int GRACE_PERIOD = 3600 * 1000; // between 1 and 2h + + /** + * Generate a new cookie value from the user (email) and an offset. + *

+ * You should use an offset of "0" when creating the cookie, and an offset + * of "0" or "-1" if required when checking for the value (the idea is to + * allow a cookie to persist across two timespans; if not, the cookie will + * be expired the very second we switch to a new timespan). + * + * @param value + * the value to generate a cookie for -- you must be able to + * regenerate it in order to check it later + * @param offset + * the offset (should be 0 for creating, 0 then -1 if needed for + * checking) + * + * @return the new cookie + */ + static public String generateCookie(String value, int offset) { + long unixTime = (long) Math.floor(new Date().getTime() / GRACE_PERIOD) + + offset; + return HashUtils.sha512(value + Long.toString(unixTime)); + } + + /** + * Check the given cookie. + * + * @param value + * the value to generate a cookie for -- you must be able to + * regenerate it in order to check it later + * @param cookie + * the cookie to validate + * + * @return TRUE if it is correct + */ + static public boolean validateCookie(String value, String cookie) { + if (cookie != null) + cookie = cookie.trim(); + + String newCookie = generateCookie(value, 0); + if (!newCookie.equals(cookie)) { + newCookie = generateCookie(value, -1); + } + + return newCookie.equals(cookie); + } +} diff --git a/CryptUtils.java b/CryptUtils.java new file mode 100644 index 0000000..638f82f --- /dev/null +++ b/CryptUtils.java @@ -0,0 +1,441 @@ +package be.nikiroo.utils; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.security.InvalidKeyException; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.CipherInputStream; +import javax.crypto.CipherOutputStream; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; +import javax.net.ssl.SSLException; + +import be.nikiroo.utils.streams.Base64InputStream; +import be.nikiroo.utils.streams.Base64OutputStream; + +/** + * Small utility class to do AES encryption/decryption. + *

+ * It is multi-thread compatible, but beware: + *

+ *

+ * Do not assume it is secure; it just here to offer a more-or-less protected + * exchange of data because anonymous and self-signed certificates backed SSL is + * against Google wishes, and I need Android support. + * + * @author niki + */ +public class CryptUtils { + static private final String AES_NAME = "AES/CFB128/NoPadding"; + + private Cipher ecipher; + private Cipher dcipher; + private byte[] bytes32; + + /** + * Small and lazy-easy way to initialize a 128 bits key with + * {@link CryptUtils}. + *

+ * Some part of the key will be used to generate a 128 bits key and + * initialize the {@link CryptUtils}; even NULL will generate something. + *

+ * This is most probably not secure. Do not use if you actually care + * about security. + * + * @param key + * the {@link String} to use as a base for the key, can be NULL + */ + public CryptUtils(String key) { + try { + init(key2key(key)); + } catch (InvalidKeyException e) { + // We made sure that the key is correct, so nothing here + e.printStackTrace(); + } + } + + /** + * Create a new instance of {@link CryptUtils} with the given 128 bits key. + *

+ * The key must be exactly 128 bits long. + * + * @param bytes32 + * the 128 bits (32 bytes) of the key + * + * @throws InvalidKeyException + * if the key is not an array of 128 bits + */ + public CryptUtils(byte[] bytes32) throws InvalidKeyException { + init(bytes32); + } + + /** + * Wrap the given {@link InputStream} so it is transparently encrypted by + * the current {@link CryptUtils}. + * + * @param in + * the {@link InputStream} to wrap + * @return the auto-encode {@link InputStream} + */ + public InputStream encrypt(InputStream in) { + Cipher ecipher = newCipher(Cipher.ENCRYPT_MODE); + return new CipherInputStream(in, ecipher); + } + + /** + * Wrap the given {@link InputStream} so it is transparently encrypted by + * the current {@link CryptUtils} and encoded in base64. + * + * @param in + * the {@link InputStream} to wrap + * + * @return the auto-encode {@link InputStream} + * + * @throws IOException + * in case of I/O error + */ + public InputStream encrypt64(InputStream in) throws IOException { + return new Base64InputStream(encrypt(in), true); + } + + /** + * Wrap the given {@link OutputStream} so it is transparently encrypted by + * the current {@link CryptUtils}. + * + * @param out + * the {@link OutputStream} to wrap + * + * @return the auto-encode {@link OutputStream} + */ + public OutputStream encrypt(OutputStream out) { + Cipher ecipher = newCipher(Cipher.ENCRYPT_MODE); + return new CipherOutputStream(out, ecipher); + } + + /** + * Wrap the given {@link OutputStream} so it is transparently encrypted by + * the current {@link CryptUtils} and encoded in base64. + * + * @param out + * the {@link OutputStream} to wrap + * + * @return the auto-encode {@link OutputStream} + * + * @throws IOException + * in case of I/O error + */ + public OutputStream encrypt64(OutputStream out) throws IOException { + return encrypt(new Base64OutputStream(out, true)); + } + + /** + * Wrap the given {@link OutputStream} so it is transparently decoded by the + * current {@link CryptUtils}. + * + * @param in + * the {@link InputStream} to wrap + * + * @return the auto-decode {@link InputStream} + */ + public InputStream decrypt(InputStream in) { + Cipher dcipher = newCipher(Cipher.DECRYPT_MODE); + return new CipherInputStream(in, dcipher); + } + + /** + * Wrap the given {@link OutputStream} so it is transparently decoded by the + * current {@link CryptUtils} and decoded from base64. + * + * @param in + * the {@link InputStream} to wrap + * + * @return the auto-decode {@link InputStream} + * + * @throws IOException + * in case of I/O error + */ + public InputStream decrypt64(InputStream in) throws IOException { + return decrypt(new Base64InputStream(in, false)); + } + + /** + * Wrap the given {@link OutputStream} so it is transparently decoded by the + * current {@link CryptUtils}. + * + * @param out + * the {@link OutputStream} to wrap + * @return the auto-decode {@link OutputStream} + */ + public OutputStream decrypt(OutputStream out) { + Cipher dcipher = newCipher(Cipher.DECRYPT_MODE); + return new CipherOutputStream(out, dcipher); + } + + /** + * Wrap the given {@link OutputStream} so it is transparently decoded by the + * current {@link CryptUtils} and decoded from base64. + * + * @param out + * the {@link OutputStream} to wrap + * + * @return the auto-decode {@link OutputStream} + * + * @throws IOException + * in case of I/O error + */ + public OutputStream decrypt64(OutputStream out) throws IOException { + return new Base64OutputStream(decrypt(out), false); + } + + /** + * This method required an array of 128 bits. + * + * @param bytes32 + * the array, which must be of 128 bits (32 bytes) + * + * @throws InvalidKeyException + * if the key is not an array of 128 bits (32 bytes) + */ + private void init(byte[] bytes32) throws InvalidKeyException { + if (bytes32 == null || bytes32.length != 32) { + throw new InvalidKeyException( + "The size of the key must be of 128 bits (32 bytes), it is: " + + (bytes32 == null ? "null" : "" + bytes32.length) + + " bytes"); + } + + this.bytes32 = bytes32; + this.ecipher = newCipher(Cipher.ENCRYPT_MODE); + this.dcipher = newCipher(Cipher.DECRYPT_MODE); + } + + /** + * Create a new {@link Cipher}of the given mode (see + * {@link Cipher#ENCRYPT_MODE} and {@link Cipher#ENCRYPT_MODE}). + * + * @param mode + * the mode ({@link Cipher#ENCRYPT_MODE} or + * {@link Cipher#ENCRYPT_MODE}) + * + * @return the new {@link Cipher} + */ + private Cipher newCipher(int mode) { + try { + // bytes32 = 32 bytes, 32 > 16 + byte[] iv = new byte[16]; + for (int i = 0; i < iv.length; i++) { + iv[i] = bytes32[i]; + } + IvParameterSpec ivspec = new IvParameterSpec(iv); + Cipher cipher = Cipher.getInstance(AES_NAME); + cipher.init(mode, new SecretKeySpec(bytes32, "AES"), ivspec); + return cipher; + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException( + "Cannot initialize encryption sub-system", e); + } + } + + /** + * Encrypt the data. + * + * @param data + * the data to encrypt + * + * @return the encrypted data + * + * @throws SSLException + * in case of I/O error (i.e., the data is not what you assumed + * it was) + */ + public byte[] encrypt(byte[] data) throws SSLException { + synchronized (ecipher) { + try { + return ecipher.doFinal(data); + } catch (IllegalBlockSizeException e) { + throw new SSLException(e); + } catch (BadPaddingException e) { + throw new SSLException(e); + } + } + } + + /** + * Encrypt the data. + * + * @param data + * the data to encrypt + * + * @return the encrypted data + * + * @throws SSLException + * in case of I/O error (i.e., the data is not what you assumed + * it was) + */ + public byte[] encrypt(String data) throws SSLException { + return encrypt(StringUtils.getBytes(data)); + } + + /** + * Encrypt the data, then encode it into Base64. + * + * @param data + * the data to encrypt + * @param zip + * TRUE to also compress the data in GZIP format; remember that + * compressed and not-compressed content are different; you need + * to know which is which when decoding + * + * @return the encrypted data, encoded in Base64 + * + * @throws SSLException + * in case of I/O error (i.e., the data is not what you assumed + * it was) + */ + public String encrypt64(String data) throws SSLException { + return encrypt64(StringUtils.getBytes(data)); + } + + /** + * Encrypt the data, then encode it into Base64. + * + * @param data + * the data to encrypt + * + * @return the encrypted data, encoded in Base64 + * + * @throws SSLException + * in case of I/O error (i.e., the data is not what you assumed + * it was) + */ + public String encrypt64(byte[] data) throws SSLException { + try { + return StringUtils.base64(encrypt(data)); + } catch (IOException e) { + // not exactly true, but we consider here that this error is a crypt + // error, not a normal I/O error + throw new SSLException(e); + } + } + + /** + * Decode the data which is assumed to be encrypted with the same utilities. + * + * @param data + * the encrypted data to decode + * + * @return the original, decoded data + * + * @throws SSLException + * in case of I/O error + */ + public byte[] decrypt(byte[] data) throws SSLException { + synchronized (dcipher) { + try { + return dcipher.doFinal(data); + } catch (IllegalBlockSizeException e) { + throw new SSLException(e); + } catch (BadPaddingException e) { + throw new SSLException(e); + } + } + } + + /** + * Decode the data which is assumed to be encrypted with the same utilities + * and to be a {@link String}. + * + * @param data + * the encrypted data to decode + * + * @return the original, decoded data,as a {@link String} + * + * @throws SSLException + * in case of I/O error + */ + public String decrypts(byte[] data) throws SSLException { + try { + return new String(decrypt(data), "UTF-8"); + } catch (UnsupportedEncodingException e) { + // UTF-8 is required in all conform JVMs + e.printStackTrace(); + return null; + } + } + + /** + * Decode the data which is assumed to be encrypted with the same utilities + * and is a Base64 encoded value. + * + * @param data + * the encrypted data to decode in Base64 format + * @param zip + * TRUE to also uncompress the data from a GZIP format + * automatically; if set to FALSE, zipped data can be returned + * + * @return the original, decoded data + * + * @throws SSLException + * in case of I/O error + */ + public byte[] decrypt64(String data) throws SSLException { + try { + return decrypt(StringUtils.unbase64(data)); + } catch (IOException e) { + // not exactly true, but we consider here that this error is a crypt + // error, not a normal I/O error + throw new SSLException(e); + } + } + + /** + * Decode the data which is assumed to be encrypted with the same utilities + * and is a Base64 encoded value, then convert it into a String (this method + * assumes the data was indeed a UTF-8 encoded {@link String}). + * + * @param data + * the encrypted data to decode in Base64 format + * @param zip + * TRUE to also uncompress the data from a GZIP format + * automatically; if set to FALSE, zipped data can be returned + * + * @return the original, decoded data + * + * @throws SSLException + * in case of I/O error + */ + public String decrypt64s(String data) throws SSLException { + try { + return new String(decrypt(StringUtils.unbase64(data)), "UTF-8"); + } catch (UnsupportedEncodingException e) { + // UTF-8 is required in all conform JVMs + e.printStackTrace(); + return null; + } catch (IOException e) { + // not exactly true, but we consider here that this error is a crypt + // error, not a normal I/O error + throw new SSLException(e); + } + } + + /** + * This is probably NOT secure! + * + * @param input + * some {@link String} input + * + * @return a 128 bits key computed from the given input + */ + static private byte[] key2key(String input) { + return StringUtils.getMd5Hash("" + input).getBytes(); + } +} diff --git a/DataLoader.java b/DataLoader.java deleted file mode 100644 index 901e8da..0000000 --- a/DataLoader.java +++ /dev/null @@ -1,396 +0,0 @@ -package be.nikiroo.fanfix; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Map; - -import be.nikiroo.fanfix.bundles.Config; -import be.nikiroo.fanfix.supported.BasicSupport; -import be.nikiroo.utils.Cache; -import be.nikiroo.utils.CacheMemory; -import be.nikiroo.utils.Downloader; -import be.nikiroo.utils.Image; -import be.nikiroo.utils.ImageUtils; -import be.nikiroo.utils.TraceHandler; - -/** - * This cache will manage Internet (and local) downloads, as well as put the - * downloaded files into a cache. - *

- * As long the cached resource is not too old, it will use it instead of - * retrieving the file again. - * - * @author niki - */ -public class DataLoader { - private Downloader downloader; - private Downloader downloaderNoCache; - private Cache cache; - private boolean offline; - - /** - * Create a new {@link DataLoader} object. - * - * @param dir - * the directory to use as cache - * @param UA - * the User-Agent to use to download the resources - * @param hoursChanging - * the number of hours after which a cached file that is thought - * to change ~often is considered too old (or -1 for - * "never too old") - * @param hoursStable - * the number of hours after which a LARGE cached file that is - * thought to change rarely is considered too old (or -1 for - * "never too old") - * - * @throws IOException - * in case of I/O error - */ - public DataLoader(File dir, String UA, int hoursChanging, int hoursStable) - throws IOException { - downloader = new Downloader(UA, new Cache(dir, hoursChanging, - hoursStable)); - downloaderNoCache = new Downloader(UA); - - cache = downloader.getCache(); - } - - /** - * Create a new {@link DataLoader} object without disk cache (will keep a - * memory cache for manual cache operations). - * - * @param UA - * the User-Agent to use to download the resources - */ - public DataLoader(String UA) { - downloader = new Downloader(UA); - downloaderNoCache = downloader; - cache = new CacheMemory(); - } - - /** - * This {@link Downloader} is forbidden to try and connect to the network. - *

- * If TRUE, it will only check the cache (even in no-cache mode!). - *

- * Default is FALSE. - * - * @return TRUE if offline - */ - public boolean isOffline() { - return offline; - } - - /** - * This {@link Downloader} is forbidden to try and connect to the network. - *

- * If TRUE, it will only check the cache (even in no-cache mode!). - *

- * Default is FALSE. - * - * @param offline TRUE for offline, FALSE for online - */ - public void setOffline(boolean offline) { - this.offline = offline; - downloader.setOffline(offline); - downloaderNoCache.setOffline(offline); - - // If we don't, we cannot support no-cache using code in OFFLINE mode - if (offline) { - downloaderNoCache.setCache(cache); - } else { - downloaderNoCache.setCache(null); - } - } - - /** - * The traces handler for this {@link Cache}. - * - * @param tracer - * the new traces handler - */ - public void setTraceHandler(TraceHandler tracer) { - downloader.setTraceHandler(tracer); - downloaderNoCache.setTraceHandler(tracer); - cache.setTraceHandler(tracer); - if (downloader.getCache() != null) { - downloader.getCache().setTraceHandler(tracer); - } - - } - - /** - * Open a resource (will load it from the cache if possible, or save it into - * the cache after downloading if not). - *

- * The cached resource will be assimilated to the given original {@link URL} - * - * @param url - * the resource to open - * @param support - * the support to use to download the resource (can be NULL) - * @param stable - * TRUE for more stable resources, FALSE when they often change - * - * @return the opened resource, NOT NULL - * - * @throws IOException - * in case of I/O error - */ - public InputStream open(URL url, BasicSupport support, boolean stable) - throws IOException { - return open(url, url, support, stable, null, null, null); - } - - /** - * Open a resource (will load it from the cache if possible, or save it into - * the cache after downloading if not). - *

- * The cached resource will be assimilated to the given original {@link URL} - * - * @param url - * the resource to open - * @param originalUrl - * the original {@link URL} before any redirection occurs, which - * is also used for the cache ID if needed (so we can retrieve - * the content with this URL if needed) - * @param support - * the support to use to download the resource - * @param stable - * TRUE for more stable resources, FALSE when they often change - * - * @return the opened resource, NOT NULL - * - * @throws IOException - * in case of I/O error - */ - public InputStream open(URL url, URL originalUrl, BasicSupport support, - boolean stable) throws IOException { - return open(url, originalUrl, support, stable, null, null, null); - } - - /** - * Open a resource (will load it from the cache if possible, or save it into - * the cache after downloading if not). - *

- * The cached resource will be assimilated to the given original {@link URL} - * - * @param url - * the resource to open - * @param originalUrl - * the original {@link URL} before any redirection occurs, which - * is also used for the cache ID if needed (so we can retrieve - * the content with this URL if needed) - * @param support - * the support to use to download the resource (can be NULL) - * @param stable - * TRUE for more stable resources, FALSE when they often change - * @param postParams - * the POST parameters - * @param getParams - * the GET parameters (priority over POST) - * @param oauth - * OAuth authorization (aka, "bearer XXXXXXX") - * - * @return the opened resource, NOT NULL - * - * @throws IOException - * in case of I/O error - */ - public InputStream open(URL url, URL originalUrl, BasicSupport support, - boolean stable, Map postParams, - Map getParams, String oauth) throws IOException { - - Map cookiesValues = null; - URL currentReferer = url; - - if (support != null) { - cookiesValues = support.getCookies(); - currentReferer = support.getCurrentReferer(); - // priority: arguments - if (oauth == null) { - oauth = support.getOAuth(); - } - } - - return downloader.open(url, originalUrl, currentReferer, cookiesValues, - postParams, getParams, oauth, stable); - } - - /** - * Open the given {@link URL} without using the cache, but still using and - * updating the cookies. - * - * @param url - * the {@link URL} to open - * @param support - * the {@link BasicSupport} used for the cookies - * @param postParams - * the POST parameters - * @param getParams - * the GET parameters (priority over POST) - * @param oauth - * OAuth authorization (aka, "bearer XXXXXXX") - * - * @return the {@link InputStream} of the opened page - * - * @throws IOException - * in case of I/O error - */ - public InputStream openNoCache(URL url, BasicSupport support, - Map postParams, Map getParams, - String oauth) throws IOException { - - Map cookiesValues = null; - URL currentReferer = url; - if (support != null) { - cookiesValues = support.getCookies(); - currentReferer = support.getCurrentReferer(); - // priority: arguments - if (oauth == null) { - oauth = support.getOAuth(); - } - } - - return downloaderNoCache.open(url, currentReferer, cookiesValues, - postParams, getParams, oauth); - } - - /** - * Refresh the resource into cache if needed. - * - * @param url - * the resource to open - * @param support - * the support to use to download the resource (can be NULL) - * @param stable - * TRUE for more stable resources, FALSE when they often change - * - * @throws IOException - * in case of I/O error - */ - public void refresh(URL url, BasicSupport support, boolean stable) - throws IOException { - if (!check(url, stable)) { - open(url, url, support, stable, null, null, null).close(); - } - } - - /** - * Check the resource to see if it is in the cache. - * - * @param url - * the resource to check - * @param stable - * a stable file (that dones't change too often) -- parameter - * used to check if the file is too old to keep or not - * - * @return TRUE if it is - * - */ - public boolean check(URL url, boolean stable) { - return downloader.getCache() != null - && downloader.getCache().check(url, false, stable); - } - - /** - * Save the given resource as an image on disk using the default image - * format for content or cover -- will automatically add the extension, too. - * - * @param img - * the resource - * @param target - * the target file without extension - * @param cover - * use the cover image format instead of the content image format - * - * @throws IOException - * in case of I/O error - */ - public void saveAsImage(Image img, File target, boolean cover) - throws IOException { - String format; - if (cover) { - format = Instance.getInstance().getConfig().getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase(); - } else { - format = Instance.getInstance().getConfig().getString(Config.FILE_FORMAT_IMAGE_FORMAT_CONTENT) - .toLowerCase(); - } - saveAsImage(img, new File(target.toString() + "." + format), format); - } - - /** - * Save the given resource as an image on disk using the given image format - * for content, or with "png" format if it fails. - * - * @param img - * the resource - * @param target - * the target file - * @param format - * the file format ("png", "jpeg", "bmp"...) - * - * @throws IOException - * in case of I/O error - */ - public void saveAsImage(Image img, File target, String format) - throws IOException { - ImageUtils.getInstance().saveAsImage(img, target, format); - } - - /** - * Manually add this item to the cache. - * - * @param in - * the input data - * @param uniqueID - * a unique ID for this resource - * - * - * @throws IOException - * in case of I/O error - */ - public void addToCache(InputStream in, String uniqueID) throws IOException { - cache.save(in, uniqueID); - } - - /** - * Return the {@link InputStream} corresponding to the given unique ID, or - * NULL if none found. - * - * @param uniqueID - * the unique ID - * - * @return the content or NULL - */ - public InputStream getFromCache(String uniqueID) { - return cache.load(uniqueID, true, true); - } - - /** - * Remove the given resource from the cache. - * - * @param uniqueID - * a unique ID used to locate the cached resource - * - * @return TRUE if it was removed - */ - public boolean removeFromCache(String uniqueID) { - return cache.remove(uniqueID); - } - - /** - * Clean the cache (delete the cached items). - * - * @param onlyOld - * only clean the files that are considered too old - * - * @return the number of cleaned items - */ - public int cleanCache(boolean onlyOld) { - return cache.clean(onlyOld); - } -} diff --git a/Downloader.java b/Downloader.java new file mode 100644 index 0000000..4191d0a --- /dev/null +++ b/Downloader.java @@ -0,0 +1,480 @@ +package be.nikiroo.utils; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.net.CookieHandler; +import java.net.CookieManager; +import java.net.CookiePolicy; +import java.net.CookieStore; +import java.net.HttpCookie; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; +import java.util.Map; +import java.util.zip.GZIPInputStream; + +/** + * This class will help you download content from Internet Sites ({@link URL} + * based). + *

+ * It allows you to control some options often required on web sites that do not + * want to simply serve HTML, but actively makes your life difficult with stupid + * checks. + * + * @author niki + */ +public class Downloader { + private String UA; + private CookieManager cookies; + private TraceHandler tracer = new TraceHandler(); + private Cache cache; + private boolean offline; + + /** + * Create a new {@link Downloader}. + * + * @param UA + * the User-Agent to use to download the resources -- note that + * some websites require one, some actively blacklist real UAs + * like the one from wget, some whitelist a couple of browsers + * only (!) -- can be NULL + */ + public Downloader(String UA) { + this(UA, null); + } + + /** + * Create a new {@link Downloader}. + * + * @param UA + * the User-Agent to use to download the resources -- note that + * some websites require one, some actively blacklist real UAs + * like the one from wget, some whitelist a couple of browsers + * only (!) -- can be NULL + * @param cache + * the {@link Cache} to use for all access (can be NULL) + */ + public Downloader(String UA, Cache cache) { + this.UA = UA; + + cookies = new CookieManager(null, CookiePolicy.ACCEPT_ALL); + CookieHandler.setDefault(cookies); + + setCache(cache); + } + + /** + * This {@link Downloader} is forbidden to try and connect to the network. + *

+ * If TRUE, it will only check the cache if any. + *

+ * Default is FALSE. + * + * @return TRUE if offline + */ + public boolean isOffline() { + return offline; + } + + /** + * This {@link Downloader} is forbidden to try and connect to the network. + *

+ * If TRUE, it will only check the cache if any. + *

+ * Default is FALSE. + * + * @param offline TRUE for offline, FALSE for online + */ + public void setOffline(boolean offline) { + this.offline = offline; + } + + /** + * The traces handler for this {@link Cache}. + * + * @return the traces handler + */ + public TraceHandler getTraceHandler() { + return tracer; + } + + /** + * The traces handler for this {@link Cache}. + * + * @param tracer + * the new traces handler + */ + public void setTraceHandler(TraceHandler tracer) { + if (tracer == null) { + tracer = new TraceHandler(false, false, false); + } + + this.tracer = tracer; + } + + /** + * The {@link Cache} to use for all access (can be NULL). + * + * @return the cache + */ + public Cache getCache() { + return cache; + } + + /** + * The {@link Cache} to use for all access (can be NULL). + * + * @param cache + * the new cache + */ + public void setCache(Cache cache) { + this.cache = cache; + } + + /** + * Clear all the cookies currently in the jar. + *

+ * As long as you don't, the cookies are kept. + */ + public void clearCookies() { + cookies.getCookieStore().removeAll(); + } + + /** + * Open the given {@link URL} and update the cookies. + * + * @param url + * the {@link URL} to open + * @return the {@link InputStream} of the opened page + * + * @throws IOException + * in case of I/O error + **/ + public InputStream open(URL url) throws IOException { + return open(url, false); + } + + /** + * Open the given {@link URL} and update the cookies. + * + * @param url + * the {@link URL} to open + * @param stable + * stable a stable file (that doesn't change too often) -- + * parameter used to check if the file is too old to keep or not + * in the cache (default is false) + * + * @return the {@link InputStream} of the opened page + * + * @throws IOException + * in case of I/O error + **/ + public InputStream open(URL url, boolean stable) throws IOException { + return open(url, url, url, null, null, null, null, stable); + } + + /** + * Open the given {@link URL} and update the cookies. + * + * @param url + * the {@link URL} to open + * @param currentReferer + * the current referer, for websites that needs this info + * @param cookiesValues + * the cookies + * @param postParams + * the POST parameters + * @param getParams + * the GET parameters (priority over POST) + * @param oauth + * OAuth authorization (aka, "bearer XXXXXXX") + * + * @return the {@link InputStream} of the opened page + * + * @throws IOException + * in case of I/O error (including offline mode + not in cache) + */ + public InputStream open(URL url, URL currentReferer, + Map cookiesValues, Map postParams, + Map getParams, String oauth) throws IOException { + return open(url, currentReferer, cookiesValues, postParams, getParams, + oauth, false); + } + + /** + * Open the given {@link URL} and update the cookies. + * + * @param url + * the {@link URL} to open + * @param currentReferer + * the current referer, for websites that needs this info + * @param cookiesValues + * the cookies + * @param postParams + * the POST parameters + * @param getParams + * the GET parameters (priority over POST) + * @param oauth + * OAuth authorization (aka, "bearer XXXXXXX") + * @param stable + * stable a stable file (that doesn't change too often) -- + * parameter used to check if the file is too old to keep or not + * in the cache (default is false) + * + * @return the {@link InputStream} of the opened page + * + * @throws IOException + * in case of I/O error (including offline mode + not in cache) + */ + public InputStream open(URL url, URL currentReferer, + Map cookiesValues, Map postParams, + Map getParams, String oauth, boolean stable) + throws IOException { + return open(url, url, currentReferer, cookiesValues, postParams, + getParams, oauth, stable); + } + + /** + * Open the given {@link URL} and update the cookies. + * + * @param url + * the {@link URL} to open + * @param originalUrl + * the original {@link URL} before any redirection occurs, which + * is also used for the cache ID if needed (so we can retrieve + * the content with this URL if needed) + * @param currentReferer + * the current referer, for websites that needs this info + * @param cookiesValues + * the cookies + * @param postParams + * the POST parameters + * @param getParams + * the GET parameters (priority over POST) + * @param oauth + * OAuth authorisation (aka, "bearer XXXXXXX") + * @param stable + * a stable file (that doesn't change too often) -- parameter + * used to check if the file is too old to keep or not in the + * cache + * + * @return the {@link InputStream} of the opened page + * + * @throws IOException + * in case of I/O error (including offline mode + not in cache) + */ + public InputStream open(URL url, final URL originalUrl, URL currentReferer, + Map cookiesValues, Map postParams, + Map getParams, String oauth, boolean stable) + throws IOException { + + tracer.trace("Request: " + url); + + if (cache != null) { + InputStream in = cache.load(originalUrl, false, stable); + if (in != null) { + tracer.trace("Use the cache: " + url); + tracer.trace("Original URL : " + originalUrl); + return in; + } + } + + String protocol = originalUrl == null ? null : originalUrl + .getProtocol(); + if (isOffline() && !"file".equalsIgnoreCase(protocol)) { + tracer.error("Downloader OFFLINE, cannot proceed to URL: " + url); + throw new IOException("Downloader is currently OFFLINE, cannot download: " + url); + } + + tracer.trace("Download: " + url); + + URLConnection conn = openConnectionWithCookies(url, currentReferer, + cookiesValues); + + // Priority: GET over POST + Map params = getParams; + if (getParams == null) { + params = postParams; + } + + StringBuilder requestData = null; + if ((params != null || oauth != null) + && conn instanceof HttpURLConnection) { + if (params != null) { + requestData = new StringBuilder(); + for (Map.Entry param : params.entrySet()) { + if (requestData.length() != 0) + requestData.append('&'); + requestData.append(URLEncoder.encode(param.getKey(), + "UTF-8")); + requestData.append('='); + requestData.append(URLEncoder.encode( + String.valueOf(param.getValue()), "UTF-8")); + } + + if (getParams == null && postParams != null) { + ((HttpURLConnection) conn).setRequestMethod("POST"); + } + + conn.setRequestProperty("Content-Type", + "application/x-www-form-urlencoded"); + conn.setRequestProperty("Content-Length", + Integer.toString(requestData.length())); + } + + if (oauth != null) { + conn.setRequestProperty("Authorization", oauth); + } + + if (requestData != null) { + conn.setDoOutput(true); + OutputStreamWriter writer = new OutputStreamWriter( + conn.getOutputStream()); + try { + writer.write(requestData.toString()); + writer.flush(); + } finally { + writer.close(); + } + } + } + + // Manual redirection, much better for POST data + if (conn instanceof HttpURLConnection) { + ((HttpURLConnection) conn).setInstanceFollowRedirects(false); + } + + conn.connect(); + + // Check if redirect + // BEWARE! POST data cannot be redirected (some webservers complain) for + // HTTP codes 302 and 303 + if (conn instanceof HttpURLConnection) { + int repCode = 0; + try { + // Can fail in some circumstances + repCode = ((HttpURLConnection) conn).getResponseCode(); + } catch (IOException e) { + } + + if (repCode / 100 == 3) { + String newUrl = conn.getHeaderField("Location"); + return open(new URL(newUrl), originalUrl, currentReferer, + cookiesValues, // + (repCode == 302 || repCode == 303) ? null : postParams, // + getParams, oauth, stable); + } + } + + try { + InputStream in = conn.getInputStream(); + if ("gzip".equals(conn.getContentEncoding())) { + in = new GZIPInputStream(in); + } + + if (in == null) { + throw new IOException("No InputStream!"); + } + + if (cache != null) { + String size = conn.getContentLength() < 0 ? "unknown size" + : StringUtils.formatNumber(conn.getContentLength()) + + "bytes"; + tracer.trace("Save to cache (" + size + "): " + originalUrl); + try { + try { + long bytes = cache.save(in, originalUrl); + tracer.trace("Saved to cache: " + + StringUtils.formatNumber(bytes) + "bytes"); + } finally { + in.close(); + } + in = cache.load(originalUrl, true, true); + } catch (IOException e) { + tracer.error(new IOException( + "Cannot save URL to cache, will ignore cache: " + + url, e)); + } + } + + if (in == null) { + throw new IOException( + "Cannot retrieve the file after storing it in the cache (??)"); + } + + return in; + } catch (IOException e) { + throw new IOException(String.format( + "Cannot find %s (current URL: %s)", originalUrl, url), e); + } + } + + /** + * Open a connection on the given {@link URL}, and manage the cookies that + * come with it. + * + * @param url + * the {@link URL} to open + * + * @return the connection + * + * @throws IOException + * in case of I/O error + */ + private URLConnection openConnectionWithCookies(URL url, + URL currentReferer, Map cookiesValues) + throws IOException { + URLConnection conn = url.openConnection(); + + String cookies = generateCookies(cookiesValues); + if (cookies != null && !cookies.isEmpty()) { + conn.setRequestProperty("Cookie", cookies); + } + + if (UA != null) { + conn.setRequestProperty("User-Agent", UA); + } + conn.setRequestProperty("Accept-Encoding", "gzip"); + conn.setRequestProperty("Accept", "*/*"); + conn.setRequestProperty("Charset", "utf-8"); + + if (currentReferer != null) { + conn.setRequestProperty("Referer", currentReferer.toString()); + conn.setRequestProperty("Host", currentReferer.getHost()); + } + + return conn; + } + + /** + * Generate the cookie {@link String} from the local {@link CookieStore} so + * it is ready to be passed. + * + * @return the cookie + */ + private String generateCookies(Map cookiesValues) { + StringBuilder builder = new StringBuilder(); + for (HttpCookie cookie : cookies.getCookieStore().getCookies()) { + if (builder.length() > 0) { + builder.append(';'); + } + + builder.append(cookie.toString()); + } + + if (cookiesValues != null) { + for (Map.Entry set : cookiesValues.entrySet()) { + if (builder.length() > 0) { + builder.append(';'); + } + builder.append(set.getKey()); + builder.append('='); + builder.append(set.getValue()); + } + } + + return builder.toString(); + } +} diff --git a/HashUtils.java b/HashUtils.java new file mode 100644 index 0000000..df8d7c6 --- /dev/null +++ b/HashUtils.java @@ -0,0 +1,89 @@ +package be.nikiroo.utils; + +import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * Small class to easily hash some values in a few different ways. + *

+ * Does not handle the salt itself, you have to add it yourself. + * + * @author niki + */ +public class HashUtils { + /** + * Hash the given value. + * + * @param value + * the value to hash + * + * @return the hash that can be used to confirm a value + * + * @throws RuntimeException + * if UTF-8 support is not available (!) or SHA-512 support is + * not available + * @throws NullPointerException + * if email or pass is NULL + */ + static public String sha512(String value) { + return hash("SHA-512", value); + } + + /** + * Hash the given value. + * + * @param value + * the value to hash + * + * @return the hash that can be used to confirm the a value + * + * @throws RuntimeException + * if UTF-8 support is not available (!) or MD5 support is not + * available + * @throws NullPointerException + * if email or pass is NULL + */ + static public String md5(String value) { + return hash("MD5", value); + } + + /** + * Hash the given value. + * + * @param algo + * the hash algorithm to use ("MD5" and "SHA-512" are supported) + * @param value + * the value to hash + * + * @return the hash that can be used to confirm a value + * + * @throws RuntimeException + * if UTF-8 support is not available (!) or the algorithm + * support is not available + * @throws NullPointerException + * if email or pass is NULL + */ + static private String hash(String algo, String value) { + try { + MessageDigest md = MessageDigest.getInstance(algo); + md.update(value.getBytes("UTF-8")); + byte byteData[] = md.digest(); + + StringBuffer hexString = new StringBuffer(); + for (int i = 0; i < byteData.length; i++) { + String hex = Integer.toHexString(0xff & byteData[i]); + if (hex.length() % 2 == 1) + hexString.append('0'); + hexString.append(hex); + } + + return hexString.toString(); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(algo + " hashing not available", e); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException( + "UTF-8 encoding is required in a compatible JVM", e); + } + } +} diff --git a/IOUtils.java b/IOUtils.java new file mode 100644 index 0000000..3d252ea --- /dev/null +++ b/IOUtils.java @@ -0,0 +1,493 @@ +package be.nikiroo.utils; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; + +import be.nikiroo.utils.streams.MarkableFileInputStream; + +/** + * This class offer some utilities based around Streams and Files. + * + * @author niki + */ +public class IOUtils { + /** + * Write the data to the given {@link File}. + * + * @param in + * the data source + * @param target + * the target {@link File} + * + * @return the number of bytes written + * + * @throws IOException + * in case of I/O error + */ + public static long write(InputStream in, File target) throws IOException { + OutputStream out = new FileOutputStream(target); + try { + return write(in, out); + } finally { + out.close(); + } + } + + /** + * Write the data to the given {@link OutputStream}. + * + * @param in + * the data source + * @param out + * the target {@link OutputStream} + * + * @return the number of bytes written + * + * @throws IOException + * in case of I/O error + */ + public static long write(InputStream in, OutputStream out) + throws IOException { + long written = 0; + byte buffer[] = new byte[4096]; + int len = in.read(buffer); + while (len > -1) { + out.write(buffer, 0, len); + written += len; + len = in.read(buffer); + } + + return written; + } + + /** + * Recursively Add a {@link File} (which can thus be a directory, too) to a + * {@link ZipOutputStream}. + * + * @param zip + * the stream + * @param base + * the path to prepend to the ZIP info before the actual + * {@link File} path + * @param target + * the source {@link File} (which can be a directory) + * @param targetIsRoot + * FALSE if we need to add a {@link ZipEntry} for base/target, + * TRUE to add it at the root of the ZIP + * + * @throws IOException + * in case of I/O error + */ + public static void zip(ZipOutputStream zip, String base, File target, + boolean targetIsRoot) throws IOException { + if (target.isDirectory()) { + if (!targetIsRoot) { + if (base == null || base.isEmpty()) { + base = target.getName(); + } else { + base += "/" + target.getName(); + } + zip.putNextEntry(new ZipEntry(base + "/")); + } + + File[] files = target.listFiles(); + if (files != null) { + for (File file : files) { + zip(zip, base, file, false); + } + } + } else { + if (base == null || base.isEmpty()) { + base = target.getName(); + } else { + base += "/" + target.getName(); + } + zip.putNextEntry(new ZipEntry(base)); + FileInputStream in = new FileInputStream(target); + try { + IOUtils.write(in, zip); + } finally { + in.close(); + } + } + } + + /** + * Zip the given source into dest. + * + * @param src + * the source {@link File} (which can be a directory) + * @param dest + * the destination .zip file + * @param srcIsRoot + * FALSE if we need to add a {@link ZipEntry} for src, TRUE to + * add it at the root of the ZIP + * + * @throws IOException + * in case of I/O error + */ + public static void zip(File src, File dest, boolean srcIsRoot) + throws IOException { + OutputStream out = new FileOutputStream(dest); + try { + ZipOutputStream zip = new ZipOutputStream(out); + try { + IOUtils.zip(zip, "", src, srcIsRoot); + } finally { + zip.close(); + } + } finally { + out.close(); + } + } + + /** + * Unzip the given ZIP file into the target directory. + * + * @param zipFile + * the ZIP file + * @param targetDirectory + * the target directory + * + * @return the number of extracted files (not directories) + * + * @throws IOException + * in case of I/O errors + */ + public static long unzip(File zipFile, File targetDirectory) + throws IOException { + long count = 0; + + if (targetDirectory.exists() && targetDirectory.isFile()) { + throw new IOException("Cannot unzip " + zipFile + " into " + + targetDirectory + ": it is not a directory"); + } + + targetDirectory.mkdir(); + if (!targetDirectory.exists()) { + throw new IOException("Cannot create target directory " + + targetDirectory); + } + + FileInputStream in = new FileInputStream(zipFile); + try { + ZipInputStream zipStream = new ZipInputStream(in); + try { + for (ZipEntry entry = zipStream.getNextEntry(); entry != null; entry = zipStream + .getNextEntry()) { + File file = new File(targetDirectory, entry.getName()); + if (entry.isDirectory()) { + file.mkdirs(); + } else { + IOUtils.write(zipStream, file); + count++; + } + } + } finally { + zipStream.close(); + } + } finally { + in.close(); + } + + return count; + } + + /** + * Write the {@link String} content to {@link File}. + * + * @param dir + * the directory where to write the {@link File} + * @param filename + * the {@link File} name + * @param content + * the content + * + * @throws IOException + * in case of I/O error + */ + public static void writeSmallFile(File dir, String filename, String content) + throws IOException { + if (!dir.exists()) { + dir.mkdirs(); + } + + writeSmallFile(new File(dir, filename), content); + } + + /** + * Write the {@link String} content to {@link File}. + * + * @param file + * the {@link File} to write + * @param content + * the content + * + * @throws IOException + * in case of I/O error + */ + public static void writeSmallFile(File file, String content) + throws IOException { + FileOutputStream out = new FileOutputStream(file); + try { + out.write(StringUtils.getBytes(content)); + } finally { + out.close(); + } + } + + /** + * Read the whole {@link File} content into a {@link String}. + * + * @param file + * the {@link File} + * + * @return the content + * + * @throws IOException + * in case of I/O error + */ + public static String readSmallFile(File file) throws IOException { + InputStream stream = new FileInputStream(file); + try { + return readSmallStream(stream); + } finally { + stream.close(); + } + } + + /** + * Read the whole {@link InputStream} content into a {@link String}. + * + * @param stream + * the {@link InputStream} + * + * @return the content + * + * @throws IOException + * in case of I/O error + */ + public static String readSmallStream(InputStream stream) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try { + write(stream, out); + return out.toString("UTF-8"); + } finally { + out.close(); + } + } + + /** + * Recursively delete the given {@link File}, which may of course also be a + * directory. + *

+ * Will either silently continue or throw an exception in case of error, + * depending upon the parameters. + * + * @param target + * the target to delete + * @param exception + * TRUE to throw an {@link IOException} in case of error, FALSE + * to silently continue + * + * @return TRUE if all files were deleted, FALSE if an error occurred + * + * @throws IOException + * if an error occurred and the parameters allow an exception to + * be thrown + */ + public static boolean deltree(File target, boolean exception) + throws IOException { + List list = deltree(target, null); + if (exception && !list.isEmpty()) { + StringBuilder slist = new StringBuilder(); + for (File file : list) { + slist.append("\n").append(file.getPath()); + } + + throw new IOException("Cannot delete all the files from: <" // + + target + ">:" + slist.toString()); + } + + return list.isEmpty(); + } + + /** + * Recursively delete the given {@link File}, which may of course also be a + * directory. + *

+ * Will silently continue in case of error. + * + * @param target + * the target to delete + * + * @return TRUE if all files were deleted, FALSE if an error occurred + */ + public static boolean deltree(File target) { + return deltree(target, null).isEmpty(); + } + + /** + * Recursively delete the given {@link File}, which may of course also be a + * directory. + *

+ * Will collect all {@link File} that cannot be deleted in the given + * accumulator. + * + * @param target + * the target to delete + * @param errorAcc + * the accumulator to use for errors, or NULL to create a new one + * + * @return the errors accumulator + */ + public static List deltree(File target, List errorAcc) { + if (errorAcc == null) { + errorAcc = new ArrayList(); + } + + File[] files = target.listFiles(); + if (files != null) { + for (File file : files) { + errorAcc = deltree(file, errorAcc); + } + } + + if (!target.delete()) { + errorAcc.add(target); + } + + return errorAcc; + } + + /** + * Open the resource next to the given {@link Class}. + * + * @param location + * the location where to look for the resource + * @param name + * the resource name (only the filename, no path) + * + * @return the opened resource if found, NULL if not + */ + public static InputStream openResource( + @SuppressWarnings("rawtypes") Class location, String name) { + String loc = location.getName().replace(".", "/") + .replaceAll("/[^/]*$", "/"); + return openResource(loc + name); + } + + /** + * Open the given /-separated resource (from the binary root). + * + * @param name + * the resource name (the full path, with "/" as separator) + * + * @return the opened resource if found, NULL if not + */ + public static InputStream openResource(String name) { + ClassLoader loader = IOUtils.class.getClassLoader(); + if (loader == null) { + loader = ClassLoader.getSystemClassLoader(); + } + + return loader.getResourceAsStream(name); + } + + /** + * Return a resetable {@link InputStream} from this stream, and reset it. + * + * @param in + * the input stream + * @return the resetable stream, which may be the same + * + * @throws IOException + * in case of I/O error + */ + public static InputStream forceResetableStream(InputStream in) + throws IOException { + boolean resetable = in.markSupported(); + if (resetable) { + try { + in.reset(); + } catch (IOException e) { + resetable = false; + } + } + + if (resetable) { + return in; + } + + final File tmp = File.createTempFile(".tmp-stream.", ".tmp"); + try { + write(in, tmp); + in.close(); + + return new MarkableFileInputStream(tmp) { + @Override + public void close() throws IOException { + try { + super.close(); + } finally { + tmp.delete(); + } + } + }; + } catch (IOException e) { + tmp.delete(); + throw e; + } + } + + /** + * Convert the {@link InputStream} into a byte array. + * + * @param in + * the input stream + * + * @return the array + * + * @throws IOException + * in case of I/O error + */ + public static byte[] toByteArray(InputStream in) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try { + write(in, out); + return out.toByteArray(); + } finally { + out.close(); + } + } + + /** + * Convert the {@link File} into a byte array. + * + * @param file + * the input {@link File} + * + * @return the array + * + * @throws IOException + * in case of I/O error + */ + public static byte[] toByteArray(File file) throws IOException { + FileInputStream fis = new FileInputStream(file); + try { + return toByteArray(fis); + } finally { + fis.close(); + } + } +} diff --git a/Image.java b/Image.java new file mode 100644 index 0000000..4518577 --- /dev/null +++ b/Image.java @@ -0,0 +1,281 @@ +package be.nikiroo.utils; + +import java.io.ByteArrayInputStream; +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.io.Serializable; + +import be.nikiroo.utils.streams.Base64InputStream; +import be.nikiroo.utils.streams.MarkableFileInputStream; + +/** + * This class represents an image data. + * + * @author niki + */ +public class Image implements Closeable, Serializable { + static private final long serialVersionUID = 1L; + + static private File tempRoot; + static private TempFiles tmpRepository; + static private long count = 0; + static private Object lock = new Object(); + + private Object instanceLock = new Object(); + private File data; + private long size; + + /** + * Do not use -- for serialisation purposes only. + */ + @SuppressWarnings("unused") + private Image() { + } + + /** + * Create a new {@link Image} with the given data. + * + * @param data + * the data + */ + public Image(byte[] data) { + ByteArrayInputStream in = new ByteArrayInputStream(data); + try { + this.data = getTemporaryFile(); + size = IOUtils.write(in, this.data); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + try { + in.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + /** + * Create an image from Base64 encoded data. + * + *

+ * Please use {@link Image#Image(InputStream)} when possible instead, with a + * {@link Base64InputStream}; it can be much more efficient. + * + * @param base64EncodedData + * the Base64 encoded data as a String + * + * @throws IOException + * in case of I/O error or badly formated Base64 + */ + public Image(String base64EncodedData) throws IOException { + this(new Base64InputStream(new ByteArrayInputStream( + StringUtils.getBytes(base64EncodedData)), false)); + } + + /** + * Create a new {@link Image} from a stream. + * + * @param in + * the stream + * + * @throws IOException + * in case of I/O error + */ + public Image(InputStream in) throws IOException { + data = getTemporaryFile(); + size = IOUtils.write(in, data); + } + + /** + * The size of the enclosed image in bytes. + * + * @return the size + */ + public long getSize() { + return size; + } + + /** + * Generate an {@link InputStream} that you can {@link InputStream#reset()} + * for this {@link Image}. + *

+ * This {@link InputStream} will (always) be a new one, and you are + * responsible for it. + *

+ * Note: take care that the {@link InputStream} must not live past + * the {@link Image} life time! + * + * @return the stream + * + * @throws IOException + * in case of I/O error + */ + public InputStream newInputStream() throws IOException { + return new MarkableFileInputStream(data); + } + + /** + * Read the actual image data, as a byte array. + * + * @deprecated if possible, prefer the {@link Image#newInputStream()} + * method, as it can be more efficient + * + * @return the image data + */ + @Deprecated + public byte[] getData() { + try { + InputStream in = newInputStream(); + try { + return IOUtils.toByteArray(in); + } finally { + in.close(); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** + * Convert the given {@link Image} object into a Base64 representation of + * the same {@link Image} object. + * + * @deprecated Please use {@link Image#newInputStream()} instead, it is more + * efficient + * + * @return the Base64 representation + */ + @Deprecated + public String toBase64() { + try { + Base64InputStream stream = new Base64InputStream(newInputStream(), + true); + try { + return IOUtils.readSmallStream(stream); + } finally { + stream.close(); + } + } catch (IOException e) { + return null; + } + } + + /** + * Closing the {@link Image} will delete the associated temporary file on + * disk. + *

+ * Note that even if you don't, the program will still try to delete + * all the temporary files at JVM termination. + */ + @Override + public void close() throws IOException { + synchronized (instanceLock) { + if (size >= 0) { + size = -1; + data.delete(); + data = null; + + synchronized (lock) { + count--; + if (count <= 0) { + count = 0; + tmpRepository.close(); + tmpRepository = null; + } + } + } + } + } + + @Override + protected void finalize() throws Throwable { + try { + close(); + } finally { + super.finalize(); + } + } + + /** + * Return a newly created temporary file to work on. + * + * @return the file + * + * @throws IOException + * in case of I/O error + */ + private File getTemporaryFile() throws IOException { + synchronized (lock) { + if (tmpRepository == null) { + tmpRepository = new TempFiles(tempRoot, "images"); + count = 0; + } + + count++; + + return tmpRepository.createTempFile("image"); + } + } + + /** + * Write this {@link Image} for serialization purposes; that is, write the + * content of the backing temporary file. + * + * @param out + * the {@link OutputStream} to write to + * + * @throws IOException + * in case of I/O error + */ + private void writeObject(ObjectOutputStream out) throws IOException { + InputStream in = newInputStream(); + try { + IOUtils.write(in, out); + } finally { + in.close(); + } + } + + /** + * Read an {@link Image} written by + * {@link Image#writeObject(java.io.ObjectOutputStream)}; that is, create a + * new temporary file with the saved content. + * + * @param in + * the {@link InputStream} to read from + * @throws IOException + * in case of I/O error + * @throws ClassNotFoundException + * will not be thrown by this method + */ + @SuppressWarnings("unused") + private void readObject(ObjectInputStream in) throws IOException, + ClassNotFoundException { + data = getTemporaryFile(); + IOUtils.write(in, data); + } + + /** + * Change the temporary root directory used by the program. + *

+ * Caution: the directory will be owned by the system, all its files + * now belong to us (and will most probably be deleted). + *

+ * Note: it may take some time until the new temporary root is used, we + * first need to make sure the previous one is not used anymore (i.e., we + * must reach a point where no unclosed {@link Image} remains in memory) to + * switch the temporary root. + * + * @param root + * the new temporary root, which will be owned by the + * system + */ + public static void setTemporaryFilesRoot(File root) { + tempRoot = root; + } +} diff --git a/ImageUtils.java b/ImageUtils.java new file mode 100644 index 0000000..877c8fa --- /dev/null +++ b/ImageUtils.java @@ -0,0 +1,266 @@ +package be.nikiroo.utils; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +import be.nikiroo.utils.serial.SerialUtils; + +/** + * This class offer some utilities based around images. + * + * @author niki + */ +public abstract class ImageUtils { + private static ImageUtils instance = newObject(); + + /** + * Get a (unique) instance of an {@link ImageUtils} compatible with your + * system. + * + * @return an {@link ImageUtils} + */ + public static ImageUtils getInstance() { + return instance; + } + + /** + * Save the given resource as an image on disk using the given image format + * for content, or with "png" format if it fails. + * + * @param img + * the resource + * @param target + * the target file + * @param format + * the file format ("png", "jpeg", "bmp"...) + * + * @throws IOException + * in case of I/O error + */ + public abstract void saveAsImage(Image img, File target, String format) + throws IOException; + + /** + * Scale a dimension. + * + * + * @param imageWidth + * the actual image width + * @param imageHeight + * the actual image height + * @param areaWidth + * the base width of the target dimension for snap sizes + * @param areaHeight + * the base height of the target dimension for snap sizes + * @param zoom + * the zoom factor (ignored on snap mode) + * @param snapMode + * NULL for no snap mode, TRUE to snap to width and FALSE for + * snap to height) + * + * @return the scaled size, width is [0] and height is [1] (minimum is 1x1) + */ + protected static Integer[] scaleSize(int imageWidth, int imageHeight, + int areaWidth, int areaHeight, double zoom, Boolean snapMode) { + int width; + int height; + if (snapMode == null) { + width = (int) Math.round(imageWidth * zoom); + height = (int) Math.round(imageHeight * zoom); + } else if (snapMode) { + width = areaWidth; + height = (int) Math + .round((((double) areaWidth) / imageWidth) * imageHeight); + } else { + height = areaHeight; + width = (int) Math + .round((((double) areaHeight) / imageHeight) * imageWidth); + + } + + if (width < 1) + width = 1; + if (height < 1) + height = 1; + + return new Integer[] { width, height }; + } + + /** + * Return the EXIF transformation flag of this image if any. + * + *

+ * Note: this code has been found on internet; thank you anonymous coder. + *

+ * + * @param in + * the data {@link InputStream} + * + * @return the transformation flag if any + * + * @throws IOException + * in case of IO error + */ + protected static int getExifTransorm(InputStream in) throws IOException { + int[] exif_data = new int[100]; + int set_flag = 0; + int is_motorola = 0; + + /* Read File head, check for JPEG SOI + Exif APP1 */ + for (int i = 0; i < 4; i++) + exif_data[i] = in.read(); + + if (exif_data[0] != 0xFF || exif_data[1] != 0xD8 + || exif_data[2] != 0xFF || exif_data[3] != 0xE1) + return -2; + + /* Get the marker parameter length count */ + int length = (in.read() << 8 | in.read()); + + /* Length includes itself, so must be at least 2 */ + /* Following Exif data length must be at least 6 */ + if (length < 8) + return -1; + length -= 8; + /* Read Exif head, check for "Exif" */ + for (int i = 0; i < 6; i++) + exif_data[i] = in.read(); + + if (exif_data[0] != 0x45 || exif_data[1] != 0x78 + || exif_data[2] != 0x69 || exif_data[3] != 0x66 + || exif_data[4] != 0 || exif_data[5] != 0) + return -1; + + /* Read Exif body */ + length = length > exif_data.length ? exif_data.length : length; + for (int i = 0; i < length; i++) + exif_data[i] = in.read(); + + if (length < 12) + return -1; /* Length of an IFD entry */ + + /* Discover byte order */ + if (exif_data[0] == 0x49 && exif_data[1] == 0x49) + is_motorola = 0; + else if (exif_data[0] == 0x4D && exif_data[1] == 0x4D) + is_motorola = 1; + else + return -1; + + /* Check Tag Mark */ + if (is_motorola == 1) { + if (exif_data[2] != 0) + return -1; + if (exif_data[3] != 0x2A) + return -1; + } else { + if (exif_data[3] != 0) + return -1; + if (exif_data[2] != 0x2A) + return -1; + } + + /* Get first IFD offset (offset to IFD0) */ + int offset; + if (is_motorola == 1) { + if (exif_data[4] != 0) + return -1; + if (exif_data[5] != 0) + return -1; + offset = exif_data[6]; + offset <<= 8; + offset += exif_data[7]; + } else { + if (exif_data[7] != 0) + return -1; + if (exif_data[6] != 0) + return -1; + offset = exif_data[5]; + offset <<= 8; + offset += exif_data[4]; + } + if (offset > length - 2) + return -1; /* check end of data segment */ + + /* Get the number of directory entries contained in this IFD */ + int number_of_tags; + if (is_motorola == 1) { + number_of_tags = exif_data[offset]; + number_of_tags <<= 8; + number_of_tags += exif_data[offset + 1]; + } else { + number_of_tags = exif_data[offset + 1]; + number_of_tags <<= 8; + number_of_tags += exif_data[offset]; + } + if (number_of_tags == 0) + return -1; + offset += 2; + + /* Search for Orientation Tag in IFD0 */ + for (;;) { + if (offset > length - 12) + return -1; /* check end of data segment */ + /* Get Tag number */ + int tagnum; + if (is_motorola == 1) { + tagnum = exif_data[offset]; + tagnum <<= 8; + tagnum += exif_data[offset + 1]; + } else { + tagnum = exif_data[offset + 1]; + tagnum <<= 8; + tagnum += exif_data[offset]; + } + if (tagnum == 0x0112) + break; /* found Orientation Tag */ + if (--number_of_tags == 0) + return -1; + offset += 12; + } + + /* Get the Orientation value */ + if (is_motorola == 1) { + if (exif_data[offset + 8] != 0) + return -1; + set_flag = exif_data[offset + 9]; + } else { + if (exif_data[offset + 9] != 0) + return -1; + set_flag = exif_data[offset + 8]; + } + if (set_flag > 8) + return -1; + + return set_flag; + } + + /** + * Check that the class can operate (for instance, that all the required + * libraries or frameworks are present). + * + * @return TRUE if it works + */ + abstract protected boolean check(); + + /** + * Create a new {@link ImageUtils}. + * + * @return the {@link ImageUtils} + */ + private static ImageUtils newObject() { + for (String clazz : new String[] { "be.nikiroo.utils.ui.ImageUtilsAwt", + "be.nikiroo.utils.android.ImageUtilsAndroid" }) { + try { + ImageUtils obj = (ImageUtils) SerialUtils.createObject(clazz); + if (obj.check()) { + return obj; + } + } catch (Throwable e) { + } + } + + return null; + } +} diff --git a/Instance.java b/Instance.java deleted file mode 100644 index e0a0727..0000000 --- a/Instance.java +++ /dev/null @@ -1,704 +0,0 @@ -package be.nikiroo.fanfix; - -import java.io.File; -import java.io.IOException; -import java.util.Date; - -import be.nikiroo.fanfix.bundles.Config; -import be.nikiroo.fanfix.bundles.ConfigBundle; -import be.nikiroo.fanfix.bundles.StringId; -import be.nikiroo.fanfix.bundles.StringIdBundle; -import be.nikiroo.fanfix.bundles.StringIdGuiBundle; -import be.nikiroo.fanfix.bundles.UiConfig; -import be.nikiroo.fanfix.bundles.UiConfigBundle; -import be.nikiroo.fanfix.library.BasicLibrary; -import be.nikiroo.fanfix.library.CacheLibrary; -import be.nikiroo.fanfix.library.LocalLibrary; -import be.nikiroo.fanfix.library.RemoteLibrary; -import be.nikiroo.fanfix.library.WebLibrary; -import be.nikiroo.utils.Cache; -import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.Image; -import be.nikiroo.utils.Proxy; -import be.nikiroo.utils.TempFiles; -import be.nikiroo.utils.TraceHandler; -import be.nikiroo.utils.resources.Bundles; - -/** - * Global state for the program (services and singletons). - * - * @author niki - */ -public class Instance { - static private Instance instance; - static private Object instancelock = new Object(); - - private ConfigBundle config; - private UiConfigBundle uiconfig; - private StringIdBundle trans; - private DataLoader cache; - private StringIdGuiBundle transGui; - private BasicLibrary lib; - private File coverDir; - private File readerTmp; - private File remoteDir; - private String configDir; - private TraceHandler tracer; - private TempFiles tempFiles; - - /** - * Initialise the instance -- if already initialised, nothing will happen. - *

- * Before calling this method, you may call - * {@link Bundles#setDirectory(String)} if wanted. - *

- * Note that this method will honour some environment variables, the 3 most - * important ones probably being: - *

- */ - static public void init() { - init(false); - } - - /** - * Initialise the instance -- if already initialised, nothing will happen - * unless you pass TRUE to force. - *

- * Before calling this method, you may call - * {@link Bundles#setDirectory(String)} if wanted. - *

- * Note: forcing the initialisation can be dangerous, so make sure to only - * make it under controlled circumstances -- for instance, at the start of - * the program, you could call {@link Instance#init()}, change some settings - * because you want to force those settings (it will also forbid users to - * change them!) and then call {@link Instance#init(boolean)} with - * force set to TRUE. - * - * @param force - * force the initialisation even if already initialised - */ - static public void init(boolean force) { - synchronized (instancelock) { - if (instance == null || force) { - instance = new Instance(); - } - } - - } - - /** - * Force-initialise the {@link Instance} to a known value. - *

- * Usually for DEBUG/Test purposes. - * - * @param instance - * the actual Instance to use - */ - static public void init(Instance instance) { - Instance.instance = instance; - } - - /** - * The (mostly unique) instance of this {@link Instance}. - * - * @return the (mostly unique) instance - */ - public static Instance getInstance() { - return instance; - } - - /** - * Actually initialise the instance. - *

- * Before calling this method, you may call - * {@link Bundles#setDirectory(String)} if wanted. - */ - protected Instance() { - // Before we can configure it: - Boolean debug = checkEnv("DEBUG"); - boolean trace = debug != null && debug; - tracer = new TraceHandler(true, trace, trace); - - // config dir: - configDir = getConfigDir(); - if (!new File(configDir).exists()) { - new File(configDir).mkdirs(); - } - - // Most of the rest is dependent upon this: - createConfigs(configDir, false); - - // Proxy support - Proxy.use(config.getString(Config.NETWORK_PROXY)); - - // update tracer: - if (debug == null) { - debug = config.getBoolean(Config.DEBUG_ERR, false); - trace = config.getBoolean(Config.DEBUG_TRACE, false); - } - - tracer = new TraceHandler(true, debug, trace); - - // default Library - remoteDir = new File(configDir, "remote"); - lib = createDefaultLibrary(remoteDir); - - // create cache and TMP - File tmp = getFile(Config.CACHE_DIR, configDir, "tmp"); - Image.setTemporaryFilesRoot(new File(tmp.getParent(), "tmp.images")); - - String ua = config.getString(Config.NETWORK_USER_AGENT, ""); - try { - int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, 0); - int hoursLarge = config.getInteger(Config.CACHE_MAX_TIME_STABLE, 0); - cache = new DataLoader(tmp, ua, hours, hoursLarge); - } catch (IOException e) { - tracer.error(new IOException( - "Cannot create cache (will continue without cache)", e)); - cache = new DataLoader(ua); - } - - cache.setTraceHandler(tracer); - - // readerTmp / coverDir - readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER, configDir, - "tmp-reader"); - coverDir = getFile(Config.DEFAULT_COVERS_DIR, configDir, "covers"); - coverDir.mkdirs(); - - try { - tempFiles = new TempFiles("fanfix"); - } catch (IOException e) { - tracer.error( - new IOException("Cannot create temporary directory", e)); - } - } - - /** - * The traces handler for this {@link Cache}. - *

- * It is never NULL. - * - * @return the traces handler (never NULL) - */ - public TraceHandler getTraceHandler() { - return tracer; - } - - /** - * The traces handler for this {@link Cache}. - * - * @param tracer - * the new traces handler or NULL - */ - public void setTraceHandler(TraceHandler tracer) { - if (tracer == null) { - tracer = new TraceHandler(false, false, false); - } - - this.tracer = tracer; - cache.setTraceHandler(tracer); - } - - /** - * Get the (unique) configuration service for the program. - * - * @return the configuration service - */ - public ConfigBundle getConfig() { - return config; - } - - /** - * Get the (unique) UI configuration service for the program. - * - * @return the configuration service - */ - public UiConfigBundle getUiConfig() { - return uiconfig; - } - - /** - * Reset the configuration. - * - * @param resetTrans - * also reset the translation files - */ - public void resetConfig(boolean resetTrans) { - String dir = Bundles.getDirectory(); - Bundles.setDirectory(null); - try { - try { - ConfigBundle config = new ConfigBundle(); - config.updateFile(configDir); - } catch (IOException e) { - tracer.error(e); - } - try { - UiConfigBundle uiconfig = new UiConfigBundle(); - uiconfig.updateFile(configDir); - } catch (IOException e) { - tracer.error(e); - } - - if (resetTrans) { - try { - StringIdBundle trans = new StringIdBundle(null); - trans.updateFile(configDir); - } catch (IOException e) { - tracer.error(e); - } - } - } finally { - Bundles.setDirectory(dir); - } - } - - /** - * Get the (unique) {@link DataLoader} for the program. - * - * @return the {@link DataLoader} - */ - public DataLoader getCache() { - return cache; - } - - /** - * Get the (unique) {link StringIdBundle} for the program. - *

- * This is used for the translations of the core parts of Fanfix. - * - * @return the {link StringIdBundle} - */ - public StringIdBundle getTrans() { - return trans; - } - - /** - * Get the (unique) {link StringIdGuiBundle} for the program. - *

- * This is used for the translations of the GUI parts of Fanfix. - * - * @return the {link StringIdGuiBundle} - */ - public StringIdGuiBundle getTransGui() { - return transGui; - } - - /** - * Get the (unique) {@link BasicLibrary} for the program. - * - * @return the {@link BasicLibrary} - */ - public BasicLibrary getLibrary() { - if (lib == null) { - throw new NullPointerException("We don't have a library to return"); - } - - return lib; - } - - /** - * Change the default {@link BasicLibrary} for this program. - *

- * Be careful. - * - * @param lib - * the new {@link BasicLibrary} - */ - public void setLibrary(BasicLibrary lib) { - this.lib = lib; - } - - /** - * Return the directory where to look for default cover pages. - * - * @return the default covers directory - */ - public File getCoverDir() { - return coverDir; - } - - /** - * Return the directory where to store temporary files for the local reader. - * - * @return the directory - */ - public File getReaderDir() { - return readerTmp; - } - - /** - * Return the directory where to store temporary files for the remote - * {@link LocalLibrary}. - * - * @param host - * the remote for this host - * - * @return the directory - */ - public File getRemoteDir(String host) { - return getRemoteDir(remoteDir, host); - } - - /** - * Return the directory where to store temporary files for the remote - * {@link LocalLibrary}. - * - * @param remoteDir - * the base remote directory - * @param host - * the remote for this host - * - * @return the directory - */ - private File getRemoteDir(File remoteDir, String host) { - remoteDir.mkdirs(); - - if (host != null) { - host = host.replace("fanfix://", ""); - host = host.replace("http://", ""); - host = host.replace("https://", ""); - host = host.replaceAll("[^a-zA-Z0-9=+.-]", "_"); - - return new File(remoteDir, host); - } - - return remoteDir; - } - - /** - * Check if we need to check that a new version of Fanfix is available. - * - * @return TRUE if we need to - */ - public boolean isVersionCheckNeeded() { - try { - long wait = config.getInteger(Config.NETWORK_UPDATE_INTERVAL, 0) - * 24 * 60 * 60 * 1000; - if (wait >= 0) { - String lastUpString = IOUtils - .readSmallFile(new File(configDir, "LAST_UPDATE")); - long delay = new Date().getTime() - - Long.parseLong(lastUpString); - if (delay > wait) { - return true; - } - } else { - return false; - } - } catch (Exception e) { - // No file or bad file: - return true; - } - - return false; - } - - /** - * Notify that we checked for a new version of Fanfix. - */ - public void setVersionChecked() { - try { - IOUtils.writeSmallFile(new File(configDir), "LAST_UPDATE", - Long.toString(new Date().getTime())); - } catch (IOException e) { - tracer.error(e); - } - } - - /** - * The facility to use temporary files in this program. - *

- * MUST be closed at end of program. - * - * @return the facility - */ - public TempFiles getTempFiles() { - return tempFiles; - } - - /** - * The configuration directory (will check, in order of preference, the - * system properties, the environment and then defaults to - * {@link Instance#getHome()}/.fanfix). - * - * @return the config directory - */ - private String getConfigDir() { - String configDir = System.getProperty("CONFIG_DIR"); - - if (configDir == null) { - configDir = System.getenv("CONFIG_DIR"); - } - - if (configDir == null) { - configDir = new File(getHome(), ".fanfix").getPath(); - } - - return configDir; - } - - /** - * Create the config variables ({@link Instance#config}, - * {@link Instance#uiconfig}, {@link Instance#trans} and - * {@link Instance#transGui}). - * - * @param configDir - * the directory where to find the configuration files - * @param refresh - * TRUE to reset the configuration files from the default - * included ones - */ - private void createConfigs(String configDir, boolean refresh) { - if (!refresh) { - Bundles.setDirectory(configDir); - } - - try { - config = new ConfigBundle(); - config.updateFile(configDir); - } catch (IOException e) { - tracer.error(e); - } - - try { - uiconfig = new UiConfigBundle(); - uiconfig.updateFile(configDir); - } catch (IOException e) { - tracer.error(e); - } - - // No updateFile for this one! (we do not want the user to have custom - // translations that won't accept updates from newer versions) - trans = new StringIdBundle(getLang()); - transGui = new StringIdGuiBundle(getLang()); - - // Fix an old bug (we used to store custom translation files by - // default): - if (trans.getString(StringId.INPUT_DESC_CBZ) == null) { - trans.deleteFile(configDir); - } - - Boolean noutf = checkEnv("NOUTF"); - if (noutf != null && noutf) { - trans.setUnicode(false); - transGui.setUnicode(false); - } - - Bundles.setDirectory(configDir); - } - - /** - * Create the default library as specified by the config. - * - * @param remoteDir - * the base remote directory if needed - * - * @return the default {@link BasicLibrary} - */ - private BasicLibrary createDefaultLibrary(File remoteDir) { - BasicLibrary lib = null; - - boolean useRemote = config.getBoolean(Config.REMOTE_LIBRARY_ENABLED, - false); - if (useRemote) { - String host = null; - int port = -1; - try { - host = config.getString(Config.REMOTE_LIBRARY_HOST, - "fanfix://localhost"); - port = config.getInteger(Config.REMOTE_LIBRARY_PORT, -1); - String key = config.getString(Config.REMOTE_LIBRARY_KEY); - - if (!host.startsWith("http://") && !host.startsWith("https://") - && !host.startsWith("fanfix://")) { - host = "fanfix://" + host; - } - - tracer.trace("Selecting remote library " + host + ":" + port); - - if (host.startsWith("fanfix://")) { - lib = new RemoteLibrary(key, host, port); - } else { - lib = new WebLibrary(key, host, port); - } - - lib = new CacheLibrary(getRemoteDir(remoteDir, host), lib, - uiconfig); - } catch (Exception e) { - tracer.error( - new IOException("Cannot create remote library for: " - + host + ":" + port, e)); - } - } else { - String libDir = System.getenv("BOOKS_DIR"); - if (libDir == null || libDir.isEmpty()) { - libDir = getFile(Config.LIBRARY_DIR, configDir, "$HOME/Books") - .getPath(); - } - try { - lib = new LocalLibrary(new File(libDir), config); - } catch (Exception e) { - tracer.error(new IOException( - "Cannot create library for directory: " + libDir, e)); - } - } - - return lib; - } - - /** - * Return a path, but support the special $HOME variable. - * - * @param id - * the key for the path, which may contain "$HOME" - * @param configDir - * the directory to use as base if not absolute - * @param def - * the default value if none (will be configDir-rooted if needed) - * @return the path, with expanded "$HOME" if needed - */ - protected File getFile(Config id, String configDir, String def) { - String path = config.getString(id, def); - return getFile(path, configDir); - } - - /** - * Return a path, but support the special $HOME variable. - * - * @param id - * the key for the path, which may contain "$HOME" - * @param configDir - * the directory to use as base if not absolute - * @param def - * the default value if none (will be configDir-rooted if needed) - * @return the path, with expanded "$HOME" if needed - */ - protected File getFile(UiConfig id, String configDir, String def) { - String path = uiconfig.getString(id, def); - return getFile(path, configDir); - } - - /** - * Return a path, but support the special $HOME variable. - * - * @param path - * the path, which may contain "$HOME" - * @param configDir - * the directory to use as base if not absolute - * @return the path, with expanded "$HOME" if needed - */ - protected File getFile(String path, String configDir) { - File file = null; - if (path != null && !path.isEmpty()) { - path = path.replace('/', File.separatorChar); - if (path.contains("$HOME")) { - path = path.replace("$HOME", getHome()); - } else if (!path.startsWith("/")) { - path = new File(configDir, path).getPath(); - } - - file = new File(path); - } - - return file; - } - - /** - * Return the home directory from the environment (FANFIX_DIR) or the system - * properties. - *

- * The environment variable is tested first. Then, the custom property - * "fanfix.home" is tried, followed by the usual "user.home" then - * "java.io.tmp" if nothing else is found. - * - * @return the home - */ - protected String getHome() { - String home = System.getenv("FANFIX_DIR"); - if (home != null && new File(home).isFile()) { - home = null; - } - - if (home == null || home.trim().isEmpty()) { - home = System.getProperty("fanfix.home"); - if (home != null && new File(home).isFile()) { - home = null; - } - } - - if (home == null || home.trim().isEmpty()) { - home = System.getProperty("user.home"); - if (!new File(home).isDirectory()) { - home = null; - } - } - - if (home == null || home.trim().isEmpty()) { - home = System.getProperty("java.io.tmpdir"); - if (!new File(home).isDirectory()) { - home = null; - } - } - - if (home == null) { - home = ""; - } - - return home; - } - - /** - * The language to use for the application (NULL = default system language). - * - * @return the language - */ - protected String getLang() { - String lang = config.getString(Config.LANG); - - if (lang == null || lang.isEmpty()) { - if (System.getenv("LANG") != null - && !System.getenv("LANG").isEmpty()) { - lang = System.getenv("LANG"); - } - } - - if (lang != null && lang.isEmpty()) { - lang = null; - } - - return lang; - } - - /** - * Check that the given environment variable is "enabled". - * - * @param key - * the variable to check - * - * @return TRUE if it is - */ - protected Boolean checkEnv(String key) { - String value = System.getenv(key); - if (value != null) { - value = value.trim().toLowerCase(); - if ("yes".equals(value) || "true".equals(value) - || "on".equals(value) || "1".equals(value) - || "y".equals(value)) { - return true; - } - - return false; - } - - return null; - } -} diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 9cecc1d..0000000 --- a/LICENSE +++ /dev/null @@ -1,674 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU General Public License is a free, copyleft license for -software and other kinds of works. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - To protect your rights, we need to prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must pass on to the recipients the same -freedoms that you received. You must make sure that they, too, receive -or can get the source code. And you must show them these terms so they -know their rights. - - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. - - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. - - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Use with the GNU Affero General Public License. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - {one line to give the program's name and a brief idea of what it does.} - Copyright (C) {year} {name of author} - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - {project} Copyright (C) {year} {fullname} - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. diff --git a/LoginResult.java b/LoginResult.java new file mode 100644 index 0000000..ddc148b --- /dev/null +++ b/LoginResult.java @@ -0,0 +1,211 @@ +package be.nikiroo.utils; + +import java.util.ArrayList; +import java.util.List; + +/** + * A simple login facility using cookies. + * + * @author niki + */ +public class LoginResult { + private boolean success; + private String cookie; + private boolean badLogin; + private boolean badCookie; + private String option; + + /** + * Generate a failed login. + * + * @param badLogin + * TRUE if the login failed because of a who/key/subkey error + * @param badCookie + * TRUE if the login failed because of a bad cookie + * + */ + public LoginResult(boolean badLogin, boolean badCookie) { + this.badLogin = badLogin; + this.badCookie = badCookie; + } + + /** + * Generate a successful login for the given user. + * + * @param who + * the user (can be NULL) + * @param key + * the password (can be NULL) + * @param subkey + * a sub-password (can be NULL) + * @param option + * an option assigned to this login (can be NULL) + */ + public LoginResult(String who, String key, String subkey, String option) { + this.option = option; + this.cookie = generateCookie(who, key, subkey, option); + this.success = true; + } + + /** + * Generate a login via this token and checks its validity. + *

+ * Will fail with a NULL token, but + * {@link LoginResult#isBadCookie()} will still be false. + * + * @param cookie + * the token to check (if NULL, will simply fail but + * {@link LoginResult#isBadCookie()} will still be false) + * @param who + * the user (can be NULL) + * @param key + * the password (can be NULL) + */ + public LoginResult(String cookie, String who, String key) { + this(cookie, who, key, null, true); + } + + /** + * Generate a login via this token and checks its validity. + *

+ * Will fail with a NULL token, but + * {@link LoginResult#isBadCookie()} will still be false. + * + * @param cookie + * the token to check (if NULL, will simply fail but + * {@link LoginResult#isBadCookie()} will still be false) + * @param who + * the user (can be NULL) + * @param key + * the password (can be NULL) + * @param subkeys + * the list of candidate subkey (can be NULL) + * @param allowNoSubkey + * allow the login if no subkey was present in the token + */ + public LoginResult(String cookie, String who, String key, + List subkeys, boolean allowNoSubkey) { + if (cookie != null) { + String hashes[] = cookie.split("~"); + if (hashes.length >= 2) { + String wookie = hashes[0]; + String rehashed = hashes[1]; + String opts = hashes.length > 2 ? hashes[2] : ""; + + if (CookieUtils.validateCookie(who + key, wookie)) { + if (subkeys == null) { + subkeys = new ArrayList(); + } + + if (allowNoSubkey) { + subkeys = new ArrayList(subkeys); + subkeys.add(""); + } + + for (String subkey : subkeys) { + if (CookieUtils.validateCookie(wookie + subkey + opts, + rehashed)) { + this.cookie = generateCookie(who, key, subkey, + opts); + this.option = opts; + this.success = true; + } + } + } + } + + this.badCookie = !success; + } + + // No token -> no bad token + } + + /** + * The login wa successful. + * + * @return TRUE if it is + */ + public boolean isSuccess() { + return success; + } + + /** + * The refreshed token if the login is successful (NULL if not). + * + * @return the token, or NULL + */ + public String getCookie() { + return cookie; + } + + /** + * An option that was used to generate this login (always NULL if the login + * was not successful). + *

+ * It can come from a manually generated {@link LoginResult}, but also from + * a {@link LoginResult} generated with a token. + * + * @return the option + */ + public String getOption() { + return option; + } + + /** + * The login failed because of a who/key/subkey error. + * + * @return TRUE if it failed because of a who/key/subkey error + */ + public boolean isBadLogin() { + return badLogin; + } + + /** + * The login failed because the cookie was not accepted + * + * @return TRUE if it failed because the cookie was not accepted + */ + public boolean isBadCookie() { + return badCookie; + } + + @Override + public String toString() { + if (success) + return "Login succeeded"; + + if (badLogin && badCookie) + return "Login failed because of bad login and bad cookie"; + + if (badLogin) + return "Login failed because of bad login"; + + if (badCookie) + return "Login failed because of bad cookie"; + + return "Login failed without giving a reason"; + } + + /** + * Generate a cookie. + * + * @param who + * the user name (can be NULL) + * @param key + * the password (can be NULL) + * @param subkey + * a subkey (can be NULL) + * @param option + * an option linked to the login (can be NULL) + * + * @return a fresh cookie + */ + private String generateCookie(String who, String key, String subkey, + String option) { + String wookie = CookieUtils.generateCookie(who + key, 0); + return wookie + "~" + + CookieUtils.generateCookie( + wookie + (subkey == null ? "" : subkey) + option, 0) + + "~" + option; + } +} \ No newline at end of file diff --git a/Main.java b/Main.java deleted file mode 100644 index 3536544..0000000 --- a/Main.java +++ /dev/null @@ -1,1141 +0,0 @@ -package be.nikiroo.fanfix; - -import java.io.File; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; - -import javax.net.ssl.SSLException; - -import be.nikiroo.fanfix.bundles.Config; -import be.nikiroo.fanfix.bundles.StringId; -import be.nikiroo.fanfix.data.Chapter; -import be.nikiroo.fanfix.data.MetaData; -import be.nikiroo.fanfix.data.Story; -import be.nikiroo.fanfix.library.BasicLibrary; -import be.nikiroo.fanfix.library.CacheLibrary; -import be.nikiroo.fanfix.library.LocalLibrary; -import be.nikiroo.fanfix.library.RemoteLibrary; -import be.nikiroo.fanfix.library.RemoteLibraryServer; -import be.nikiroo.fanfix.library.WebLibrary; -import be.nikiroo.fanfix.library.WebLibraryServer; -import be.nikiroo.fanfix.output.BasicOutput; -import be.nikiroo.fanfix.output.BasicOutput.OutputType; -import be.nikiroo.fanfix.reader.BasicReader; -import be.nikiroo.fanfix.reader.CliReader; -import be.nikiroo.fanfix.searchable.BasicSearchable; -import be.nikiroo.fanfix.supported.BasicSupport; -import be.nikiroo.fanfix.supported.SupportType; -import be.nikiroo.utils.Progress; -import be.nikiroo.utils.Version; -import be.nikiroo.utils.VersionCheck; - -/** - * Main program entry point. - * - * @author niki - */ -public class Main { - private enum MainAction { - IMPORT, EXPORT, CONVERT, READ, READ_URL, LIST, HELP, START, VERSION, SERVER, STOP_SERVER, REMOTE, SET_SOURCE, SET_TITLE, SET_AUTHOR, SEARCH, SEARCH_TAG - } - - /** - * Main program entry point. - *

- * Known environment variables: - *

- *

- *

- * - * @param args - * see method description - */ - public static void main(String[] args) { - new Main().start(args); - } - - /** - * Start the default handling for the application. - *

- * If specific actions were asked (with correct parameters), they will be - * forwarded to the different protected methods that you can override. - *

- * At the end of the method, {@link Main#exit(int)} will be called; by - * default, it calls {@link System#exit(int)} if the status is not 0. - * - * @param args - * the arguments received from the system - */ - public void start(String [] args) { - // Only one line, but very important: - Instance.init(); - - String urlString = null; - String luid = null; - String sourceString = null; - String titleString = null; - String authorString = null; - String chapString = null; - String target = null; - String key = null; - MainAction action = MainAction.START; - Boolean plusInfo = null; - String host = null; - Integer port = null; - SupportType searchOn = null; - String search = null; - List tags = new ArrayList(); - Integer page = null; - Integer item = null; - - boolean noMoreActions = false; - - int exitCode = 0; - for (int i = 0; exitCode == 0 && i < args.length; i++) { - if (args[i] == null) - continue; - - // Action (--) handling: - if (!noMoreActions && args[i].startsWith("--")) { - if (args[i].equals("--")) { - noMoreActions = true; - } else { - try { - action = MainAction.valueOf(args[i].substring(2) - .toUpperCase().replace("-", "_")); - } catch (Exception e) { - Instance.getInstance().getTraceHandler() - .error(new IllegalArgumentException("Unknown action: " + args[i], e)); - exitCode = 255; - } - } - - continue; - } - - switch (action) { - case IMPORT: - if (urlString == null) { - urlString = args[i]; - } else { - exitCode = 255; - } - break; - case EXPORT: - if (luid == null) { - luid = args[i]; - } else if (sourceString == null) { - sourceString = args[i]; - } else if (target == null) { - target = args[i]; - } else { - exitCode = 255; - } - break; - case CONVERT: - if (urlString == null) { - urlString = args[i]; - } else if (sourceString == null) { - sourceString = args[i]; - } else if (target == null) { - target = args[i]; - } else if (plusInfo == null) { - if ("+info".equals(args[i])) { - plusInfo = true; - } else { - exitCode = 255; - } - } else { - exitCode = 255; - } - break; - case LIST: - if (sourceString == null) { - sourceString = args[i]; - } else { - exitCode = 255; - } - break; - case SET_SOURCE: - if (luid == null) { - luid = args[i]; - } else if (sourceString == null) { - sourceString = args[i]; - } else { - exitCode = 255; - } - break; - case SET_TITLE: - if (luid == null) { - luid = args[i]; - } else if (sourceString == null) { - titleString = args[i]; - } else { - exitCode = 255; - } - break; - case SET_AUTHOR: - if (luid == null) { - luid = args[i]; - } else if (sourceString == null) { - authorString = args[i]; - } else { - exitCode = 255; - } - break; - case READ: - if (luid == null) { - luid = args[i]; - } else if (chapString == null) { - chapString = args[i]; - } else { - exitCode = 255; - } - break; - case READ_URL: - if (urlString == null) { - urlString = args[i]; - } else if (chapString == null) { - chapString = args[i]; - } else { - exitCode = 255; - } - break; - case SEARCH: - if (searchOn == null) { - searchOn = SupportType.valueOfAllOkUC(args[i]); - - if (searchOn == null) { - Instance.getInstance().getTraceHandler().error("Website not known: <" + args[i] + ">"); - exitCode = 41; - break; - } - - if (BasicSearchable.getSearchable(searchOn) == null) { - Instance.getInstance().getTraceHandler().error("Website not supported: " + searchOn); - exitCode = 42; - break; - } - } else if (search == null) { - search = args[i]; - } else if (page != null && page == -1) { - try { - page = Integer.parseInt(args[i]); - } catch (Exception e) { - page = -2; - } - } else if (item != null && item == -1) { - try { - item = Integer.parseInt(args[i]); - } catch (Exception e) { - item = -2; - } - } else if (page == null || item == null) { - if (page == null && "page".equals(args[i])) { - page = -1; - } else if (item == null && "item".equals(args[i])) { - item = -1; - } else { - exitCode = 255; - } - } else { - exitCode = 255; - } - break; - case SEARCH_TAG: - if (searchOn == null) { - searchOn = SupportType.valueOfAllOkUC(args[i]); - - if (searchOn == null) { - Instance.getInstance().getTraceHandler().error("Website not known: <" + args[i] + ">"); - exitCode = 255; - } - - if (BasicSearchable.getSearchable(searchOn) == null) { - Instance.getInstance().getTraceHandler().error("Website not supported: " + searchOn); - exitCode = 255; - } - } else if (page == null && item == null) { - if ("page".equals(args[i])) { - page = -1; - } else if ("item".equals(args[i])) { - item = -1; - } else { - try { - int index = Integer.parseInt(args[i]); - tags.add(index); - } catch (NumberFormatException e) { - Instance.getInstance().getTraceHandler().error("Invalid tag index: " + args[i]); - exitCode = 255; - } - } - } else if (page != null && page == -1) { - try { - page = Integer.parseInt(args[i]); - } catch (Exception e) { - page = -2; - } - } else if (item != null && item == -1) { - try { - item = Integer.parseInt(args[i]); - } catch (Exception e) { - item = -2; - } - } else if (page == null || item == null) { - if (page == null && "page".equals(args[i])) { - page = -1; - } else if (item == null && "item".equals(args[i])) { - item = -1; - } else { - exitCode = 255; - } - } else { - exitCode = 255; - } - break; - case HELP: - exitCode = 255; - break; - case START: - exitCode = 255; // not supposed to be selected by user - break; - case VERSION: - exitCode = 255; // no arguments for this option - break; - case SERVER: - exitCode = 255; // no arguments for this option - break; - case STOP_SERVER: - exitCode = 255; // no arguments for this option - break; - case REMOTE: - if (key == null) { - key = args[i]; - } else if (host == null) { - host = args[i]; - } else if (port == null) { - port = Integer.parseInt(args[i]); - - BasicLibrary lib; - if (host.startsWith("http://") - || host.startsWith("https://")) { - lib = new WebLibrary(key, host, port); - } else { - lib = new RemoteLibrary(key, host, port); - } - - lib = new CacheLibrary( - Instance.getInstance().getRemoteDir(host), lib, - Instance.getInstance().getUiConfig()); - - Instance.getInstance().setLibrary(lib); - - action = MainAction.START; - } else { - exitCode = 255; - } - break; - } - } - - final Progress mainProgress = new Progress(0, 80); - mainProgress.addProgressListener(new Progress.ProgressListener() { - private int current = mainProgress.getMin(); - - @Override - public void progress(Progress progress, String name) { - int diff = progress.getProgress() - current; - current += diff; - - if (diff <= 0) - return; - - StringBuilder builder = new StringBuilder(); - for (int i = 0; i < diff; i++) { - builder.append('.'); - } - - System.err.print(builder.toString()); - - if (progress.isDone()) { - System.err.println(""); - } - } - }); - Progress pg = new Progress(); - mainProgress.addProgress(pg, mainProgress.getMax()); - - VersionCheck updates = checkUpdates(); - - if (exitCode == 0) { - switch (action) { - case IMPORT: - if (updates != null) { - // we consider it read - Instance.getInstance().setVersionChecked(); - } - - try { - exitCode = imprt(BasicReader.getUrl(urlString), pg); - } catch (MalformedURLException e) { - Instance.getInstance().getTraceHandler().error(e); - exitCode = 1; - } - - break; - case EXPORT: - if (updates != null) { - // we consider it read - Instance.getInstance().setVersionChecked(); - } - - OutputType exportType = OutputType.valueOfNullOkUC(sourceString, null); - if (exportType == null) { - Instance.getInstance().getTraceHandler().error(new Exception(trans(StringId.OUTPUT_DESC, sourceString))); - exitCode = 1; - break; - } - - exitCode = export(luid, exportType, target, pg); - - break; - case CONVERT: - if (updates != null) { - // we consider it read - Instance.getInstance().setVersionChecked(); - } - - OutputType convertType = OutputType.valueOfAllOkUC(sourceString, null); - if (convertType == null) { - Instance.getInstance().getTraceHandler() - .error(new IOException(trans(StringId.ERR_BAD_OUTPUT_TYPE, sourceString))); - - exitCode = 2; - break; - } - - exitCode = convert(urlString, convertType, target, - plusInfo == null ? false : plusInfo, pg); - - break; - case LIST: - exitCode = list(sourceString); - break; - case SET_SOURCE: - try { - Instance.getInstance().getLibrary().changeSource(luid, sourceString, pg); - } catch (IOException e1) { - Instance.getInstance().getTraceHandler().error(e1); - exitCode = 21; - } - break; - case SET_TITLE: - try { - Instance.getInstance().getLibrary().changeTitle(luid, titleString, pg); - } catch (IOException e1) { - Instance.getInstance().getTraceHandler().error(e1); - exitCode = 22; - } - break; - case SET_AUTHOR: - try { - Instance.getInstance().getLibrary().changeAuthor(luid, authorString, pg); - } catch (IOException e1) { - Instance.getInstance().getTraceHandler().error(e1); - exitCode = 23; - } - break; - case READ: - if (luid == null || luid.isEmpty()) { - syntax(false); - exitCode = 255; - break; - } - - try { - Integer chap = null; - if (chapString != null) { - try { - chap = Integer.parseInt(chapString); - } catch (NumberFormatException e) { - Instance.getInstance().getTraceHandler().error(new IOException( - "Chapter number cannot be parsed: " + chapString, e)); - exitCode = 2; - break; - } - } - - BasicLibrary lib = Instance.getInstance().getLibrary(); - exitCode = read(lib.getStory(luid, null), chap); - } catch (IOException e) { - Instance.getInstance().getTraceHandler() - .error(new IOException("Failed to read book", e)); - exitCode = 2; - } - - break; - case READ_URL: - if (urlString == null || urlString.isEmpty()) { - syntax(false); - exitCode = 255; - break; - } - - try { - Integer chap = null; - if (chapString != null) { - try { - chap = Integer.parseInt(chapString); - } catch (NumberFormatException e) { - Instance.getInstance().getTraceHandler().error(new IOException( - "Chapter number cannot be parsed: " + chapString, e)); - exitCode = 2; - break; - } - } - - BasicSupport support = BasicSupport - .getSupport(BasicReader.getUrl(urlString)); - if (support == null) { - Instance.getInstance().getTraceHandler() - .error("URL not supported: " + urlString); - exitCode = 2; - break; - } - - exitCode = read(support.process(null), chap); - } catch (IOException e) { - Instance.getInstance().getTraceHandler() - .error(new IOException("Failed to read book", e)); - exitCode = 2; - } - - break; - case SEARCH: - page = page == null ? 1 : page; - if (page < 0) { - Instance.getInstance().getTraceHandler().error("Incorrect page number"); - exitCode = 255; - break; - } - - item = item == null ? 0 : item; - if (item < 0) { - Instance.getInstance().getTraceHandler().error("Incorrect item number"); - exitCode = 255; - break; - } - - if (searchOn == null) { - try { - search(); - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - exitCode = 1; - } - } else if (search != null) { - try { - searchKeywords(searchOn, search, page, item); - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - exitCode = 20; - } - } else { - exitCode = 255; - } - - break; - case SEARCH_TAG: - if (searchOn == null) { - exitCode = 255; - break; - } - - page = page == null ? 1 : page; - if (page < 0) { - Instance.getInstance().getTraceHandler().error("Incorrect page number"); - exitCode = 255; - break; - } - - item = item == null ? 0 : item; - if (item < 0) { - Instance.getInstance().getTraceHandler().error("Incorrect item number"); - exitCode = 255; - break; - } - - try { - searchTags(searchOn, page, item, - tags.toArray(new Integer[] {})); - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - } - - break; - case HELP: - syntax(true); - exitCode = 0; - break; - case VERSION: - if (updates != null) { - // we consider it read - Instance.getInstance().setVersionChecked(); - } - - System.out - .println(String.format("Fanfix version %s" - + "%nhttps://github.com/nikiroo/fanfix/" - + "%n\tWritten by Nikiroo", - Version.getCurrentVersion())); - break; - case START: - try { - start(); - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - exitCode = 66; - } - break; - case SERVER: - try { - startServer(); - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - } - - break; - case STOP_SERVER: - // Can be given via "--remote XX XX XX" - if (key == null) { - key = Instance.getInstance().getConfig() - .getString(Config.SERVER_KEY); - - // If a subkey in RW mode exists, use it - for (String subkey : Instance.getInstance().getConfig() - .getList(Config.SERVER_ALLOWED_SUBKEYS, - new ArrayList())) { - if ((subkey + "|").contains("|rw|")) { - key = key + "|" + subkey; - break; - } - } - } - - if (port == null) { - port = Instance.getInstance().getConfig().getInteger(Config.SERVER_PORT); - } - - if (host == null) { - String mode = Instance.getInstance().getConfig() - .getString(Config.SERVER_MODE, "fanfix"); - if ("http".equals(mode)) { - host = "http://localhost"; - } else if ("https".equals(mode)) { - host = "https://localhost"; - } else if ("fanfix".equals(mode)) { - host = "fanfix://localhost"; - } - } - - if (port == null) { - System.err.println("No port given nor configured in the config file"); - exitCode = 15; - break; - } - try { - stopServer(key, host, port); - } catch (SSLException e) { - Instance.getInstance().getTraceHandler().error( - "Bad access key for remote library"); - exitCode = 43; - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - exitCode = 44; - } - - break; - case REMOTE: - exitCode = 255; // should not be reachable (REMOTE -> START) - break; - } - } - - try { - Instance.getInstance().getTempFiles().close(); - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(new IOException( - "Cannot dispose of the temporary files", e)); - } - - if (exitCode == 255) { - syntax(false); - } - - exit(exitCode); - } - - /** - * A normal invocation of the program (without parameters or at least - * without "action" parameters). - *

- * You will probably want to override that one if you offer a user - * interface. - * - * @throws IOException - * in case of I/O error - */ - protected void start() throws IOException { - new CliReader().listBooks(null); - } - - /** - * Will check if updates are available, synchronously. - *

- * For this, it will simply forward the call to - * {@link Main#checkUpdates(String)} with a value of "nikiroo/fanfix". - *

- * You may want to override it so you call the forward method with the right - * parameters (or also if you want it to be asynchronous). - * - * @return the newer version information or NULL if nothing new - */ - protected VersionCheck checkUpdates() { - return checkUpdates("nikiroo/fanfix"); - } - - /** - * Will check if updates are available on a specific GitHub project. - *

- * Will be called by {@link Main#checkUpdates()}, but if you override that - * one you mall call it with another project. - * - * @param githubProject - * the GitHub project, for instance "nikiroo/fanfix" - * - * @return the newer version information or NULL if nothing new - */ - protected VersionCheck checkUpdates(String githubProject) { - try { - VersionCheck updates = VersionCheck.check(githubProject, - Instance.getInstance().getTrans().getLocale()); - if (updates.isNewVersionAvailable()) { - notifyUpdates(updates); - return updates; - } - } catch (IOException e) { - // Maybe no internet. Do not report any update. - } - - return null; - } - - /** - * Notify the user about available updates. - *

- * Will only be called when a version is available. - *

- * Note that you can call {@link Instance#setVersionChecked()} on it if the - * user has read the information (by default, it is marked read only on - * certain other actions). - * - * @param updates - * the new version information - */ - protected void notifyUpdates(VersionCheck updates) { - // Sent to syserr so not to cause problem if one tries to capture a - // story content in text mode - System.err.println( - "A new version of the program is available at https://github.com/nikiroo/fanfix/releases"); - System.err.println(""); - for (Version v : updates.getNewer()) { - System.err.println("\tVersion " + v); - System.err.println("\t-------------"); - System.err.println(""); - for (String it : updates.getChanges().get(v)) { - System.err.println("\t- " + it); - } - System.err.println(""); - } - } - - /** - * Import the given resource into the {@link LocalLibrary}. - * - * @param url - * the resource to import - * @param pg - * the optional progress reporter - * - * @return the exit return code (0 = success) - */ - protected static int imprt(URL url, Progress pg) { - try { - MetaData meta = Instance.getInstance().getLibrary().imprt(url, pg); - System.out.println(meta.getLuid() + ": \"" + meta.getTitle() + "\" imported."); - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - return 1; - } - - return 0; - } - - /** - * Export the {@link Story} from the {@link LocalLibrary} to the given - * target. - * - * @param luid - * the story LUID - * @param type - * the {@link OutputType} to use - * @param target - * the target - * @param pg - * the optional progress reporter - * - * @return the exit return code (0 = success) - */ - protected static int export(String luid, OutputType type, String target, - Progress pg) { - try { - Instance.getInstance().getLibrary().export(luid, type, target, pg); - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - return 4; - } - - return 0; - } - - /** - * List the stories of the given source from the {@link LocalLibrary} - * (unless NULL is passed, in which case all stories will be listed). - * - * @param source - * the source to list the known stories of, or NULL to list all - * stories - * - * @return the exit return code (0 = success) - */ - protected int list(String source) { - try { - new CliReader().listBooks(source); - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - return 66; - } - - return 0; - } - - /** - * Start the current reader for this {@link Story}. - * - * @param story - * the story to read - * @param chap - * which {@link Chapter} to read (starting at 1), or NULL to get - * the {@link Story} description - * - * @return the exit return code (0 = success) - */ - protected int read(Story story, Integer chap) { - if (story != null) { - try { - if (chap == null) { - new CliReader().listChapters(story); - } else { - new CliReader().printChapter(story, chap); - } - } catch (IOException e) { - Instance.getInstance().getTraceHandler() - .error(new IOException("Failed to read book", e)); - return 2; - } - } else { - Instance.getInstance().getTraceHandler() - .error("Cannot find book: " + story); - return 2; - } - - return 0; - } - - /** - * Convert the {@link Story} into another format. - * - * @param urlString - * the source {@link Story} to convert - * @param type - * the {@link OutputType} to convert to - * @param target - * the target file - * @param infoCover - * TRUE to also export the cover and info file, even if the given - * {@link OutputType} does not usually save them - * @param pg - * the optional progress reporter - * - * @return the exit return code (0 = success) - */ - protected int convert(String urlString, OutputType type, - String target, boolean infoCover, Progress pg) { - int exitCode = 0; - - Instance.getInstance().getTraceHandler().trace("Convert: " + urlString); - String sourceName = urlString; - try { - URL source = BasicReader.getUrl(urlString); - sourceName = source.toString(); - if (sourceName.startsWith("file://")) { - sourceName = sourceName.substring("file://".length()); - } - - try { - BasicSupport support = BasicSupport.getSupport(source); - - if (support != null) { - Instance.getInstance().getTraceHandler() - .trace("Support found: " + support.getClass()); - Progress pgIn = new Progress(); - Progress pgOut = new Progress(); - if (pg != null) { - pg.setMax(2); - pg.addProgress(pgIn, 1); - pg.addProgress(pgOut, 1); - } - - Story story = support.process(pgIn); - try { - target = new File(target).getAbsolutePath(); - BasicOutput.getOutput(type, infoCover, infoCover) - .process(story, target, pgOut); - } catch (IOException e) { - Instance.getInstance().getTraceHandler() - .error(new IOException( - trans(StringId.ERR_SAVING, target), e)); - exitCode = 5; - } - } else { - Instance.getInstance().getTraceHandler() - .error(new IOException( - trans(StringId.ERR_NOT_SUPPORTED, source))); - - exitCode = 4; - } - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(new IOException( - trans(StringId.ERR_LOADING, sourceName), e)); - exitCode = 3; - } - } catch (MalformedURLException e) { - Instance.getInstance().getTraceHandler().error(new IOException(trans(StringId.ERR_BAD_URL, sourceName), e)); - exitCode = 1; - } - - return exitCode; - } - - /** - * Display the correct syntax of the program to the user to stdout, or an - * error message if the syntax used was wrong on stderr. - * - * @param showHelp - * TRUE to show the syntax help, FALSE to show "syntax error" - */ - protected void syntax(boolean showHelp) { - if (showHelp) { - StringBuilder builder = new StringBuilder(); - for (SupportType type : SupportType.values()) { - builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(), - type.getDesc())); - builder.append('\n'); - } - - String typesIn = builder.toString(); - builder.setLength(0); - - for (OutputType type : OutputType.values()) { - builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(), - type.getDesc(true))); - builder.append('\n'); - } - - String typesOut = builder.toString(); - - System.out.println(trans(StringId.HELP_SYNTAX, typesIn, typesOut)); - } else { - System.err.println(trans(StringId.ERR_SYNTAX)); - } - } - - /** - * Starts a search operation (i.e., list the available web sites we can - * search on). - * - * @throws IOException - * in case of I/O errors - */ - protected void search() throws IOException { - new CliReader().listSearchables(); - } - - /** - * Search for books by keywords on the given supported web site. - * - * @param searchOn - * the web site to search on - * @param search - * the keyword to look for - * @param page - * the page of results to get, or 0 to inquire about the number - * of pages - * @param item - * the index of the book we are interested by, or 0 to query - * about how many books are in that page of results - * - * @throws IOException - * in case of I/O error - */ - protected void searchKeywords(SupportType searchOn, String search, - int page, Integer item) throws IOException { - new CliReader().searchBooksByKeyword(searchOn, search, page, item); - } - - /** - * Search for books by tags on the given supported web site. - * - * @param searchOn - * the web site to search on - * @param page - * the page of results to get, or 0 to inquire about the number - * of pages - * @param item - * the index of the book we are interested by, or 0 to query - * about how many books are in that page of results - * @param tags - * the tags to look for - * - * @throws IOException - * in case of I/O error - */ - protected void searchTags(SupportType searchOn, Integer page, Integer item, - Integer[] tags) throws IOException { - new CliReader().searchBooksByTag(searchOn, page, item, tags); - } - - /** - * Start a Fanfix server. - * - * @throws IOException - * in case of I/O errors - * @throws SSLException - * when the key was not accepted - */ - private void startServer() throws IOException { - String mode = Instance.getInstance().getConfig() - .getString(Config.SERVER_MODE, "fanfix"); - if (mode.equals("fanfix")) { - RemoteLibraryServer server = new RemoteLibraryServer(); - server.setTraceHandler(Instance.getInstance().getTraceHandler()); - server.run(); - } else if (mode.equals("http")) { - WebLibraryServer server = new WebLibraryServer(false); - server.setTraceHandler(Instance.getInstance().getTraceHandler()); - server.run(); - } else if (mode.equals("https")) { - WebLibraryServer server = new WebLibraryServer(true); - server.setTraceHandler(Instance.getInstance().getTraceHandler()); - server.run(); - } else { - throw new IOException("Unknown server mode: " + mode); - } - } - - /** - * Stop a running Fanfix server. - * - * @param key - * the key to contact the Fanfix server - * @param host - * the host on which it runs - * @param port - * the port on which it runs - * - * @throws IOException - * in case of I/O errors - * @throws SSLException - * when the key was not accepted - */ - private void stopServer(String key, String host, int port) - throws IOException, SSLException { - if (host.startsWith("http://") || host.startsWith("https://")) { - new WebLibrary(key, host, port).stop(); - } else { - new RemoteLibrary(key, host, port).stop(); - } - } - - /** - * We are done and ready to exit. - *

- * By default, it will call {@link System#exit(int)} if the status is not 0. - * - * @param status - * the exit status - */ - protected void exit(int status) { - if (status != 0) { - System.exit(status); - } - } - - /** - * Simple shortcut method to call {link Instance#getTrans()#getString()}. - * - * @param id - * the ID to translate - * - * @return the translated result - */ - static private String trans(StringId id, Object... params) { - return Instance.getInstance().getTrans().getString(id, params); - } -} diff --git a/MarkableFileInputStream.java b/MarkableFileInputStream.java new file mode 100644 index 0000000..3f28064 --- /dev/null +++ b/MarkableFileInputStream.java @@ -0,0 +1,22 @@ +package be.nikiroo.utils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; + +/** + * Class was moved to {@link be.nikiroo.utils.streams.MarkableFileInputStream}. + * + * @author niki + */ +@Deprecated +public class MarkableFileInputStream extends + be.nikiroo.utils.streams.MarkableFileInputStream { + public MarkableFileInputStream(File file) throws FileNotFoundException { + super(file); + } + + public MarkableFileInputStream(FileInputStream fis) { + super(fis); + } +} diff --git a/NanoHTTPD.java b/NanoHTTPD.java new file mode 100644 index 0000000..8d183c1 --- /dev/null +++ b/NanoHTTPD.java @@ -0,0 +1,2358 @@ +package be.nikiroo.utils; + +/* + * #%L + * NanoHttpd-Core + * %% + * Copyright (C) 2012 - 2015 nanohttpd + * %% + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the nanohttpd nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * #L% + */ + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.Closeable; +import java.io.DataOutput; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.RandomAccessFile; +import java.io.UnsupportedEncodingException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketException; +import java.net.SocketTimeoutException; +import java.net.URL; +import java.net.URLDecoder; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; +import java.security.KeyStore; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collections; +import java.util.Date; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.StringTokenizer; +import java.util.TimeZone; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.zip.GZIPOutputStream; + +import javax.net.ssl.KeyManager; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLException; +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLServerSocketFactory; +import javax.net.ssl.TrustManagerFactory; + +import be.nikiroo.utils.NanoHTTPD.Response.IStatus; +import be.nikiroo.utils.NanoHTTPD.Response.Status; + +/** + * A simple, tiny, nicely embeddable HTTP server in Java + *

+ *

+ * NanoHTTPD + *

+ * Copyright (c) 2012-2013 by Paul S. Hawke, 2001,2005-2013 by Jarno Elonen, + * 2010 by Konstantinos Togias + *

+ *

+ *

+ * Features + limitations: + *

+ *

+ *

+ * How to use: + *

+ *

+ * See the separate "LICENSE.md" file for the distribution license (Modified BSD + * licence) + */ +public abstract class NanoHTTPD { + + /** + * Pluggable strategy for asynchronously executing requests. + */ + public interface AsyncRunner { + + void closeAll(); + + void closed(ClientHandler clientHandler); + + void exec(ClientHandler code); + } + + /** + * The runnable that will be used for every new client connection. + */ + public class ClientHandler implements Runnable { + + private final InputStream inputStream; + + private final Socket acceptSocket; + + public ClientHandler(InputStream inputStream, Socket acceptSocket) { + this.inputStream = inputStream; + this.acceptSocket = acceptSocket; + } + + public void close() { + safeClose(this.inputStream); + safeClose(this.acceptSocket); + } + + @Override + public void run() { + OutputStream outputStream = null; + try { + outputStream = this.acceptSocket.getOutputStream(); + TempFileManager tempFileManager = NanoHTTPD.this.tempFileManagerFactory.create(); + HTTPSession session = new HTTPSession(tempFileManager, this.inputStream, outputStream, this.acceptSocket.getInetAddress()); + while (!this.acceptSocket.isClosed()) { + session.execute(); + } + } catch (Exception e) { + // When the socket is closed by the client, + // we throw our own SocketException + // to break the "keep alive" loop above. If + // the exception was anything other + // than the expected SocketException OR a + // SocketTimeoutException, print the + // stacktrace + if (!(e instanceof SocketException && "NanoHttpd Shutdown".equals(e.getMessage())) && !(e instanceof SocketTimeoutException)) { + NanoHTTPD.LOG.log(Level.SEVERE, "Communication with the client broken, or an bug in the handler code", e); + } + } finally { + safeClose(outputStream); + safeClose(this.inputStream); + safeClose(this.acceptSocket); + NanoHTTPD.this.asyncRunner.closed(this); + } + } + } + + public static class Cookie { + + public static String getHTTPTime(int days) { + Calendar calendar = Calendar.getInstance(); + SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); + dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); + calendar.add(Calendar.DAY_OF_MONTH, days); + return dateFormat.format(calendar.getTime()); + } + + private final String n, v, e; + + public Cookie(String name, String value) { + this(name, value, 30); + } + + public Cookie(String name, String value, int numDays) { + this.n = name; + this.v = value; + this.e = getHTTPTime(numDays); + } + + public Cookie(String name, String value, String expires) { + this.n = name; + this.v = value; + this.e = expires; + } + + public String getHTTPHeader() { + String fmt = "%s=%s; expires=%s"; + return String.format(fmt, this.n, this.v, this.e); + } + } + + /** + * Provides rudimentary support for cookies. Doesn't support 'path', + * 'secure' nor 'httpOnly'. Feel free to improve it and/or add unsupported + * features. + * + * @author LordFokas + */ + public class CookieHandler implements Iterable { + + private final HashMap cookies = new HashMap(); + + private final ArrayList queue = new ArrayList(); + + public CookieHandler(Map httpHeaders) { + String raw = httpHeaders.get("cookie"); + if (raw != null) { + String[] tokens = raw.split(";"); + for (String token : tokens) { + String[] data = token.trim().split("="); + if (data.length == 2) { + this.cookies.put(data[0], data[1]); + } + } + } + } + + /** + * Set a cookie with an expiration date from a month ago, effectively + * deleting it on the client side. + * + * @param name + * The cookie name. + */ + public void delete(String name) { + set(name, "-delete-", -30); + } + + @Override + public Iterator iterator() { + return this.cookies.keySet().iterator(); + } + + /** + * Read a cookie from the HTTP Headers. + * + * @param name + * The cookie's name. + * @return The cookie's value if it exists, null otherwise. + */ + public String read(String name) { + return this.cookies.get(name); + } + + public void set(Cookie cookie) { + this.queue.add(cookie); + } + + /** + * Sets a cookie. + * + * @param name + * The cookie's name. + * @param value + * The cookie's value. + * @param expires + * How many days until the cookie expires. + */ + public void set(String name, String value, int expires) { + this.queue.add(new Cookie(name, value, Cookie.getHTTPTime(expires))); + } + + /** + * Internally used by the webserver to add all queued cookies into the + * Response's HTTP Headers. + * + * @param response + * The Response object to which headers the queued cookies + * will be added. + */ + public void unloadQueue(Response response) { + for (Cookie cookie : this.queue) { + response.addHeader("Set-Cookie", cookie.getHTTPHeader()); + } + } + } + + /** + * Default threading strategy for NanoHTTPD. + *

+ *

+ * By default, the server spawns a new Thread for every incoming request. + * These are set to daemon status, and named according to the request + * number. The name is useful when profiling the application. + *

+ */ + public static class DefaultAsyncRunner implements AsyncRunner { + + private long requestCount; + + private final List running = Collections.synchronizedList(new ArrayList()); + + /** + * @return a list with currently running clients. + */ + public List getRunning() { + return running; + } + + @Override + public void closeAll() { + // copy of the list for concurrency + for (ClientHandler clientHandler : new ArrayList(this.running)) { + clientHandler.close(); + } + } + + @Override + public void closed(ClientHandler clientHandler) { + this.running.remove(clientHandler); + } + + @Override + public void exec(ClientHandler clientHandler) { + ++this.requestCount; + Thread t = new Thread(clientHandler); + t.setDaemon(true); + t.setName("NanoHttpd Request Processor (#" + this.requestCount + ")"); + this.running.add(clientHandler); + t.start(); + } + } + + /** + * Default strategy for creating and cleaning up temporary files. + *

+ *

+ * By default, files are created by File.createTempFile() in + * the directory specified. + *

+ */ + public static class DefaultTempFile implements TempFile { + + private final File file; + + private final OutputStream fstream; + + public DefaultTempFile(File tempdir) throws IOException { + this.file = File.createTempFile("NanoHTTPD-", "", tempdir); + this.fstream = new FileOutputStream(this.file); + } + + @Override + public void delete() throws Exception { + safeClose(this.fstream); + if (!this.file.delete()) { + throw new Exception("could not delete temporary file: " + this.file.getAbsolutePath()); + } + } + + @Override + public String getName() { + return this.file.getAbsolutePath(); + } + + @Override + public OutputStream open() throws Exception { + return this.fstream; + } + } + + /** + * Default strategy for creating and cleaning up temporary files. + *

+ *

+ * This class stores its files in the standard location (that is, wherever + * java.io.tmpdir points to). Files are added to an internal + * list, and deleted when no longer needed (that is, when + * clear() is invoked at the end of processing a request). + *

+ */ + public static class DefaultTempFileManager implements TempFileManager { + + private final File tmpdir; + + private final List tempFiles; + + public DefaultTempFileManager() { + this.tmpdir = new File(System.getProperty("java.io.tmpdir")); + if (!tmpdir.exists()) { + tmpdir.mkdirs(); + } + this.tempFiles = new ArrayList(); + } + + @Override + public void clear() { + for (TempFile file : this.tempFiles) { + try { + file.delete(); + } catch (Exception ignored) { + NanoHTTPD.LOG.log(Level.WARNING, "could not delete file ", ignored); + } + } + this.tempFiles.clear(); + } + + @Override + public TempFile createTempFile(String filename_hint) throws Exception { + DefaultTempFile tempFile = new DefaultTempFile(this.tmpdir); + this.tempFiles.add(tempFile); + return tempFile; + } + } + + /** + * Default strategy for creating and cleaning up temporary files. + */ + private class DefaultTempFileManagerFactory implements TempFileManagerFactory { + + @Override + public TempFileManager create() { + return new DefaultTempFileManager(); + } + } + + /** + * Creates a normal ServerSocket for TCP connections + */ + public static class DefaultServerSocketFactory implements ServerSocketFactory { + + @Override + public ServerSocket create() throws IOException { + return new ServerSocket(); + } + + } + + /** + * Creates a new SSLServerSocket + */ + public static class SecureServerSocketFactory implements ServerSocketFactory { + + private SSLServerSocketFactory sslServerSocketFactory; + + private String[] sslProtocols; + + public SecureServerSocketFactory(SSLServerSocketFactory sslServerSocketFactory, String[] sslProtocols) { + this.sslServerSocketFactory = sslServerSocketFactory; + this.sslProtocols = sslProtocols; + } + + @Override + public ServerSocket create() throws IOException { + SSLServerSocket ss = null; + ss = (SSLServerSocket) this.sslServerSocketFactory.createServerSocket(); + if (this.sslProtocols != null) { + ss.setEnabledProtocols(this.sslProtocols); + } else { + ss.setEnabledProtocols(ss.getSupportedProtocols()); + } + ss.setUseClientMode(false); + ss.setWantClientAuth(false); + ss.setNeedClientAuth(false); + return ss; + } + + } + + private static final String CONTENT_DISPOSITION_REGEX = "([ |\t]*Content-Disposition[ |\t]*:)(.*)"; + + private static final Pattern CONTENT_DISPOSITION_PATTERN = Pattern.compile(CONTENT_DISPOSITION_REGEX, Pattern.CASE_INSENSITIVE); + + private static final String CONTENT_TYPE_REGEX = "([ |\t]*content-type[ |\t]*:)(.*)"; + + private static final Pattern CONTENT_TYPE_PATTERN = Pattern.compile(CONTENT_TYPE_REGEX, Pattern.CASE_INSENSITIVE); + + private static final String CONTENT_DISPOSITION_ATTRIBUTE_REGEX = "[ |\t]*([a-zA-Z]*)[ |\t]*=[ |\t]*['|\"]([^\"^']*)['|\"]"; + + private static final Pattern CONTENT_DISPOSITION_ATTRIBUTE_PATTERN = Pattern.compile(CONTENT_DISPOSITION_ATTRIBUTE_REGEX); + + protected static class ContentType { + + private static final String ASCII_ENCODING = "US-ASCII"; + + private static final String MULTIPART_FORM_DATA_HEADER = "multipart/form-data"; + + private static final String CONTENT_REGEX = "[ |\t]*([^/^ ^;^,]+/[^ ^;^,]+)"; + + private static final Pattern MIME_PATTERN = Pattern.compile(CONTENT_REGEX, Pattern.CASE_INSENSITIVE); + + private static final String CHARSET_REGEX = "[ |\t]*(charset)[ |\t]*=[ |\t]*['|\"]?([^\"^'^;^,]*)['|\"]?"; + + private static final Pattern CHARSET_PATTERN = Pattern.compile(CHARSET_REGEX, Pattern.CASE_INSENSITIVE); + + private static final String BOUNDARY_REGEX = "[ |\t]*(boundary)[ |\t]*=[ |\t]*['|\"]?([^\"^'^;^,]*)['|\"]?"; + + private static final Pattern BOUNDARY_PATTERN = Pattern.compile(BOUNDARY_REGEX, Pattern.CASE_INSENSITIVE); + + private final String contentTypeHeader; + + private final String contentType; + + private final String encoding; + + private final String boundary; + + public ContentType(String contentTypeHeader) { + this.contentTypeHeader = contentTypeHeader; + if (contentTypeHeader != null) { + contentType = getDetailFromContentHeader(contentTypeHeader, MIME_PATTERN, "", 1); + encoding = getDetailFromContentHeader(contentTypeHeader, CHARSET_PATTERN, null, 2); + } else { + contentType = ""; + encoding = "UTF-8"; + } + if (MULTIPART_FORM_DATA_HEADER.equalsIgnoreCase(contentType)) { + boundary = getDetailFromContentHeader(contentTypeHeader, BOUNDARY_PATTERN, null, 2); + } else { + boundary = null; + } + } + + private String getDetailFromContentHeader(String contentTypeHeader, Pattern pattern, String defaultValue, int group) { + Matcher matcher = pattern.matcher(contentTypeHeader); + return matcher.find() ? matcher.group(group) : defaultValue; + } + + public String getContentTypeHeader() { + return contentTypeHeader; + } + + public String getContentType() { + return contentType; + } + + public String getEncoding() { + return encoding == null ? ASCII_ENCODING : encoding; + } + + public String getBoundary() { + return boundary; + } + + public boolean isMultipart() { + return MULTIPART_FORM_DATA_HEADER.equalsIgnoreCase(contentType); + } + + public ContentType tryUTF8() { + if (encoding == null) { + return new ContentType(this.contentTypeHeader + "; charset=UTF-8"); + } + return this; + } + } + + protected class HTTPSession implements IHTTPSession { + + private static final int REQUEST_BUFFER_LEN = 512; + + private static final int MEMORY_STORE_LIMIT = 1024; + + public static final int BUFSIZE = 8192; + + public static final int MAX_HEADER_SIZE = 1024; + + private final TempFileManager tempFileManager; + + private final OutputStream outputStream; + + private final BufferedInputStream inputStream; + + private int splitbyte; + + private int rlen; + + private String uri; + + private Method method; + + private Map> parms; + + private Map headers; + + private CookieHandler cookies; + + private String queryParameterString; + + private String remoteIp; + + private String remoteHostname; + + private String protocolVersion; + + public HTTPSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream) { + this.tempFileManager = tempFileManager; + this.inputStream = new BufferedInputStream(inputStream, HTTPSession.BUFSIZE); + this.outputStream = outputStream; + } + + public HTTPSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) { + this.tempFileManager = tempFileManager; + this.inputStream = new BufferedInputStream(inputStream, HTTPSession.BUFSIZE); + this.outputStream = outputStream; + this.remoteIp = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "127.0.0.1" : inetAddress.getHostAddress().toString(); + this.remoteHostname = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "localhost" : inetAddress.getHostName().toString(); + this.headers = new HashMap(); + } + + /** + * Decodes the sent headers and loads the data into Key/value pairs + */ + private void decodeHeader(BufferedReader in, Map pre, Map> parms, Map headers) throws ResponseException { + try { + // Read the request line + String inLine = in.readLine(); + if (inLine == null) { + return; + } + + StringTokenizer st = new StringTokenizer(inLine); + if (!st.hasMoreTokens()) { + throw new ResponseException(Response.Status.BAD_REQUEST, "BAD REQUEST: Syntax error. Usage: GET /example/file.html"); + } + + pre.put("method", st.nextToken()); + + if (!st.hasMoreTokens()) { + throw new ResponseException(Response.Status.BAD_REQUEST, "BAD REQUEST: Missing URI. Usage: GET /example/file.html"); + } + + String uri = st.nextToken(); + + // Decode parameters from the URI + int qmi = uri.indexOf('?'); + if (qmi >= 0) { + decodeParms(uri.substring(qmi + 1), parms); + uri = decodePercent(uri.substring(0, qmi)); + } else { + uri = decodePercent(uri); + } + + // If there's another token, its protocol version, + // followed by HTTP headers. + // NOTE: this now forces header names lower case since they are + // case insensitive and vary by client. + if (st.hasMoreTokens()) { + protocolVersion = st.nextToken(); + } else { + protocolVersion = "HTTP/1.1"; + NanoHTTPD.LOG.log(Level.FINE, "no protocol version specified, strange. Assuming HTTP/1.1."); + } + String line = in.readLine(); + while (line != null && !line.trim().isEmpty()) { + int p = line.indexOf(':'); + if (p >= 0) { + headers.put(line.substring(0, p).trim().toLowerCase(Locale.US), line.substring(p + 1).trim()); + } + line = in.readLine(); + } + + pre.put("uri", uri); + } catch (IOException ioe) { + throw new ResponseException(Response.Status.INTERNAL_ERROR, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage(), ioe); + } + } + + /** + * Decodes the Multipart Body data and put it into Key/Value pairs. + */ + private void decodeMultipartFormData(ContentType contentType, ByteBuffer fbuf, Map> parms, Map files) throws ResponseException { + int pcount = 0; + try { + int[] boundaryIdxs = getBoundaryPositions(fbuf, contentType.getBoundary().getBytes()); + if (boundaryIdxs.length < 2) { + throw new ResponseException(Response.Status.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but contains less than two boundary strings."); + } + + byte[] partHeaderBuff = new byte[MAX_HEADER_SIZE]; + for (int boundaryIdx = 0; boundaryIdx < boundaryIdxs.length - 1; boundaryIdx++) { + fbuf.position(boundaryIdxs[boundaryIdx]); + int len = (fbuf.remaining() < MAX_HEADER_SIZE) ? fbuf.remaining() : MAX_HEADER_SIZE; + fbuf.get(partHeaderBuff, 0, len); + BufferedReader in = + new BufferedReader(new InputStreamReader(new ByteArrayInputStream(partHeaderBuff, 0, len), Charset.forName(contentType.getEncoding())), len); + + int headerLines = 0; + // First line is boundary string + String mpline = in.readLine(); + headerLines++; + if (mpline == null || !mpline.contains(contentType.getBoundary())) { + throw new ResponseException(Response.Status.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but chunk does not start with boundary."); + } + + String partName = null, fileName = null, partContentType = null; + // Parse the reset of the header lines + mpline = in.readLine(); + headerLines++; + while (mpline != null && mpline.trim().length() > 0) { + Matcher matcher = CONTENT_DISPOSITION_PATTERN.matcher(mpline); + if (matcher.matches()) { + String attributeString = matcher.group(2); + matcher = CONTENT_DISPOSITION_ATTRIBUTE_PATTERN.matcher(attributeString); + while (matcher.find()) { + String key = matcher.group(1); + if ("name".equalsIgnoreCase(key)) { + partName = matcher.group(2); + } else if ("filename".equalsIgnoreCase(key)) { + fileName = matcher.group(2); + // add these two line to support multiple + // files uploaded using the same field Id + if (!fileName.isEmpty()) { + if (pcount > 0) + partName = partName + String.valueOf(pcount++); + else + pcount++; + } + } + } + } + matcher = CONTENT_TYPE_PATTERN.matcher(mpline); + if (matcher.matches()) { + partContentType = matcher.group(2).trim(); + } + mpline = in.readLine(); + headerLines++; + } + int partHeaderLength = 0; + while (headerLines-- > 0) { + partHeaderLength = scipOverNewLine(partHeaderBuff, partHeaderLength); + } + // Read the part data + if (partHeaderLength >= len - 4) { + throw new ResponseException(Response.Status.INTERNAL_ERROR, "Multipart header size exceeds MAX_HEADER_SIZE."); + } + int partDataStart = boundaryIdxs[boundaryIdx] + partHeaderLength; + int partDataEnd = boundaryIdxs[boundaryIdx + 1] - 4; + + fbuf.position(partDataStart); + + List values = parms.get(partName); + if (values == null) { + values = new ArrayList(); + parms.put(partName, values); + } + + if (partContentType == null) { + // Read the part into a string + byte[] data_bytes = new byte[partDataEnd - partDataStart]; + fbuf.get(data_bytes); + + values.add(new String(data_bytes, contentType.getEncoding())); + } else { + // Read it into a file + String path = saveTmpFile(fbuf, partDataStart, partDataEnd - partDataStart, fileName); + if (!files.containsKey(partName)) { + files.put(partName, path); + } else { + int count = 2; + while (files.containsKey(partName + count)) { + count++; + } + files.put(partName + count, path); + } + values.add(fileName); + } + } + } catch (ResponseException re) { + throw re; + } catch (Exception e) { + throw new ResponseException(Response.Status.INTERNAL_ERROR, e.toString()); + } + } + + private int scipOverNewLine(byte[] partHeaderBuff, int index) { + while (partHeaderBuff[index] != '\n') { + index++; + } + return ++index; + } + + /** + * Decodes parameters in percent-encoded URI-format ( e.g. + * "name=Jack%20Daniels&pass=Single%20Malt" ) and adds them to given + * Map. + */ + private void decodeParms(String parms, Map> p) { + if (parms == null) { + this.queryParameterString = ""; + return; + } + + this.queryParameterString = parms; + StringTokenizer st = new StringTokenizer(parms, "&"); + while (st.hasMoreTokens()) { + String e = st.nextToken(); + int sep = e.indexOf('='); + String key = null; + String value = null; + + if (sep >= 0) { + key = decodePercent(e.substring(0, sep)).trim(); + value = decodePercent(e.substring(sep + 1)); + } else { + key = decodePercent(e).trim(); + value = ""; + } + + List values = p.get(key); + if (values == null) { + values = new ArrayList(); + p.put(key, values); + } + + values.add(value); + } + } + + @Override + public void execute() throws IOException { + Response r = null; + try { + // Read the first 8192 bytes. + // The full header should fit in here. + // Apache's default header limit is 8KB. + // Do NOT assume that a single read will get the entire header + // at once! + byte[] buf = new byte[HTTPSession.BUFSIZE]; + this.splitbyte = 0; + this.rlen = 0; + + int read = -1; + this.inputStream.mark(HTTPSession.BUFSIZE); + try { + read = this.inputStream.read(buf, 0, HTTPSession.BUFSIZE); + } catch (SSLException e) { + throw e; + } catch (IOException e) { + safeClose(this.inputStream); + safeClose(this.outputStream); + throw new SocketException("NanoHttpd Shutdown"); + } + if (read == -1) { + // socket was been closed + safeClose(this.inputStream); + safeClose(this.outputStream); + throw new SocketException("NanoHttpd Shutdown"); + } + while (read > 0) { + this.rlen += read; + this.splitbyte = findHeaderEnd(buf, this.rlen); + if (this.splitbyte > 0) { + break; + } + read = this.inputStream.read(buf, this.rlen, HTTPSession.BUFSIZE - this.rlen); + } + + if (this.splitbyte < this.rlen) { + this.inputStream.reset(); + this.inputStream.skip(this.splitbyte); + } + + this.parms = new HashMap>(); + if (null == this.headers) { + this.headers = new HashMap(); + } else { + this.headers.clear(); + } + + // Create a BufferedReader for parsing the header. + BufferedReader hin = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buf, 0, this.rlen))); + + // Decode the header into parms and header java properties + Map pre = new HashMap(); + decodeHeader(hin, pre, this.parms, this.headers); + + if (null != this.remoteIp) { + this.headers.put("remote-addr", this.remoteIp); + this.headers.put("http-client-ip", this.remoteIp); + } + + this.method = Method.lookup(pre.get("method")); + if (this.method == null) { + throw new ResponseException(Response.Status.BAD_REQUEST, "BAD REQUEST: Syntax error. HTTP verb " + pre.get("method") + " unhandled."); + } + + this.uri = pre.get("uri"); + + this.cookies = new CookieHandler(this.headers); + + String connection = this.headers.get("connection"); + boolean keepAlive = "HTTP/1.1".equals(protocolVersion) && (connection == null || !connection.matches("(?i).*close.*")); + + // Ok, now do the serve() + + // TODO: long body_size = getBodySize(); + // TODO: long pos_before_serve = this.inputStream.totalRead() + // (requires implementation for totalRead()) + r = serve(this); + // TODO: this.inputStream.skip(body_size - + // (this.inputStream.totalRead() - pos_before_serve)) + + if (r == null) { + throw new ResponseException(Response.Status.INTERNAL_ERROR, "SERVER INTERNAL ERROR: Serve() returned a null response."); + } else { + String acceptEncoding = this.headers.get("accept-encoding"); + this.cookies.unloadQueue(r); + r.setRequestMethod(this.method); + r.setGzipEncoding(useGzipWhenAccepted(r) && acceptEncoding != null && acceptEncoding.contains("gzip")); + r.setKeepAlive(keepAlive); + r.send(this.outputStream); + } + if (!keepAlive || r.isCloseConnection()) { + throw new SocketException("NanoHttpd Shutdown"); + } + } catch (SocketException e) { + // throw it out to close socket object (finalAccept) + throw e; + } catch (SocketTimeoutException ste) { + // treat socket timeouts the same way we treat socket exceptions + // i.e. close the stream & finalAccept object by throwing the + // exception up the call stack. + throw ste; + } catch (SSLException ssle) { + Response resp = newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SSL PROTOCOL FAILURE: " + ssle.getMessage()); + resp.send(this.outputStream); + safeClose(this.outputStream); + } catch (IOException ioe) { + Response resp = newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); + resp.send(this.outputStream); + safeClose(this.outputStream); + } catch (ResponseException re) { + Response resp = newFixedLengthResponse(re.getStatus(), NanoHTTPD.MIME_PLAINTEXT, re.getMessage()); + resp.send(this.outputStream); + safeClose(this.outputStream); + } finally { + safeClose(r); + this.tempFileManager.clear(); + } + } + + /** + * Find byte index separating header from body. It must be the last byte + * of the first two sequential new lines. + */ + private int findHeaderEnd(final byte[] buf, int rlen) { + int splitbyte = 0; + while (splitbyte + 1 < rlen) { + + // RFC2616 + if (buf[splitbyte] == '\r' && buf[splitbyte + 1] == '\n' && splitbyte + 3 < rlen && buf[splitbyte + 2] == '\r' && buf[splitbyte + 3] == '\n') { + return splitbyte + 4; + } + + // tolerance + if (buf[splitbyte] == '\n' && buf[splitbyte + 1] == '\n') { + return splitbyte + 2; + } + splitbyte++; + } + return 0; + } + + /** + * Find the byte positions where multipart boundaries start. This reads + * a large block at a time and uses a temporary buffer to optimize + * (memory mapped) file access. + */ + private int[] getBoundaryPositions(ByteBuffer b, byte[] boundary) { + int[] res = new int[0]; + if (b.remaining() < boundary.length) { + return res; + } + + int search_window_pos = 0; + byte[] search_window = new byte[4 * 1024 + boundary.length]; + + int first_fill = (b.remaining() < search_window.length) ? b.remaining() : search_window.length; + b.get(search_window, 0, first_fill); + int new_bytes = first_fill - boundary.length; + + do { + // Search the search_window + for (int j = 0; j < new_bytes; j++) { + for (int i = 0; i < boundary.length; i++) { + if (search_window[j + i] != boundary[i]) + break; + if (i == boundary.length - 1) { + // Match found, add it to results + int[] new_res = new int[res.length + 1]; + System.arraycopy(res, 0, new_res, 0, res.length); + new_res[res.length] = search_window_pos + j; + res = new_res; + } + } + } + search_window_pos += new_bytes; + + // Copy the end of the buffer to the start + System.arraycopy(search_window, search_window.length - boundary.length, search_window, 0, boundary.length); + + // Refill search_window + new_bytes = search_window.length - boundary.length; + new_bytes = (b.remaining() < new_bytes) ? b.remaining() : new_bytes; + b.get(search_window, boundary.length, new_bytes); + } while (new_bytes > 0); + return res; + } + + @Override + public CookieHandler getCookies() { + return this.cookies; + } + + @Override + public final Map getHeaders() { + return this.headers; + } + + @Override + public final InputStream getInputStream() { + return this.inputStream; + } + + @Override + public final Method getMethod() { + return this.method; + } + + /** + * @deprecated use {@link #getParameters()} instead. + */ + @Override + @Deprecated + public final Map getParms() { + Map result = new HashMap(); + for (String key : this.parms.keySet()) { + result.put(key, this.parms.get(key).get(0)); + } + + return result; + } + + @Override + public final Map> getParameters() { + return this.parms; + } + + @Override + public String getQueryParameterString() { + return this.queryParameterString; + } + + private RandomAccessFile getTmpBucket() { + try { + TempFile tempFile = this.tempFileManager.createTempFile(null); + return new RandomAccessFile(tempFile.getName(), "rw"); + } catch (Exception e) { + throw new Error(e); // we won't recover, so throw an error + } + } + + @Override + public final String getUri() { + return this.uri; + } + + /** + * Deduce body length in bytes. Either from "content-length" header or + * read bytes. + */ + public long getBodySize() { + if (this.headers.containsKey("content-length")) { + return Long.parseLong(this.headers.get("content-length")); + } else if (this.splitbyte < this.rlen) { + return this.rlen - this.splitbyte; + } + return 0; + } + + @Override + public void parseBody(Map files) throws IOException, ResponseException { + RandomAccessFile randomAccessFile = null; + try { + long size = getBodySize(); + ByteArrayOutputStream baos = null; + DataOutput requestDataOutput = null; + + // Store the request in memory or a file, depending on size + if (size < MEMORY_STORE_LIMIT) { + baos = new ByteArrayOutputStream(); + requestDataOutput = new DataOutputStream(baos); + } else { + randomAccessFile = getTmpBucket(); + requestDataOutput = randomAccessFile; + } + + // Read all the body and write it to request_data_output + byte[] buf = new byte[REQUEST_BUFFER_LEN]; + while (this.rlen >= 0 && size > 0) { + this.rlen = this.inputStream.read(buf, 0, (int) Math.min(size, REQUEST_BUFFER_LEN)); + size -= this.rlen; + if (this.rlen > 0) { + requestDataOutput.write(buf, 0, this.rlen); + } + } + + ByteBuffer fbuf = null; + if (baos != null) { + fbuf = ByteBuffer.wrap(baos.toByteArray(), 0, baos.size()); + } else { + fbuf = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length()); + randomAccessFile.seek(0); + } + + // If the method is POST, there may be parameters + // in data section, too, read it: + if (Method.POST.equals(this.method)) { + ContentType contentType = new ContentType(this.headers.get("content-type")); + if (contentType.isMultipart()) { + String boundary = contentType.getBoundary(); + if (boundary == null) { + throw new ResponseException(Response.Status.BAD_REQUEST, + "BAD REQUEST: Content type is multipart/form-data but boundary missing. Usage: GET /example/file.html"); + } + decodeMultipartFormData(contentType, fbuf, this.parms, files); + } else { + byte[] postBytes = new byte[fbuf.remaining()]; + fbuf.get(postBytes); + String postLine = new String(postBytes, contentType.getEncoding()).trim(); + // Handle application/x-www-form-urlencoded + if ("application/x-www-form-urlencoded".equalsIgnoreCase(contentType.getContentType())) { + decodeParms(postLine, this.parms); + } else if (postLine.length() != 0) { + // Special case for raw POST data => create a + // special files entry "postData" with raw content + // data + files.put("postData", postLine); + } + } + } else if (Method.PUT.equals(this.method)) { + files.put("content", saveTmpFile(fbuf, 0, fbuf.limit(), null)); + } + } finally { + safeClose(randomAccessFile); + } + } + + /** + * Retrieves the content of a sent file and saves it to a temporary + * file. The full path to the saved file is returned. + */ + private String saveTmpFile(ByteBuffer b, int offset, int len, String filename_hint) { + String path = ""; + if (len > 0) { + FileOutputStream fileOutputStream = null; + try { + TempFile tempFile = this.tempFileManager.createTempFile(filename_hint); + ByteBuffer src = b.duplicate(); + fileOutputStream = new FileOutputStream(tempFile.getName()); + FileChannel dest = fileOutputStream.getChannel(); + src.position(offset).limit(offset + len); + dest.write(src.slice()); + path = tempFile.getName(); + } catch (Exception e) { // Catch exception if any + throw new Error(e); // we won't recover, so throw an error + } finally { + safeClose(fileOutputStream); + } + } + return path; + } + + @Override + public String getRemoteIpAddress() { + return this.remoteIp; + } + + @Override + public String getRemoteHostName() { + return this.remoteHostname; + } + } + + /** + * Handles one session, i.e. parses the HTTP request and returns the + * response. + */ + public interface IHTTPSession { + + void execute() throws IOException; + + CookieHandler getCookies(); + + Map getHeaders(); + + InputStream getInputStream(); + + Method getMethod(); + + /** + * This method will only return the first value for a given parameter. + * You will want to use getParameters if you expect multiple values for + * a given key. + * + * @deprecated use {@link #getParameters()} instead. + */ + @Deprecated + Map getParms(); + + Map> getParameters(); + + String getQueryParameterString(); + + /** + * @return the path part of the URL. + */ + String getUri(); + + /** + * Adds the files in the request body to the files map. + * + * @param files + * map to modify + */ + void parseBody(Map files) throws IOException, ResponseException; + + /** + * Get the remote ip address of the requester. + * + * @return the IP address. + */ + String getRemoteIpAddress(); + + /** + * Get the remote hostname of the requester. + * + * @return the hostname. + */ + String getRemoteHostName(); + } + + /** + * HTTP Request methods, with the ability to decode a String + * back to its enum value. + */ + public enum Method { + GET, + PUT, + POST, + DELETE, + HEAD, + OPTIONS, + TRACE, + CONNECT, + PATCH, + PROPFIND, + PROPPATCH, + MKCOL, + MOVE, + COPY, + LOCK, + UNLOCK; + + static Method lookup(String method) { + if (method == null) + return null; + + try { + return valueOf(method); + } catch (IllegalArgumentException e) { + // TODO: Log it? + return null; + } + } + } + + /** + * HTTP response. Return one of these from serve(). + */ + public static class Response implements Closeable { + + public interface IStatus { + + String getDescription(); + + int getRequestStatus(); + } + + /** + * Some HTTP response status codes + */ + public enum Status implements IStatus { + SWITCH_PROTOCOL(101, "Switching Protocols"), + + OK(200, "OK"), + CREATED(201, "Created"), + ACCEPTED(202, "Accepted"), + NO_CONTENT(204, "No Content"), + PARTIAL_CONTENT(206, "Partial Content"), + MULTI_STATUS(207, "Multi-Status"), + + REDIRECT(301, "Moved Permanently"), + /** + * Many user agents mishandle 302 in ways that violate the RFC1945 + * spec (i.e., redirect a POST to a GET). 303 and 307 were added in + * RFC2616 to address this. You should prefer 303 and 307 unless the + * calling user agent does not support 303 and 307 functionality + */ + @Deprecated + FOUND(302, "Found"), + REDIRECT_SEE_OTHER(303, "See Other"), + NOT_MODIFIED(304, "Not Modified"), + TEMPORARY_REDIRECT(307, "Temporary Redirect"), + + BAD_REQUEST(400, "Bad Request"), + UNAUTHORIZED(401, "Unauthorized"), + FORBIDDEN(403, "Forbidden"), + NOT_FOUND(404, "Not Found"), + METHOD_NOT_ALLOWED(405, "Method Not Allowed"), + NOT_ACCEPTABLE(406, "Not Acceptable"), + REQUEST_TIMEOUT(408, "Request Timeout"), + CONFLICT(409, "Conflict"), + GONE(410, "Gone"), + LENGTH_REQUIRED(411, "Length Required"), + PRECONDITION_FAILED(412, "Precondition Failed"), + PAYLOAD_TOO_LARGE(413, "Payload Too Large"), + UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"), + RANGE_NOT_SATISFIABLE(416, "Requested Range Not Satisfiable"), + EXPECTATION_FAILED(417, "Expectation Failed"), + TOO_MANY_REQUESTS(429, "Too Many Requests"), + + INTERNAL_ERROR(500, "Internal Server Error"), + NOT_IMPLEMENTED(501, "Not Implemented"), + SERVICE_UNAVAILABLE(503, "Service Unavailable"), + UNSUPPORTED_HTTP_VERSION(505, "HTTP Version Not Supported"); + + private final int requestStatus; + + private final String description; + + Status(int requestStatus, String description) { + this.requestStatus = requestStatus; + this.description = description; + } + + public static Status lookup(int requestStatus) { + for (Status status : Status.values()) { + if (status.getRequestStatus() == requestStatus) { + return status; + } + } + return null; + } + + @Override + public String getDescription() { + return "" + this.requestStatus + " " + this.description; + } + + @Override + public int getRequestStatus() { + return this.requestStatus; + } + + } + + /** + * Output stream that will automatically send every write to the wrapped + * OutputStream according to chunked transfer: + * http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1 + */ + private static class ChunkedOutputStream extends FilterOutputStream { + + public ChunkedOutputStream(OutputStream out) { + super(out); + } + + @Override + public void write(int b) throws IOException { + byte[] data = { + (byte) b + }; + write(data, 0, 1); + } + + @Override + public void write(byte[] b) throws IOException { + write(b, 0, b.length); + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + if (len == 0) + return; + out.write(String.format("%x\r\n", len).getBytes()); + out.write(b, off, len); + out.write("\r\n".getBytes()); + } + + public void finish() throws IOException { + out.write("0\r\n\r\n".getBytes()); + } + + } + + /** + * HTTP status code after processing, e.g. "200 OK", Status.OK + */ + private IStatus status; + + /** + * MIME type of content, e.g. "text/html" + */ + private String mimeType; + + /** + * Data of the response, may be null. + */ + private InputStream data; + + private long contentLength; + + /** + * Headers for the HTTP response. Use addHeader() to add lines. the + * lowercase map is automatically kept up to date. + */ + @SuppressWarnings("serial") + private final Map header = new HashMap() { + + public String put(String key, String value) { + lowerCaseHeader.put(key == null ? key : key.toLowerCase(), value); + return super.put(key, value); + }; + }; + + /** + * copy of the header map with all the keys lowercase for faster + * searching. + */ + private final Map lowerCaseHeader = new HashMap(); + + /** + * The request method that spawned this response. + */ + private Method requestMethod; + + /** + * Use chunkedTransfer + */ + private boolean chunkedTransfer; + + private boolean encodeAsGzip; + + private boolean keepAlive; + + /** + * Creates a fixed length response if totalBytes>=0, otherwise chunked. + */ + protected Response(IStatus status, String mimeType, InputStream data, long totalBytes) { + this.status = status; + this.mimeType = mimeType; + if (data == null) { + this.data = new ByteArrayInputStream(new byte[0]); + this.contentLength = 0L; + } else { + this.data = data; + this.contentLength = totalBytes; + } + this.chunkedTransfer = this.contentLength < 0; + keepAlive = true; + } + + @Override + public void close() throws IOException { + if (this.data != null) { + this.data.close(); + } + } + + /** + * Adds given line to the header. + */ + public void addHeader(String name, String value) { + this.header.put(name, value); + } + + /** + * Indicate to close the connection after the Response has been sent. + * + * @param close + * {@code true} to hint connection closing, {@code false} to + * let connection be closed by client. + */ + public void closeConnection(boolean close) { + if (close) + this.header.put("connection", "close"); + else + this.header.remove("connection"); + } + + /** + * @return {@code true} if connection is to be closed after this + * Response has been sent. + */ + public boolean isCloseConnection() { + return "close".equals(getHeader("connection")); + } + + public InputStream getData() { + return this.data; + } + + public String getHeader(String name) { + return this.lowerCaseHeader.get(name.toLowerCase()); + } + + public String getMimeType() { + return this.mimeType; + } + + public Method getRequestMethod() { + return this.requestMethod; + } + + public IStatus getStatus() { + return this.status; + } + + public void setGzipEncoding(boolean encodeAsGzip) { + this.encodeAsGzip = encodeAsGzip; + } + + public void setKeepAlive(boolean useKeepAlive) { + this.keepAlive = useKeepAlive; + } + + /** + * Sends given response to the socket. + */ + protected void send(OutputStream outputStream) { + SimpleDateFormat gmtFrmt = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss 'GMT'", Locale.US); + gmtFrmt.setTimeZone(TimeZone.getTimeZone("GMT")); + + try { + if (this.status == null) { + throw new Error("sendResponse(): Status can't be null."); + } + PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream, new ContentType(this.mimeType).getEncoding())), false); + pw.append("HTTP/1.1 ").append(this.status.getDescription()).append(" \r\n"); + if (this.mimeType != null) { + printHeader(pw, "Content-Type", this.mimeType); + } + if (getHeader("date") == null) { + printHeader(pw, "Date", gmtFrmt.format(new Date())); + } + for (Entry entry : this.header.entrySet()) { + printHeader(pw, entry.getKey(), entry.getValue()); + } + if (getHeader("connection") == null) { + printHeader(pw, "Connection", (this.keepAlive ? "keep-alive" : "close")); + } + if (getHeader("content-length") != null) { + encodeAsGzip = false; + } + if (encodeAsGzip) { + printHeader(pw, "Content-Encoding", "gzip"); + setChunkedTransfer(true); + } + long pending = this.data != null ? this.contentLength : 0; + if (this.requestMethod != Method.HEAD && this.chunkedTransfer) { + printHeader(pw, "Transfer-Encoding", "chunked"); + } else if (!encodeAsGzip) { + pending = sendContentLengthHeaderIfNotAlreadyPresent(pw, pending); + } + pw.append("\r\n"); + pw.flush(); + sendBodyWithCorrectTransferAndEncoding(outputStream, pending); + outputStream.flush(); + safeClose(this.data); + } catch (IOException ioe) { + NanoHTTPD.LOG.log(Level.SEVERE, "Could not send response to the client", ioe); + } + } + + @SuppressWarnings("static-method") + protected void printHeader(PrintWriter pw, String key, String value) { + pw.append(key).append(": ").append(value).append("\r\n"); + } + + protected long sendContentLengthHeaderIfNotAlreadyPresent(PrintWriter pw, long defaultSize) { + String contentLengthString = getHeader("content-length"); + long size = defaultSize; + if (contentLengthString != null) { + try { + size = Long.parseLong(contentLengthString); + } catch (NumberFormatException ex) { + LOG.severe("content-length was no number " + contentLengthString); + } + } + pw.print("Content-Length: " + size + "\r\n"); + return size; + } + + private void sendBodyWithCorrectTransferAndEncoding(OutputStream outputStream, long pending) throws IOException { + if (this.requestMethod != Method.HEAD && this.chunkedTransfer) { + ChunkedOutputStream chunkedOutputStream = new ChunkedOutputStream(outputStream); + sendBodyWithCorrectEncoding(chunkedOutputStream, -1); + chunkedOutputStream.finish(); + } else { + sendBodyWithCorrectEncoding(outputStream, pending); + } + } + + private void sendBodyWithCorrectEncoding(OutputStream outputStream, long pending) throws IOException { + if (encodeAsGzip) { + GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream); + sendBody(gzipOutputStream, -1); + gzipOutputStream.finish(); + } else { + sendBody(outputStream, pending); + } + } + + /** + * Sends the body to the specified OutputStream. The pending parameter + * limits the maximum amounts of bytes sent unless it is -1, in which + * case everything is sent. + * + * @param outputStream + * the OutputStream to send data to + * @param pending + * -1 to send everything, otherwise sets a max limit to the + * number of bytes sent + * @throws IOException + * if something goes wrong while sending the data. + */ + private void sendBody(OutputStream outputStream, long pending) throws IOException { + long BUFFER_SIZE = 16 * 1024; + byte[] buff = new byte[(int) BUFFER_SIZE]; + boolean sendEverything = pending == -1; + while (pending > 0 || sendEverything) { + long bytesToRead = sendEverything ? BUFFER_SIZE : Math.min(pending, BUFFER_SIZE); + int read = this.data.read(buff, 0, (int) bytesToRead); + if (read <= 0) { + break; + } + outputStream.write(buff, 0, read); + if (!sendEverything) { + pending -= read; + } + } + } + + public void setChunkedTransfer(boolean chunkedTransfer) { + this.chunkedTransfer = chunkedTransfer; + } + + public void setData(InputStream data) { + this.data = data; + } + + public void setMimeType(String mimeType) { + this.mimeType = mimeType; + } + + public void setRequestMethod(Method requestMethod) { + this.requestMethod = requestMethod; + } + + public void setStatus(IStatus status) { + this.status = status; + } + } + + public static final class ResponseException extends Exception { + + private static final long serialVersionUID = 6569838532917408380L; + + private final Response.Status status; + + public ResponseException(Response.Status status, String message) { + super(message); + this.status = status; + } + + public ResponseException(Response.Status status, String message, Exception e) { + super(message, e); + this.status = status; + } + + public Response.Status getStatus() { + return this.status; + } + } + + /** + * The runnable that will be used for the main listening thread. + */ + public class ServerRunnable implements Runnable { + + private final int timeout; + + private IOException bindException; + + private boolean hasBinded = false; + + public ServerRunnable(int timeout) { + this.timeout = timeout; + } + + @Override + public void run() { + try { + myServerSocket.bind(hostname != null ? new InetSocketAddress(hostname, myPort) : new InetSocketAddress(myPort)); + hasBinded = true; + } catch (IOException e) { + this.bindException = e; + return; + } + do { + try { + final Socket finalAccept = NanoHTTPD.this.myServerSocket.accept(); + if (this.timeout > 0) { + finalAccept.setSoTimeout(this.timeout); + } + final InputStream inputStream = finalAccept.getInputStream(); + NanoHTTPD.this.asyncRunner.exec(createClientHandler(finalAccept, inputStream)); + } catch (IOException e) { + NanoHTTPD.LOG.log(Level.FINE, "Communication with the client broken", e); + } + } while (!NanoHTTPD.this.myServerSocket.isClosed()); + } + } + + /** + * A temp file. + *

+ *

+ * Temp files are responsible for managing the actual temporary storage and + * cleaning themselves up when no longer needed. + *

+ */ + public interface TempFile { + + public void delete() throws Exception; + + public String getName(); + + public OutputStream open() throws Exception; + } + + /** + * Temp file manager. + *

+ *

+ * Temp file managers are created 1-to-1 with incoming requests, to create + * and cleanup temporary files created as a result of handling the request. + *

+ */ + public interface TempFileManager { + + void clear(); + + public TempFile createTempFile(String filename_hint) throws Exception; + } + + /** + * Factory to create temp file managers. + */ + public interface TempFileManagerFactory { + + public TempFileManager create(); + } + + /** + * Factory to create ServerSocketFactories. + */ + public interface ServerSocketFactory { + + public ServerSocket create() throws IOException; + + } + + /** + * Maximum time to wait on Socket.getInputStream().read() (in milliseconds) + * This is required as the Keep-Alive HTTP connections would otherwise block + * the socket reading thread forever (or as long the browser is open). + */ + public static final int SOCKET_READ_TIMEOUT = 5000; + + /** + * Common MIME type for dynamic content: plain text + */ + public static final String MIME_PLAINTEXT = "text/plain"; + + /** + * Common MIME type for dynamic content: html + */ + public static final String MIME_HTML = "text/html"; + + /** + * Pseudo-Parameter to use to store the actual query string in the + * parameters map for later re-processing. + */ + private static final String QUERY_STRING_PARAMETER = "NanoHttpd.QUERY_STRING"; + + /** + * logger to log to. + */ + private static final Logger LOG = Logger.getLogger(NanoHTTPD.class.getName()); + + /** + * Hashtable mapping (String)FILENAME_EXTENSION -> (String)MIME_TYPE + */ + protected static Map MIME_TYPES; + + public static Map mimeTypes() { + if (MIME_TYPES == null) { + MIME_TYPES = new HashMap(); + loadMimeTypes(MIME_TYPES, "META-INF/nanohttpd/default-mimetypes.properties"); + loadMimeTypes(MIME_TYPES, "META-INF/nanohttpd/mimetypes.properties"); + if (MIME_TYPES.isEmpty()) { + LOG.log(Level.WARNING, "no mime types found in the classpath! please provide mimetypes.properties"); + } + } + return MIME_TYPES; + } + + @SuppressWarnings({ + "unchecked", + "rawtypes" + }) + private static void loadMimeTypes(Map result, String resourceName) { + try { + Enumeration resources = NanoHTTPD.class.getClassLoader().getResources(resourceName); + while (resources.hasMoreElements()) { + URL url = (URL) resources.nextElement(); + Properties properties = new Properties(); + InputStream stream = null; + try { + stream = url.openStream(); + properties.load(stream); + } catch (IOException e) { + LOG.log(Level.SEVERE, "could not load mimetypes from " + url, e); + } finally { + safeClose(stream); + } + result.putAll((Map) properties); + } + } catch (IOException e) { + LOG.log(Level.INFO, "no mime types available at " + resourceName); + } + }; + + /** + * Creates an SSLSocketFactory for HTTPS. Pass a loaded KeyStore and an + * array of loaded KeyManagers. These objects must properly + * loaded/initialized by the caller. + */ + public static SSLServerSocketFactory makeSSLSocketFactory(KeyStore loadedKeyStore, KeyManager[] keyManagers) throws IOException { + SSLServerSocketFactory res = null; + try { + TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + trustManagerFactory.init(loadedKeyStore); + SSLContext ctx = SSLContext.getInstance("TLS"); + ctx.init(keyManagers, trustManagerFactory.getTrustManagers(), null); + res = ctx.getServerSocketFactory(); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + return res; + } + + /** + * Creates an SSLSocketFactory for HTTPS. Pass a loaded KeyStore and a + * loaded KeyManagerFactory. These objects must properly loaded/initialized + * by the caller. + */ + public static SSLServerSocketFactory makeSSLSocketFactory(KeyStore loadedKeyStore, KeyManagerFactory loadedKeyFactory) throws IOException { + try { + return makeSSLSocketFactory(loadedKeyStore, loadedKeyFactory.getKeyManagers()); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + + /** + * Creates an SSLSocketFactory for HTTPS. Pass a KeyStore resource with your + * certificate and passphrase + */ + public static SSLServerSocketFactory makeSSLSocketFactory(String keyAndTrustStoreClasspathPath, char[] passphrase) throws IOException { + try { + KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); + InputStream keystoreStream = NanoHTTPD.class.getResourceAsStream(keyAndTrustStoreClasspathPath); + + if (keystoreStream == null) { + throw new IOException("Unable to load keystore from classpath: " + keyAndTrustStoreClasspathPath); + } + + keystore.load(keystoreStream, passphrase); + KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); + keyManagerFactory.init(keystore, passphrase); + return makeSSLSocketFactory(keystore, keyManagerFactory); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + + /** + * Get MIME type from file name extension, if possible + * + * @param uri + * the string representing a file + * @return the connected mime/type + */ + public static String getMimeTypeForFile(String uri) { + int dot = uri.lastIndexOf('.'); + String mime = null; + if (dot >= 0) { + mime = mimeTypes().get(uri.substring(dot + 1).toLowerCase()); + } + return mime == null ? "application/octet-stream" : mime; + } + + private static final void safeClose(Object closeable) { + try { + if (closeable != null) { + if (closeable instanceof Closeable) { + ((Closeable) closeable).close(); + } else if (closeable instanceof Socket) { + ((Socket) closeable).close(); + } else if (closeable instanceof ServerSocket) { + ((ServerSocket) closeable).close(); + } else { + throw new IllegalArgumentException("Unknown object to close"); + } + } + } catch (IOException e) { + NanoHTTPD.LOG.log(Level.SEVERE, "Could not close", e); + } + } + + private final String hostname; + + private final int myPort; + + private volatile ServerSocket myServerSocket; + + private ServerSocketFactory serverSocketFactory = new DefaultServerSocketFactory(); + + private Thread myThread; + + /** + * Pluggable strategy for asynchronously executing requests. + */ + protected AsyncRunner asyncRunner; + + /** + * Pluggable strategy for creating and cleaning up temporary files. + */ + private TempFileManagerFactory tempFileManagerFactory; + + /** + * Constructs an HTTP server on given port. + */ + public NanoHTTPD(int port) { + this(null, port); + } + + // ------------------------------------------------------------------------------- + // // + // + // Threading Strategy. + // + // ------------------------------------------------------------------------------- + // // + + /** + * Constructs an HTTP server on given hostname and port. + */ + public NanoHTTPD(String hostname, int port) { + this.hostname = hostname; + this.myPort = port; + setTempFileManagerFactory(new DefaultTempFileManagerFactory()); + setAsyncRunner(new DefaultAsyncRunner()); + } + + /** + * Forcibly closes all connections that are open. + */ + public synchronized void closeAllConnections() { + stop(); + } + + /** + * create a instance of the client handler, subclasses can return a subclass + * of the ClientHandler. + * + * @param finalAccept + * the socket the cleint is connected to + * @param inputStream + * the input stream + * @return the client handler + */ + protected ClientHandler createClientHandler(final Socket finalAccept, final InputStream inputStream) { + return new ClientHandler(inputStream, finalAccept); + } + + /** + * Instantiate the server runnable, can be overwritten by subclasses to + * provide a subclass of the ServerRunnable. + * + * @param timeout + * the socet timeout to use. + * @return the server runnable. + */ + protected ServerRunnable createServerRunnable(final int timeout) { + return new ServerRunnable(timeout); + } + + /** + * Decode parameters from a URL, handing the case where a single parameter + * name might have been supplied several times, by return lists of values. + * In general these lists will contain a single element. + * + * @param parms + * original NanoHTTPD parameters values, as passed to the + * serve() method. + * @return a map of String (parameter name) to + * List<String> (a list of the values supplied). + */ + protected static Map> decodeParameters(Map parms) { + return decodeParameters(parms.get(NanoHTTPD.QUERY_STRING_PARAMETER)); + } + + // ------------------------------------------------------------------------------- + // // + + /** + * Decode parameters from a URL, handing the case where a single parameter + * name might have been supplied several times, by return lists of values. + * In general these lists will contain a single element. + * + * @param queryString + * a query string pulled from the URL. + * @return a map of String (parameter name) to + * List<String> (a list of the values supplied). + */ + protected static Map> decodeParameters(String queryString) { + Map> parms = new HashMap>(); + if (queryString != null) { + StringTokenizer st = new StringTokenizer(queryString, "&"); + while (st.hasMoreTokens()) { + String e = st.nextToken(); + int sep = e.indexOf('='); + String propertyName = sep >= 0 ? decodePercent(e.substring(0, sep)).trim() : decodePercent(e).trim(); + if (!parms.containsKey(propertyName)) { + parms.put(propertyName, new ArrayList()); + } + String propertyValue = sep >= 0 ? decodePercent(e.substring(sep + 1)) : null; + if (propertyValue != null) { + parms.get(propertyName).add(propertyValue); + } + } + } + return parms; + } + + /** + * Decode percent encoded String values. + * + * @param str + * the percent encoded String + * @return expanded form of the input, for example "foo%20bar" becomes + * "foo bar" + */ + protected static String decodePercent(String str) { + String decoded = null; + try { + decoded = URLDecoder.decode(str, "UTF8"); + } catch (UnsupportedEncodingException ignored) { + NanoHTTPD.LOG.log(Level.WARNING, "Encoding not supported, ignored", ignored); + } + return decoded; + } + + /** + * @return true if the gzip compression should be used if the client + * accespts it. Default this option is on for text content and off + * for everything. Override this for custom semantics. + */ + @SuppressWarnings("static-method") + protected boolean useGzipWhenAccepted(Response r) { + return r.getMimeType() != null && (r.getMimeType().toLowerCase().contains("text/") || r.getMimeType().toLowerCase().contains("/json")); + } + + public final int getListeningPort() { + return this.myServerSocket == null ? -1 : this.myServerSocket.getLocalPort(); + } + + public final boolean isAlive() { + return wasStarted() && !this.myServerSocket.isClosed() && this.myThread.isAlive(); + } + + public ServerSocketFactory getServerSocketFactory() { + return serverSocketFactory; + } + + public void setServerSocketFactory(ServerSocketFactory serverSocketFactory) { + this.serverSocketFactory = serverSocketFactory; + } + + public String getHostname() { + return hostname; + } + + public TempFileManagerFactory getTempFileManagerFactory() { + return tempFileManagerFactory; + } + + /** + * Call before start() to serve over HTTPS instead of HTTP + */ + public void makeSecure(SSLServerSocketFactory sslServerSocketFactory, String[] sslProtocols) { + this.serverSocketFactory = new SecureServerSocketFactory(sslServerSocketFactory, sslProtocols); + } + + /** + * Create a response with unknown length (using HTTP 1.1 chunking). + */ + public static Response newChunkedResponse(IStatus status, String mimeType, InputStream data) { + return new Response(status, mimeType, data, -1); + } + + /** + * Create a response with known length. + */ + public static Response newFixedLengthResponse(IStatus status, String mimeType, InputStream data, long totalBytes) { + return new Response(status, mimeType, data, totalBytes); + } + + /** + * Create a text response with known length. + */ + public static Response newFixedLengthResponse(IStatus status, String mimeType, String txt) { + ContentType contentType = new ContentType(mimeType); + if (txt == null) { + return newFixedLengthResponse(status, mimeType, new ByteArrayInputStream(new byte[0]), 0); + } else { + byte[] bytes; + try { + CharsetEncoder newEncoder = Charset.forName(contentType.getEncoding()).newEncoder(); + if (!newEncoder.canEncode(txt)) { + contentType = contentType.tryUTF8(); + } + bytes = txt.getBytes(contentType.getEncoding()); + } catch (UnsupportedEncodingException e) { + NanoHTTPD.LOG.log(Level.SEVERE, "encoding problem, responding nothing", e); + bytes = new byte[0]; + } + return newFixedLengthResponse(status, contentType.getContentTypeHeader(), new ByteArrayInputStream(bytes), bytes.length); + } + } + + /** + * Create a text response with known length. + */ + public static Response newFixedLengthResponse(String msg) { + return newFixedLengthResponse(Status.OK, NanoHTTPD.MIME_HTML, msg); + } + + /** + * Override this to customize the server. + *

+ *

+ * (By default, this returns a 404 "Not Found" plain text error response.) + * + * @param session + * The HTTP session + * @return HTTP response, see class Response for details + */ + public Response serve(IHTTPSession session) { + Map files = new HashMap(); + Method method = session.getMethod(); + if (Method.PUT.equals(method) || Method.POST.equals(method)) { + try { + session.parseBody(files); + } catch (IOException ioe) { + return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); + } catch (ResponseException re) { + return newFixedLengthResponse(re.getStatus(), NanoHTTPD.MIME_PLAINTEXT, re.getMessage()); + } + } + + Map parms = session.getParms(); + parms.put(NanoHTTPD.QUERY_STRING_PARAMETER, session.getQueryParameterString()); + return serve(session.getUri(), method, session.getHeaders(), parms, files); + } + + /** + * Override this to customize the server. + *

+ *

+ * (By default, this returns a 404 "Not Found" plain text error response.) + * + * @param uri + * Percent-decoded URI without parameters, for example + * "/index.cgi" + * @param method + * "GET", "POST" etc. + * @param parms + * Parsed, percent decoded parameters from URI and, in case of + * POST, data. + * @param headers + * Header entries, percent decoded + * @return HTTP response, see class Response for details + */ + @Deprecated + public Response serve(String uri, Method method, Map headers, Map parms, Map files) { + return newFixedLengthResponse(Response.Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "Not Found"); + } + + /** + * Pluggable strategy for asynchronously executing requests. + * + * @param asyncRunner + * new strategy for handling threads. + */ + public void setAsyncRunner(AsyncRunner asyncRunner) { + this.asyncRunner = asyncRunner; + } + + /** + * Pluggable strategy for creating and cleaning up temporary files. + * + * @param tempFileManagerFactory + * new strategy for handling temp files. + */ + public void setTempFileManagerFactory(TempFileManagerFactory tempFileManagerFactory) { + this.tempFileManagerFactory = tempFileManagerFactory; + } + + /** + * Start the server. + * + * @throws IOException + * if the socket is in use. + */ + public void start() throws IOException { + start(NanoHTTPD.SOCKET_READ_TIMEOUT); + } + + /** + * Starts the server (in setDaemon(true) mode). + */ + public void start(final int timeout) throws IOException { + start(timeout, true); + } + + /** + * Start the server. + * + * @param timeout + * timeout to use for socket connections. + * @param daemon + * start the thread daemon or not. + * @throws IOException + * if the socket is in use. + */ + public void start(final int timeout, boolean daemon) throws IOException { + this.myServerSocket = this.getServerSocketFactory().create(); + this.myServerSocket.setReuseAddress(true); + + ServerRunnable serverRunnable = createServerRunnable(timeout); + this.myThread = new Thread(serverRunnable); + this.myThread.setDaemon(daemon); + this.myThread.setName("NanoHttpd Main Listener"); + this.myThread.start(); + while (!serverRunnable.hasBinded && serverRunnable.bindException == null) { + try { + Thread.sleep(10L); + } catch (Throwable e) { + // on android this may not be allowed, that's why we + // catch throwable the wait should be very short because we are + // just waiting for the bind of the socket + } + } + if (serverRunnable.bindException != null) { + throw serverRunnable.bindException; + } + } + + /** + * Stop the server. + */ + public void stop() { + try { + safeClose(this.myServerSocket); + this.asyncRunner.closeAll(); + if (this.myThread != null) { + this.myThread.join(); + } + } catch (Exception e) { + NanoHTTPD.LOG.log(Level.SEVERE, "Could not stop all connections", e); + } + } + + public final boolean wasStarted() { + return this.myServerSocket != null && this.myThread != null; + } +} diff --git a/Progress.java b/Progress.java new file mode 100644 index 0000000..bb143ef --- /dev/null +++ b/Progress.java @@ -0,0 +1,535 @@ +package be.nikiroo.utils; + +import java.util.ArrayList; +import java.util.EventListener; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +/** + * Progress reporting system, possibly nested. + *

+ * A {@link Progress} can have a name, and that name will be reported through + * the event system (it will report the first non-null name in the stack from + * the {@link Progress} from which the event originated to the parent the event + * is listened on). + *

+ * The {@link Progress} also has a table of keys/values shared amongst all the + * hierarchy (note that when adding a {@link Progress} to others, its values + * will be prioritized if some with the same keys were already present in the + * hierarchy). + * + * @author niki + */ +public class Progress { + /** + * This event listener is designed to report progress events from + * {@link Progress}. + * + * @author niki + */ + public interface ProgressListener extends EventListener { + /** + * A progression event. + * + * @param progress + * the {@link Progress} object that generated it, not + * necessarily the same as the one where the listener was + * attached (it could be a child {@link Progress} of this + * {@link Progress}). + * @param name + * the first non-null name of the {@link Progress} step that + * generated this event + */ + public void progress(Progress progress, String name); + } + + private Map map = new HashMap(); + private Progress parent = null; + private Object lock = new Object(); + private String name; + private Map children; + private List listeners; + private int min; + private int max; + private double relativeLocalProgress; + private double relativeProgress; // children included + + /** + * Create a new default unnamed {@link Progress}, from 0 to 100. + */ + public Progress() { + this(null); + } + + /** + * Create a new default {@link Progress}, from 0 to 100. + * + * @param name + * the name of this {@link Progress} step + */ + public Progress(String name) { + this(name, 0, 100); + } + + /** + * Create a new unnamed {@link Progress}, from min to max. + * + * @param min + * the minimum progress value (and starting value) -- must be + * non-negative + * @param max + * the maximum progress value + */ + public Progress(int min, int max) { + this(null, min, max); + } + + /** + * Create a new {@link Progress}, from min to max. + * + * @param name + * the name of this {@link Progress} step + * @param min + * the minimum progress value (and starting value) -- must be + * non-negative + * @param max + * the maximum progress value + */ + public Progress(String name, int min, int max) { + this.name = name; + this.children = new HashMap(); + this.listeners = new ArrayList(); + setMinMax(min, max); + setProgress(min); + } + + /** + * The name of this {@link Progress} step. + * + * @return the name, can be NULL + */ + public String getName() { + return name; + } + + /** + * The name of this {@link Progress} step. + * + * @param name + * the new name + */ + public void setName(String name) { + this.name = name; + changed(this, name); + } + + /** + * The minimum progress value. + * + * @return the min + */ + public int getMin() { + return min; + } + + /** + * The minimum progress value. + * + * @param min + * the min to set + * + * + * @throws RuntimeException + * if min < 0 or if min > max + */ + public void setMin(int min) { + if (min < 0) { + throw new RuntimeException("negative values not supported"); + } + + synchronized (lock) { + if (min > max) { + throw new RuntimeException( + "The minimum progress value must be <= the maximum progress value"); + } + + this.min = min; + } + } + + /** + * The maximum progress value. + * + * @return the max + */ + public int getMax() { + return max; + } + + /** + * The maximum progress value (must be >= the minimum progress value). + * + * @param max + * the max to set + * + * + * @throws RuntimeException + * if max < min + */ + public void setMax(int max) { + synchronized (lock) { + if (max < min) { + throw new Error( + "The maximum progress value must be >= the minimum progress value"); + } + + this.max = max; + } + } + + /** + * Set both the minimum and maximum progress values. + * + * @param min + * the min + * @param max + * the max + * + * @throws RuntimeException + * if min < 0 or if min > max + */ + public void setMinMax(int min, int max) { + if (min < 0) { + throw new RuntimeException("negative values not supported"); + } + + if (min > max) { + throw new RuntimeException( + "The minimum progress value must be <= the maximum progress value"); + } + + synchronized (lock) { + this.min = min; + this.max = max; + } + } + + /** + * Get the total progress value (including the optional children + * {@link Progress}) on a {@link Progress#getMin()} to + * {@link Progress#getMax()} scale. + * + * @return the progress the value + */ + public int getProgress() { + return (int) Math.round(relativeProgress * (max - min)); + } + + /** + * Set the local progress value (not including the optional children + * {@link Progress}), on a {@link Progress#getMin()} to + * {@link Progress#getMax()} scale. + * + * @param progress + * the progress to set + */ + public void setProgress(int progress) { + synchronized (lock) { + double childrenProgress = relativeProgress - relativeLocalProgress; + + relativeLocalProgress = ((double) progress) / (max - min); + + setRelativeProgress(this, name, + relativeLocalProgress + childrenProgress); + } + } + + /** + * Get the total progress value (including the optional children + * {@link Progress}) on a 0.0 to 1.0 scale. + * + * @return the progress + */ + public double getRelativeProgress() { + return relativeProgress; + } + + /** + * Set the total progress value (including the optional children + * {@link Progress}), on a 0 to 1 scale. + *

+ * Will generate a changed event from this very {@link Progress}. + * + * @param relativeProgress + * the progress to set + */ + public void setRelativeProgress(double relativeProgress) { + setRelativeProgress(this, name, relativeProgress); + } + + /** + * Set the total progress value (including the optional children + * {@link Progress}), on a 0 to 1 scale. + * + * @param pg + * the {@link Progress} to report as the progression emitter (can + * be NULL, will then be considered the same as this) + * @param name + * the current name (if it is NULL, the first non-null name in + * the hierarchy will overwrite it) of the {@link Progress} who + * emitted this change + * @param relativeProgress + * the progress to set + */ + private void setRelativeProgress(Progress pg, String name, + double relativeProgress) { + synchronized (lock) { + relativeProgress = Math.max(0, relativeProgress); + relativeProgress = Math.min(1, relativeProgress); + this.relativeProgress = relativeProgress; + + changed(pg, name); + } + } + + /** + * Get the total progress value (including the optional children + * {@link Progress}) on a 0 to 1 scale. + * + * @return the progress the value + */ + private int getLocalProgress() { + return (int) Math.round(relativeLocalProgress * (max - min)); + } + + /** + * Add some value to the current progression of this {@link Progress}. + * + * @param step + * the amount to add + */ + public void add(int step) { + synchronized (lock) { + setProgress(getLocalProgress() + step); + } + } + + /** + * Check if the action corresponding to this {@link Progress} is done (i.e., + * if its progress value == its max value). + * + * @return TRUE if it is + */ + public boolean isDone() { + return getProgress() == max; + } + + /** + * Mark the {@link Progress} as done by setting its value to max. + */ + public void done() { + synchronized (lock) { + double childrenProgress = relativeProgress - relativeLocalProgress; + relativeLocalProgress = 1 - childrenProgress; + setRelativeProgress(this, name, 1d); + } + } + + /** + * Return the list of direct children of this {@link Progress}. + *

+ * Can return an empty list, but never NULL. + * + * @return the children (Who will think of the children??) + */ + public List getChildren() { + synchronized (lock) { + return new ArrayList(children.keySet()); + } + } + + /** + * The weight of this children if it is actually a child of this. + * + * @param child + * the child to get the weight of + * + * @return NULL if this is not a child of this + */ + public Double getWeight(Progress child) { + synchronized (lock) { + return children.get(child); + } + } + + /** + * Notify the listeners that this {@link Progress} changed value. + * + * @param pg + * the emmiter, that is, the (sub-){link Progress} that just + * reported some change, not always the same as this + * @param name + * the current name (if it is NULL, the first non-null name in + * the hierarchy will overwrite it) of the {@link Progress} who + * emitted this change + */ + private void changed(Progress pg, String name) { + if (pg == null) { + pg = this; + } + + if (name == null) { + name = this.name; + } + + synchronized (lock) { + for (ProgressListener l : listeners) { + l.progress(pg, name); + } + } + } + + /** + * Add a {@link ProgressListener} that will trigger on progress changes. + *

+ * Note: the {@link Progress} that will be reported will be the active + * progress, not necessarily the same as the current one (it could be a + * child {@link Progress} of this {@link Progress}). + * + * @param l + * the listener + */ + public void addProgressListener(ProgressListener l) { + synchronized (lock) { + this.listeners.add(l); + } + } + + /** + * Remove a {@link ProgressListener} that would trigger on progress changes. + * + * @param l + * the listener + * + * @return TRUE if it was found (and removed) + */ + public boolean removeProgressListener(ProgressListener l) { + synchronized (lock) { + return this.listeners.remove(l); + } + } + + /** + * Add a child {@link Progress} of the given weight. + * + * @param progress + * the child {@link Progress} to add + * @param weight + * the weight (on a {@link Progress#getMin()} to + * {@link Progress#getMax()} scale) of this child + * {@link Progress} in relation to its parent + * + * @throws RuntimeException + * if weight exceed {@link Progress#getMax()} or if progress + * already has a parent + */ + public void addProgress(Progress progress, double weight) { + if (weight < min || weight > max) { + throw new RuntimeException(String.format( + "Progress object %s cannot have a weight of %f, " + + "it is outside of its parent (%s) range (%d)", + progress.name, weight, name, max)); + } + + if (progress.parent != null) { + throw new RuntimeException(String.format( + "Progress object %s cannot be added to %s, " + + "as it already has a parent (%s)", + progress.name, name, progress.parent.name)); + } + + ProgressListener progressListener = new ProgressListener() { + @Override + public void progress(Progress pg, String name) { + synchronized (lock) { + double total = relativeLocalProgress; + for (Entry entry : children.entrySet()) { + total += (entry.getValue() / (max - min)) + * entry.getKey().getRelativeProgress(); + } + + setRelativeProgress(pg, name, total); + } + } + }; + + synchronized (lock) { + // Should not happen but just in case + if (this.map != progress.map) { + this.map.putAll(progress.map); + } + progress.map = this.map; + progress.parent = this; + this.children.put(progress, weight); + progress.addProgressListener(progressListener); + } + } + + /** + * Set the given value for the given key on this {@link Progress} and it's + * children. + * + * @param key + * the key + * @param value + * the value + */ + public void put(Object key, Object value) { + map.put(key, value); + } + + /** + * Return the value associated with this key as a {@link String} if any, + * NULL if not. + *

+ * If the value is not NULL but not a {@link String}, it will be converted + * via {@link Object#toString()}. + * + * @param key + * the key to check + * + * @return the value or NULL + */ + public String getString(Object key) { + Object value = map.get(key); + if (value == null) { + return null; + } + + return value.toString(); + } + + /** + * Return the value associated with this key if any, NULL if not. + * + * @param key + * the key to check + * + * @return the value or NULL + */ + public Object get(Object key) { + return map.get(key); + } + + @Override + public String toString() { + return "[Progress]" // + + (name == null || name.isEmpty() ? "" : " " + name) // + + ": " + getProgress() + " / " + getMax() // + + (children.isEmpty() ? "" + : " (with " + children.size() + " children)") // + ; + } +} diff --git a/Proxy.java b/Proxy.java new file mode 100644 index 0000000..750b3ee --- /dev/null +++ b/Proxy.java @@ -0,0 +1,150 @@ +package be.nikiroo.utils; + +import java.net.Authenticator; +import java.net.PasswordAuthentication; + +/** + * Simple proxy helper to select a default internet proxy. + * + * @author niki + */ +public class Proxy { + /** + * Use the proxy described by this string: + *

    + *
  • ((user(:pass)@)proxy:port)
  • + *
  • System proxy is noted :
  • + *
+ * Some examples: + *
    + *
  • → do not use any proxy
  • + *
  • : → use the system proxy
  • + *
  • user@prox.com → use the proxy "prox.com" with default port + * and user "user"
  • + *
  • prox.com:8080 → use the proxy "prox.com" on port 8080
  • + *
  • user:pass@prox.com:8080 → use "prox.com" on port 8080 + * authenticated as "user" with password "pass"
  • + *
  • user:pass@: → use the system proxy authenticated as user + * "user" with password "pass"
  • + *
+ * + * @param proxy + * the proxy + */ + static public void use(String proxy) { + if (proxy != null && !proxy.isEmpty()) { + String user = null; + String password = null; + int port = 8080; + + if (proxy.contains("@")) { + int pos = proxy.indexOf("@"); + user = proxy.substring(0, pos); + proxy = proxy.substring(pos + 1); + if (user.contains(":")) { + pos = user.indexOf(":"); + password = user.substring(pos + 1); + user = user.substring(0, pos); + } + } + + if (proxy.equals(":")) { + proxy = null; + } else if (proxy.contains(":")) { + int pos = proxy.indexOf(":"); + try { + port = Integer.parseInt(proxy.substring(0, pos)); + proxy = proxy.substring(pos + 1); + } catch (Exception e) { + } + } + + if (proxy == null) { + Proxy.useSystemProxy(user, password); + } else { + Proxy.useProxy(proxy, port, user, password); + } + } + } + + /** + * Use the system proxy. + */ + static public void useSystemProxy() { + useSystemProxy(null, null); + } + + /** + * Use the system proxy with the given login/password, for authenticated + * proxies. + * + * @param user + * the user name or login + * @param password + * the password + */ + static public void useSystemProxy(String user, String password) { + System.setProperty("java.net.useSystemProxies", "true"); + auth(user, password); + } + + /** + * Use the give proxy. + * + * @param host + * the proxy host name or IP address + * @param port + * the port to use + */ + static public void useProxy(String host, int port) { + useProxy(host, port, null, null); + } + + /** + * Use the given proxy with the given login/password, for authenticated + * proxies. + * + * @param user + * the user name or login + * @param password + * the password + * @param host + * the proxy host name or IP address + * @param port + * the port to use + * @param user + * the user name or login + * @param password + * the password + */ + static public void useProxy(String host, int port, String user, + String password) { + System.setProperty("http.proxyHost", host); + System.setProperty("http.proxyPort", Integer.toString(port)); + auth(user, password); + } + + /** + * Select the default authenticator for proxy requests. + * + * @param user + * the user name or login + * @param password + * the password + */ + static private void auth(final String user, final String password) { + if (user != null && password != null) { + Authenticator proxy = new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + if (getRequestorType() == RequestorType.PROXY) { + return new PasswordAuthentication(user, + password.toCharArray()); + } + return null; + } + }; + Authenticator.setDefault(proxy); + } + } +} diff --git a/StringJustifier.java b/StringJustifier.java new file mode 100644 index 0000000..ed20291 --- /dev/null +++ b/StringJustifier.java @@ -0,0 +1,286 @@ +/* + * This file was taken from: + * Jexer - Java Text User Interface + * + * The MIT License (MIT) + * + * Copyright (C) 2017 Kevin Lamonte + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * @author Kevin Lamonte [kevin.lamonte@gmail.com] + * @version 1 + * + * I added some changes to integrate it here. + * @author Niki + */ +package be.nikiroo.utils; + +import java.util.LinkedList; +import java.util.List; + +/** + * StringJustifier contains methods to convert one or more long lines of strings + * into justified text paragraphs. + */ +class StringJustifier { + /** + * Process the given text into a list of left-justified lines of a given + * max-width. + * + * @param data + * the text to justify + * @param width + * the maximum width of a line + * + * @return the list of justified lines + */ + static List left(final String data, final int width) { + return left(data, width, false); + } + + /** + * Right-justify a string into a list of lines. + * + * @param str + * the string + * @param n + * the maximum number of characters in a line + * @return the list of lines + */ + static List right(final String str, final int n) { + List result = new LinkedList(); + + /* + * Same as left(), but preceed each line with spaces to make it n chars + * long. + */ + List lines = left(str, n); + for (String line : lines) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < n - line.length(); i++) { + sb.append(' '); + } + sb.append(line); + result.add(sb.toString()); + } + + return result; + } + + /** + * Center a string into a list of lines. + * + * @param str + * the string + * @param n + * the maximum number of characters in a line + * @return the list of lines + */ + static List center(final String str, final int n) { + List result = new LinkedList(); + + /* + * Same as left(), but preceed/succeed each line with spaces to make it + * n chars long. + */ + List lines = left(str, n); + for (String line : lines) { + StringBuilder sb = new StringBuilder(); + int l = (n - line.length()) / 2; + int r = n - line.length() - l; + for (int i = 0; i < l; i++) { + sb.append(' '); + } + sb.append(line); + for (int i = 0; i < r; i++) { + sb.append(' '); + } + result.add(sb.toString()); + } + + return result; + } + + /** + * Fully-justify a string into a list of lines. + * + * @param str + * the string + * @param n + * the maximum number of characters in a line + * @return the list of lines + */ + static List full(final String str, final int n) { + List result = new LinkedList(); + + /* + * Same as left(true), but insert spaces between words to make each line + * n chars long. The "algorithm" here is pretty dumb: it performs a + * split on space and then re-inserts multiples of n between words. + */ + List lines = left(str, n, true); + for (int lineI = 0; lineI < lines.size() - 1; lineI++) { + String line = lines.get(lineI); + String[] words = line.split(" "); + if (words.length > 1) { + int charCount = 0; + for (int i = 0; i < words.length; i++) { + charCount += words[i].length(); + } + int spaceCount = n - charCount; + int q = spaceCount / (words.length - 1); + int r = spaceCount % (words.length - 1); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < words.length - 1; i++) { + sb.append(words[i]); + for (int j = 0; j < q; j++) { + sb.append(' '); + } + if (r > 0) { + sb.append(' '); + r--; + } + } + for (int j = 0; j < r; j++) { + sb.append(' '); + } + sb.append(words[words.length - 1]); + result.add(sb.toString()); + } else { + result.add(line); + } + } + if (lines.size() > 0) { + result.add(lines.get(lines.size() - 1)); + } + + return result; + } + + /** + * Process the given text into a list of left-justified lines of a given + * max-width. + * + * @param data + * the text to justify + * @param width + * the maximum width of a line + * @param minTwoWords + * use 2 words per line minimum if the text allows it + * + * @return the list of justified lines + */ + static private List left(final String data, final int width, + boolean minTwoWords) { + List lines = new LinkedList(); + + for (String dataLine : data.split("\n")) { + String line = rightTrim(dataLine.replace("\t", " ")); + + if (width > 0 && line.length() > width) { + while (line.length() > 0) { + int i = Math.min(line.length(), width - 1); // -1 for "-" + + boolean needDash = true; + // find the best space if any and if needed + int prevSpace = 0; + if (i < line.length()) { + prevSpace = -1; + int space = line.indexOf(' '); + int numOfSpaces = 0; + + while (space > -1 && space <= i) { + prevSpace = space; + space = line.indexOf(' ', space + 1); + numOfSpaces++; + } + + if (prevSpace > 0 && (!minTwoWords || numOfSpaces >= 2)) { + i = prevSpace; + needDash = false; + } + } + // + + // no dash before space/dash + if ((i + 1) < line.length()) { + char car = line.charAt(i); + char nextCar = line.charAt(i + 1); + if (car == ' ' || car == '-' || nextCar == ' ') { + needDash = false; + } else if (i > 0) { + char prevCar = line.charAt(i - 1); + if (prevCar == ' ' || prevCar == '-') { + needDash = false; + i--; + } + } + } + + // if the space freed by the removed dash allows it, or if + // it is the last char, add the next char + if (!needDash || i >= line.length() - 1) { + int checkI = Math.min(i + 1, line.length()); + if (checkI == i || checkI <= width) { + needDash = false; + i = checkI; + } + } + + // no dash before parenthesis (but cannot add one more + // after) + if ((i + 1) < line.length()) { + char nextCar = line.charAt(i + 1); + if (nextCar == '(' || nextCar == ')') { + needDash = false; + } + } + + if (needDash) { + lines.add(rightTrim(line.substring(0, i)) + "-"); + } else { + lines.add(rightTrim(line.substring(0, i))); + } + + // full trim (remove spaces when cutting) + line = line.substring(i).trim(); + } + } else { + lines.add(line); + } + } + + return lines; + } + + /** + * Trim the given {@link String} on the right only. + * + * @param data + * the source {@link String} + * @return the right-trimmed String or Empty if it was NULL + */ + static private String rightTrim(String data) { + if (data == null) + return ""; + + return ("|" + data).trim().substring(1); + } +} diff --git a/StringUtils.java b/StringUtils.java new file mode 100644 index 0000000..be1c654 --- /dev/null +++ b/StringUtils.java @@ -0,0 +1,1165 @@ +package be.nikiroo.utils; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.text.Normalizer; +import java.text.Normalizer.Form; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.Map.Entry; +import java.util.regex.Pattern; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +import org.unbescape.html.HtmlEscape; +import org.unbescape.html.HtmlEscapeLevel; +import org.unbescape.html.HtmlEscapeType; + +import be.nikiroo.utils.streams.Base64InputStream; +import be.nikiroo.utils.streams.Base64OutputStream; + +/** + * This class offer some utilities based around {@link String}s. + * + * @author niki + */ +public class StringUtils { + /** + * This enum type will decide the alignment of a {@link String} when padding + * or justification is applied (if there is enough horizontal space for it + * to be aligned). + */ + public enum Alignment { + /** Aligned at left. */ + LEFT, + /** Centered. */ + CENTER, + /** Aligned at right. */ + RIGHT, + /** Full justified (to both left and right). */ + JUSTIFY, + + // Old Deprecated values: + + /** DEPRECATED: please use LEFT. */ + @Deprecated + Beginning, + /** DEPRECATED: please use CENTER. */ + @Deprecated + Center, + /** DEPRECATED: please use RIGHT. */ + @Deprecated + End; + + /** + * Return the non-deprecated version of this enum if needed (or return + * self if not). + * + * @return the non-deprecated value + */ + Alignment undeprecate() { + if (this == Beginning) + return LEFT; + if (this == Center) + return CENTER; + if (this == End) + return RIGHT; + return this; + } + } + + static private Pattern marks = getMarks(); + + /** + * Fix the size of the given {@link String} either with space-padding or by + * shortening it. + * + * @param text + * the {@link String} to fix + * @param width + * the size of the resulting {@link String} or -1 for a noop + * + * @return the resulting {@link String} of size size + */ + static public String padString(String text, int width) { + return padString(text, width, true, null); + } + + /** + * Fix the size of the given {@link String} either with space-padding or by + * optionally shortening it. + * + * @param text + * the {@link String} to fix + * @param width + * the size of the resulting {@link String} if the text fits or + * if cut is TRUE or -1 for a noop + * @param cut + * cut the {@link String} shorter if needed + * @param align + * align the {@link String} in this position if we have enough + * space (default is Alignment.Beginning) + * + * @return the resulting {@link String} of size size minimum + */ + static public String padString(String text, int width, boolean cut, + Alignment align) { + + if (align == null) { + align = Alignment.LEFT; + } + + align = align.undeprecate(); + + if (width >= 0) { + if (text == null) + text = ""; + + int diff = width - text.length(); + + if (diff < 0) { + if (cut) + text = text.substring(0, width); + } else if (diff > 0) { + if (diff < 2 && align != Alignment.RIGHT) + align = Alignment.LEFT; + + switch (align) { + case RIGHT: + text = new String(new char[diff]).replace('\0', ' ') + text; + break; + case CENTER: + int pad1 = (diff) / 2; + int pad2 = (diff + 1) / 2; + text = new String(new char[pad1]).replace('\0', ' ') + text + + new String(new char[pad2]).replace('\0', ' '); + break; + case LEFT: + default: + text = text + new String(new char[diff]).replace('\0', ' '); + break; + } + } + } + + return text; + } + + /** + * Justify a text into width-sized (at the maximum) lines and return all the + * lines concatenated into a single '\\n'-separated line of text. + * + * @param text + * the {@link String} to justify + * @param width + * the maximum size of the resulting lines + * + * @return a list of justified text lines concatenated into a single + * '\\n'-separated line of text + */ + static public String justifyTexts(String text, int width) { + StringBuilder builder = new StringBuilder(); + for (String line : justifyText(text, width, null)) { + if (builder.length() > 0) { + builder.append('\n'); + } + builder.append(line); + } + + return builder.toString(); + } + + /** + * Justify a text into width-sized (at the maximum) lines. + * + * @param text + * the {@link String} to justify + * @param width + * the maximum size of the resulting lines + * + * @return a list of justified text lines + */ + static public List justifyText(String text, int width) { + return justifyText(text, width, null); + } + + /** + * Justify a text into width-sized (at the maximum) lines. + * + * @param text + * the {@link String} to justify + * @param width + * the maximum size of the resulting lines + * @param align + * align the lines in this position (default is + * Alignment.Beginning) + * + * @return a list of justified text lines + */ + static public List justifyText(String text, int width, + Alignment align) { + if (align == null) { + align = Alignment.LEFT; + } + + align = align.undeprecate(); + + switch (align) { + case CENTER: + return StringJustifier.center(text, width); + case RIGHT: + return StringJustifier.right(text, width); + case JUSTIFY: + return StringJustifier.full(text, width); + case LEFT: + default: + return StringJustifier.left(text, width); + } + } + + /** + * Justify a text into width-sized (at the maximum) lines. + * + * @param text + * the {@link String} to justify + * @param width + * the maximum size of the resulting lines + * + * @return a list of justified text lines + */ + static public List justifyText(List text, int width) { + return justifyText(text, width, null); + } + + /** + * Justify a text into width-sized (at the maximum) lines. + * + * @param text + * the {@link String} to justify + * @param width + * the maximum size of the resulting lines + * @param align + * align the lines in this position (default is + * Alignment.Beginning) + * + * @return a list of justified text lines + */ + static public List justifyText(List text, int width, + Alignment align) { + List result = new ArrayList(); + + // Content <-> Bullet spacing (null = no spacing) + List> lines = new ArrayList>(); + StringBuilder previous = null; + StringBuilder tmp = new StringBuilder(); + String previousItemBulletSpacing = null; + String itemBulletSpacing = null; + for (String inputLine : text) { + boolean previousLineComplete = true; + + String current = inputLine.replace("\t", " "); + itemBulletSpacing = getItemSpacing(current); + boolean bullet = isItemLine(current); + if ((previousItemBulletSpacing == null || itemBulletSpacing + .length() <= previousItemBulletSpacing.length()) && !bullet) { + itemBulletSpacing = null; + } + + if (itemBulletSpacing != null) { + current = current.trim(); + if (!current.isEmpty() && bullet) { + current = current.substring(1); + } + current = current.trim(); + previousLineComplete = bullet; + } else { + tmp.setLength(0); + for (String word : current.split(" ")) { + if (word.isEmpty()) { + continue; + } + + if (tmp.length() > 0) { + tmp.append(' '); + } + tmp.append(word.trim()); + } + current = tmp.toString(); + + previousLineComplete = current.isEmpty() + || previousItemBulletSpacing != null + || (previous != null && isFullLine(previous)) + || isHrLine(current) || isHrLine(previous); + } + + if (previous == null) { + previous = new StringBuilder(); + } else { + if (previousLineComplete) { + lines.add(new AbstractMap.SimpleEntry( + previous.toString(), previousItemBulletSpacing)); + previous.setLength(0); + previousItemBulletSpacing = itemBulletSpacing; + } else { + previous.append(' '); + } + } + + previous.append(current); + + } + + if (previous != null) { + lines.add(new AbstractMap.SimpleEntry(previous + .toString(), previousItemBulletSpacing)); + } + + for (Entry line : lines) { + String content = line.getKey(); + String spacing = line.getValue(); + + String bullet = "- "; + if (spacing == null) { + bullet = ""; + spacing = ""; + } + + if (spacing.length() > width + 3) { + spacing = ""; + } + + for (String subline : StringUtils.justifyText(content, width + - (spacing.length() + bullet.length()), align)) { + result.add(spacing + bullet + subline); + if (!bullet.isEmpty()) { + bullet = " "; + } + } + } + + return result; + } + + /** + * Sanitise the given input to make it more Terminal-friendly by removing + * combining characters. + * + * @param input + * the input to sanitise + * @param allowUnicode + * allow Unicode or only allow ASCII Latin characters + * + * @return the sanitised {@link String} + */ + static public String sanitize(String input, boolean allowUnicode) { + return sanitize(input, allowUnicode, !allowUnicode); + } + + /** + * Sanitise the given input to make it more Terminal-friendly by removing + * combining characters. + * + * @param input + * the input to sanitise + * @param allowUnicode + * allow Unicode or only allow ASCII Latin characters + * @param removeAllAccents + * TRUE to replace all accentuated characters by their non + * accentuated counter-parts + * + * @return the sanitised {@link String} + */ + static public String sanitize(String input, boolean allowUnicode, + boolean removeAllAccents) { + + if (removeAllAccents) { + input = Normalizer.normalize(input, Form.NFKD); + if (marks != null) { + input = marks.matcher(input).replaceAll(""); + } + } + + input = Normalizer.normalize(input, Form.NFKC); + + if (!allowUnicode) { + StringBuilder builder = new StringBuilder(); + for (int index = 0; index < input.length(); index++) { + char car = input.charAt(index); + // displayable chars in ASCII are in the range 32<->255, + // except DEL (127) + if (car >= 32 && car <= 255 && car != 127) { + builder.append(car); + } + } + input = builder.toString(); + } + + return input; + } + + /** + * Convert between the time in milliseconds to a {@link String} in a "fixed" + * way (to exchange data over the wire, for instance). + *

+ * Precise to the second. + * + * @param time + * the specified number of milliseconds since the standard base + * time known as "the epoch", namely January 1, 1970, 00:00:00 + * GMT + * + * @return the time as a {@link String} + */ + static public String fromTime(long time) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + return sdf.format(new Date(time)); + } + + /** + * Convert between the time as a {@link String} to milliseconds in a "fixed" + * way (to exchange data over the wire, for instance). + *

+ * Precise to the second. + * + * @param displayTime + * the time as a {@link String} + * + * @return the number of milliseconds since the standard base time known as + * "the epoch", namely January 1, 1970, 00:00:00 GMT, or -1 in case + * of error + * + * @throws ParseException + * in case of parse error + */ + static public long toTime(String displayTime) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + return sdf.parse(displayTime).getTime(); + } + + /** + * Return a hash of the given {@link String}. + * + * @param input + * the input data + * + * @return the hash + * + * @deprecated please use {@link HashUtils} + */ + @Deprecated + static public String getMd5Hash(String input) { + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update(getBytes(input)); + byte byteData[] = md.digest(); + + StringBuffer hexString = new StringBuffer(); + for (int i = 0; i < byteData.length; i++) { + String hex = Integer.toHexString(0xff & byteData[i]); + if (hex.length() == 1) + hexString.append('0'); + hexString.append(hex); + } + + return hexString.toString(); + } catch (NoSuchAlgorithmException e) { + return input; + } + } + + /** + * Remove the HTML content from the given input, and un-html-ize the rest. + * + * @param html + * the HTML-encoded content + * + * @return the HTML-free equivalent content + */ + public static String unhtml(String html) { + StringBuilder builder = new StringBuilder(); + + int inTag = 0; + for (char car : html.toCharArray()) { + if (car == '<') { + inTag++; + } else if (car == '>') { + inTag--; + } else if (inTag <= 0) { + builder.append(car); + } + } + + char nbsp = ' '; // non-breakable space (a special char) + char space = ' '; + return HtmlEscape.unescapeHtml(builder.toString()).replace(nbsp, space); + } + + /** + * Escape the given {@link String} so it can be used in XML, as content. + * + * @param input + * the input {@link String} + * + * @return the escaped {@link String} + */ + public static String xmlEscape(String input) { + if (input == null) { + return ""; + } + + return HtmlEscape.escapeHtml(input, + HtmlEscapeType.HTML4_NAMED_REFERENCES_DEFAULT_TO_HEXA, + HtmlEscapeLevel.LEVEL_1_ONLY_MARKUP_SIGNIFICANT); + } + + /** + * Escape the given {@link String} so it can be used in XML, as text content + * inside double-quotes. + * + * @param input + * the input {@link String} + * + * @return the escaped {@link String} + */ + public static String xmlEscapeQuote(String input) { + if (input == null) { + return ""; + } + + return HtmlEscape.escapeHtml(input, + HtmlEscapeType.HTML4_NAMED_REFERENCES_DEFAULT_TO_HEXA, + HtmlEscapeLevel.LEVEL_1_ONLY_MARKUP_SIGNIFICANT); + } + + /** + * Zip the data and then encode it into Base64. + * + * @param data + * the data + * + * @return the Base64 zipped version + * + * @throws IOException + * in case of I/O error + */ + public static String zip64(String data) throws IOException { + try { + return zip64(getBytes(data)); + } catch (UnsupportedEncodingException e) { + // All conforming JVM are required to support UTF-8 + e.printStackTrace(); + return null; + } + } + + /** + * Zip the data and then encode it into Base64. + * + * @param data + * the data + * + * @return the Base64 zipped version + * + * @throws IOException + * in case of I/O error + */ + public static String zip64(byte[] data) throws IOException { + // 1. compress + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + try { + OutputStream out = new GZIPOutputStream(bout); + try { + out.write(data); + } finally { + out.close(); + } + } finally { + data = bout.toByteArray(); + bout.close(); + } + + // 2. base64 + InputStream in = new ByteArrayInputStream(data); + try { + in = new Base64InputStream(in, true); + return new String(IOUtils.toByteArray(in), "UTF-8"); + } finally { + in.close(); + } + } + + /** + * Unconvert from Base64 then unzip the content, which is assumed to be a + * String. + * + * @param data + * the data in Base64 format + * + * @return the raw data + * + * @throws IOException + * in case of I/O error + */ + public static String unzip64s(String data) throws IOException { + return new String(unzip64(data), "UTF-8"); + } + + /** + * Unconvert from Base64 then unzip the content. + * + * @param data + * the data in Base64 format + * + * @return the raw data + * + * @throws IOException + * in case of I/O error + */ + public static byte[] unzip64(String data) throws IOException { + InputStream in = new Base64InputStream(new ByteArrayInputStream( + getBytes(data)), false); + try { + in = new GZIPInputStream(in); + return IOUtils.toByteArray(in); + } finally { + in.close(); + } + } + + /** + * Convert the given data to Base64 format. + * + * @param data + * the data to convert + * + * @return the Base64 {@link String} representation of the data + * + * @throws IOException + * in case of I/O errors + */ + public static String base64(String data) throws IOException { + return base64(getBytes(data)); + } + + /** + * Convert the given data to Base64 format. + * + * @param data + * the data to convert + * + * @return the Base64 {@link String} representation of the data + * + * @throws IOException + * in case of I/O errors + */ + public static String base64(byte[] data) throws IOException { + Base64InputStream in = new Base64InputStream(new ByteArrayInputStream( + data), true); + try { + return new String(IOUtils.toByteArray(in), "UTF-8"); + } finally { + in.close(); + } + } + + /** + * Unconvert the given data from Base64 format back to a raw array of bytes. + * + * @param data + * the data to unconvert + * + * @return the raw data represented by the given Base64 {@link String}, + * + * @throws IOException + * in case of I/O errors + */ + public static byte[] unbase64(String data) throws IOException { + Base64InputStream in = new Base64InputStream(new ByteArrayInputStream( + getBytes(data)), false); + try { + return IOUtils.toByteArray(in); + } finally { + in.close(); + } + } + + /** + * Unonvert the given data from Base64 format back to a {@link String}. + * + * @param data + * the data to unconvert + * + * @return the {@link String} represented by the given Base64 {@link String} + * + * @throws IOException + * in case of I/O errors + */ + public static String unbase64s(String data) throws IOException { + return new String(unbase64(data), "UTF-8"); + } + + /** + * Return a display {@link String} for the given value, which can be + * suffixed with "k" or "M" depending upon the number, if it is big enough. + *

+ *

+ * Examples: + *

    + *
  • 8 765 becomes "8 k"
  • + *
  • 998 765 becomes "998 k"
  • + *
  • 12 987 364 becomes "12 M"
  • + *
  • 5 534 333 221 becomes "5 G"
  • + *
+ * + * @param value + * the value to convert + * + * @return the display value + */ + public static String formatNumber(long value) { + return formatNumber(value, 0); + } + + /** + * Return a display {@link String} for the given value, which can be + * suffixed with "k" or "M" depending upon the number, if it is big enough. + *

+ * Examples (assuming decimalPositions = 1): + *

    + *
  • 8 765 becomes "8.7 k"
  • + *
  • 998 765 becomes "998.7 k"
  • + *
  • 12 987 364 becomes "12.9 M"
  • + *
  • 5 534 333 221 becomes "5.5 G"
  • + *
+ * + * @param value + * the value to convert + * @param decimalPositions + * the number of decimal positions to keep + * + * @return the display value + */ + public static String formatNumber(long value, int decimalPositions) { + long userValue = value; + String suffix = " "; + long mult = 1; + + if (value >= 1000000000l) { + mult = 1000000000l; + userValue = value / 1000000000l; + suffix = " G"; + } else if (value >= 1000000l) { + mult = 1000000l; + userValue = value / 1000000l; + suffix = " M"; + } else if (value >= 1000l) { + mult = 1000l; + userValue = value / 1000l; + suffix = " k"; + } + + String deci = ""; + if (decimalPositions > 0) { + deci = Long.toString(value % mult); + int size = Long.toString(mult).length() - 1; + while (deci.length() < size) { + deci = "0" + deci; + } + + deci = deci.substring(0, Math.min(decimalPositions, deci.length())); + while (deci.length() < decimalPositions) { + deci += "0"; + } + + deci = "." + deci; + } + + return Long.toString(userValue) + deci + suffix; + } + + /** + * The reverse operation to {@link StringUtils#formatNumber(long)}: it will + * read a "display" number that can contain a "M" or "k" suffix and return + * the full value. + *

+ * Of course, the conversion to and from display form is lossy (example: + * 6870 to "6.5k" to 6500). + * + * @param value + * the value in display form with possible "M" and "k" suffixes, + * can be NULL + * + * @return the value as a number, or 0 if not possible to convert + */ + public static long toNumber(String value) { + return toNumber(value, 0l); + } + + /** + * The reverse operation to {@link StringUtils#formatNumber(long)}: it will + * read a "display" number that can contain a "M" or "k" suffix and return + * the full value. + *

+ * Of course, the conversion to and from display form is lossy (example: + * 6870 to "6.5k" to 6500). + * + * @param value + * the value in display form with possible "M" and "k" suffixes, + * can be NULL + * @param def + * the default value if it is not possible to convert the given + * value to a number + * + * @return the value as a number, or 0 if not possible to convert + */ + public static long toNumber(String value, long def) { + long count = def; + if (value != null) { + value = value.trim().toLowerCase(); + try { + long mult = 1; + if (value.endsWith("g")) { + value = value.substring(0, value.length() - 1).trim(); + mult = 1000000000; + } else if (value.endsWith("m")) { + value = value.substring(0, value.length() - 1).trim(); + mult = 1000000; + } else if (value.endsWith("k")) { + value = value.substring(0, value.length() - 1).trim(); + mult = 1000; + } + + long deci = 0; + if (value.contains(".")) { + String[] tab = value.split("\\."); + if (tab.length != 2) { + throw new NumberFormatException(value); + } + double decimal = Double.parseDouble("0." + + tab[tab.length - 1]); + deci = ((long) (mult * decimal)); + value = tab[0]; + } + count = mult * Long.parseLong(value) + deci; + } catch (Exception e) { + } + } + + return count; + } + + /** + * Return the bytes array representation of the given {@link String} in + * UTF-8. + * + * @param str + * the {@link String} to transform into bytes + * @return the content in bytes + */ + static public byte[] getBytes(String str) { + try { + return str.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + // All conforming JVM must support UTF-8 + e.printStackTrace(); + return null; + } + } + + /** + * The "remove accents" pattern. + * + * @return the pattern, or NULL if a problem happens + */ + private static Pattern getMarks() { + try { + return Pattern + .compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+"); + } catch (Exception e) { + // Can fail on Android... + return null; + } + } + + // + // justify List related: + // + + /** + * Check if this line ends as a complete line (ends with a "." or similar). + *

+ * Note that we consider an empty line as full, and a line ending with + * spaces as not complete. + * + * @param line + * the line to check + * + * @return TRUE if it does + */ + static private boolean isFullLine(StringBuilder line) { + if (line.length() == 0) { + return true; + } + + char lastCar = line.charAt(line.length() - 1); + switch (lastCar) { + case '.': // points + case '?': + case '!': + + case '\'': // quotes + case '‘': + case '’': + + case '"': // double quotes + case '”': + case '“': + case '»': + case '«': + return true; + default: + return false; + } + } + + /** + * Check if this line represent an item in a list or description (i.e., + * check that the first non-space char is "-"). + * + * @param line + * the line to check + * + * @return TRUE if it is + */ + static private boolean isItemLine(String line) { + String spacing = getItemSpacing(line); + return spacing != null && !spacing.isEmpty() + && line.charAt(spacing.length()) == '-'; + } + + /** + * Return all the spaces that start this line (or Empty if none). + * + * @param line + * the line to get the starting spaces from + * + * @return the left spacing + */ + static private String getItemSpacing(String line) { + int i; + for (i = 0; i < line.length(); i++) { + if (line.charAt(i) != ' ') { + return line.substring(0, i); + } + } + + return ""; + } + + /** + * This line is an horizontal spacer line. + * + * @param line + * the line to test + * + * @return TRUE if it is + */ + static private boolean isHrLine(CharSequence line) { + int count = 0; + if (line != null) { + for (int i = 0; i < line.length(); i++) { + char car = line.charAt(i); + if (car == ' ' || car == '\t' || car == '*' || car == '-' + || car == '_' || car == '~' || car == '=' || car == '/' + || car == '\\') { + count++; + } else { + return false; + } + } + } + + return count > 2; + } + + // Deprecated functions, please do not use // + + /** + * @deprecated please use {@link StringUtils#zip64(byte[])} or + * {@link StringUtils#base64(byte[])} instead. + * + * @param data + * the data to encode + * @param zip + * TRUE to zip it before Base64 encoding it, FALSE for Base64 + * encoding only + * + * @return the encoded data + * + * @throws IOException + * in case of I/O error + */ + @Deprecated + public static String base64(String data, boolean zip) throws IOException { + return base64(getBytes(data), zip); + } + + /** + * @deprecated please use {@link StringUtils#zip64(String)} or + * {@link StringUtils#base64(String)} instead. + * + * @param data + * the data to encode + * @param zip + * TRUE to zip it before Base64 encoding it, FALSE for Base64 + * encoding only + * + * @return the encoded data + * + * @throws IOException + * in case of I/O error + */ + @Deprecated + public static String base64(byte[] data, boolean zip) throws IOException { + if (zip) { + return zip64(data); + } + + Base64InputStream b64 = new Base64InputStream(new ByteArrayInputStream( + data), true); + try { + return IOUtils.readSmallStream(b64); + } finally { + b64.close(); + } + } + + /** + * @deprecated please use {@link Base64OutputStream} and + * {@link GZIPOutputStream} instead. + * + * @param breakLines + * NOT USED ANYMORE, it is always considered FALSE now + */ + @Deprecated + public static OutputStream base64(OutputStream data, boolean zip, + boolean breakLines) throws IOException { + OutputStream out = new Base64OutputStream(data); + if (zip) { + out = new java.util.zip.GZIPOutputStream(out); + } + + return out; + } + + /** + * Unconvert the given data from Base64 format back to a raw array of bytes. + *

+ * Will automatically detect zipped data and also uncompress it before + * returning, unless ZIP is false. + * + * @deprecated DO NOT USE ANYMORE (bad perf, will be dropped) + * + * @param data + * the data to unconvert + * @param zip + * TRUE to also uncompress the data from a GZIP format + * automatically; if set to FALSE, zipped data can be returned + * + * @return the raw data represented by the given Base64 {@link String}, + * optionally compressed with GZIP + * + * @throws IOException + * in case of I/O errors + */ + @Deprecated + public static byte[] unbase64(String data, boolean zip) throws IOException { + byte[] buffer = unbase64(data); + if (!zip) { + return buffer; + } + + try { + GZIPInputStream zipped = new GZIPInputStream( + new ByteArrayInputStream(buffer)); + try { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try { + IOUtils.write(zipped, out); + return out.toByteArray(); + } finally { + out.close(); + } + } finally { + zipped.close(); + } + } catch (Exception e) { + return buffer; + } + } + + /** + * Unconvert the given data from Base64 format back to a raw array of bytes. + *

+ * Will automatically detect zipped data and also uncompress it before + * returning, unless ZIP is false. + * + * @deprecated DO NOT USE ANYMORE (bad perf, will be dropped) + * + * @param data + * the data to unconvert + * @param zip + * TRUE to also uncompress the data from a GZIP format + * automatically; if set to FALSE, zipped data can be returned + * + * @return the raw data represented by the given Base64 {@link String}, + * optionally compressed with GZIP + * + * @throws IOException + * in case of I/O errors + */ + @Deprecated + public static InputStream unbase64(InputStream data, boolean zip) + throws IOException { + return new ByteArrayInputStream(unbase64(IOUtils.readSmallStream(data), + zip)); + } + + /** + * @deprecated DO NOT USE ANYMORE (bad perf, will be dropped) + */ + @Deprecated + public static byte[] unbase64(byte[] data, int offset, int count, + boolean zip) throws IOException { + byte[] dataPart = Arrays.copyOfRange(data, offset, offset + count); + return unbase64(new String(dataPart, "UTF-8"), zip); + } + + /** + * @deprecated DO NOT USE ANYMORE (bad perf, will be dropped) + */ + @Deprecated + public static String unbase64s(String data, boolean zip) throws IOException { + return new String(unbase64(data, zip), "UTF-8"); + } + + /** + * @deprecated DO NOT USE ANYMORE (bad perf, will be dropped) + */ + @Deprecated + public static String unbase64s(byte[] data, int offset, int count, + boolean zip) throws IOException { + return new String(unbase64(data, offset, count, zip), "UTF-8"); + } +} diff --git a/TempFiles.java b/TempFiles.java new file mode 100644 index 0000000..b54f0bc --- /dev/null +++ b/TempFiles.java @@ -0,0 +1,187 @@ +package be.nikiroo.utils; + +import java.io.Closeable; +import java.io.File; +import java.io.IOException; + +/** + * A small utility class to generate auto-delete temporary files in a + * centralised location. + * + * @author niki + */ +public class TempFiles implements Closeable { + /** + * Root directory of this instance, owned by it, where all temporary files + * must reside. + */ + protected File root; + + /** + * Create a new {@link TempFiles} -- each instance is separate and have a + * dedicated sub-directory in a shared temporary root. + *

+ * The whole repository will be deleted on close (if you fail to call it, + * the program will try to call it on JVM termination). + * + * @param name + * the instance name (will be part of the final directory + * name) + * + * @throws IOException + * in case of I/O error + */ + public TempFiles(String name) throws IOException { + this(null, name); + } + + /** + * Create a new {@link TempFiles} -- each instance is separate and have a + * dedicated sub-directory in a given temporary root. + *

+ * The whole repository will be deleted on close (if you fail to call it, + * the program will try to call it on JVM termination). + *

+ * Be careful, this instance will own the given root directory, and + * will most probably delete all its files. + * + * @param base + * the root base directory to use for all the temporary files of + * this instance (if NULL, will be the default temporary + * directory of the OS) + * @param name + * the instance name (will be part of the final directory + * name) + * + * @throws IOException + * in case of I/O error + */ + public TempFiles(File base, String name) throws IOException { + if (base == null) { + base = File.createTempFile(".temp", ""); + } + + root = base; + + if (root.exists()) { + IOUtils.deltree(root, true); + } + + root = new File(root.getParentFile(), ".temp"); + root.mkdir(); + if (!root.exists()) { + throw new IOException("Cannot create root directory: " + root); + } + + root.deleteOnExit(); + + root = createTempFile(name); + IOUtils.deltree(root, true); + + root.mkdir(); + if (!root.exists()) { + throw new IOException("Cannot create root subdirectory: " + root); + } + } + + /** + * Create an auto-delete temporary file. + * + * @param name + * a base for the final filename (only a part of said + * filename) + * + * @return the newly created file + * + * @throws IOException + * in case of I/O errors + */ + public synchronized File createTempFile(String name) throws IOException { + name += "_"; + while (name.length() < 3) { + name += "_"; + } + + while (true) { + File tmp = File.createTempFile(name, ""); + IOUtils.deltree(tmp, true); + + File test = new File(root, tmp.getName()); + if (!test.exists()) { + test.createNewFile(); + if (!test.exists()) { + throw new IOException( + "Cannot create temporary file: " + test); + } + + test.deleteOnExit(); + return test; + } + } + } + + /** + * Create an auto-delete temporary directory. + *

+ * Note that creating 2 temporary directories with the same name will result + * in two different directories, even if the final name is the same + * (the absolute path will be different). + * + * @param name + * the actual directory name (not path) + * + * @return the newly created file + * + * @throws IOException + * in case of I/O errors, or if the name was a path instead of a + * name + */ + public synchronized File createTempDir(String name) throws IOException { + File localRoot = createTempFile(name); + IOUtils.deltree(localRoot, true); + + localRoot.mkdir(); + if (!localRoot.exists()) { + throw new IOException("Cannot create subdirectory: " + localRoot); + } + + File dir = new File(localRoot, name); + if (!dir.getName().equals(name)) { + throw new IOException( + "Cannot create temporary directory with a path, only names are allowed: " + + dir); + } + + dir.mkdir(); + dir.deleteOnExit(); + + if (!dir.exists()) { + throw new IOException("Cannot create subdirectory: " + dir); + } + + return dir; + } + + @Override + public synchronized void close() throws IOException { + File root = this.root; + this.root = null; + + if (root != null) { + IOUtils.deltree(root); + + // Since we allocate temp directories from a base point, + // try and remove that base point + root.getParentFile().delete(); // (only works if empty) + } + } + + @Override + protected void finalize() throws Throwable { + try { + close(); + } finally { + super.finalize(); + } + } +} diff --git a/TraceHandler.java b/TraceHandler.java new file mode 100644 index 0000000..0a09712 --- /dev/null +++ b/TraceHandler.java @@ -0,0 +1,156 @@ +package be.nikiroo.utils; + +/** + * A handler when a trace message is sent or when a recoverable exception was + * caught by the program. + * + * @author niki + */ +public class TraceHandler { + private final boolean showErrors; + private final boolean showErrorDetails; + private final int traceLevel; + private final int maxPrintSize; + + /** + * Create a default {@link TraceHandler} that will print errors on stderr + * (without details) and no traces. + */ + public TraceHandler() { + this(true, false, false); + } + + /** + * Create a default {@link TraceHandler}. + * + * @param showErrors + * show errors on stderr + * @param showErrorDetails + * show more details when printing errors + * @param showTraces + * show level 1 traces on stderr, or no traces at all + */ + public TraceHandler(boolean showErrors, boolean showErrorDetails, + boolean showTraces) { + this(showErrors, showErrorDetails, showTraces ? 1 : 0); + } + + /** + * Create a default {@link TraceHandler}. + * + * @param showErrors + * show errors on stderr + * @param showErrorDetails + * show more details when printing errors + * @param traceLevel + * show traces of this level or lower (0 means "no traces", + * higher means more traces) + */ + public TraceHandler(boolean showErrors, boolean showErrorDetails, + int traceLevel) { + this(showErrors, showErrorDetails, traceLevel, -1); + } + + /** + * Create a default {@link TraceHandler}. + * + * @param showErrors + * show errors on stderr + * @param showErrorDetails + * show more details when printing errors + * @param traceLevel + * show traces of this level or lower (0 means "no traces", + * higher means more traces) + * @param maxPrintSize + * the maximum size at which to truncate traces data (or -1 for + * "no limit") + */ + public TraceHandler(boolean showErrors, boolean showErrorDetails, + int traceLevel, int maxPrintSize) { + this.showErrors = showErrors; + this.showErrorDetails = showErrorDetails; + this.traceLevel = Math.max(traceLevel, 0); + this.maxPrintSize = maxPrintSize; + } + + /** + * The trace level of this {@link TraceHandler}. + * + * @return the level + */ + public int getTraceLevel() { + return traceLevel; + } + + /** + * An exception happened, log it. + * + * @param e + * the exception + */ + public void error(Exception e) { + if (showErrors) { + if (showErrorDetails) { + long now = System.currentTimeMillis(); + System.err.print(StringUtils.fromTime(now) + ": "); + e.printStackTrace(); + } else { + error(e.toString()); + } + } + } + + /** + * An error happened, log it. + * + * @param message + * the error message + */ + public void error(String message) { + if (showErrors) { + long now = System.currentTimeMillis(); + System.err.println(StringUtils.fromTime(now) + ": " + message); + } + } + + /** + * A trace happened, show it. + *

+ * By default, will only be effective if {@link TraceHandler#traceLevel} is + * not 0. + *

+ * A call to this method is equivalent to a call to + * {@link TraceHandler#trace(String, int)} with a level of 1. + * + * @param message + * the trace message + */ + public void trace(String message) { + trace(message, 1); + } + + /** + * A trace happened, show it. + *

+ * By default, will only be effective if {@link TraceHandler#traceLevel} is + * not 0 and the level is lower or equal to it. + * + * @param message + * the trace message + * @param level + * the trace level + */ + public void trace(String message, int level) { + if (traceLevel > 0 && level <= traceLevel) { + long now = System.currentTimeMillis(); + System.err.print(StringUtils.fromTime(now) + ": "); + if (maxPrintSize > 0 && message.length() > maxPrintSize) { + + System.err + .println(message.substring(0, maxPrintSize) + "[...]"); + } else { + System.err.println(message); + } + } + } +} diff --git a/VERSION b/VERSION deleted file mode 100644 index 47ae1f7..0000000 --- a/VERSION +++ /dev/null @@ -1 +0,0 @@ -3.1.2-dev diff --git a/Version.java b/Version.java new file mode 100644 index 0000000..269edb6 --- /dev/null +++ b/Version.java @@ -0,0 +1,366 @@ +package be.nikiroo.utils; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * This class describe a program {@link Version}. + * + * @author niki + */ +public class Version implements Comparable { + private String version; + private int major; + private int minor; + private int patch; + private String tag; + private int tagVersion; + + /** + * Create a new, empty {@link Version}. + * + */ + public Version() { + } + + /** + * Create a new {@link Version} with the given values. + * + * @param major + * the major version + * @param minor + * the minor version + * @param patch + * the patch version + */ + public Version(int major, int minor, int patch) { + this(major, minor, patch, null, -1); + } + + /** + * Create a new {@link Version} with the given values. + * + * @param major + * the major version + * @param minor + * the minor version + * @param patch + * the patch version + * @param tag + * a tag name for this version + */ + public Version(int major, int minor, int patch, String tag) { + this(major, minor, patch, tag, -1); + } + + /** + * Create a new {@link Version} with the given values. + * + * @param major + * the major version + * @param minor + * the minor version + * @param patch + * the patch version the patch version + * @param tag + * a tag name for this version + * @param tagVersion + * the version of the tagged version + */ + public Version(int major, int minor, int patch, String tag, int tagVersion) { + if (tagVersion >= 0 && tag == null) { + throw new java.lang.IllegalArgumentException( + "A tag version cannot be used without a tag"); + } + + this.major = major; + this.minor = minor; + this.patch = patch; + this.tag = tag; + this.tagVersion = tagVersion; + + this.version = generateVersion(); + } + + /** + * Create a new {@link Version} with the given value, which must be in the + * form MAJOR.MINOR.PATCH(-TAG(TAG_VERSION)). + * + * @param version + * the version (MAJOR.MINOR.PATCH, + * MAJOR.MINOR.PATCH-TAG or + * MAJOR.MINOR.PATCH-TAGVERSIONTAG) + */ + public Version(String version) { + try { + String[] tab = version.split("\\."); + this.major = Integer.parseInt(tab[0].trim()); + this.minor = Integer.parseInt(tab[1].trim()); + if (tab[2].contains("-")) { + int posInVersion = version.indexOf('.'); + posInVersion = version.indexOf('.', posInVersion + 1); + String rest = version.substring(posInVersion + 1); + + int posInRest = rest.indexOf('-'); + this.patch = Integer.parseInt(rest.substring(0, posInRest) + .trim()); + + posInVersion = version.indexOf('-'); + this.tag = version.substring(posInVersion + 1).trim(); + this.tagVersion = -1; + + StringBuilder str = new StringBuilder(); + while (!tag.isEmpty() && tag.charAt(tag.length() - 1) >= '0' + && tag.charAt(tag.length() - 1) <= '9') { + str.insert(0, tag.charAt(tag.length() - 1)); + tag = tag.substring(0, tag.length() - 1); + } + + if (str.length() > 0) { + this.tagVersion = Integer.parseInt(str.toString()); + } + } else { + this.patch = Integer.parseInt(tab[2].trim()); + this.tag = null; + this.tagVersion = -1; + } + + this.version = generateVersion(); + } catch (Exception e) { + this.major = 0; + this.minor = 0; + this.patch = 0; + this.tag = null; + this.tagVersion = -1; + this.version = null; + } + } + + /** + * The 'major' version. + *

+ * This version should only change when API-incompatible changes are made to + * the program. + * + * @return the major version + */ + public int getMajor() { + return major; + } + + /** + * The 'minor' version. + *

+ * This version should only change when new, backwards-compatible + * functionality has been added to the program. + * + * @return the minor version + */ + public int getMinor() { + return minor; + } + + /** + * The 'patch' version. + *

+ * This version should change when backwards-compatible bugfixes have been + * added to the program. + * + * @return the patch version + */ + public int getPatch() { + return patch; + } + + /** + * A tag name for this version. + * + * @return the tag + */ + public String getTag() { + return tag; + } + + /** + * The version of the tag, or -1 for no version. + * + * @return the tag version + */ + public int getTagVersion() { + return tagVersion; + } + + /** + * Check if this {@link Version} is "empty" (i.e., the version was not + * parse-able or not given). + * + * @return TRUE if it is empty + */ + public boolean isEmpty() { + return version == null; + } + + /** + * Check if we are more recent than the given {@link Version}. + *

+ * Note that a tagged version is considered newer than a non-tagged version, + * but two tagged versions with different tags are not comparable. + *

+ * Also, an empty version is always considered older. + * + * @param o + * the other {@link Version} + * @return TRUE if this {@link Version} is more recent than the given one + */ + public boolean isNewerThan(Version o) { + if (isEmpty()) { + return false; + } else if (o.isEmpty()) { + return true; + } + + if (major > o.major) { + return true; + } + + if (major == o.major && minor > o.minor) { + return true; + } + + if (major == o.major && minor == o.minor && patch > o.patch) { + return true; + } + + // a tagged version is considered newer than a non-tagged one + if (major == o.major && minor == o.minor && patch == o.patch + && tag != null && o.tag == null) { + return true; + } + + // 2 <> tagged versions are not comparable + boolean sameTag = (tag == null && o.tag == null) + || (tag != null && tag.equals(o.tag)); + if (major == o.major && minor == o.minor && patch == o.patch && sameTag + && tagVersion > o.tagVersion) { + return true; + } + + return false; + } + + /** + * Check if we are older than the given {@link Version}. + *

+ * Note that a tagged version is considered newer than a non-tagged version, + * but two tagged versions with different tags are not comparable. + *

+ * Also, an empty version is always considered older. + * + * @param o + * the other {@link Version} + * @return TRUE if this {@link Version} is older than the given one + */ + public boolean isOlderThan(Version o) { + if (o.isEmpty()) { + return false; + } else if (isEmpty()) { + return true; + } + + // 2 <> tagged versions are not comparable + boolean sameTag = (tag == null && o.tag == null) + || (tag != null && tag.equals(o.tag)); + if (major == o.major && minor == o.minor && patch == o.patch + && !sameTag) { + return false; + } + + return !equals(o) && !isNewerThan(o); + } + + /** + * Return the version of the running program if it follows the VERSION + * convention (i.e., if it has a file called VERSION containing the version + * as a {@link String} in its binary root, and if this {@link String} + * follows the Major/Minor/Patch convention). + *

+ * If it does not, return an empty {@link Version} object. + * + * @return the {@link Version} of the program, or an empty {@link Version} + * (does not return NULL) + */ + public static Version getCurrentVersion() { + String version = null; + + InputStream in = IOUtils.openResource("VERSION"); + if (in != null) { + try { + ByteArrayOutputStream ba = new ByteArrayOutputStream(); + IOUtils.write(in, ba); + in.close(); + + version = ba.toString("UTF-8").trim(); + } catch (IOException e) { + } + } + + return new Version(version); + } + + @Override + public int compareTo(Version o) { + if (equals(o)) { + return 0; + } else if (isNewerThan(o)) { + return 1; + } else { + return -1; + } + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof Version) { + Version o = (Version) obj; + if (isEmpty()) { + return o.isEmpty(); + } + + boolean sameTag = (tag == null && o.tag == null) + || (tag != null && tag.equals(o.tag)); + return o.major == major && o.minor == minor && o.patch == patch + && sameTag && o.tagVersion == tagVersion; + } + + return false; + } + + @Override + public int hashCode() { + return version == null ? 0 : version.hashCode(); + } + + /** + * Return a user-readable form of this {@link Version}. + */ + @Override + public String toString() { + return version == null ? "[unknown]" : version; + } + + /** + * Generate the clean version {@link String} from the current values. + * + * @return the clean version string + */ + private String generateVersion() { + String tagSuffix = ""; + if (tag != null) { + tagSuffix = "-" + tag + + (tagVersion >= 0 ? Integer.toString(tagVersion) : ""); + } + + return String.format("%d.%d.%d%s", major, minor, patch, tagSuffix); + } +} diff --git a/VersionCheck.java b/VersionCheck.java new file mode 100644 index 0000000..0e16c2b --- /dev/null +++ b/VersionCheck.java @@ -0,0 +1,172 @@ +package be.nikiroo.utils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +/** + * Version checker: can check the current version of the program against a + * remote changelog, and list the missed updates and their description. + * + * @author niki + */ +public class VersionCheck { + private static final String base = "https://github.com/${PROJECT}/raw/master/changelog${LANG}.md"; + private static Downloader downloader = new Downloader(null); + + private Version current; + private List newer; + private Map> changes; + + /** + * Create a new {@link VersionCheck}. + * + * @param current + * the current version of the program + * @param newer + * the list of available {@link Version}s newer the current one + * @param changes + * the list of changes + */ + private VersionCheck(Version current, List newer, + Map> changes) { + this.current = current; + this.newer = newer; + this.changes = changes; + } + + /** + * Check if there are more recent {@link Version}s of this program + * available. + * + * @return TRUE if there is at least one + */ + public boolean isNewVersionAvailable() { + return !newer.isEmpty(); + } + + /** + * The current {@link Version} of the program. + * + * @return the current {@link Version} + */ + public Version getCurrentVersion() { + return current; + } + + /** + * The list of available {@link Version}s newer than the current one. + * + * @return the newer {@link Version}s + */ + public List getNewer() { + return newer; + } + + /** + * The list of changes for each available {@link Version} newer than the + * current one. + * + * @return the list of changes + */ + public Map> getChanges() { + return changes; + } + + /** + * Check if there are available {@link Version}s of this program more recent + * than the current one. + * + * @param githubProject + * the GitHub project to check on, for instance "nikiroo/fanfix" + * @param lang + * the current locale, so we can try to get the changelog in the + * correct language (can be NULL, will fetch the default + * changelog) + * + * @return a {@link VersionCheck} + * + * @throws IOException + * in case of I/O error + */ + public static VersionCheck check(String githubProject, Locale lang) + throws IOException { + Version current = Version.getCurrentVersion(); + List newer = new ArrayList(); + Map> changes = new HashMap>(); + + // Use the right project: + String base = VersionCheck.base.replace("${PROJECT}", githubProject); + + // Prepare the URLs according to the user's language (we take here + // "-fr_BE" as an example): + String fr = lang == null ? "" : "-" + lang.getLanguage(); + String BE = lang == null ? "" + : "_" + lang.getCountry().replace(".UTF8", ""); + String urlFrBE = base.replace("${LANG}", fr + BE); + String urlFr = base.replace("${LANG}", "-" + fr); + String urlDefault = base.replace("${LANG}", ""); + + InputStream in = null; + for (String url : new String[] { urlFrBE, urlFr, urlDefault }) { + try { + in = downloader.open(new URL(url), false); + break; + } catch (IOException e) { + } + } + + if (in == null) { + throw new IOException("No changelog found"); + } + + BufferedReader reader = new BufferedReader( + new InputStreamReader(in, "UTF-8")); + try { + Version version = new Version(); + for (String line = reader.readLine(); line != null; line = reader + .readLine()) { + if (line.startsWith("## Version ")) { + version = new Version( + line.substring("## Version ".length())); + if (version.isNewerThan(current)) { + newer.add(version); + changes.put(version, new ArrayList()); + } else { + version = new Version(); + } + } else if (!version.isEmpty() && !newer.isEmpty() + && !line.isEmpty()) { + List ch = changes.get(newer.get(newer.size() - 1)); + if (!ch.isEmpty() && !line.startsWith("- ")) { + int i = ch.size() - 1; + ch.set(i, ch.get(i) + " " + line.trim()); + } else { + ch.add(line.substring("- ".length()).trim()); + } + } + } + } finally { + reader.close(); + } + + return new VersionCheck(current, newer, changes); + } + + @Override + public String toString() { + return String.format( + "Version checker: version [%s], %d releases behind latest version [%s]", // + current, // + newer.size(), // + newer.isEmpty() ? current : newer.get(newer.size() - 1)// + ); + } +} diff --git a/android/ImageUtilsAndroid.class b/android/ImageUtilsAndroid.class new file mode 100644 index 0000000..844712a Binary files /dev/null and b/android/ImageUtilsAndroid.class differ diff --git a/android/ImageUtilsAndroid.java b/android/ImageUtilsAndroid.java new file mode 100644 index 0000000..c2e269c --- /dev/null +++ b/android/ImageUtilsAndroid.java @@ -0,0 +1,99 @@ +package be.nikiroo.utils.android; + +import android.graphics.Bitmap; +import android.graphics.Bitmap.Config; +import android.graphics.BitmapFactory; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.stream.Stream; + +import be.nikiroo.utils.Image; +import be.nikiroo.utils.ImageUtils; +import be.nikiroo.utils.StringUtils; + +/** + * This class offer some utilities based around images and uses the Android + * framework. + * + * @author niki + */ +public class ImageUtilsAndroid extends ImageUtils { + @Override + protected boolean check() { + // If we can get the class, it means we have access to it + Config c = Config.ALPHA_8; + return true; + } + + @Override + public void saveAsImage(Image img, File target, String format) + throws IOException { + FileOutputStream fos = new FileOutputStream(target); + try { + Bitmap image = fromImage(img); + + boolean ok = false; + try { + ok = image.compress( + Bitmap.CompressFormat.valueOf(format.toUpperCase()), + 90, fos); + } catch (Exception e) { + ok = false; + } + + // Some formats are not reliable + // Second chance: PNG + if (!ok && !format.equals("png")) { + ok = image.compress(Bitmap.CompressFormat.PNG, 90, fos); + } + + if (!ok) { + throw new IOException( + "Cannot find a writer for this image and format: " + + format); + } + } catch (IOException e) { + throw new IOException("Cannot write image to " + target, e); + } finally { + fos.close(); + } + } + + /** + * Convert the given {@link Image} into a {@link Bitmap} object. + * + * @param img + * the {@link Image} + * @return the {@link Image} object + * @throws IOException + * in case of IO error + */ + static public Bitmap fromImage(Image img) throws IOException { + InputStream stream = img.newInputStream(); + try { + Bitmap image = BitmapFactory.decodeStream(stream); + if (image == null) { + String extra = ""; + if (img.getSize() <= 2048) { + try { + extra = ", content: " + + new String(img.getData(), "UTF-8"); + } catch (Exception e) { + extra = ", content unavailable"; + } + } + String ssize = StringUtils.formatNumber(img.getSize()); + throw new IOException( + "Failed to convert input to image, size was: " + ssize + + extra); + } + + return image; + } finally { + stream.close(); + } + } +} diff --git a/android/test/TestAndroid.class b/android/test/TestAndroid.class new file mode 100644 index 0000000..216aa20 Binary files /dev/null and b/android/test/TestAndroid.class differ diff --git a/android/test/TestAndroid.java b/android/test/TestAndroid.java new file mode 100644 index 0000000..2ded4e1 --- /dev/null +++ b/android/test/TestAndroid.java @@ -0,0 +1,7 @@ +package be.nikiroo.utils.android.test; + +import be.nikiroo.utils.android.ImageUtilsAndroid; + +public class TestAndroid { + ImageUtilsAndroid a = new ImageUtilsAndroid(); +} diff --git a/bundles/Config.java b/bundles/Config.java deleted file mode 100644 index 86744b4..0000000 --- a/bundles/Config.java +++ /dev/null @@ -1,188 +0,0 @@ -package be.nikiroo.fanfix.bundles; - -import be.nikiroo.utils.resources.Meta; -import be.nikiroo.utils.resources.Meta.Format; - -/** - * The configuration options. - * - * @author niki - */ -@SuppressWarnings("javadoc") -public enum Config { - - // Note: all hidden values are subject to be removed in a later version - - @Meta(description = "The language to use for in the program (example: en-GB, fr-BE...) or nothing for default system language (can be overwritten with the variable $LANG)",// - format = Format.LOCALE, list = { "en-GB", "fr-BE" }) - LANG, // - @Meta(description = "File format options",// - group = true) - FILE_FORMAT, // - @Meta(description = "How to save non-images documents in the library",// - format = Format.FIXED_LIST, list = { "INFO_TEXT", "EPUB", "HTML", "TEXT" }, def = "INFO_TEXT") - FILE_FORMAT_NON_IMAGES_DOCUMENT_TYPE, // - @Meta(description = "How to save images documents in the library",// - format = Format.FIXED_LIST, list = { "CBZ", "HTML" }, def = "CBZ") - FILE_FORMAT_IMAGES_DOCUMENT_TYPE, // - @Meta(description = "How to save cover images",// - format = Format.FIXED_LIST, list = { "PNG", "JPG", "BMP" }, def = "PNG") - FILE_FORMAT_IMAGE_FORMAT_COVER, // - @Meta(description = "How to save content images",// - format = Format.FIXED_LIST, list = { "PNG", "JPG", "BMP" }, def = "JPG") - FILE_FORMAT_IMAGE_FORMAT_CONTENT, // - - @Meta(description = "Cache management",// - group = true) - CACHE, // - @Meta(description = "The directory where to store temporary files; any relative path uses the applciation config directory as base, $HOME notation is supported, / is always accepted as directory separator",// - format = Format.DIRECTORY, def = "tmp/") - CACHE_DIR, // - @Meta(description = "The delay in hours after which a cached resource that is thought to change ~often is considered too old and triggers a refresh delay (or 0 for no cache, or -1 for infinite time)", // - format = Format.INT, def = "24") - CACHE_MAX_TIME_CHANGING, // - @Meta(description = "The delay in hours after which a cached resource that is thought to change rarely is considered too old and triggers a refresh delay (or 0 for no cache, or -1 for infinite time)", // - format = Format.INT, def = "720") - CACHE_MAX_TIME_STABLE, // - - @Meta(description = "The directory where to get the default story covers; any relative path uses the applciation config directory as base, $HOME notation is supported, / is always accepted as directory separator",// - format = Format.DIRECTORY, def = "covers/") - DEFAULT_COVERS_DIR, // - @Meta(description = "The directory where to store the library (can be overriden by the environment variable \"BOOKS_DIR\"; any relative path uses the applciation config directory as base, $HOME notation is supported, / is always accepted as directory separator",// - format = Format.DIRECTORY, def = "$HOME/Books/") - LIBRARY_DIR, // - - @Meta(description = "Remote library\nA remote library can be configured to fetch the stories from a remote Fanfix server",// - group = true) - REMOTE_LIBRARY, // - @Meta(description = "Use the remote Fanfix server configured here instead of the local library (if FALSE, the local library will be used instead)",// - format = Format.BOOLEAN, def = "false") - REMOTE_LIBRARY_ENABLED, // - @Meta(description = "The remote Fanfix server to connect to (fanfix://, http://, https:// -- if not specified, fanfix:// is assumed)",// - format = Format.STRING) - REMOTE_LIBRARY_HOST, // - @Meta(description = "The port to use for the remote Fanfix server",// - format = Format.INT, def = "58365") - REMOTE_LIBRARY_PORT, // - @Meta(description = "The key is structured: \"KEY|SUBKEY|wl|rw\"\n- \"KEY\" is the actual encryption key (it can actually be empty, which will still encrypt the messages but of course it will be easier to guess the key)\n- \"SUBKEY\" is the (optional) subkey to use to get additional privileges\n- \"wl\" is a special privilege that allows that subkey to ignore white lists\n- \"rw\" is a special privilege that allows that subkey to modify the library, even if it is not in RW (by default) mode\n\nSome examples:\n- \"super-secret\": a normal key, no special privileges\n- \"you-will-not-guess|azOpd8|wl\": a white-list ignoring key\n- \"new-password|subpass|rw\": a key that allows modifications on the library",// - format = Format.PASSWORD) - REMOTE_LIBRARY_KEY, // - - @Meta(description = "Network configuration",// - group = true) - NETWORK, // - @Meta(description = "The user-agent to use to download files",// - def = "Mozilla/5.0 (X11; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0 -- ELinks/0.9.3 (Linux 2.6.11 i686; 80x24) -- Fanfix (https://github.com/nikiroo/fanfix/)") - NETWORK_USER_AGENT, // - @Meta(description = "The proxy server to use under the format 'user:pass@proxy:port', 'user@proxy:port', 'proxy:port' or ':' alone (system proxy); an empty String means no proxy",// - format = Format.STRING, def = "") - NETWORK_PROXY, // - @Meta(description = "If the last update check was done at least that many days ago, check for updates at startup (-1 for 'no checks')", // - format = Format.INT, def = "1") - NETWORK_UPDATE_INTERVAL, // - - @Meta(description = "Remote Server configuration\nNote that the key is structured: \"KEY|SUBKEY|wl|rw\"\n- \"KEY\" is the actual encryption key (it can actually be empty, which will still encrypt the messages but of course it will be easier to guess the key)\n- \"SUBKEY\" is the (optional) subkey to use to get additional privileges\n- \"wl\" is a special privilege that allows that subkey to ignore white lists\n- \"rw\" is a special privilege that allows that subkey to modify the library, even if it is not in RW (by default) mode\n\nSome examples:\n- \"super-secret\": a normal key, no special privileges\n- \"you-will-not-guess|azOpd8|wl\": a white-list ignoring key\n- \"new-password|subpass|rw\": a key that allows modifications on the library",// - group = true) - SERVER, // - @Meta(description = "Remote Server mode: you can use the fanfix protocol (which is encrypted), http (which is not) or https (which requires a keystore.jks file)",// - format = Format.FIXED_LIST, list = { "fanfix", "http", "https" }, def = "fanfix") - SERVER_MODE, - @Meta(description = "The port on which we can start the server (must be a valid port, from 1 to 65535)", // - format = Format.INT, def = "58365") - SERVER_PORT, // - @Meta(description = "A keystore.jks file, required to use HTTPS (the server will refuse to start in HTTPS mode without this file)", // - format = Format.STRING, def = "") - SERVER_SSL_KEYSTORE, - @Meta(description = "The pass phrase required to open the keystore.jks file (required for HTTPS mode)", // - format = Format.PASSWORD, def = "") - SERVER_SSL_KEYSTORE_PASS, - @Meta(description = "The encryption key for the server (NOT including a subkey), it cannot contain the pipe character \"|\" but can be empty -- is used to encrypt the traffic in fanfix mode (even if empty, traffic will be encrypted in fanfix mode), and used as a password for HTTP (clear text protocol) and HTTPS modes",// - format = Format.PASSWORD, def = "") - SERVER_KEY, // - @Meta(description = "Allow write access to the clients (download story, move story...) without RW subkeys", // - format = Format.BOOLEAN, def = "true") - SERVER_RW, // - @Meta(description = "If not empty, only the EXACT listed sources will be available for clients without a WL subkey",// - array = true, format = Format.STRING, def = "") - SERVER_WHITELIST, // - @Meta(description = "Those sources will not be available for clients without a BL subkey",// - array = true, format = Format.STRING, def = "") - SERVER_BLACKLIST, // - @Meta(description = "The subkeys that the server will allow, including the modes\nA subkey is used as a login for HTTP (clear text protocol) and HTTPS modes", // - array = true, format = Format.STRING, def = "") - SERVER_ALLOWED_SUBKEYS, // - @Meta(description = "The maximum size of the cache, in MegaBytes, for HTTP and HTTPS servers", // - format = Format.INT, def = "100") - SERVER_MAX_CACHE_MB, - - @Meta(description = "DEBUG options",// - group = true) - DEBUG, // - @Meta(description = "Show debug information on errors",// - format = Format.BOOLEAN, def = "false") - DEBUG_ERR, // - @Meta(description = "Show debug trace information",// - format = Format.BOOLEAN, def = "false") - DEBUG_TRACE, // - - @Meta(description = "Internal configuration\nThose options are internal to the program and should probably not be changed",// - hidden = true, group = true) - CONF, // - @Meta(description = "LaTeX configuration",// - hidden = true, group = true) - CONF_LATEX_LANG, // - @Meta(description = "LaTeX output language (full name) for \"English\"",// - hidden = true, format = Format.STRING, def = "english") - CONF_LATEX_LANG_EN, // - @Meta(description = "LaTeX output language (full name) for \"French\"",// - hidden = true, format = Format.STRING, def = "french") - CONF_LATEX_LANG_FR, // - @Meta(description = "other 'by' prefixes before author name, used to identify the author",// - hidden = true, array = true, format = Format.STRING, def = "\"by\",\"par\",\"de\",\"©\",\"(c)\"") - CONF_BYS, // - @Meta(description = "List of languages codes used for chapter identification (should not be changed)", // - hidden = true, array = true, format = Format.STRING, def = "\"EN\",\"FR\"") - CONF_CHAPTER, // - @Meta(description = "Chapter identification string in English, used to identify a starting chapter in text mode",// - hidden = true, format = Format.STRING, def = "Chapter") - CONF_CHAPTER_EN, // - @Meta(description = "Chapter identification string in French, used to identify a starting chapter in text mode",// - hidden = true, format = Format.STRING, def = "Chapitre") - CONF_CHAPTER_FR, // - - @Meta(description = "YiffStar/SoFurry credentials\nYou can give your YiffStar credentials here to have access to all the stories, though it should not be necessary anymore (some stories used to beblocked for anonymous viewers)",// - group = true) - LOGIN_YIFFSTAR, // - @Meta(description = "Your YiffStar/SoFurry login",// - format = Format.STRING) - LOGIN_YIFFSTAR_USER, // - @Meta(description = "Your YiffStar/SoFurry password",// - format = Format.PASSWORD) - LOGIN_YIFFSTAR_PASS, // - - @Meta(description = "FimFiction APIKEY credentials\nFimFiction can be queried via an API, but requires an API key to do that. One has been created for this program, but if you have another API key you can set it here. You can also set a login and password instead, in that case, a new API key will be generated (and stored) if you still haven't set one.",// - group = true) - LOGIN_FIMFICTION_APIKEY, // - @Meta(description = "The login of the API key used to create a new token from FimFiction", // - format = Format.STRING) - LOGIN_FIMFICTION_APIKEY_CLIENT_ID, // - @Meta(description = "The password of the API key used to create a new token from FimFiction", // - format = Format.PASSWORD) - LOGIN_FIMFICTION_APIKEY_CLIENT_SECRET, // - @Meta(description = "Do not use the new API, even if we have a token, and force HTML scraping",// - format = Format.BOOLEAN, def = "false") - LOGIN_FIMFICTION_APIKEY_FORCE_HTML, // - @Meta(description = "The token required to use the beta APIv2 from FimFiction (see APIKEY_CLIENT_* if you want to generate a new one from your own API key)", // - format = Format.PASSWORD, def = "Bearer WnZ5oHlzQoDocv1GcgHfcoqctHkSwL-D") - LOGIN_FIMFICTION_APIKEY_TOKEN, // - - @Meta(description = "e621.net credentials\nYou can give your e621.net credentials here to have access to all the comics and ignore the default blacklist",// - group = true) - LOGIN_E621, // - @Meta(description = "Your e621.net login",// - format = Format.STRING) - LOGIN_E621_LOGIN, // - @Meta(description = "Your e621.net API KEY",// - format = Format.PASSWORD) - LOGIN_E621_APIKEY, // -} diff --git a/bundles/ConfigBundle.java b/bundles/ConfigBundle.java deleted file mode 100644 index ce72b3d..0000000 --- a/bundles/ConfigBundle.java +++ /dev/null @@ -1,41 +0,0 @@ -package be.nikiroo.fanfix.bundles; - -import java.io.File; -import java.io.IOException; - -import be.nikiroo.utils.resources.Bundle; - -/** - * This class manages the configuration of the application. - * - * @author niki - */ -public class ConfigBundle extends Bundle { - /** - * Create a new {@link ConfigBundle}. - */ - public ConfigBundle() { - super(Config.class, Target.config5, null); - } - - /** - * Update resource file. - * - * @param args - * not used - * - * @throws IOException - * in case of I/O error - */ - public static void main(String[] args) throws IOException { - String path = new File(".").getAbsolutePath() - + "/src/be/nikiroo/fanfix/bundles/"; - new ConfigBundle().updateFile(path); - System.out.println("Path updated: " + path); - } - - @Override - protected String getBundleDisplayName() { - return "Configuration options"; - } -} diff --git a/bundles/StringId.java b/bundles/StringId.java deleted file mode 100644 index 9772248..0000000 --- a/bundles/StringId.java +++ /dev/null @@ -1,151 +0,0 @@ -package be.nikiroo.fanfix.bundles; - -import java.io.IOException; -import java.io.Writer; - -import be.nikiroo.utils.resources.Bundle; -import be.nikiroo.utils.resources.Meta; - -/** - * The {@link Enum} representing textual information to be translated to the - * user as a key. - * - * Note that each key that should be translated must be annotated with a - * {@link Meta} annotation. - * - * @author niki - */ -@SuppressWarnings("javadoc") -public enum StringId { - /** - * A special key used for technical reasons only, without annotations so it - * is not visible in .properties files. - *

- * Use it when you need NO translation. - */ - NULL, // - /** - * A special key used for technical reasons only, without annotations so it - * is not visible in .properties files. - *

- * Use it when you need a real translation but still don't have a key. - */ - DUMMY, // - @Meta(info = "%s = supported input, %s = supported output", description = "help message for the syntax") - HELP_SYNTAX, // - @Meta(description = "syntax error message") - ERR_SYNTAX, // - @Meta(info = "%s = support name, %s = support desc", description = "an input or output support type description") - ERR_SYNTAX_TYPE, // - @Meta(info = "%s = input string", description = "Error when retrieving data") - ERR_LOADING, // - @Meta(info = "%s = save target", description = "Error when saving to given target") - ERR_SAVING, // - @Meta(info = "%s = bad output format", description = "Error when unknown output format") - ERR_BAD_OUTPUT_TYPE, // - @Meta(info = "%s = input string", description = "Error when converting input to URL/File") - ERR_BAD_URL, // - @Meta(info = "%s = input url", description = "URL/File not supported") - ERR_NOT_SUPPORTED, // - @Meta(info = "%s = cover URL", description = "Failed to download cover : %s") - ERR_BS_NO_COVER, // - @Meta(def = "`", info = "single char", description = "Canonical OPEN SINGLE QUOTE char (for instance: ‘)") - OPEN_SINGLE_QUOTE, // - @Meta(def = "‘", info = "single char", description = "Canonical CLOSE SINGLE QUOTE char (for instance: ’)") - CLOSE_SINGLE_QUOTE, // - @Meta(def = "“", info = "single char", description = "Canonical OPEN DOUBLE QUOTE char (for instance: “)") - OPEN_DOUBLE_QUOTE, // - @Meta(def = "”", info = "single char", description = "Canonical CLOSE DOUBLE QUOTE char (for instance: ”)") - CLOSE_DOUBLE_QUOTE, // - @Meta(def = "Description", description = "Name of the description fake chapter") - DESCRIPTION, // - @Meta(def = "Chapter %d: %s", info = "%d = number, %s = name", description = "Name of a chapter with a name") - CHAPTER_NAMED, // - @Meta(def = "Chapter %d", info = "%d = number, %s = name", description = "Name of a chapter without name") - CHAPTER_UNNAMED, // - @Meta(info = "%s = type", description = "Default description when the type is not known by i18n") - INPUT_DESC, // - @Meta(description = "Description of this input type") - INPUT_DESC_EPUB, // - @Meta(description = "Description of this input type") - INPUT_DESC_TEXT, // - @Meta(description = "Description of this input type") - INPUT_DESC_INFO_TEXT, // - @Meta(description = "Description of this input type") - INPUT_DESC_FANFICTION, // - @Meta(description = "Description of this input type") - INPUT_DESC_FIMFICTION, // - @Meta(description = "Description of this input type") - INPUT_DESC_MANGAFOX, // - @Meta(description = "Description of this input type") - INPUT_DESC_E621, // - @Meta(description = "Description of this input type") - INPUT_DESC_E_HENTAI, // - @Meta(description = "Description of this input type") - INPUT_DESC_YIFFSTAR, // - @Meta(description = "Description of this input type") - INPUT_DESC_CBZ, // - @Meta(description = "Description of this input type") - INPUT_DESC_HTML, // - @Meta(info = "%s = type", description = "Default description when the type is not known by i18n") - OUTPUT_DESC, // - @Meta(description = "Description of this output type") - OUTPUT_DESC_EPUB, // - @Meta(description = "Description of this output type") - OUTPUT_DESC_TEXT, // - @Meta(description = "Description of this output type") - OUTPUT_DESC_INFO_TEXT, // - @Meta(description = "Description of this output type") - OUTPUT_DESC_CBZ, // - @Meta(description = "Description of this output type") - OUTPUT_DESC_HTML, // - @Meta(description = "Description of this output type") - OUTPUT_DESC_LATEX, // - @Meta(description = "Description of this output type") - OUTPUT_DESC_SYSOUT, // - @Meta(group = true, info = "%s = type", description = "Default description when the type is not known by i18n") - OUTPUT_DESC_SHORT, // - @Meta(description = "Short description of this output type") - OUTPUT_DESC_SHORT_EPUB, // - @Meta(description = "Short description of this output type") - OUTPUT_DESC_SHORT_TEXT, // - @Meta(description = "Short description of this output type") - OUTPUT_DESC_SHORT_INFO_TEXT, // - @Meta(description = "Short description of this output type") - OUTPUT_DESC_SHORT_CBZ, // - @Meta(description = "Short description of this output type") - OUTPUT_DESC_SHORT_LATEX, // - @Meta(description = "Short description of this output type") - OUTPUT_DESC_SHORT_SYSOUT, // - @Meta(description = "Short description of this output type") - OUTPUT_DESC_SHORT_HTML, // - @Meta(info = "%s = the unknown 2-code language", description = "Error message for unknown 2-letter LaTeX language code") - LATEX_LANG_UNKNOWN, // - @Meta(def = "by", description = "'by' prefix before author name used to output the author, make sure it is covered by Config.BYS for input detection") - BY, // - - ; - - /** - * Write the header found in the configuration .properties file of - * this {@link Bundle}. - * - * @param writer - * the {@link Writer} to write the header in - * @param name - * the file name - * - * @throws IOException - * in case of IO error - */ - static public void writeHeader(Writer writer, String name) - throws IOException { - writer.write("# " + name + " translation file (UTF-8)\n"); - writer.write("# \n"); - writer.write("# Note that any key can be doubled with a _NOUTF suffix\n"); - writer.write("# to use when the NOUTF env variable is set to 1\n"); - writer.write("# \n"); - writer.write("# Also, the comments always refer to the key below them.\n"); - writer.write("# \n"); - } -} diff --git a/bundles/StringIdBundle.java b/bundles/StringIdBundle.java deleted file mode 100644 index b9a0d79..0000000 --- a/bundles/StringIdBundle.java +++ /dev/null @@ -1,40 +0,0 @@ -package be.nikiroo.fanfix.bundles; - -import java.io.File; -import java.io.IOException; - -import be.nikiroo.utils.resources.TransBundle; - -/** - * This class manages the translation resources of the application (Core). - * - * @author niki - */ -public class StringIdBundle extends TransBundle { - /** - * Create a translation service for the given language (will fall back to - * the default one if not found). - * - * @param lang - * the language to use - */ - public StringIdBundle(String lang) { - super(StringId.class, Target.resources_core, lang); - } - - /** - * Update resource file. - * - * @param args - * not used - * - * @throws IOException - * in case of I/O error - */ - public static void main(String[] args) throws IOException { - String path = new File(".").getAbsolutePath() - + "/src/be/nikiroo/fanfix/bundles/"; - new StringIdBundle(null).updateFile(path); - System.out.println("Path updated: " + path); - } -} diff --git a/bundles/StringIdGui.java b/bundles/StringIdGui.java deleted file mode 100644 index c109f42..0000000 --- a/bundles/StringIdGui.java +++ /dev/null @@ -1,199 +0,0 @@ -package be.nikiroo.fanfix.bundles; - -import java.io.IOException; -import java.io.Writer; - -import be.nikiroo.utils.resources.Bundle; -import be.nikiroo.utils.resources.Meta; -import be.nikiroo.utils.resources.Meta.Format; - -/** - * The {@link Enum} representing textual information to be translated to the - * user as a key. - * - * Note that each key that should be translated must be annotated with a - * {@link Meta} annotation. - * - * @author niki - */ -@SuppressWarnings("javadoc") -public enum StringIdGui { - /** - * A special key used for technical reasons only, without annotations so it - * is not visible in .properties files. - *

- * Use it when you need NO translation. - */ - NULL, // - /** - * A special key used for technical reasons only, without annotations so it - * is not visible in .properties files. - *

- * Use it when you need a real translation but still don't have a key. - */ - DUMMY, // - @Meta(def = "Fanfix %s", format = Format.STRING, description = "the title of the main window of Fanfix, the library", info = "%s = current Fanfix version") - // The titles/subtitles: - TITLE_LIBRARY, // - @Meta(def = "Fanfix %s", format = Format.STRING, description = "the title of the main window of Fanfix, the library, when the library has a name (i.e., is not local)", info = "%s = current Fanfix version, %s = library name") - TITLE_LIBRARY_WITH_NAME, // - @Meta(def = "Fanfix Configuration", format = Format.STRING, description = "the title of the configuration window of Fanfix, also the name of the menu button") - TITLE_CONFIG, // - @Meta(def = "This is where you configure the options of the program.", format = Format.STRING, description = "the subtitle of the configuration window of Fanfix") - SUBTITLE_CONFIG, // - @Meta(def = "UI Configuration", format = Format.STRING, description = "the title of the UI configuration window of Fanfix, also the name of the menu button") - TITLE_CONFIG_UI, // - @Meta(def = "This is where you configure the graphical appearence of the program.", format = Format.STRING, description = "the subtitle of the UI configuration window of Fanfix") - SUBTITLE_CONFIG_UI, // - @Meta(def = "Save", format = Format.STRING, description = "the title of the 'save to/export to' window of Fanfix") - TITLE_SAVE, // - @Meta(def = "Moving story", format = Format.STRING, description = "the title of the 'move to' window of Fanfix") - TITLE_MOVE_TO, // - @Meta(def = "Move to:", format = Format.STRING, description = "the subtitle of the 'move to' window of Fanfix") - SUBTITLE_MOVE_TO, // - @Meta(def = "Delete story", format = Format.STRING, description = "the title of the 'delete' window of Fanfix") - TITLE_DELETE, // - @Meta(def = "Delete %s: %s", format = Format.STRING, description = "the subtitle of the 'delete' window of Fanfix", info = "%s = LUID of the story, %s = title of the story") - SUBTITLE_DELETE, // - @Meta(def = "Library error", format = Format.STRING, description = "the title of the 'library error' dialogue") - TITLE_ERROR_LIBRARY, // - @Meta(def = "Importing from URL", format = Format.STRING, description = "the title of the 'import URL' dialogue") - TITLE_IMPORT_URL, // - @Meta(def = "URL of the story to import:", format = Format.STRING, description = "the subtitle of the 'import URL' dialogue") - SUBTITLE_IMPORT_URL, // - @Meta(def = "Error", format = Format.STRING, description = "the title of general error dialogues") - TITLE_ERROR, // - @Meta(def = "%s: %s", format = Format.STRING, description = "the title of a story for the properties dialogue, the viewers...", info = "%s = LUID of the story, %s = title of the story") - TITLE_STORY, // - - // - - @Meta(def = "A new version of the program is available at %s", format = Format.STRING, description = "HTML text used to notify of a new version", info = "%s = url link in HTML") - NEW_VERSION_AVAILABLE, // - @Meta(def = "Updates available", format = Format.STRING, description = "text used as title for the update dialogue") - NEW_VERSION_TITLE, // - @Meta(def = "Version %s", format = Format.STRING, description = "HTML text used to specify a newer version title and number, used for each version newer than the current one", info = "%s = the newer version number") - NEW_VERSION_VERSION, // - @Meta(def = "%s words", format = Format.STRING, description = "show the number of words of a book", info = "%s = the number") - BOOK_COUNT_WORDS, // - @Meta(def = "%s images", format = Format.STRING, description = "show the number of images of a book", info = "%s = the number") - BOOK_COUNT_IMAGES, // - @Meta(def = "%s stories", format = Format.STRING, description = "show the number of stories of a meta-book (a book representing allthe types/sources or all the authors present)", info = "%s = the number") - BOOK_COUNT_STORIES, // - - // Menu (and popup) items: - - @Meta(def = "File", format = Format.STRING, description = "the file menu") - MENU_FILE, // - @Meta(def = "Exit", format = Format.STRING, description = "the file/exit menu button") - MENU_FILE_EXIT, // - @Meta(def = "Import File...", format = Format.STRING, description = "the file/import_file menu button") - MENU_FILE_IMPORT_FILE, // - @Meta(def = "Import URL...", format = Format.STRING, description = "the file/import_url menu button") - MENU_FILE_IMPORT_URL, // - @Meta(def = "Save as...", format = Format.STRING, description = "the file/export menu button") - MENU_FILE_EXPORT, // - @Meta(def = "Move to", format = Format.STRING, description = "the file/move to menu button") - MENU_FILE_MOVE_TO, // - @Meta(def = "Set author", format = Format.STRING, description = "the file/set author menu button") - MENU_FILE_SET_AUTHOR, // - @Meta(def = "New source...", format = Format.STRING, description = "the file/move to/new type-source menu button, that will trigger a dialogue to create a new type/source") - MENU_FILE_MOVE_TO_NEW_TYPE, // - @Meta(def = "New author...", format = Format.STRING, description = "the file/move to/new author menu button, that will trigger a dialogue to create a new author") - MENU_FILE_MOVE_TO_NEW_AUTHOR, // - @Meta(def = "Rename...", format = Format.STRING, description = "the file/rename menu item, that will trigger a dialogue to ask for a new title for the story") - MENU_FILE_RENAME, // - @Meta(def = "Properties", format = Format.STRING, description = "the file/Properties menu item, that will trigger a dialogue to show the properties of the story") - MENU_FILE_PROPERTIES, // - @Meta(def = "Open", format = Format.STRING, description = "the file/open menu item, that will open the story or fake-story (an author or a source/type)") - MENU_FILE_OPEN, // - @Meta(def = "Edit", format = Format.STRING, description = "the edit menu") - MENU_EDIT, // - @Meta(def = "Prefetch to cache", format = Format.STRING, description = "the edit/send to cache menu button, to download the story into the cache if not already done") - MENU_EDIT_DOWNLOAD_TO_CACHE, // - @Meta(def = "Clear cache", format = Format.STRING, description = "the clear cache menu button, to clear the cache for a single book") - MENU_EDIT_CLEAR_CACHE, // - @Meta(def = "Redownload", format = Format.STRING, description = "the edit/redownload menu button, to download the latest version of the book") - MENU_EDIT_REDOWNLOAD, // - @Meta(def = "Delete", format = Format.STRING, description = "the edit/delete menu button") - MENU_EDIT_DELETE, // - @Meta(def = "Set as cover for source", format = Format.STRING, description = "the edit/Set as cover for source menu button") - MENU_EDIT_SET_COVER_FOR_SOURCE, // - @Meta(def = "Set as cover for author", format = Format.STRING, description = "the edit/Set as cover for author menu button") - MENU_EDIT_SET_COVER_FOR_AUTHOR, // - @Meta(def = "Search", format = Format.STRING, description = "the search menu to open the earch stories on one of the searchable websites") - MENU_SEARCH, - @Meta(def = "View", format = Format.STRING, description = "the view menu") - MENU_VIEW, // - @Meta(def = "Word count", format = Format.STRING, description = "the view/word_count menu button, to show the word/image/story count as secondary info") - MENU_VIEW_WCOUNT, // - @Meta(def = "Author", format = Format.STRING, description = "the view/author menu button, to show the author as secondary info") - MENU_VIEW_AUTHOR, // - @Meta(def = "Sources", format = Format.STRING, description = "the sources menu, to select the books from a specific source; also used as a title for the source books") - MENU_SOURCES, // - @Meta(def = "Authors", format = Format.STRING, description = "the authors menu, to select the books of a specific author; also used as a title for the author books") - MENU_AUTHORS, // - @Meta(def = "Options", format = Format.STRING, description = "the options menu, to configure Fanfix from the GUI") - MENU_OPTIONS, // - @Meta(def = "All", format = Format.STRING, description = "a special menu button to select all the sources/types or authors, by group (one book = one group)") - MENU_XXX_ALL_GROUPED, // - @Meta(def = "Listing", format = Format.STRING, description = "a special menu button to select all the sources/types or authors, in a listing (all the included books are listed, grouped by source/type or author)") - MENU_XXX_ALL_LISTING, // - @Meta(def = "[unknown]", format = Format.STRING, description = "a special menu button to select the books without author") - MENU_AUTHORS_UNKNOWN, // - - // Progress names - @Meta(def = "Reload books", format = Format.STRING, description = "progress bar caption for the 'reload books' step of all outOfUi operations") - PROGRESS_OUT_OF_UI_RELOAD_BOOKS, // - @Meta(def = "Change the source of the book to %s", format = Format.STRING, description = "progress bar caption for the 'change source' step of the ReDownload operation", info = "%s = new source name") - PROGRESS_CHANGE_SOURCE, // - - // Error messages - @Meta(def = "An error occured when contacting the library", format = Format.STRING, description = "default description if the error is not known") - ERROR_LIB_STATUS, // - @Meta(def = "You are not allowed to access this library", format = Format.STRING, description = "library access not allowed") - ERROR_LIB_STATUS_UNAUTHORIZED, // - @Meta(def = "Library not valid", format = Format.STRING, description = "the library is invalid (not correctly set up)") - ERROR_LIB_STATUS_INVALID, // - @Meta(def = "Library currently unavailable", format = Format.STRING, description = "the library is out of commission") - ERROR_LIB_STATUS_UNAVAILABLE, // - @Meta(def = "Cannot open the selected book", format = Format.STRING, description = "cannot open the book, internal or external viewer") - ERROR_CANNOT_OPEN, // - @Meta(def = "URL not supported: %s", format = Format.STRING, description = "URL is not supported by Fanfix", info = "%s = URL") - ERROR_URL_NOT_SUPPORTED, // - @Meta(def = "Failed to import %s:\n%s", format = Format.STRING, description = "cannot import the URL", info = "%s = URL, %s = reasons") - ERROR_URL_IMPORT_FAILED, - - // Others - @Meta(def = "  Chapitre %d / %d", format = Format.STRING, description = "(html) the chapter progression value used on the viewers", info = "%d = chapter number, %d = total chapters") - CHAPTER_HTML_UNNAMED, // - @Meta(def = "  Chapitre %d / %d: %s", format = Format.STRING, description = "(html) the chapter progression value used on the viewers", info = "%d = chapter number, %d = total chapters, %s = chapter name") - CHAPTER_HTML_NAMED, // - @Meta(def = "Image %d / %d", format = Format.STRING, description = "(NO html) the chapter progression value used on the viewers", info = "%d = current image number, %d = total images") - IMAGE_PROGRESSION, // - - ; - - /** - * Write the header found in the configuration .properties file of - * this {@link Bundle}. - * - * @param writer - * the {@link Writer} to write the header in - * @param name - * the file name - * - * @throws IOException - * in case of IO error - */ - static public void writeHeader(Writer writer, String name) - throws IOException { - writer.write("# " + name + " translation file (UTF-8)\n"); - writer.write("# \n"); - writer.write("# Note that any key can be doubled with a _NOUTF suffix\n"); - writer.write("# to use when the NOUTF env variable is set to 1\n"); - writer.write("# \n"); - writer.write("# Also, the comments always refer to the key below them.\n"); - writer.write("# \n"); - } -} diff --git a/bundles/StringIdGuiBundle.java b/bundles/StringIdGuiBundle.java deleted file mode 100644 index c036381..0000000 --- a/bundles/StringIdGuiBundle.java +++ /dev/null @@ -1,40 +0,0 @@ -package be.nikiroo.fanfix.bundles; - -import java.io.File; -import java.io.IOException; - -import be.nikiroo.utils.resources.TransBundle; - -/** - * This class manages the translation resources of the application (GUI). - * - * @author niki - */ -public class StringIdGuiBundle extends TransBundle { - /** - * Create a translation service for the given language (will fall back to - * the default one if not found). - * - * @param lang - * the language to use - */ - public StringIdGuiBundle(String lang) { - super(StringIdGui.class, Target.resources_gui, lang); - } - - /** - * Update resource file. - * - * @param args - * not used - * - * @throws IOException - * in case of I/O error - */ - public static void main(String[] args) throws IOException { - String path = new File(".").getAbsolutePath() - + "/src/be/nikiroo/fanfix/bundles/"; - new StringIdGuiBundle(null).updateFile(path); - System.out.println("Path updated: " + path); - } -} diff --git a/bundles/Target.java b/bundles/Target.java deleted file mode 100644 index 64284c6..0000000 --- a/bundles/Target.java +++ /dev/null @@ -1,27 +0,0 @@ -package be.nikiroo.fanfix.bundles; - -import be.nikiroo.utils.resources.Bundle; - -/** - * The type of configuration information the associated {@link Bundle} will - * convey. - *

- * Those values can change when the file is not compatible anymore. - * - * @author niki - */ -public enum Target { - /** - * Configuration options that the user can change in the - * .properties file - */ - config5, - /** Translation resources (Core) */ - resources_core, - /** Translation resources (GUI) */ - resources_gui, - /** UI resources (from colours to behaviour) */ - ui, - /** Description of UI resources. */ - ui_description, -} diff --git a/bundles/UiConfig.java b/bundles/UiConfig.java deleted file mode 100644 index 2122ccf..0000000 --- a/bundles/UiConfig.java +++ /dev/null @@ -1,59 +0,0 @@ -package be.nikiroo.fanfix.bundles; - -import be.nikiroo.utils.resources.Meta; -import be.nikiroo.utils.resources.Meta.Format; - -/** - * The configuration options. - * - * @author niki - */ -@SuppressWarnings("javadoc") -public enum UiConfig { - @Meta(description = "The directory where to store temporary files for the GUI reader; any relative path uses the applciation config directory as base, $HOME notation is supported, / is always accepted as directory separator",// - format = Format.DIRECTORY, def = "tmp-reader/") - CACHE_DIR_LOCAL_READER, // - @Meta(description = "How to save the cached stories for the GUI Reader (non-images documents) -- those files will be sent to the reader",// - format = Format.COMBO_LIST, list = { "INFO_TEXT", "EPUB", "HTML", "TEXT" }, def = "EPUB") - GUI_NON_IMAGES_DOCUMENT_TYPE, // - @Meta(description = "How to save the cached stories for the GUI Reader (images documents) -- those files will be sent to the reader",// - format = Format.COMBO_LIST, list = { "CBZ", "HTML" }, def = "CBZ") - GUI_IMAGES_DOCUMENT_TYPE, // - @Meta(description = "Use the internal reader for images documents",// - format = Format.BOOLEAN, def = "true") - IMAGES_DOCUMENT_USE_INTERNAL_READER, // - @Meta(description = "The external viewer for images documents (or empty to use the system default program for the given file type)",// - format = Format.STRING) - IMAGES_DOCUMENT_READER, // - @Meta(description = "Use the internal reader for non-images documents",// - format = Format.BOOLEAN, def = "true") - NON_IMAGES_DOCUMENT_USE_INTERNAL_READER, // - @Meta(description = "The external viewer for non-images documents (or empty to use the system default program for the given file type)",// - format = Format.STRING) - NON_IMAGES_DOCUMENT_READER, // - @Meta(description = "The icon to use for the program",// - format = Format.FIXED_LIST, def = "default", list = { "default", "alternative", "magic-book", "pony-book", "pony-library" }) - PROGRAM_ICON, // - // - // GUI settings (hidden in config) - // - @Meta(description = "Show the side panel by default",// - hidden = true, format = Format.BOOLEAN, def = "true") - SHOW_SIDE_PANEL, // - @Meta(description = "Show the details panel by default",// - hidden = true, format = Format.BOOLEAN, def = "true") - SHOW_DETAILS_PANEL, // - @Meta(description = "Show thumbnails by default in the books view",// - hidden = true, format = Format.BOOLEAN, def = "false") - SHOW_THUMBNAILS, // - @Meta(description = "Show a words/images count instead of the author by default in the books view",// - hidden = true, format = Format.BOOLEAN, def = "false") - SHOW_WORDCOUNT, // - // - // Deprecated - // - @Meta(description = "The background colour of the library if you don't like the default system one",// - hidden = true, format = Format.COLOR) - @Deprecated - BACKGROUND_COLOR, // -} diff --git a/bundles/UiConfigBundle.java b/bundles/UiConfigBundle.java deleted file mode 100644 index 8b2c008..0000000 --- a/bundles/UiConfigBundle.java +++ /dev/null @@ -1,39 +0,0 @@ -package be.nikiroo.fanfix.bundles; - -import java.io.File; -import java.io.IOException; - -import be.nikiroo.utils.resources.Bundle; - -/** - * This class manages the configuration of UI of the application (colours and - * behaviour) - * - * @author niki - */ -public class UiConfigBundle extends Bundle { - public UiConfigBundle() { - super(UiConfig.class, Target.ui, new UiConfigBundleDesc()); - } - - /** - * Update resource file. - * - * @param args - * not used - * - * @throws IOException - * in case of I/O error - */ - public static void main(String[] args) throws IOException { - String path = new File(".").getAbsolutePath() - + "/src/be/nikiroo/fanfix/bundles/"; - new UiConfigBundle().updateFile(path); - System.out.println("Path updated: " + path); - } - - @Override - protected String getBundleDisplayName() { - return "UI configuration options"; - } -} diff --git a/bundles/UiConfigBundleDesc.java b/bundles/UiConfigBundleDesc.java deleted file mode 100644 index da42950..0000000 --- a/bundles/UiConfigBundleDesc.java +++ /dev/null @@ -1,39 +0,0 @@ -package be.nikiroo.fanfix.bundles; - -import java.io.File; -import java.io.IOException; - -import be.nikiroo.utils.resources.TransBundle; - -/** - * This class manages the configuration of UI of the application (colours and - * behaviour) - * - * @author niki - */ -public class UiConfigBundleDesc extends TransBundle { - public UiConfigBundleDesc() { - super(UiConfig.class, Target.ui_description); - } - - /** - * Update resource file. - * - * @param args - * not used - * - * @throws IOException - * in case of I/O error - */ - public static void main(String[] args) throws IOException { - String path = new File(".").getAbsolutePath() - + "/src/be/nikiroo/fanfix/bundles/"; - new UiConfigBundleDesc().updateFile(path); - System.out.println("Path updated: " + path); - } - - @Override - protected String getBundleDisplayName() { - return "UI configuration options description"; - } -} diff --git a/bundles/package-info.java b/bundles/package-info.java deleted file mode 100644 index 80cdd15..0000000 --- a/bundles/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * This package encloses the different - * {@link be.nikiroo.utils.resources.Bundle} and their associated - * {@link java.lang.Enum}s used by the application. - * - * @author niki - */ -package be.nikiroo.fanfix.bundles; \ No newline at end of file diff --git a/bundles/resources_core.properties b/bundles/resources_core.properties deleted file mode 100644 index dc7881a..0000000 --- a/bundles/resources_core.properties +++ /dev/null @@ -1,207 +0,0 @@ -# United Kingdom (en_GB) resources_core translation file (UTF-8) -# -# Note that any key can be doubled with a _NOUTF suffix -# to use when the NOUTF env variable is set to 1 -# -# Also, the comments always refer to the key below them. -# - - -# help message for the syntax -# (FORMAT: STRING) -HELP_SYNTAX = Valid options:\n\ -\t--import [URL]: import into library\n\ -\t--export [id] [output_type] [target]: export story to target\n\ -\t--convert [URL] [output_type] [target] (+info): convert URL into target\n\ -\t--read [id] ([chapter number]): read the given story from the library\n\ -\t--read-url [URL] ([chapter number]): convert on the fly and read the \n\ -\t\tstory, without saving it\n\ -\t--search WEBSITE [free text] ([page] ([item])): search for the given terms, show the\n\ -\t\tgiven page (page 0 means "how many page do we have", starts at page 1)\n\ -\t--search-tag WEBSITE ([tag 1] [tag2...] ([page] ([item]))): list the known tags or \n\ -\t\tsearch the stories for the given tag(s), show the given page of results\n\ -\t--search: list the supported websites (where)\n\ -\t--search [where] [keywords] (page [page]) (item [item]): search on the supported \n\ -\t\twebsite and display the given results page of stories it found, or the story \n\ -\t\tdetails if asked\n\ -\t--search-tag [where]: list all the tags supported by this website\n\ -\t--search-tag [index 1]... (page [page]) (item [item]): search for the given stories or \n\ -\t\tsubtags, tag by tag, and display information about a specific page of results or \n\ -\t\tabout a specific item if requested\n\ -\t--list ([type]) : list the stories present in the library\n\ -\t--set-source [id] [new source]: change the source of the given story\n\ -\t--set-title [id] [new title]: change the title of the given story\n\ -\t--set-author [id] [new author]: change the author of the given story\n\ -\t--set-reader [reader type]: set the reader type to CLI, TUI or GUI for \n\ -\t\tthis command\n\ -\t--server: start the server mode (see config file for parameters)\n\ -\t--stop-server: stop the remote server running on this port\n\ -\t\tif any (key must be set to the same value)\n\ -\t--remote [key] [host] [port]: select this remote server to get \n\ -\t\t(or update or...) the stories from (key must be set to the \n\ -\t\tsame value)\n\ -\t--help: this help message\n\ -\t--version: return the version of the program\n\ -\n\ -Supported input types:\n\ -%s\n\ -\n\ -Supported output types:\n\ -%s -# syntax error message -# (FORMAT: STRING) -ERR_SYNTAX = Syntax error (try "--help") -# an input or output support type description -# (FORMAT: STRING) -ERR_SYNTAX_TYPE = > %s: %s -# Error when retrieving data -# (FORMAT: STRING) -ERR_LOADING = Error when retrieving data from: %s -# Error when saving to given target -# (FORMAT: STRING) -ERR_SAVING = Error when saving to target: %s -# Error when unknown output format -# (FORMAT: STRING) -ERR_BAD_OUTPUT_TYPE = Unknown output type: %s -# Error when converting input to URL/File -# (FORMAT: STRING) -ERR_BAD_URL = Cannot understand file or protocol: %s -# URL/File not supported -# (FORMAT: STRING) -ERR_NOT_SUPPORTED = URL not supported: %s -# Failed to download cover : %s -# (FORMAT: STRING) -ERR_BS_NO_COVER = Failed to download cover: %s -# Canonical OPEN SINGLE QUOTE char (for instance: ‘) -# (FORMAT: STRING) -OPEN_SINGLE_QUOTE = ‘ -# Canonical CLOSE SINGLE QUOTE char (for instance: ’) -# (FORMAT: STRING) -CLOSE_SINGLE_QUOTE = ’ -# Canonical OPEN DOUBLE QUOTE char (for instance: “) -# (FORMAT: STRING) -OPEN_DOUBLE_QUOTE = “ -# Canonical CLOSE DOUBLE QUOTE char (for instance: ”) -# (FORMAT: STRING) -CLOSE_DOUBLE_QUOTE = ” -# Name of the description fake chapter -# (FORMAT: STRING) -DESCRIPTION = Description -# Name of a chapter with a name -# (FORMAT: STRING) -CHAPTER_NAMED = Chapter %d: %s -# Name of a chapter without name -# (FORMAT: STRING) -CHAPTER_UNNAMED = Chapter %d -# Default description when the type is not known by i18n -# (FORMAT: STRING) -INPUT_DESC = Unknown type: %s -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_EPUB = EPUB files created by this program (we do not support "all" EPUB files) -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_TEXT = Stories encoded in textual format, with a few rules :\n\ -\tthe title must be on the first line, \n\ -\tthe author (preceded by nothing, "by " or "©") must be on the second \n\ -\t\tline, possibly with the publication date in parenthesis\n\ -\t\t(i.e., "By Unknown (3rd October 1998)"), \n\ -\tchapters must be declared with "Chapter x" or "Chapter x: NAME OF THE \n\ -\t\tCHAPTER", where "x" is the chapter number,\n\ -\ta description of the story must be given as chapter number 0,\n\ -\ta cover image may be present with the same filename but a PNG, \n\ -\t\tJPEG or JPG extension. -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_INFO_TEXT = Contains the same information as the TEXT format, but with a \n\ -\tcompanion ".info" file to store some metadata -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_FANFICTION = Fanfictions of many, many different universes, from TV shows to \n\ -\tnovels to games. -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_FIMFICTION = Fanfictions devoted to the My Little Pony show -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_MANGAHUB = A well filled repository of mangas, in English -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_E621 = Furry website supporting comics, including MLP -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_E_HENTAI = Website offering many comics/mangas, mostly but not always NSFW -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_YIFFSTAR = A Furry website, story-oriented -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_CBZ = CBZ files coming from this very program -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_HTML = HTML files coming from this very program -# Default description when the type is not known by i18n -# (FORMAT: STRING) -OUTPUT_DESC = Unknown type: %s -# Description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_EPUB = Standard EPUB file working on most e-book readers and viewers -# Description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_TEXT = Local stories encoded in textual format, with a few rules :\n\ -\tthe title must be on the first line, \n\ -\tthe author (preceded by nothing, "by " or "©") must be on the second \n\ -\t\tline, possibly with the publication date in parenthesis \n\ -\t\t(i.e., "By Unknown (3rd October 1998)"), \n\ -\tchapters must be declared with "Chapter x" or "Chapter x: NAME OF THE \n\ -\t\tCHAPTER", where "x" is the chapter number,\n\ -\ta description of the story must be given as chapter number 0,\n\ -\ta cover image may be present with the same filename but a PNG, JPEG \n\ -\t\tor JPG extension. -# Description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_INFO_TEXT = Contains the same information as the TEXT format, but with a \n\ -\tcompanion ".info" file to store some metadata -# Description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_CBZ = CBZ file (basically a ZIP file containing images -- we store the images \n\ -\tin PNG format by default) -# Description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_HTML = HTML files (a directory containing the resources and "index.html") -# Description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_LATEX = A LaTeX file using the "book" template -# Description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SYSOUT = A simple DEBUG console output -# Default description when the type is not known by i18n -# This item is used as a group, its content is not expected to be used. -OUTPUT_DESC_SHORT = %s -# Short description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SHORT_EPUB = Electronic book (.epub) -# Short description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SHORT_TEXT = Plain text (.txt) -# Short description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SHORT_INFO_TEXT = Plain text and metadata -# Short description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SHORT_CBZ = Comic book (.cbz) -# Short description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SHORT_LATEX = LaTeX (.tex) -# Short description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SHORT_SYSOUT = Console output -# Short description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SHORT_HTML = HTML files with resources (directory, .html) -# Error message for unknown 2-letter LaTeX language code -# (FORMAT: STRING) -LATEX_LANG_UNKNOWN = Unknown language: %s -# 'by' prefix before author name used to output the author, make sure it is covered by Config.BYS for input detection -# (FORMAT: STRING) -BY = by diff --git a/bundles/resources_core_fr.properties b/bundles/resources_core_fr.properties deleted file mode 100644 index a64a5a0..0000000 --- a/bundles/resources_core_fr.properties +++ /dev/null @@ -1,192 +0,0 @@ -# français (fr) resources_core translation file (UTF-8) -# -# Note that any key can be doubled with a _NOUTF suffix -# to use when the NOUTF env variable is set to 1 -# -# Also, the comments always refer to the key below them. -# - - -# help message for the syntax -# (FORMAT: STRING) -HELP_SYNTAX = Options reconnues :\n\ -\t--import [URL]: importer une histoire dans la librairie\n\ -\t--export [id] [output_type] [target]: exporter l'histoire "id" vers le fichier donné\n\ -\t--convert [URL] [output_type] [target] (+info): convertir l'histoire vers le fichier donné, et forcer l'ajout d'un fichier .info si +info est utilisé\n\ -\t--read [id] ([chapter number]): afficher l'histoire "id"\n\ -\t--read-url [URL] ([chapter number]): convertir l'histoire et la lire à la volée, sans la sauver\n\ -\t--search: liste les sites supportés (where)\n\ -\t--search [where] [keywords] (page [page]) (item [item]): lance une recherche et \n\ -\t\taffiche les résultats de la page page (page 1 par défaut), et de l'item item \n\ -\t\tspécifique si demandé\n\ -\t--search-tag [where]: liste tous les tags supportés par ce site web\n\ -\t--search-tag [index 1]... (page [page]) (item [item]): affine la recherche, tag par tag,\n\ -\t\tet affiche si besoin les sous-tags, les histoires ou les infos précises de \n\ -\t\tl'histoire demandée\n\ -\t--list ([type]): lister les histoires presentes dans la librairie et leurs IDs\n\ -\t--set-source [id] [nouvelle source]: change la source de l'histoire\n\ -\t--set-title [id] [nouveau titre]: change le titre de l'histoire\n\ -\t--set-author [id] [nouvel auteur]: change l'auteur de l'histoire\n\ -\t--set-reader [reader type]: changer le type de lecteur pour la commande en cours sur CLI, TUI ou GUI\n\ -\t--server: démarre le mode serveur (les paramètres sont dans le fichier de config)\n\ -\t--stop-server: arrêter le serveur distant sur ce port (key doit avoir la même valeur) \n\ -\t--remote [key] [host] [port]: contacter ce server au lieu de la librairie habituelle (key doit avoir la même valeur)\n\ -\t--help: afficher la liste des options disponibles\n\ -\t--version: retourne la version du programme\n\ -\n\ -Types supportés en entrée :\n\ -%s\n\ -\n\ -Types supportés en sortie :\n\ -%s -# syntax error message -# (FORMAT: STRING) -ERR_SYNTAX = Erreur de syntaxe (essayez "--help") -# an input or output support type description -# (FORMAT: STRING) -ERR_SYNTAX_TYPE = > %s : %s -# Error when retrieving data -# (FORMAT: STRING) -ERR_LOADING = Erreur de récupération des données depuis : %s -# Error when saving to given target -# (FORMAT: STRING) -ERR_SAVING = Erreur lors de la sauvegarde sur : %s -# Error when unknown output format -# (FORMAT: STRING) -ERR_BAD_OUTPUT_TYPE = Type de sortie inconnu : %s -# Error when converting input to URL/File -# (FORMAT: STRING) -ERR_BAD_URL = Protocole ou type de fichier inconnu : %s -# URL/File not supported -# (FORMAT: STRING) -ERR_NOT_SUPPORTED = Site web non supporté : %s -# Failed to download cover : %s -# (FORMAT: STRING) -ERR_BS_NO_COVER = Échec de la récupération de la page de couverture : %s -# Canonical OPEN SINGLE QUOTE char (for instance: ‘) -# (FORMAT: STRING) -OPEN_SINGLE_QUOTE = ‘ -# Canonical CLOSE SINGLE QUOTE char (for instance: ’) -# (FORMAT: STRING) -CLOSE_SINGLE_QUOTE = ’ -# Canonical OPEN DOUBLE QUOTE char (for instance: “) -# (FORMAT: STRING) -OPEN_DOUBLE_QUOTE = “ -# Canonical CLOSE DOUBLE QUOTE char (for instance: ”) -# (FORMAT: STRING) -CLOSE_DOUBLE_QUOTE = ” -# Name of the description fake chapter -# (FORMAT: STRING) -DESCRIPTION = Description -# Name of a chapter with a name -# (FORMAT: STRING) -CHAPTER_NAMED = Chapitre %d : %s -# Name of a chapter without name -# (FORMAT: STRING) -CHAPTER_UNNAMED = Chapitre %d -# Default description when the type is not known by i18n -# (FORMAT: STRING) -INPUT_DESC = Type d'entrée inconnu : %s -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_EPUB = Les fichiers .epub créés avec Fanfix (nous ne supportons pas les autres fichiers .epub, du moins pour le moment) -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_TEXT = Les histoires enregistrées en texte (.txt), avec quelques règles spécifiques : \n\ -\tle titre doit être sur la première ligne\n\ -\tl'auteur (précédé de rien, "Par ", "De " ou "©") doit être sur la deuxième ligne, optionnellement suivi de la date de publication entre parenthèses (i.e., "Par Quelqu'un (3 octobre 1998)")\n\ -\tles chapitres doivent être déclarés avec "Chapitre x" ou "Chapitre x: NOM DU CHAPTITRE", où "x" est le numéro du chapitre\n\ -\tune description de l'histoire doit être donnée en tant que chaptire 0\n\ -\tune image de couverture peut être présente avec le même nom de fichier que l'histoire, mais une extension .png, .jpeg ou .jpg -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_INFO_TEXT = Fort proche du format texte, mais avec un fichier .info accompagnant l'histoire pour y enregistrer quelques metadata (le fichier de metadata est supposé être créé par Fanfix, ou être compatible avec) -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_FANFICTION = Fanfictions venant d'une multitude d'univers différents, depuis les shows télévisés aux livres en passant par les jeux-vidéos -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_FIMFICTION = Fanfictions dévouées à la série My Little Pony -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_MANGAHUB = Un site répertoriant une quantité non négligeable de mangas, en anglais -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_E621 = Un site Furry proposant des comics, y compris de MLP -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_E_HENTAI = Un site web proposant beaucoup de comics/mangas, souvent mais pas toujours NSFW -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_YIFFSTAR = Un site web Furry, orienté sur les histoires plutôt que les images -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_CBZ = Les fichiers .cbz (une collection d'images zipées), de préférence créés avec Fanfix (même si les autres .cbz sont aussi supportés, mais sans la majorité des metadata de Fanfix dans ce cas) -# Description of this input type -# (FORMAT: STRING) -INPUT_DESC_HTML = Les fichiers HTML que vous pouvez ouvrir avec n'importe quel navigateur ; remarquez que Fanfix créera un répertoire pour y mettre les fichiers nécessaires, dont un fichier "index.html" pour afficher le tout -- nous ne supportons en entrée que les fichiers HTML créés par Fanfix -# Default description when the type is not known by i18n -# (FORMAT: STRING) -OUTPUT_DESC = Type de sortie inconnu : %s -# Description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_EPUB = Standard EPUB file working on most e-book readers and viewers -# Description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_TEXT = Local stories encoded in textual format, with a few rules :\n\ -\tthe title must be on the first line, \n\ -\tthe author (preceded by nothing, "by " or "©") must be on the second \n\ -\t\tline, possibly with the publication date in parenthesis \n\ -\t\t(i.e., "By Unknown (3rd October 1998)"), \n\ -\tchapters must be declared with "Chapter x" or "Chapter x: NAME OF THE \n\ -\t\tCHAPTER", where "x" is the chapter number,\n\ -\ta description of the story must be given as chapter number 0,\n\ -\ta cover image may be present with the same filename but a PNG, JPEG \n\ -\t\tor JPG extension. -# Description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_INFO_TEXT = Contains the same information as the TEXT format, but with a \n\ -\tcompanion ".info" file to store some metadata -# Description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_CBZ = CBZ file (basically a ZIP file containing images -- we store the images \n\ -\tin PNG format by default) -# Description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_HTML = HTML files (a directory containing the resources and "index.html") -# Description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_LATEX = A LaTeX file using the "book" template -# Description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SYSOUT = A simple DEBUG console output -# Default description when the type is not known by i18n -# This item is used as a group, its content is not expected to be used. -OUTPUT_DESC_SHORT = %s -# Short description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SHORT_EPUB = Electronic book (.epub) -# Short description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SHORT_TEXT = Plain text (.txt) -# Short description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SHORT_INFO_TEXT = Plain text and metadata -# Short description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SHORT_CBZ = Comic book (.cbz) -# Short description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SHORT_LATEX = LaTeX (.tex) -# Short description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SHORT_SYSOUT = Console output -# Short description of this output type -# (FORMAT: STRING) -OUTPUT_DESC_SHORT_HTML = HTML files with resources (directory, .html) -# Error message for unknown 2-letter LaTeX language code -# (FORMAT: STRING) -LATEX_LANG_UNKNOWN = Unknown language: %s -# 'by' prefix before author name used to output the author, make sure it is covered by Config.BYS for input detection -# (FORMAT: STRING) -BY = by diff --git a/bundles/resources_gui.properties b/bundles/resources_gui.properties deleted file mode 100644 index 40be5eb..0000000 --- a/bundles/resources_gui.properties +++ /dev/null @@ -1,199 +0,0 @@ -# United Kingdom (en_GB) resources_gui translation file (UTF-8) -# -# Note that any key can be doubled with a _NOUTF suffix -# to use when the NOUTF env variable is set to 1 -# -# Also, the comments always refer to the key below them. -# - - -# the title of the main window of Fanfix, the library -# (FORMAT: STRING) -TITLE_LIBRARY = Fanfix %s -# the title of the main window of Fanfix, the library, when the library has a name (i.e., is not local) -# (FORMAT: STRING) -TITLE_LIBRARY_WITH_NAME = Fanfix %s -# the title of the configuration window of Fanfix, also the name of the menu button -# (FORMAT: STRING) -TITLE_CONFIG = Fanfix Configuration -# the subtitle of the configuration window of Fanfix -# (FORMAT: STRING) -SUBTITLE_CONFIG = This is where you configure the options of the program. -# the title of the UI configuration window of Fanfix, also the name of the menu button -# (FORMAT: STRING) -TITLE_CONFIG_UI = UI Configuration -# the subtitle of the UI configuration window of Fanfix -# (FORMAT: STRING) -SUBTITLE_CONFIG_UI = This is where you configure the graphical appearence of the program. -# the title of the 'save to/export to' window of Fanfix -# (FORMAT: STRING) -TITLE_SAVE = Save -# the title of the 'move to' window of Fanfix -# (FORMAT: STRING) -TITLE_MOVE_TO = Moving story -# the subtitle of the 'move to' window of Fanfix -# (FORMAT: STRING) -SUBTITLE_MOVE_TO = Move to: -# the title of the 'delete' window of Fanfix -# (FORMAT: STRING) -TITLE_DELETE = Delete story -# the subtitle of the 'delete' window of Fanfix -# (FORMAT: STRING) -SUBTITLE_DELETE = Delete %s: %s -# the title of the 'library error' dialogue -# (FORMAT: STRING) -TITLE_ERROR_LIBRARY = Library error -# the title of the 'import URL' dialogue -# (FORMAT: STRING) -TITLE_IMPORT_URL = Importing from URL -# the subtitle of the 'import URL' dialogue -# (FORMAT: STRING) -SUBTITLE_IMPORT_URL = URL of the story to import: -# the title of general error dialogues -# (FORMAT: STRING) -TITLE_ERROR = Error -# the title of a story for the properties dialogue, the viewers... -# (FORMAT: STRING) -TITLE_STORY = %s: %s -# HTML text used to notify of a new version -# (FORMAT: STRING) -NEW_VERSION_AVAILABLE = A new version of the program is available at %s -# text used as title for the update dialogue -# (FORMAT: STRING) -NEW_VERSION_TITLE = Updates available -# HTML text used to specify a newer version title and number, used for each version newer than the current one -# (FORMAT: STRING) -NEW_VERSION_VERSION = Version %s -# show the number of words of a book -# (FORMAT: STRING) -BOOK_COUNT_WORDS = %s words -# show the number of images of a book -# (FORMAT: STRING) -BOOK_COUNT_IMAGES = %s images -# show the number of stories of a meta-book (a book representing allthe types/sources or all the authors present) -# (FORMAT: STRING) -BOOK_COUNT_STORIES = %s stories -# the file menu -# (FORMAT: STRING) -MENU_FILE = File -# the file/exit menu button -# (FORMAT: STRING) -MENU_FILE_EXIT = Exit -# the file/import_file menu button -# (FORMAT: STRING) -MENU_FILE_IMPORT_FILE = Import File... -# the file/import_url menu button -# (FORMAT: STRING) -MENU_FILE_IMPORT_URL = Import URL... -# the file/export menu button -# (FORMAT: STRING) -MENU_FILE_EXPORT = Save as... -# the file/move to menu button -# (FORMAT: STRING) -MENU_FILE_MOVE_TO = Move to -# the file/set author menu button -# (FORMAT: STRING) -MENU_FILE_SET_AUTHOR = Set author -# the file/move to/new type-source menu button, that will trigger a dialogue to create a new type/source -# (FORMAT: STRING) -MENU_FILE_MOVE_TO_NEW_TYPE = New source... -# the file/move to/new author menu button, that will trigger a dialogue to create a new author -# (FORMAT: STRING) -MENU_FILE_MOVE_TO_NEW_AUTHOR = New author... -# the file/rename menu item, that will trigger a dialogue to ask for a new title for the story -# (FORMAT: STRING) -MENU_FILE_RENAME = Rename... -# the file/Properties menu item, that will trigger a dialogue to show the properties of the story -# (FORMAT: STRING) -MENU_FILE_PROPERTIES = Properties -# the file/open menu item, that will open the story or fake-story (an author or a source/type) -# (FORMAT: STRING) -MENU_FILE_OPEN = Open -# the edit menu -# (FORMAT: STRING) -MENU_EDIT = Edit -# the edit/send to cache menu button, to download the story into the cache if not already done -# (FORMAT: STRING) -MENU_EDIT_DOWNLOAD_TO_CACHE = Prefetch to cache -# the clear cache menu button, to clear the cache for a single book -# (FORMAT: STRING) -MENU_EDIT_CLEAR_CACHE = Clear cache -# the edit/redownload menu button, to download the latest version of the book -# (FORMAT: STRING) -MENU_EDIT_REDOWNLOAD = Redownload -# the edit/delete menu button -# (FORMAT: STRING) -MENU_EDIT_DELETE = Delete -# the edit/Set as cover for source menu button -# (FORMAT: STRING) -MENU_EDIT_SET_COVER_FOR_SOURCE = Set as cover for source -# the edit/Set as cover for author menu button -# (FORMAT: STRING) -MENU_EDIT_SET_COVER_FOR_AUTHOR = Set as cover for author -# the search menu to open the earch stories on one of the searchable websites -# (FORMAT: STRING) -MENU_SEARCH = Search -# the view menu -# (FORMAT: STRING) -MENU_VIEW = View -# the view/word_count menu button, to show the word/image/story count as secondary info -# (FORMAT: STRING) -MENU_VIEW_WCOUNT = Word count -# the view/author menu button, to show the author as secondary info -# (FORMAT: STRING) -MENU_VIEW_AUTHOR = Author -# the sources menu, to select the books from a specific source; also used as a title for the source books -# (FORMAT: STRING) -MENU_SOURCES = Sources -# the authors menu, to select the books of a specific author; also used as a title for the author books -# (FORMAT: STRING) -MENU_AUTHORS = Authors -# the options menu, to configure Fanfix from the GUI -# (FORMAT: STRING) -MENU_OPTIONS = Options -# a special menu button to select all the sources/types or authors, by group (one book = one group) -# (FORMAT: STRING) -MENU_XXX_ALL_GROUPED = All -# a special menu button to select all the sources/types or authors, in a listing (all the included books are listed, grouped by source/type or author) -# (FORMAT: STRING) -MENU_XXX_ALL_LISTING = Listing -# a special menu button to select the books without author -# (FORMAT: STRING) -MENU_AUTHORS_UNKNOWN = [unknown] -# progress bar caption for the 'reload books' step of all outOfUi operations -# (FORMAT: STRING) -PROGRESS_OUT_OF_UI_RELOAD_BOOKS = Reload books -# progress bar caption for the 'change source' step of the ReDownload operation -# (FORMAT: STRING) -PROGRESS_CHANGE_SOURCE = Change the source of the book to %s -# default description if the error is not known -# (FORMAT: STRING) -ERROR_LIB_STATUS = An error occured when contacting the library -# library access not allowed -# (FORMAT: STRING) -ERROR_LIB_STATUS_UNAUTHORIZED = You are not allowed to access this library -# the library is invalid (not correctly set up) -# (FORMAT: STRING) -ERROR_LIB_STATUS_INVALID = Library not valid -# the library is out of commission -# (FORMAT: STRING) -ERROR_LIB_STATUS_UNAVAILABLE = Library currently unavailable -# cannot open the book, internal or external viewer -# (FORMAT: STRING) -ERROR_CANNOT_OPEN = Cannot open the selected book -# URL is not supported by Fanfix -# (FORMAT: STRING) -ERROR_URL_NOT_SUPPORTED = URL not supported: %s -# cannot import the URL -# (FORMAT: STRING) -ERROR_URL_IMPORT_FAILED = Failed to import %s:\n\ -%s -# (html) the chapter progression value used on the viewers -# (FORMAT: STRING) -CHAPTER_HTML_UNNAMED =   Chapter %d/%d -# (html) the chapter progression value used on the viewers -# (FORMAT: STRING) -CHAPTER_HTML_NAMED =   Chapter %d/%d: %s -# (NO html) the chapter progression value used on the viewers -# (FORMAT: STRING) -IMAGE_PROGRESSION = Image %d / %d diff --git a/bundles/resources_gui_fr.properties b/bundles/resources_gui_fr.properties deleted file mode 100644 index 25ff542..0000000 --- a/bundles/resources_gui_fr.properties +++ /dev/null @@ -1,199 +0,0 @@ -# français (fr) resources_gui translation file (UTF-8) -# -# Note that any key can be doubled with a _NOUTF suffix -# to use when the NOUTF env variable is set to 1 -# -# Also, the comments always refer to the key below them. -# - - -# the title of the main window of Fanfix, the library -# (FORMAT: STRING) -TITLE_LIBRARY = Fanfix %s -# the title of the main window of Fanfix, the library, when the library has a name (i.e., is not local) -# (FORMAT: STRING) -TITLE_LIBRARY_WITH_NAME = Fanfix %s -# the title of the configuration window of Fanfix, also the name of the menu button -# (FORMAT: STRING) -TITLE_CONFIG = Configuration de Fanfix -# the subtitle of the configuration window of Fanfix -# (FORMAT: STRING) -SUBTITLE_CONFIG = C'est ici que vous pouvez configurer les options du programme. -# the title of the UI configuration window of Fanfix, also the name of the menu button -# (FORMAT: STRING) -TITLE_CONFIG_UI = Configuration de l'interface -# the subtitle of the UI configuration window of Fanfix -# (FORMAT: STRING) -SUBTITLE_CONFIG_UI = C'est ici que vous pouvez configurer les options de l'apparence de l'application. -# the title of the 'save to/export to' window of Fanfix -# (FORMAT: STRING) -TITLE_SAVE = Sauver -# the title of the 'move to' window of Fanfix -# (FORMAT: STRING) -TITLE_MOVE_TO = Déplacer le livre -# the subtitle of the 'move to' window of Fanfix -# (FORMAT: STRING) -SUBTITLE_MOVE_TO = Déplacer vers : -# the title of the 'delete' window of Fanfix -# (FORMAT: STRING) -TITLE_DELETE = Supprimer le livre -# the subtitle of the 'delete' window of Fanfix -# (FORMAT: STRING) -SUBTITLE_DELETE = Supprimer %s : %s -# the title of the 'library error' dialogue -# (FORMAT: STRING) -TITLE_ERROR_LIBRARY = Erreur avec la librairie -# the title of the 'import URL' dialogue -# (FORMAT: STRING) -TITLE_IMPORT_URL = Importer depuis une URL -# the subtitle of the 'import URL' dialogue -# (FORMAT: STRING) -SUBTITLE_IMPORT_URL = L'URL du livre à importer -# the title of general error dialogues -# (FORMAT: STRING) -TITLE_ERROR = Error -# the title of a story for the properties dialogue, the viewers... -# (FORMAT: STRING) -TITLE_STORY = %s: %s -# HTML text used to notify of a new version -# (FORMAT: STRING) -NEW_VERSION_AVAILABLE = Une nouvelle version du programme est disponible sur %s -# text used as title for the update dialogue -# (FORMAT: STRING) -NEW_VERSION_TITLE = Mise-à-jour disponible -# HTML text used to specify a newer version title and number, used for each version newer than the current one -# (FORMAT: STRING) -NEW_VERSION_VERSION = Version %s -# show the number of words of a book -# (FORMAT: STRING) -BOOK_COUNT_WORDS = %s mots -# show the number of images of a book -# (FORMAT: STRING) -BOOK_COUNT_IMAGES = %s images -# show the number of stories of a meta-book (a book representing allthe types/sources or all the authors present) -# (FORMAT: STRING) -BOOK_COUNT_STORIES = %s livres -# the file menu -# (FORMAT: STRING) -MENU_FILE = Fichier -# the file/exit menu button -# (FORMAT: STRING) -MENU_FILE_EXIT = Quiter -# the file/import_file menu button -# (FORMAT: STRING) -MENU_FILE_IMPORT_FILE = Importer un fichier... -# the file/import_url menu button -# (FORMAT: STRING) -MENU_FILE_IMPORT_URL = Importer une URL... -# the file/export menu button -# (FORMAT: STRING) -MENU_FILE_EXPORT = Sauver sous... -# the file/move to menu button -# (FORMAT: STRING) -MENU_FILE_MOVE_TO = Déplacer vers -# the file/set author menu button -# (FORMAT: STRING) -MENU_FILE_SET_AUTHOR = Changer l'auteur -# the file/move to/new type-source menu button, that will trigger a dialogue to create a new type/source -# (FORMAT: STRING) -MENU_FILE_MOVE_TO_NEW_TYPE = Nouvelle source... -# the file/move to/new author menu button, that will trigger a dialogue to create a new author -# (FORMAT: STRING) -MENU_FILE_MOVE_TO_NEW_AUTHOR = Nouvel auteur... -# the file/rename menu item, that will trigger a dialogue to ask for a new title for the story -# (FORMAT: STRING) -MENU_FILE_RENAME = Renommer... -# the file/Properties menu item, that will trigger a dialogue to show the properties of the story -# (FORMAT: STRING) -MENU_FILE_PROPERTIES = Propriétés -# the file/open menu item, that will open the story or fake-story (an author or a source/type) -# (FORMAT: STRING) -MENU_FILE_OPEN = Ouvrir -# the edit menu -# (FORMAT: STRING) -MENU_EDIT = Edition -# the edit/send to cache menu button, to download the story into the cache if not already done -# (FORMAT: STRING) -MENU_EDIT_DOWNLOAD_TO_CACHE = Précharger en cache -# the clear cache menu button, to clear the cache for a single book -# (FORMAT: STRING) -MENU_EDIT_CLEAR_CACHE = Nettoyer le cache -# the edit/redownload menu button, to download the latest version of the book -# (FORMAT: STRING) -MENU_EDIT_REDOWNLOAD = Re-downloader -# the edit/delete menu button -# (FORMAT: STRING) -MENU_EDIT_DELETE = Supprimer -# the edit/Set as cover for source menu button -# (FORMAT: STRING) -MENU_EDIT_SET_COVER_FOR_SOURCE = Utiliser comme cover pour la source -# the edit/Set as cover for author menu button -# (FORMAT: STRING) -MENU_EDIT_SET_COVER_FOR_AUTHOR = Utiliser comme cover pour l'auteur -# the search menu to open the earch stories on one of the searchable websites -# (FORMAT: STRING) -MENU_SEARCH = Recherche -# the view menu -# (FORMAT: STRING) -MENU_VIEW = Affichage -# the view/word_count menu button, to show the word/image/story count as secondary info -# (FORMAT: STRING) -MENU_VIEW_WCOUNT = Nombre de mots -# the view/author menu button, to show the author as secondary info -# (FORMAT: STRING) -MENU_VIEW_AUTHOR = Auteur -# the sources menu, to select the books from a specific source; also used as a title for the source books -# (FORMAT: STRING) -MENU_SOURCES = Sources -# the authors menu, to select the books of a specific author; also used as a title for the author books -# (FORMAT: STRING) -MENU_AUTHORS = Auteurs -# the options menu, to configure Fanfix from the GUI -# (FORMAT: STRING) -MENU_OPTIONS = Options -# a special menu button to select all the sources/types or authors, by group (one book = one group) -# (FORMAT: STRING) -MENU_XXX_ALL_GROUPED = Tout -# a special menu button to select all the sources/types or authors, in a listing (all the included books are listed, grouped by source/type or author) -# (FORMAT: STRING) -MENU_XXX_ALL_LISTING = Listing -# a special menu button to select the books without author -# (FORMAT: STRING) -MENU_AUTHORS_UNKNOWN = [inconnu] -# progress bar caption for the 'reload books' step of all outOfUi operations -# (FORMAT: STRING) -PROGRESS_OUT_OF_UI_RELOAD_BOOKS = Recharger les livres -# progress bar caption for the 'change source' step of the ReDownload operation -# (FORMAT: STRING) -PROGRESS_CHANGE_SOURCE = Change la source du livre en %s -# default description if the error is not known -# (FORMAT: STRING) -ERROR_LIB_STATUS = Une erreur est survenue en contactant la librairie -# library access not allowed -# (FORMAT: STRING) -ERROR_LIB_STATUS_UNAUTHORIZED = Vous n'êtes pas autorisé à accéder à cette librairie -# the library is invalid (not correctly set up) -# (FORMAT: STRING) -ERROR_LIB_STATUS_INVALID = Librairie invalide -# the library is out of commission -# (FORMAT: STRING) -ERROR_LIB_STATUS_UNAVAILABLE = Librairie indisponible -# cannot open the book, internal or external viewer -# (FORMAT: STRING) -ERROR_CANNOT_OPEN = Impossible d'ouvrir le livre sélectionné -# URL is not supported by Fanfix -# (FORMAT: STRING) -ERROR_URL_NOT_SUPPORTED = URL non supportée : %s -# cannot import the URL -# (FORMAT: STRING) -ERROR_URL_IMPORT_FAILED = Erreur lors de l'import de %s:\n\ -%s -# (html) the chapter progression value used on the viewers -# (FORMAT: STRING) -CHAPTER_HTML_UNNAMED =   Chapitre %d / %d -# (html) the chapter progression value used on the viewers -# (FORMAT: STRING) -CHAPTER_HTML_NAMED =   Chapitre %d / %d: %s -# (NO html) the chapter progression value used on the viewers -# (FORMAT: STRING) -IMAGE_PROGRESSION = Image %d / %d diff --git a/bundles/ui_description.properties b/bundles/ui_description.properties deleted file mode 100644 index c8def83..0000000 --- a/bundles/ui_description.properties +++ /dev/null @@ -1,35 +0,0 @@ -# United Kingdom (en_GB) UI configuration options description translation file (UTF-8) -# -# Note that any key can be doubled with a _NOUTF suffix -# to use when the NOUTF env variable is set to 1 -# -# Also, the comments always refer to the key below them. -# - - -# The directory where to store temporary files for the GUI reader; any relative path uses the applciation config directory as base, $HOME notation is supported, / is always accepted as directory separator -# (FORMAT: DIRECTORY) absolute path, $HOME variable supported, / is always accepted as dir separator -CACHE_DIR_LOCAL_READER = The directory where to store temporary files for the GUI reader; any relative path uses the applciation config directory as base, $HOME notation is supported, / is always accepted as directory separator -# The type of output for the GUI Reader for non-images documents -# (FORMAT: COMBO_LIST) One of the known output type -# ALLOWED VALUES: "INFO_TEXT" "EPUB" "HTML" "TEXT" -GUI_NON_IMAGES_DOCUMENT_TYPE = -# The type of output for the GUI Reader for images documents -# (FORMAT: COMBO_LIST) -# ALLOWED VALUES: "CBZ" "HTML" -GUI_IMAGES_DOCUMENT_TYPE = -# Use the internal reader for images documents -- this is TRUE by default -# (FORMAT: BOOLEAN) -IMAGES_DOCUMENT_USE_INTERNAL_READER = -# The command launched for images documents -- default to the system default for the current file type -# (FORMAT: STRING) A command to start -IMAGES_DOCUMENT_READER = -# Use the internal reader for non images documents -- this is TRUE by default -# (FORMAT: BOOLEAN) -NON_IMAGES_DOCUMENT_USE_INTERNAL_READER = -# The command launched for non images documents -- default to the system default for the current file type -# (FORMAT: STRING) A command to start -NON_IMAGES_DOCUMENT_READER = -# The background colour if you don't want the default system one -# (FORMAT: COLOR) -BACKGROUND_COLOR = diff --git a/data/Chapter.java b/data/Chapter.java deleted file mode 100644 index d490058..0000000 --- a/data/Chapter.java +++ /dev/null @@ -1,154 +0,0 @@ -package be.nikiroo.fanfix.data; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -/** - * A chapter in the story (or the resume/description). - * - * @author niki - */ -public class Chapter implements Iterable, Cloneable, Serializable { - private static final long serialVersionUID = 1L; - - private String name; - private int number; - private List paragraphs = new ArrayList(); - private List empty = new ArrayList(); - private long words; - - /** - * Empty constructor, not to use. - */ - @SuppressWarnings("unused") - private Chapter() { - // for serialisation purposes - } - - /** - * Create a new {@link Chapter} with the given information. - * - * @param number - * the chapter number, or 0 for the description/resume. - * @param name - * the chapter name - */ - public Chapter(int number, String name) { - this.number = number; - this.name = name; - } - - /** - * The chapter name. - * - * @return the name - */ - public String getName() { - return name; - } - - /** - * The chapter name. - * - * @param name - * the name to set - */ - public void setName(String name) { - this.name = name; - } - - /** - * The chapter number, or 0 for the description/resume. - * - * @return the number - */ - public int getNumber() { - return number; - } - - /** - * The chapter number, or 0 for the description/resume. - * - * @param number - * the number to set - */ - public void setNumber(int number) { - this.number = number; - } - - /** - * The included paragraphs. - * - * @return the paragraphs - */ - public List getParagraphs() { - return paragraphs; - } - - /** - * The included paragraphs. - * - * @param paragraphs - * the paragraphs to set - */ - public void setParagraphs(List paragraphs) { - this.paragraphs = paragraphs; - } - - /** - * Get an iterator on the {@link Paragraph}s. - */ - @Override - public Iterator iterator() { - return paragraphs == null ? empty.iterator() : paragraphs.iterator(); - } - - /** - * The number of words (or images) in this {@link Chapter}. - * - * @return the number of words - */ - public long getWords() { - return words; - } - - /** - * The number of words (or images) in this {@link Chapter}. - * - * @param words - * the number of words to set - */ - public void setWords(long words) { - this.words = words; - } - - /** - * Display a DEBUG {@link String} representation of this object. - */ - @Override - public String toString() { - return "Chapter " + number + ": " + name; - } - - @Override - public Chapter clone() { - Chapter chap = null; - try { - chap = (Chapter) super.clone(); - } catch (CloneNotSupportedException e) { - // Did the clones rebel? - System.err.println(e); - } - - if (paragraphs != null) { - chap.paragraphs = new ArrayList(); - for (Paragraph para : paragraphs) { - chap.paragraphs.add(para.clone()); - } - } - - return chap; - } -} diff --git a/data/JsonIO.java b/data/JsonIO.java deleted file mode 100644 index 501a0d9..0000000 --- a/data/JsonIO.java +++ /dev/null @@ -1,431 +0,0 @@ -package be.nikiroo.fanfix.data; - -import java.util.ArrayList; -import java.util.List; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import be.nikiroo.fanfix.data.Paragraph.ParagraphType; -import be.nikiroo.utils.Progress; - -public class JsonIO { - static public JSONObject toJson(MetaData meta) { - if (meta == null) { - return null; - } - - JSONObject json = new JSONObject(); - - put(json, "", MetaData.class.getName()); - put(json, "luid", meta.getLuid()); - put(json, "title", meta.getTitle()); - put(json, "author", meta.getAuthor()); - put(json, "source", meta.getSource()); - put(json, "url", meta.getUrl()); - put(json, "words", meta.getWords()); - put(json, "creation_date", meta.getCreationDate()); - put(json, "date", meta.getDate()); - put(json, "lang", meta.getLang()); - put(json, "publisher", meta.getPublisher()); - put(json, "subject", meta.getSubject()); - put(json, "type", meta.getType()); - put(json, "uuid", meta.getUuid()); - put(json, "fake_cover", meta.isFakeCover()); - put(json, "image_document", meta.isImageDocument()); - - put(json, "resume", toJson(meta.getResume())); - put(json, "tags", new JSONArray(meta.getTags())); - - return json; - } - - /** - * // no image - * - * @param json - * - * @return - * - * @throws JSONException - * when it cannot be converted - */ - static public MetaData toMetaData(JSONObject json) { - if (json == null) { - return null; - } - - MetaData meta = new MetaData(); - - meta.setLuid(getString(json, "luid")); - meta.setTitle(getString(json, "title")); - meta.setAuthor(getString(json, "author")); - meta.setSource(getString(json, "source")); - meta.setUrl(getString(json, "url")); - meta.setWords(getLong(json, "words", 0)); - meta.setCreationDate(getString(json, "creation_date")); - meta.setDate(getString(json, "date")); - meta.setLang(getString(json, "lang")); - meta.setPublisher(getString(json, "publisher")); - meta.setSubject(getString(json, "subject")); - meta.setType(getString(json, "type")); - meta.setUuid(getString(json, "uuid")); - meta.setFakeCover(getBoolean(json, "fake_cover", false)); - meta.setImageDocument(getBoolean(json, "image_document", false)); - - meta.setResume(toChapter(getJson(json, "resume"))); - meta.setTags(toListString(getJsonArr(json, "tags"))); - - return meta; - } - - static public JSONObject toJson(Story story) { - if (story == null) { - return null; - } - - JSONObject json = new JSONObject(); - put(json, "", Story.class.getName()); - put(json, "meta", toJson(story.getMeta())); - - List chapters = new ArrayList(); - for (Chapter chap : story) { - chapters.add(toJson(chap)); - } - put(json, "chapters", new JSONArray(chapters)); - - return json; - } - - /** - * - * @param json - * - * @return - * - * @throws JSONException - * when it cannot be converted - */ - static public Story toStory(JSONObject json) { - if (json == null) { - return null; - } - - Story story = new Story(); - story.setMeta(toMetaData(getJson(json, "meta"))); - story.setChapters(toListChapter(getJsonArr(json, "chapters"))); - - return story; - } - - static public JSONObject toJson(Chapter chap) { - if (chap == null) { - return null; - } - - JSONObject json = new JSONObject(); - put(json, "", Chapter.class.getName()); - put(json, "name", chap.getName()); - put(json, "number", chap.getNumber()); - put(json, "words", chap.getWords()); - - List paragraphs = new ArrayList(); - for (Paragraph para : chap) { - paragraphs.add(toJson(para)); - } - put(json, "paragraphs", new JSONArray(paragraphs)); - - return json; - } - - /** - * - * @param json - * - * @return - * - * @throws JSONException - * when it cannot be converted - */ - static public Chapter toChapter(JSONObject json) { - if (json == null) { - return null; - } - - Chapter chap = new Chapter(getInt(json, "number", 0), - getString(json, "name")); - chap.setWords(getLong(json, "words", 0)); - - chap.setParagraphs(toListParagraph(getJsonArr(json, "paragraphs"))); - - return chap; - } - - // no images - static public JSONObject toJson(Paragraph para) { - if (para == null) { - return null; - } - - JSONObject json = new JSONObject(); - - put(json, "", Paragraph.class.getName()); - put(json, "content", para.getContent()); - put(json, "words", para.getWords()); - - put(json, "type", para.getType().toString()); - - return json; - } - - /** - * // no images - * - * @param json - * - * @return - * - * @throws JSONException - * when it cannot be converted - */ - static public Paragraph toParagraph(JSONObject json) { - if (json == null) { - return null; - } - - Paragraph para = new Paragraph( - ParagraphType.valueOf(getString(json, "type")), - getString(json, "content"), getLong(json, "words", 0)); - - return para; - } - - // only supported option: a MetaData called "meta" - static public JSONObject toJson(Progress pg) { - return toJson(pg, null); - } - - // only supported option: a MetaData called "meta" - static private JSONObject toJson(Progress pg, Double weight) { - if (pg == null) { - return null; - } - - // Supported keys: meta (only keep the key on the main parent, where - // weight is NULL) - MetaData meta = null; - if (weight == null) { - Object ometa = pg.get("meta"); - if (ometa instanceof MetaData) { - meta = getMetaLight((MetaData) ometa); - } - } - // - - JSONObject json = new JSONObject(); - - put(json, "", Progress.class.getName()); - put(json, "name", pg.getName()); - put(json, "min", pg.getMin()); - put(json, "max", pg.getMax()); - put(json, "progress", pg.getRelativeProgress()); - put(json, "weight", weight); - put(json, "meta", meta); - - List children = new ArrayList(); - for (Progress child : pg.getChildren()) { - children.add(toJson(child, pg.getWeight(child))); - } - put(json, "children", new JSONArray(children)); - - return json; - } - - // only supported option: a MetaData called "meta" - static public Progress toProgress(JSONObject json) { - if (json == null) { - return null; - } - - Progress pg = new Progress( // - getString(json, "name"), // - getInt(json, "min", 0), // - getInt(json, "max", 100) // - ); - - pg.setRelativeProgress(getDouble(json, "progress", 0)); - - Object meta = getObject(json, "meta"); - if (meta != null) { - pg.put("meta", meta); - } - - JSONArray jchildren = getJsonArr(json, "children"); - for (int i = 0; i < jchildren.length(); i++) { - try { - JSONObject jchild = jchildren.getJSONObject(i); - Double weight = getDouble(jchild, "weight", 0); - pg.addProgress(toProgress(jchild), weight); - } catch (Exception e) { - } - } - - return pg; - } - - static public List toListString(JSONArray array) { - if (array != null) { - List values = new ArrayList(); - for (int i = 0; i < array.length(); i++) { - values.add(array.getString(i)); - } - return values; - } - - return null; - } - - static public List toListParagraph(JSONArray array) { - if (array != null) { - List values = new ArrayList(); - for (int i = 0; i < array.length(); i++) { - JSONObject value = array.getJSONObject(i); - values.add(toParagraph(value)); - } - return values; - } - - return null; - } - - static private List toListChapter(JSONArray array) { - if (array != null) { - List values = new ArrayList(); - for (int i = 0; i < array.length(); i++) { - JSONObject value = array.getJSONObject(i); - values.add(toChapter(value)); - } - return values; - } - - return null; - } - - static private void put(JSONObject json, String key, Object o) { - json.put(key, o == null ? JSONObject.NULL : o); - } - - static private Object getObject(JSONObject json, String key) { - if (json.has(key)) { - try { - return json.get(key); - } catch (Exception e) { - // Can fail if content was NULL! - } - } - - return null; - } - - static private String getString(JSONObject json, String key) { - Object o = getObject(json, key); - if (o instanceof String) - return (String) o; - - return null; - } - - static private long getLong(JSONObject json, String key, long def) { - Object o = getObject(json, key); - if (o instanceof Byte) - return (Byte) o; - if (o instanceof Short) - return (Short) o; - if (o instanceof Integer) - return (Integer) o; - if (o instanceof Long) - return (Long) o; - - return def; - } - - static private double getDouble(JSONObject json, String key, double def) { - Object o = getObject(json, key); - if (o instanceof Byte) - return (Byte) o; - if (o instanceof Short) - return (Short) o; - if (o instanceof Integer) - return (Integer) o; - if (o instanceof Long) - return (Long) o; - if (o instanceof Float) - return (Float) o; - if (o instanceof Double) - return (Double) o; - - return def; - } - - static private boolean getBoolean(JSONObject json, String key, - boolean def) { - Object o = getObject(json, key); - if (o instanceof Boolean) { - return (Boolean) o; - } - - return def; - } - - static private int getInt(JSONObject json, String key, int def) { - Object o = getObject(json, key); - if (o instanceof Byte) - return (Byte) o; - if (o instanceof Short) - return (Short) o; - if (o instanceof Integer) - return (Integer) o; - if (o instanceof Long) { - try { - return (int) (long) ((Long) o); - } catch (Exception e) { - } - } - - return def; - } - - static private JSONObject getJson(JSONObject json, String key) { - Object o = getObject(json, key); - if (o instanceof JSONObject) { - return (JSONObject) o; - } - - return null; - } - - static private JSONArray getJsonArr(JSONObject json, String key) { - Object o = getObject(json, key); - if (o instanceof JSONArray) { - return (JSONArray) o; - } - - return null; - } - - // null -> null - static private MetaData getMetaLight(MetaData meta) { - MetaData light = null; - if (meta != null) { - if (meta.getCover() == null) { - light = meta; - } else { - light = meta.clone(); - light.setCover(null); - } - } - - return light; - } -} diff --git a/data/MetaData.java b/data/MetaData.java deleted file mode 100644 index 1c6ad42..0000000 --- a/data/MetaData.java +++ /dev/null @@ -1,576 +0,0 @@ -package be.nikiroo.fanfix.data; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; - -import be.nikiroo.fanfix.supported.SupportType; -import be.nikiroo.utils.Image; -import be.nikiroo.utils.StringUtils; - -/** - * The meta data associated to a {@link Story} object. - *

- * Note that some earlier version of the program did not save the resume as an - * external file; for those stories, the resume is not fetched until the story - * is. - *

- * The cover is never fetched until the story is. - * - * @author niki - */ -public class MetaData implements Cloneable, Comparable, Serializable { - private static final long serialVersionUID = 1L; - - private String title; - private String author; - private String date; - private Chapter resume; - private List tags; - private Image cover; - private String subject; - private String source; - private String url; - private String uuid; - private String luid; - private String lang; - private String publisher; - private String type; - private boolean imageDocument; - private long words; - private String creationDate; - private boolean fakeCover; - - /** - * Create an empty {@link MetaData}. - */ - public MetaData() { - } - - /** - * The title of the story. - * - * @return the title - */ - public String getTitle() { - return title; - } - - /** - * The title of the story. - * - * @param title - * the title to set - */ - public void setTitle(String title) { - this.title = title; - } - - /** - * The author of the story. - * - * @return the author - */ - public String getAuthor() { - return author; - } - - /** - * The author of the story. - * - * @param author - * the author to set - */ - public void setAuthor(String author) { - this.author = author; - } - - /** - * The story publication date, we try to use "YYYY-mm-dd" when possible. - * - * @return the date - */ - public String getDate() { - return date; - } - - /** - * The story publication date, we try to use "YYYY-mm-dd" when possible. - * - * @param date - * the date to set - */ - public void setDate(String date) { - this.date = date; - } - - /** - * The tags associated with this story. - * - * @return the tags - */ - public List getTags() { - return tags; - } - - /** - * The tags associated with this story. - * - * @param tags - * the tags to set - */ - public void setTags(List tags) { - this.tags = tags; - } - - /** - * The story resume (a.k.a. description). - *

- * This can be NULL if we don't have a resume for this {@link Story}. - *

- * Note that some earlier version of the program did not save the resume as - * an external file; for those stories, the resume is not fetched until the - * story is. - * - * @return the resume - */ - public Chapter getResume() { - return resume; - } - - /** - * The story resume (a.k.a. description). - *

- * Note that some earlier version of the program did not save the resume as - * an external file; for those stories, the resume is not fetched until the - * story is. - * - * @param resume - * the resume to set - */ - public void setResume(Chapter resume) { - this.resume = resume; - } - - /** - * The cover image of the story, if any (can be NULL). - *

- * The cover is not fetched until the story is. - * - * @return the cover - */ - public Image getCover() { - return cover; - } - - /** - * The cover image of the story, if any (can be NULL). - *

- * The cover is not fetched until the story is. - * - * @param cover - * the cover to set - */ - public void setCover(Image cover) { - this.cover = cover; - } - - /** - * The subject of the story (for instance, if it is a fanfiction, what is the - * original work; if it is a technical text, what is the technical - * subject...). - * - * @return the subject - */ - public String getSubject() { - return subject; - } - - /** - * The subject of the story (for instance, if it is a fanfiction, what is - * the original work; if it is a technical text, what is the technical - * subject...). - * - * @param subject - * the subject to set - */ - public void setSubject(String subject) { - this.subject = subject; - } - - /** - * The source of this story -- a very user-visible piece of data. - *

- * It is initialised with the same value as {@link MetaData#getPublisher()}, - * but the user is allowed to change it into any value -- this is a sort of - * 'category'. - * - * @return the source - */ - public String getSource() { - return source; - } - - /** - * The source of this story -- a very user-visible piece of data. - *

- * It is initialised with the same value as {@link MetaData#getPublisher()}, - * but the user is allowed to change it into any value -- this is a sort of - * 'category'. - * - * @param source - * the source to set - */ - public void setSource(String source) { - this.source = source; - } - - /** - * The original URL from which this {@link Story} was imported. - * - * @return the url - */ - public String getUrl() { - return url; - } - - /** - * The original URL from which this {@link Story} was imported. - * - * @param url - * the new url to set - */ - public void setUrl(String url) { - this.url = url; - } - - /** - * A unique value representing the story (it is often a URL). - * - * @return the uuid - */ - public String getUuid() { - return uuid; - } - - /** - * A unique value representing the story (it is often a URL). - * - * @param uuid - * the uuid to set - */ - public void setUuid(String uuid) { - this.uuid = uuid; - } - - /** - * A unique value representing the story in the local library (usually a - * numerical value 0-padded with a minimum size of 3; but this is subject to - * change and you can also obviously have more than 1000 stories -- - * a luid may potentially be anything else, including non-numeric - * characters). - *

- * A NULL or empty luid represents an incomplete, corrupted or fake - * {@link Story}. - * - * @return the luid - */ - public String getLuid() { - return luid; - } - - /** - * A unique value representing the story in the local library (usually a - * numerical value 0-padded with a minimum size of 3; but this is subject to - * change and you can also obviously have more than 1000 stories -- - * a luid may potentially be anything else, including non-numeric - * characters). - *

- * A NULL or empty luid represents an incomplete, corrupted or fake - * {@link Story}. - * - * @param luid - * the luid to set - */ - public void setLuid(String luid) { - this.luid = luid; - } - - /** - * The 2-letter code language of this story. - * - * @return the lang - */ - public String getLang() { - return lang; - } - - /** - * The 2-letter code language of this story. - * - * @param lang - * the lang to set - */ - public void setLang(String lang) { - this.lang = lang; - } - - /** - * The story publisher -- which is also the user representation of the - * output type this {@link Story} is in (see {@link SupportType}). - *

- * It allows you to know where the {@link Story} comes from, and is not - * supposed to change. - *

- * It's the user representation of the enum - * ({@link SupportType#getSourceName()}, not - * {@link SupportType#toString()}). - * - * @return the publisher - */ - public String getPublisher() { - return publisher; - } - - /** - * The story publisher -- which is also the user representation of the - * output type this {@link Story} is in (see {@link SupportType}). - *

- * It allows you to know where the {@link Story} comes from, and is not - * supposed to change. - *

- * It's the user representation of the enum - * ({@link SupportType#getSourceName()}, not - * {@link SupportType#toString()}). - * - * @param publisher - * the publisher to set - */ - public void setPublisher(String publisher) { - this.publisher = publisher; - } - - /** - * The output type this {@link Story} is in (see {@link SupportType}). - *

- * It allows you to know where the {@link Story} comes from, and is not - * supposed to change. - *

- * It's the direct representation of the enum - * ({@link SupportType#toString()}, not - * {@link SupportType#getSourceName()}). - * - * @return the type the type - */ - public String getType() { - return type; - } - - /** - * The output type this {@link Story} is in (see {@link SupportType}). - *

- * It allows you to know where the {@link Story} comes from, and is not - * supposed to change. - *

- * It's the direct representation of the enum - * ({@link SupportType#toString()}, not - * {@link SupportType#getSourceName()}). - * - * @param type - * the new type to set - */ - public void setType(String type) { - this.type = type; - } - - /** - * Document catering mostly to image files. - *

- * I.E., this is a comics or a manga, not a textual story with actual words. - *

- * In image documents, all the paragraphs are supposed to be images. - * - * @return the imageDocument state - */ - public boolean isImageDocument() { - return imageDocument; - } - - /** - * Document catering mostly to image files. - *

- * I.E., this is a comics or a manga, not a textual story with actual words. - *

- * In image documents, all the paragraphs are supposed to be images. - * - * @param imageDocument - * the imageDocument state to set - */ - public void setImageDocument(boolean imageDocument) { - this.imageDocument = imageDocument; - } - - /** - * The number of words (or images if this is an image document -- see - * {@link MetaData#isImageDocument()}) in the related {@link Story}. - * - * @return the number of words/images - */ - public long getWords() { - return words; - } - - /** - * The number of words (or images if this is an image document -- see - * {@link MetaData#isImageDocument()}) in the related {@link Story}. - * - * @param words - * the number of words/images to set - */ - public void setWords(long words) { - this.words = words; - } - - /** - * The (Fanfix) {@link Story} creation date, i.e., when the {@link Story} - * was fetched via Fanfix. - * - * @return the creation date - */ - public String getCreationDate() { - return creationDate; - } - - /** - * The (Fanfix) {@link Story} creation date, i.e., when the {@link Story} - * was fetched via Fanfix. - * - * @param creationDate - * the creation date to set - */ - public void setCreationDate(String creationDate) { - this.creationDate = creationDate; - } - - /** - * The cover in this {@link MetaData} object is "fake", in the sense that it - * comes from the actual content images. - * - * @return TRUE for a fake cover - */ - public boolean isFakeCover() { - return fakeCover; - } - - /** - * The cover in this {@link MetaData} object is "fake", in the sense that it - * comes from the actual content images - * - * @param fakeCover - * TRUE for a fake cover - */ - public void setFakeCover(boolean fakeCover) { - this.fakeCover = fakeCover; - } - - @Override - public int compareTo(MetaData o) { - if (o == null) { - return 1; - } - - String id = (getTitle() == null ? "" : getTitle()) - + (getUuid() == null ? "" : getUuid()) - + (getLuid() == null ? "" : getLuid()); - String oId = (getTitle() == null ? "" : o.getTitle()) - + (getUuid() == null ? "" : o.getUuid()) - + (o.getLuid() == null ? "" : o.getLuid()); - - return id.compareToIgnoreCase(oId); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof MetaData)) { - return false; - } - - return compareTo((MetaData) obj) == 0; - } - - @Override - public int hashCode() { - String uuid = getUuid(); - if (uuid == null) { - uuid = "" + title + author + source; - } - - return uuid.hashCode(); - } - - @Override - public MetaData clone() { - MetaData meta = null; - try { - meta = (MetaData) super.clone(); - } catch (CloneNotSupportedException e) { - // Did the clones rebel? - System.err.println(e); - } - - if (tags != null) { - meta.tags = new ArrayList(tags); - } - - if (resume != null) { - meta.resume = resume.clone(); - } - - return meta; - } - - /** - * Display a DEBUG {@link String} representation of this object. - *

- * This is not efficient, nor intended to be. - */ - @Override - public String toString() { - String title = ""; - if (getTitle() != null) { - title = getTitle(); - } - - StringBuilder tags = new StringBuilder(); - if (getTags() != null) { - for (String tag : getTags()) { - if (tags.length() > 0) { - tags.append(", "); - } - tags.append(tag); - } - } - - String resume = ""; - if (getResume() != null) { - for (Paragraph para : getResume()) { - resume += "\n\t"; - resume += para.toString().substring(0, - Math.min(para.toString().length(), 120)); - } - resume += "\n"; - } - - String cover = "none"; - if (getCover() != null) { - cover = StringUtils.formatNumber(getCover().getSize()) - + "bytes"; - } - - return String.format( - "Meta %s:\n\tTitle: [%s]\n\tAuthor: [%s]\n\tDate: [%s]\n\tTags: [%s]\n\tWord count: [%s]" - + "\n\tResume: [%s]\n\tCover: [%s]", - luid, title, getAuthor(), getDate(), tags.toString(), - "" + words, resume, cover); - } -} diff --git a/data/Paragraph.java b/data/Paragraph.java deleted file mode 100644 index d5a0f1c..0000000 --- a/data/Paragraph.java +++ /dev/null @@ -1,182 +0,0 @@ -package be.nikiroo.fanfix.data; - -import java.io.Serializable; - -import be.nikiroo.utils.Image; - -/** - * A paragraph in a chapter of the story. - * - * @author niki - */ -public class Paragraph implements Cloneable, Serializable { - private static final long serialVersionUID = 1L; - - /** - * A paragraph type, that will dictate how the paragraph will be handled. - * - * @author niki - */ - public enum ParagraphType { - /** Normal paragraph (text) */ - NORMAL, - /** Blank line */ - BLANK, - /** A Break paragraph, i.e.: HR (Horizontal Line) or '* * *' or whatever */ - BREAK, - /** Quotation (dialogue) */ - QUOTE, - /** An image (no text) */ - IMAGE, ; - - /** - * This paragraph type is of a text kind (quote or not). - * - * @param allowEmpty - * allow empty text as text, too (blanks, breaks...) - * @return TRUE if it is - */ - public boolean isText(boolean allowEmpty) { - return (this == NORMAL || this == QUOTE) - || (allowEmpty && (this == BLANK || this == BREAK)); - } - } - - private ParagraphType type; - private String content; - private Image contentImage; - private long words; - - /** - * Empty constructor, not to use. - */ - @SuppressWarnings("unused") - private Paragraph() { - // for serialisation purposes - } - - /** - * Create a new {@link Paragraph} with the given image. - * - * @param contentImage - * the image - */ - public Paragraph(Image contentImage) { - this(ParagraphType.IMAGE, null, 1); - this.contentImage = contentImage; - } - - /** - * Create a new {@link Paragraph} with the given values. - * - * @param type - * the {@link ParagraphType} - * @param content - * the content of this paragraph - * @param words - * the number of words (or images) - */ - public Paragraph(ParagraphType type, String content, long words) { - this.type = type; - this.content = content; - this.words = words; - } - - /** - * The {@link ParagraphType}. - * - * @return the type - */ - public ParagraphType getType() { - return type; - } - - /** - * The {@link ParagraphType}. - * - * @param type - * the type to set - */ - public void setType(ParagraphType type) { - this.type = type; - } - - /** - * The content of this {@link Paragraph} if it is not an image. - * - * @return the content - */ - public String getContent() { - return content; - } - - /** - * The content of this {@link Paragraph}. - * - * @param content - * the content to set - */ - public void setContent(String content) { - this.content = content; - } - - /** - * The content of this {@link Paragraph} if it is an image. - * - * @return the content - */ - public Image getContentImage() { - return contentImage; - } - - /** - * The content of this {@link Paragraph} if it is an image. - * - * @param contentImage - * the content - */ - public void setContentImage(Image contentImage) { - this.contentImage = contentImage; - } - - /** - * The number of words (or images) in this {@link Paragraph}. - * - * @return the number of words - */ - public long getWords() { - return words; - } - - /** - * The number of words (or images) in this {@link Paragraph}. - * - * @param words - * the number of words to set - */ - public void setWords(long words) { - this.words = words; - } - - /** - * Display a DEBUG {@link String} representation of this object. - */ - @Override - public String toString() { - return String.format("%s: [%s]", "" + type, content == null ? "N/A" - : content); - } - - @Override - public Paragraph clone() { - Paragraph para = null; - try { - para = (Paragraph) super.clone(); - } catch (CloneNotSupportedException e) { - // Did the clones rebel? - System.err.println(e); - } - - return para; - } -} diff --git a/data/Story.java b/data/Story.java deleted file mode 100644 index fc3f909..0000000 --- a/data/Story.java +++ /dev/null @@ -1,101 +0,0 @@ -package be.nikiroo.fanfix.data; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -/** - * The main data class, where the whole story resides. - * - * @author niki - */ -public class Story implements Iterable, Cloneable, Serializable { - private static final long serialVersionUID = 1L; - - private MetaData meta; - private List chapters = new ArrayList(); - private List empty = new ArrayList(); - - /** - * The metadata about this {@link Story}. - * - * @return the meta - */ - public MetaData getMeta() { - return meta; - } - - /** - * The metadata about this {@link Story}. - * - * @param meta - * the meta to set - */ - public void setMeta(MetaData meta) { - this.meta = meta; - } - - /** - * The chapters of the story. - * - * @return the chapters - */ - public List getChapters() { - return chapters; - } - - /** - * The chapters of the story. - * - * @param chapters - * the chapters to set - */ - public void setChapters(List chapters) { - this.chapters = chapters; - } - - /** - * Get an iterator on the {@link Chapter}s. - */ - @Override - public Iterator iterator() { - return chapters == null ? empty.iterator() : chapters.iterator(); - } - - /** - * Display a DEBUG {@link String} representation of this object. - *

- * This is not efficient, nor intended to be. - */ - @Override - public String toString() { - if (getMeta() != null) - return "Story: [\n" + getMeta().toString() + "\n]"; - return "Story: [ no metadata found ]"; - } - - @Override - public Story clone() { - Story story = null; - try { - story = (Story) super.clone(); - } catch (CloneNotSupportedException e) { - // Did the clones rebel? - System.err.println(e); - } - - if (meta != null) { - story.meta = meta.clone(); - } - - if (chapters != null) { - story.chapters = new ArrayList(); - for (Chapter chap : chapters) { - story.chapters.add(chap.clone()); - } - } - - return story; - } -} diff --git a/data/package-info.java b/data/package-info.java deleted file mode 100644 index 57db36b..0000000 --- a/data/package-info.java +++ /dev/null @@ -1,9 +0,0 @@ -/** - * This package contains the data structure used by the program, without the - * logic behind them. - *

- * All the classes inside are serializable. - * - * @author niki - */ -package be.nikiroo.fanfix.data; \ No newline at end of file diff --git a/derename.sh b/derename.sh deleted file mode 100755 index 6c8cbff..0000000 --- a/derename.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -git status | grep renamed: | sed 's/[^:]*: *\([^>]*\) -> \(.*\)/\1>\2/g' | while read -r ln; do - old="`echo "$ln" | cut -f1 -d'>'`" - new="`echo "$ln" | cut -f2 -d'>'`" - mkdir -p "`dirname "$old"`" - git mv "$new" "$old" - rmdir "`dirname "$new"`" 2>/dev/null - true -done - diff --git a/library/BasicLibrary.java b/library/BasicLibrary.java deleted file mode 100644 index f77d0ed..0000000 --- a/library/BasicLibrary.java +++ /dev/null @@ -1,998 +0,0 @@ -package be.nikiroo.fanfix.library; - -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.net.UnknownHostException; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix.data.MetaData; -import be.nikiroo.fanfix.data.Story; -import be.nikiroo.fanfix.output.BasicOutput; -import be.nikiroo.fanfix.output.BasicOutput.OutputType; -import be.nikiroo.fanfix.supported.BasicSupport; -import be.nikiroo.fanfix.supported.SupportType; -import be.nikiroo.utils.Image; -import be.nikiroo.utils.Progress; -import be.nikiroo.utils.StringUtils; - -/** - * Manage a library of Stories: import, export, list, modify. - *

- * Each {@link Story} object will be associated with a (local to the library) - * unique ID, the LUID, which will be used to identify the {@link Story}. - *

- * Most of the {@link BasicLibrary} functions work on a partial (cover - * MAY not be included) {@link MetaData} object. - * - * @author niki - */ -abstract public class BasicLibrary { - /** - * A {@link BasicLibrary} status. - * - * @author niki - */ - public enum Status { - /** The library is ready and r/w. */ - READ_WRITE, - /** The library is ready, but read-only. */ - READ_ONLY, - /** You are not allowed to access this library. */ - UNAUTHORIZED, - /** The library is invalid, and will never work as is. */ - INVALID, - /** The library is currently out of commission, but may work later. */ - UNAVAILABLE; - - /** - * The library is available (you can query it). - *

- * It does not specify if it is read-only or not. - * - * @return TRUE if it is - */ - public boolean isReady() { - return (this == READ_WRITE || this == READ_ONLY); - } - - /** - * This library can be modified (= you are allowed to modify it). - * - * @return TRUE if it is - */ - public boolean isWritable() { - return (this == READ_WRITE); - } - } - - /** - * Return a name for this library (the UI may display this). - *

- * Must not be NULL. - * - * @return the name, or an empty {@link String} if none - */ - public String getLibraryName() { - return ""; - } - - /** - * The library status. - * - * @return the current status - */ - public Status getStatus() { - return Status.READ_WRITE; - } - - /** - * Retrieve the main {@link File} corresponding to the given {@link Story}, - * which can be passed to an external reader or instance. - *

- * Do NOT alter this file. - * - * @param luid - * the Library UID of the story, can be NULL - * @param pg - * the optional {@link Progress} - * - * @return the corresponding {@link Story} - * - * @throws IOException - * in case of IOException - */ - public abstract File getFile(String luid, Progress pg) throws IOException; - - /** - * Return the cover image associated to this story. - * - * @param luid - * the Library UID of the story - * - * @return the cover image - * - * @throws IOException - * in case of IOException - */ - public abstract Image getCover(String luid) throws IOException; - - /** - * Retrieve the list of {@link MetaData} known by this {@link BasicLibrary} - * in a easy-to-filter version. - * - * @param pg - * the optional {@link Progress} - * @return the list of {@link MetaData} as a {@link MetaResultList} you can - * query - * @throws IOException - * in case of I/O eror - */ - public MetaResultList getList(Progress pg) throws IOException { - // TODO: ensure it is the main used interface - - return new MetaResultList(getMetas(pg)); - } - - // TODO: make something for (normal and custom) non-story covers - - /** - * Return the cover image associated to this source. - *

- * By default, return the custom cover if any, and if not, return the cover - * of the first story with this source. - * - * @param source - * the source - * - * @return the cover image or NULL - * - * @throws IOException - * in case of IOException - */ - public Image getSourceCover(String source) throws IOException { - Image custom = getCustomSourceCover(source); - if (custom != null) { - return custom; - } - - List metas = getList().filter(source, null, null); - if (metas.size() > 0) { - return getCover(metas.get(0).getLuid()); - } - - return null; - } - - /** - * Return the cover image associated to this author. - *

- * By default, return the custom cover if any, and if not, return the cover - * of the first story with this author. - * - * @param author - * the author - * - * @return the cover image or NULL - * - * @throws IOException - * in case of IOException - */ - public Image getAuthorCover(String author) throws IOException { - Image custom = getCustomAuthorCover(author); - if (custom != null) { - return custom; - } - - List metas = getList().filter(null, author, null); - if (metas.size() > 0) { - return getCover(metas.get(0).getLuid()); - } - - return null; - } - - /** - * Return the custom cover image associated to this source. - *

- * By default, return NULL. - * - * @param source - * the source to look for - * - * @return the custom cover or NULL if none - * - * @throws IOException - * in case of IOException - */ - @SuppressWarnings("unused") - public Image getCustomSourceCover(String source) throws IOException { - return null; - } - - /** - * Return the custom cover image associated to this author. - *

- * By default, return NULL. - * - * @param author - * the author to look for - * - * @return the custom cover or NULL if none - * - * @throws IOException - * in case of IOException - */ - @SuppressWarnings("unused") - public Image getCustomAuthorCover(String author) throws IOException { - return null; - } - - /** - * Set the source cover to the given story cover. - * - * @param source - * the source to change - * @param luid - * the story LUID - * - * @throws IOException - * in case of IOException - */ - public abstract void setSourceCover(String source, String luid) - throws IOException; - - /** - * Set the author cover to the given story cover. - * - * @param author - * the author to change - * @param luid - * the story LUID - * - * @throws IOException - * in case of IOException - */ - public abstract void setAuthorCover(String author, String luid) - throws IOException; - - /** - * Return the list of stories (represented by their {@link MetaData}, which - * MAY not have the cover included). - *

- * The returned list MUST be a copy, not the original one. - * - * @param pg - * the optional {@link Progress} - * - * @return the list (can be empty but not NULL) - * - * @throws IOException - * in case of IOException - */ - protected abstract List getMetas(Progress pg) throws IOException; - - /** - * Invalidate the {@link Story} cache (when the content should be re-read - * because it was changed). - */ - protected void invalidateInfo() { - invalidateInfo(null); - } - - /** - * Invalidate the {@link Story} cache (when the content is removed). - *

- * All the cache can be deleted if NULL is passed as meta. - * - * @param luid - * the LUID of the {@link Story} to clear from the cache, or NULL - * for all stories - */ - protected abstract void invalidateInfo(String luid); - - /** - * Invalidate the {@link Story} cache (when the content has changed, but we - * already have it) with the new given meta. - * - * @param meta - * the {@link Story} to clear from the cache - * - * @throws IOException - * in case of IOException - */ - protected abstract void updateInfo(MetaData meta) throws IOException; - - /** - * Return the next LUID that can be used. - * - * @return the next luid - */ - protected abstract String getNextId(); - - /** - * Delete the target {@link Story}. - * - * @param luid - * the LUID of the {@link Story} - * - * @throws IOException - * in case of I/O error or if the {@link Story} wa not found - */ - protected abstract void doDelete(String luid) throws IOException; - - /** - * Actually save the story to the back-end. - * - * @param story - * the {@link Story} to save - * @param pg - * the optional {@link Progress} - * - * @return the saved {@link Story} (which may have changed, especially - * regarding the {@link MetaData}) - * - * @throws IOException - * in case of I/O error - */ - protected abstract Story doSave(Story story, Progress pg) - throws IOException; - - /** - * Refresh the {@link BasicLibrary}, that is, make sure all metas are - * loaded. - * - * @param pg - * the optional progress reporter - */ - public void refresh(Progress pg) { - try { - getMetas(pg); - } catch (IOException e) { - // We will let it fail later - } - } - - /** - * Check if the {@link Story} denoted by this Library UID is present in the - * cache (if we have no cache, we default to true). - * - * @param luid - * the Library UID - * - * @return TRUE if it is - */ - public boolean isCached(@SuppressWarnings("unused") String luid) { - // By default, everything is cached - return true; - } - - /** - * Clear the {@link Story} from the cache, if needed. - *

- * The next time we try to retrieve the {@link Story}, it may be required to - * cache it again. - * - * @param luid - * the story to clear - * - * @throws IOException - * in case of I/O error - */ - @SuppressWarnings("unused") - public void clearFromCache(String luid) throws IOException { - // By default, this is a noop. - } - - /** - * @return the same as getList() - * @throws IOException - * in case of I/O error - * @deprecated please use {@link BasicLibrary#getList()} and - * {@link MetaResultList#getSources()} instead. - */ - @Deprecated - public List getSources() throws IOException { - return getList().getSources(); - } - - /** - * @return the same as getList() - * @throws IOException - * in case of I/O error - * @deprecated please use {@link BasicLibrary#getList()} and - * {@link MetaResultList#getSourcesGrouped()} instead. - */ - @Deprecated - public Map> getSourcesGrouped() throws IOException { - return getList().getSourcesGrouped(); - } - - /** - * @return the same as getList() - * @throws IOException - * in case of I/O error - * @deprecated please use {@link BasicLibrary#getList()} and - * {@link MetaResultList#getAuthors()} instead. - */ - @Deprecated - public List getAuthors() throws IOException { - return getList().getAuthors(); - } - - /** - * @return the same as getList() - * @throws IOException - * in case of I/O error - * @deprecated please use {@link BasicLibrary#getList()} and - * {@link MetaResultList#getAuthorsGrouped()} instead. - */ - @Deprecated - public Map> getAuthorsGrouped() throws IOException { - return getList().getAuthorsGrouped(); - } - - /** - * List all the stories in the {@link BasicLibrary}. - *

- * Cover images MAYBE not included. - * - * @return the stories - * - * @throws IOException - * in case of IOException - */ - public MetaResultList getList() throws IOException { - return getList(null); - } - - /** - * Retrieve a {@link MetaData} corresponding to the given {@link Story}, - * cover image MAY not be included. - * - * @param luid - * the Library UID of the story, can be NULL - * - * @return the corresponding {@link Story} or NULL if not found - * - * @throws IOException - * in case of IOException - */ - public MetaData getInfo(String luid) throws IOException { - if (luid != null) { - for (MetaData meta : getMetas(null)) { - if (luid.equals(meta.getLuid())) { - return meta; - } - } - } - - return null; - } - - /** - * Retrieve a specific {@link Story}. - *

- * Note that it will update both the cover and the resume in meta. - * - * @param luid - * the Library UID of the story - * @param pg - * the optional progress reporter - * - * @return the corresponding {@link Story} or NULL if not found - * - * @throws IOException - * in case of IOException - */ - public Story getStory(String luid, Progress pg) throws IOException { - Progress pgMetas = new Progress(); - Progress pgStory = new Progress(); - if (pg != null) { - pg.setMinMax(0, 100); - pg.addProgress(pgMetas, 10); - pg.addProgress(pgStory, 90); - } - - MetaData meta = null; - for (MetaData oneMeta : getMetas(pgMetas)) { - if (oneMeta.getLuid().equals(luid)) { - meta = oneMeta; - break; - } - } - - pgMetas.done(); - - Story story = getStory(luid, meta, pgStory); - pgStory.done(); - - return story; - } - - /** - * Retrieve a specific {@link Story}. - *

- * Note that it will update both the cover and the resume in meta. - * - * @param luid - * the LUID of the story - * @param meta - * the meta of the story - * @param pg - * the optional progress reporter - * - * @return the corresponding {@link Story} or NULL if not found - * - * @throws IOException - * in case of IOException - */ - public synchronized Story getStory(String luid, MetaData meta, Progress pg) - throws IOException { - - if (pg == null) { - pg = new Progress(); - } - - Progress pgGet = new Progress(); - Progress pgProcess = new Progress(); - - pg.setMinMax(0, 2); - pg.addProgress(pgGet, 1); - pg.addProgress(pgProcess, 1); - - Story story = null; - File file = null; - - if (luid != null && meta != null) { - file = getFile(luid, pgGet); - } - - pgGet.done(); - try { - if (file != null) { - SupportType type = SupportType.valueOfAllOkUC(meta.getType()); - if (type == null) { - throw new IOException("Unknown type: " + meta.getType()); - } - - URL url = file.toURI().toURL(); - story = BasicSupport.getSupport(type, url) // - .process(pgProcess); - - // Because we do not want to clear the meta cache: - meta.setCover(story.getMeta().getCover()); - meta.setResume(story.getMeta().getResume()); - story.setMeta(meta); - } - } catch (IOException e) { - // We should not have not-supported files in the library - Instance.getInstance().getTraceHandler() - .error(new IOException(String.format( - "Cannot load file of type '%s' from library: %s", - meta.getType(), file), e)); - } finally { - pgProcess.done(); - pg.done(); - } - - return story; - } - - /** - * Import the {@link Story} at the given {@link URL} into the - * {@link BasicLibrary}. - * - * @param url - * the {@link URL} to import - * @param pg - * the optional progress reporter - * - * @return the imported Story {@link MetaData} - * - * @throws UnknownHostException - * if the host is not supported - * @throws IOException - * in case of I/O error - */ - public MetaData imprt(URL url, Progress pg) throws IOException { - return imprt(url, null, pg); - } - - /** - * Import the {@link Story} at the given {@link URL} into the - * {@link BasicLibrary}. - * - * @param url - * the {@link URL} to import - * @param luid - * the LUID to use - * @param pg - * the optional progress reporter - * - * @return the imported Story {@link MetaData} - * - * @throws UnknownHostException - * if the host is not supported - * @throws IOException - * in case of I/O error - */ - MetaData imprt(URL url, String luid, Progress pg) throws IOException { - if (pg == null) - pg = new Progress(); - - pg.setMinMax(0, 1000); - Progress pgProcess = new Progress(); - Progress pgSave = new Progress(); - pg.addProgress(pgProcess, 800); - pg.addProgress(pgSave, 200); - - BasicSupport support = BasicSupport.getSupport(url); - if (support == null) { - throw new UnknownHostException("" + url); - } - - Story story = save(support.process(pgProcess), luid, pgSave); - pg.done(); - - return story.getMeta(); - } - - /** - * Import the story from one library to another, and keep the same LUID. - * - * @param other - * the other library to import from - * @param luid - * the Library UID - * @param pg - * the optional progress reporter - * - * @throws IOException - * in case of I/O error - */ - public void imprt(BasicLibrary other, String luid, Progress pg) - throws IOException { - Progress pgGetStory = new Progress(); - Progress pgSave = new Progress(); - if (pg == null) { - pg = new Progress(); - } - - pg.setMinMax(0, 2); - pg.addProgress(pgGetStory, 1); - pg.addProgress(pgSave, 1); - - Story story = other.getStory(luid, pgGetStory); - if (story != null) { - story = this.save(story, luid, pgSave); - pg.done(); - } else { - pg.done(); - throw new IOException("Cannot find story in Library: " + luid); - } - } - - /** - * Export the {@link Story} to the given target in the given format. - * - * @param luid - * the {@link Story} ID - * @param type - * the {@link OutputType} to transform it to - * @param target - * the target to save to - * @param pg - * the optional progress reporter - * - * @return the saved resource (the main saved {@link File}) - * - * @throws IOException - * in case of I/O error - */ - public File export(String luid, OutputType type, String target, Progress pg) - throws IOException { - Progress pgGetStory = new Progress(); - Progress pgOut = new Progress(); - if (pg != null) { - pg.setMax(2); - pg.addProgress(pgGetStory, 1); - pg.addProgress(pgOut, 1); - } - - BasicOutput out = BasicOutput.getOutput(type, false, false); - if (out == null) { - throw new IOException("Output type not supported: " + type); - } - - Story story = getStory(luid, pgGetStory); - if (story == null) { - throw new IOException("Cannot find story to export: " + luid); - } - - return out.process(story, target, pgOut); - } - - /** - * Save a {@link Story} to the {@link BasicLibrary}. - * - * @param story - * the {@link Story} to save - * @param pg - * the optional progress reporter - * - * @return the same {@link Story}, whose LUID may have changed - * - * @throws IOException - * in case of I/O error - */ - public Story save(Story story, Progress pg) throws IOException { - return save(story, null, pg); - } - - /** - * Save a {@link Story} to the {@link BasicLibrary} -- the LUID must - * be correct, or NULL to get the next free one. - *

- * Will override any previous {@link Story} with the same LUID. - * - * @param story - * the {@link Story} to save - * @param luid - * the correct LUID or NULL to get the next free one - * @param pg - * the optional progress reporter - * - * @return the same {@link Story}, whose LUID may have changed - * - * @throws IOException - * in case of I/O error - */ - public synchronized Story save(Story story, String luid, Progress pg) - throws IOException { - if (pg == null) { - pg = new Progress(); - } - - Instance.getInstance().getTraceHandler().trace( - this.getClass().getSimpleName() + ": saving story " + luid); - - // Do not change the original metadata, but change the original story - MetaData meta = story.getMeta().clone(); - story.setMeta(meta); - - pg.setName("Saving story"); - - if (luid == null || luid.isEmpty()) { - meta.setLuid(getNextId()); - } else { - meta.setLuid(luid); - } - - if (luid != null && getInfo(luid) != null) { - delete(luid); - } - - story = doSave(story, pg); - - updateInfo(story.getMeta()); - - Instance.getInstance().getTraceHandler() - .trace(this.getClass().getSimpleName() + ": story saved (" - + luid + ")"); - - pg.setName(meta.getTitle()); - pg.done(); - return story; - } - - /** - * Delete the given {@link Story} from this {@link BasicLibrary}. - * - * @param luid - * the LUID of the target {@link Story} - * - * @throws IOException - * in case of I/O error - */ - public synchronized void delete(String luid) throws IOException { - Instance.getInstance().getTraceHandler().trace( - this.getClass().getSimpleName() + ": deleting story " + luid); - - doDelete(luid); - invalidateInfo(luid); - - Instance.getInstance().getTraceHandler() - .trace(this.getClass().getSimpleName() + ": story deleted (" - + luid + ")"); - } - - /** - * Change the type (source) of the given {@link Story}. - * - * @param luid - * the {@link Story} LUID - * @param newSource - * the new source - * @param pg - * the optional progress reporter - * - * @throws IOException - * in case of I/O error or if the {@link Story} was not found - */ - public synchronized void changeSource(String luid, String newSource, - Progress pg) throws IOException { - MetaData meta = getInfo(luid); - if (meta == null) { - throw new IOException("Story not found: " + luid); - } - - changeSTA(luid, newSource, meta.getTitle(), meta.getAuthor(), pg); - } - - /** - * Change the title (name) of the given {@link Story}. - * - * @param luid - * the {@link Story} LUID - * @param newTitle - * the new title - * @param pg - * the optional progress reporter - * - * @throws IOException - * in case of I/O error or if the {@link Story} was not found - */ - public synchronized void changeTitle(String luid, String newTitle, - Progress pg) throws IOException { - MetaData meta = getInfo(luid); - if (meta == null) { - throw new IOException("Story not found: " + luid); - } - - changeSTA(luid, meta.getSource(), newTitle, meta.getAuthor(), pg); - } - - /** - * Change the author of the given {@link Story}. - * - * @param luid - * the {@link Story} LUID - * @param newAuthor - * the new author - * @param pg - * the optional progress reporter - * - * @throws IOException - * in case of I/O error or if the {@link Story} was not found - */ - public synchronized void changeAuthor(String luid, String newAuthor, - Progress pg) throws IOException { - MetaData meta = getInfo(luid); - if (meta == null) { - throw new IOException("Story not found: " + luid); - } - - changeSTA(luid, meta.getSource(), meta.getTitle(), newAuthor, pg); - } - - /** - * Change the Source, Title and Author of the {@link Story} in one single - * go. - * - * @param luid - * the {@link Story} LUID - * @param newSource - * the new source - * @param newTitle - * the new title - * @param newAuthor - * the new author - * @param pg - * the optional progress reporter - * - * @throws IOException - * in case of I/O error or if the {@link Story} was not found - */ - protected synchronized void changeSTA(String luid, String newSource, - String newTitle, String newAuthor, Progress pg) throws IOException { - MetaData meta = getInfo(luid); - if (meta == null) { - throw new IOException("Story not found: " + luid); - } - - meta.setSource(newSource); - meta.setTitle(newTitle); - meta.setAuthor(newAuthor); - saveMeta(meta, pg); - } - - /** - * Save back the current state of the {@link MetaData} (LUID MUST NOT - * change) for this {@link Story}. - *

- * By default, delete the old {@link Story} then recreate a new - * {@link Story}. - *

- * Note that this behaviour can lead to data loss in case of problems! - * - * @param meta - * the new {@link MetaData} (LUID MUST NOT change) - * @param pg - * the optional {@link Progress} - * - * @throws IOException - * in case of I/O error or if the {@link Story} was not found - */ - protected synchronized void saveMeta(MetaData meta, Progress pg) - throws IOException { - if (pg == null) { - pg = new Progress(); - } - - Progress pgGet = new Progress(); - Progress pgSet = new Progress(); - pg.addProgress(pgGet, 50); - pg.addProgress(pgSet, 50); - - Story story = getStory(meta.getLuid(), pgGet); - if (story == null) { - throw new IOException("Story not found: " + meta.getLuid()); - } - - // TODO: this is not safe! - delete(meta.getLuid()); - story.setMeta(meta); - save(story, meta.getLuid(), pgSet); - - pg.done(); - } - - /** - * Describe a {@link Story} from its {@link MetaData} and return a list of - * title/value that represent this {@link Story}. - * - * @param meta - * the {@link MetaData} to represent - * - * @return the information, translated and sorted - */ - static public Map getMetaDesc(MetaData meta) { - Map metaDesc = new LinkedHashMap(); - - // TODO: i18n - - StringBuilder tags = new StringBuilder(); - for (String tag : meta.getTags()) { - if (tags.length() > 0) { - tags.append(", "); - } - tags.append(tag); - } - - // TODO: i18n - metaDesc.put("Author", meta.getAuthor()); - metaDesc.put("Published on", meta.getPublisher()); - metaDesc.put("Publication date", meta.getDate()); - metaDesc.put("Creation date", meta.getCreationDate()); - String count = ""; - if (meta.getWords() > 0) { - count = StringUtils.formatNumber(meta.getWords()); - } - if (meta.isImageDocument()) { - metaDesc.put("Number of images", count); - } else { - metaDesc.put("Number of words", count); - } - metaDesc.put("Source", meta.getSource()); - metaDesc.put("Subject", meta.getSubject()); - metaDesc.put("Language", meta.getLang()); - metaDesc.put("Tags", tags.toString()); - metaDesc.put("URL", meta.getUrl()); - - return metaDesc; - } -} diff --git a/library/CacheLibrary.java b/library/CacheLibrary.java deleted file mode 100644 index e184c1b..0000000 --- a/library/CacheLibrary.java +++ /dev/null @@ -1,435 +0,0 @@ -package be.nikiroo.fanfix.library; - -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; -import java.util.TreeSet; - -import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix.bundles.UiConfig; -import be.nikiroo.fanfix.bundles.UiConfigBundle; -import be.nikiroo.fanfix.data.MetaData; -import be.nikiroo.fanfix.data.Story; -import be.nikiroo.fanfix.output.BasicOutput.OutputType; -import be.nikiroo.utils.Image; -import be.nikiroo.utils.Progress; - -/** - * This library will cache another pre-existing {@link BasicLibrary}. - * - * @author niki - */ -public class CacheLibrary extends BasicLibrary { - private List metasReal; - private List metasMixed; - private Object metasLock = new Object(); - - private BasicLibrary lib; - private LocalLibrary cacheLib; - - /** - * Create a cache library around the given one. - *

- * It will return the same result, but those will be saved to disk at the - * same time to be fetched quicker the next time. - * - * @param cacheDir - * the cache directory where to save the files to disk - * @param lib - * the original library to wrap - * @param config - * the configuration used to know which kind of default - * {@link OutputType} to use for images and non-images stories - */ - public CacheLibrary(File cacheDir, BasicLibrary lib, - UiConfigBundle config) { - this.cacheLib = new LocalLibrary(cacheDir, // - config.getString(UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE), - config.getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE), true); - this.lib = lib; - } - - @Override - public String getLibraryName() { - return lib.getLibraryName(); - } - - @Override - public Status getStatus() { - return lib.getStatus(); - } - - @Override - protected List getMetas(Progress pg) throws IOException { - if (pg == null) { - pg = new Progress(); - } - - List copy; - synchronized (metasLock) { - // We make sure that cached metas have precedence - if (metasMixed == null) { - if (metasReal == null) { - metasReal = lib.getMetas(pg); - } - - metasMixed = new ArrayList(); - TreeSet cachedLuids = new TreeSet(); - for (MetaData cachedMeta : cacheLib.getMetas(null)) { - metasMixed.add(cachedMeta); - cachedLuids.add(cachedMeta.getLuid()); - } - for (MetaData realMeta : metasReal) { - if (!cachedLuids.contains(realMeta.getLuid())) { - metasMixed.add(realMeta); - } - } - } - - copy = new ArrayList(metasMixed); - } - - pg.done(); - return copy; - } - - @Override - public synchronized Story getStory(String luid, MetaData meta, Progress pg) - throws IOException { - if (pg == null) { - pg = new Progress(); - } - - Progress pgImport = new Progress(); - Progress pgGet = new Progress(); - - pg.setMinMax(0, 4); - pg.addProgress(pgImport, 3); - pg.addProgress(pgGet, 1); - - if (!isCached(luid)) { - try { - cacheLib.imprt(lib, luid, pgImport); - updateMetaCache(metasMixed, cacheLib.getInfo(luid)); - pgImport.done(); - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - } - - pgImport.done(); - pgGet.done(); - } - - String type = cacheLib.getOutputType(meta.isImageDocument()); - MetaData cachedMeta = meta.clone(); - cachedMeta.setType(type); - - return cacheLib.getStory(luid, cachedMeta, pg); - } - - @Override - public synchronized File getFile(final String luid, Progress pg) - throws IOException { - if (pg == null) { - pg = new Progress(); - } - - Progress pgGet = new Progress(); - Progress pgRecall = new Progress(); - - pg.setMinMax(0, 5); - pg.addProgress(pgGet, 4); - pg.addProgress(pgRecall, 1); - - if (!isCached(luid)) { - getStory(luid, pgGet); - pgGet.done(); - } - - File file = cacheLib.getFile(luid, pgRecall); - pgRecall.done(); - - pg.done(); - return file; - } - - @Override - public Image getCover(final String luid) throws IOException { - if (isCached(luid)) { - return cacheLib.getCover(luid); - } - - // We could update the cache here, but it's not easy - return lib.getCover(luid); - } - - @Override - public Image getSourceCover(String source) throws IOException { - Image custom = getCustomSourceCover(source); - if (custom != null) { - return custom; - } - - Image cached = cacheLib.getSourceCover(source); - if (cached != null) { - return cached; - } - - return lib.getSourceCover(source); - } - - @Override - public Image getAuthorCover(String author) throws IOException { - Image custom = getCustomAuthorCover(author); - if (custom != null) { - return custom; - } - - Image cached = cacheLib.getAuthorCover(author); - if (cached != null) { - return cached; - } - - return lib.getAuthorCover(author); - } - - @Override - public Image getCustomSourceCover(String source) throws IOException { - Image custom = cacheLib.getCustomSourceCover(source); - if (custom == null) { - custom = lib.getCustomSourceCover(source); - if (custom != null) { - cacheLib.setSourceCover(source, custom); - } - } - - return custom; - } - - @Override - public Image getCustomAuthorCover(String author) throws IOException { - Image custom = cacheLib.getCustomAuthorCover(author); - if (custom == null) { - custom = lib.getCustomAuthorCover(author); - if (custom != null) { - cacheLib.setAuthorCover(author, custom); - } - } - - return custom; - } - - @Override - public void setSourceCover(String source, String luid) throws IOException { - lib.setSourceCover(source, luid); - cacheLib.setSourceCover(source, getCover(luid)); - } - - @Override - public void setAuthorCover(String author, String luid) throws IOException { - lib.setAuthorCover(author, luid); - cacheLib.setAuthorCover(author, getCover(luid)); - } - - /** - * Invalidate the {@link Story} cache (when the content has changed, but we - * already have it) with the new given meta. - *

- * Make sure to always use {@link MetaData} from the cached library in - * priority, here. - * - * @param meta - * the {@link Story} to clear from the cache - * - * @throws IOException - * in case of IOException - */ - @Override - @Deprecated - protected void updateInfo(MetaData meta) throws IOException { - throw new IOException( - "This method is not supported in a CacheLibrary, please use updateMetaCache"); - } - - // relplace the meta in Metas by Meta, add it if needed - // return TRUE = added - private boolean updateMetaCache(List metas, MetaData meta) { - if (meta != null && metas != null) { - synchronized (metasLock) { - boolean changed = false; - for (int i = 0; i < metas.size(); i++) { - if (metas.get(i).getLuid().equals(meta.getLuid())) { - metas.set(i, meta); - changed = true; - } - } - - if (!changed) { - metas.add(meta); - return true; - } - } - } - - return false; - } - - @Override - protected void invalidateInfo(String luid) { - if (luid == null) { - synchronized (metasLock) { - metasReal = null; - metasMixed = null; - } - } else { - invalidateInfo(metasReal, luid); - invalidateInfo(metasMixed, luid); - } - - cacheLib.invalidateInfo(luid); - lib.invalidateInfo(luid); - } - - // luid cannot be null - private void invalidateInfo(List metas, String luid) { - if (metas != null) { - synchronized (metasLock) { - for (int i = 0; i < metas.size(); i++) { - if (metas.get(i).getLuid().equals(luid)) { - metas.remove(i--); - } - } - } - } - } - - @Override - public synchronized Story save(Story story, String luid, Progress pg) - throws IOException { - Progress pgLib = new Progress(); - Progress pgCacheLib = new Progress(); - - if (pg == null) { - pg = new Progress(); - } - - pg.setMinMax(0, 2); - pg.addProgress(pgLib, 1); - pg.addProgress(pgCacheLib, 1); - - story = lib.save(story, luid, pgLib); - updateMetaCache(metasReal, story.getMeta()); - - story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib); - updateMetaCache(metasMixed, story.getMeta()); - - return story; - } - - @Override - public synchronized void delete(String luid) throws IOException { - if (isCached(luid)) { - cacheLib.delete(luid); - } - lib.delete(luid); - - invalidateInfo(luid); - } - - @Override - protected synchronized void changeSTA(String luid, String newSource, - String newTitle, String newAuthor, Progress pg) throws IOException { - if (pg == null) { - pg = new Progress(); - } - - Progress pgCache = new Progress(); - Progress pgOrig = new Progress(); - pg.setMinMax(0, 2); - pg.addProgress(pgCache, 1); - pg.addProgress(pgOrig, 1); - - MetaData meta = getInfo(luid); - if (meta == null) { - throw new IOException("Story not found: " + luid); - } - - if (isCached(luid)) { - cacheLib.changeSTA(luid, newSource, newTitle, newAuthor, pgCache); - } - pgCache.done(); - - lib.changeSTA(luid, newSource, newTitle, newAuthor, pgOrig); - pgOrig.done(); - - meta.setSource(newSource); - meta.setTitle(newTitle); - meta.setAuthor(newAuthor); - pg.done(); - - if (isCached(luid)) { - updateMetaCache(metasMixed, meta); - updateMetaCache(metasReal, lib.getInfo(luid)); - } else { - updateMetaCache(metasReal, meta); - } - } - - @Override - public boolean isCached(String luid) { - try { - return cacheLib.getInfo(luid) != null; - } catch (IOException e) { - return false; - } - } - - @Override - public void clearFromCache(String luid) throws IOException { - if (isCached(luid)) { - cacheLib.delete(luid); - } - } - - @Override - public MetaData imprt(URL url, Progress pg) throws IOException { - if (pg == null) { - pg = new Progress(); - } - - Progress pgImprt = new Progress(); - Progress pgCache = new Progress(); - pg.setMinMax(0, 10); - pg.addProgress(pgImprt, 7); - pg.addProgress(pgCache, 3); - - MetaData meta = lib.imprt(url, pgImprt); - updateMetaCache(metasReal, meta); - metasMixed = null; - - clearFromCache(meta.getLuid()); - - pg.done(); - return meta; - } - - // All the following methods are only used by Save and Delete in - // BasicLibrary: - - @Override - protected String getNextId() { - throw new java.lang.InternalError("Should not have been called"); - } - - @Override - protected void doDelete(String luid) throws IOException { - throw new java.lang.InternalError("Should not have been called"); - } - - @Override - protected Story doSave(Story story, Progress pg) throws IOException { - throw new java.lang.InternalError("Should not have been called"); - } -} diff --git a/library/LocalLibrary.java b/library/LocalLibrary.java deleted file mode 100644 index 25f2ec9..0000000 --- a/library/LocalLibrary.java +++ /dev/null @@ -1,778 +0,0 @@ -package be.nikiroo.fanfix.library; - -import java.io.File; -import java.io.FileFilter; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix.bundles.Config; -import be.nikiroo.fanfix.bundles.ConfigBundle; -import be.nikiroo.fanfix.data.MetaData; -import be.nikiroo.fanfix.data.Story; -import be.nikiroo.fanfix.output.BasicOutput; -import be.nikiroo.fanfix.output.BasicOutput.OutputType; -import be.nikiroo.fanfix.output.InfoCover; -import be.nikiroo.fanfix.supported.InfoReader; -import be.nikiroo.utils.HashUtils; -import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.Image; -import be.nikiroo.utils.Progress; - -/** - * This {@link BasicLibrary} will store the stories locally on disk. - * - * @author niki - */ -public class LocalLibrary extends BasicLibrary { - private int lastId; - private Object lock = new Object(); - private Map stories; // Files: [ infoFile, TargetFile ] - private Map sourceCovers; - private Map authorCovers; - - private File baseDir; - private OutputType text; - private OutputType image; - - /** - * Create a new {@link LocalLibrary} with the given back-end directory. - * - * @param baseDir - * the directory where to find the {@link Story} objects - * @param config - * the configuration used to know which kind of default - * {@link OutputType} to use for images and non-images stories - */ - public LocalLibrary(File baseDir, ConfigBundle config) { - this(baseDir, // - config.getString(Config.FILE_FORMAT_NON_IMAGES_DOCUMENT_TYPE), - config.getString(Config.FILE_FORMAT_IMAGES_DOCUMENT_TYPE), - false); - } - - /** - * Create a new {@link LocalLibrary} with the given back-end directory. - * - * @param baseDir - * the directory where to find the {@link Story} objects - * @param text - * the {@link OutputType} to use for non-image documents - * @param image - * the {@link OutputType} to use for image documents - * @param defaultIsHtml - * if the given text or image is invalid, use HTML by default (if - * not, it will be INFO_TEXT/CBZ by default) - */ - public LocalLibrary(File baseDir, String text, String image, - boolean defaultIsHtml) { - this(baseDir, - OutputType.valueOfAllOkUC(text, - defaultIsHtml ? OutputType.HTML : OutputType.INFO_TEXT), - OutputType.valueOfAllOkUC(image, - defaultIsHtml ? OutputType.HTML : OutputType.CBZ)); - } - - /** - * Create a new {@link LocalLibrary} with the given back-end directory. - * - * @param baseDir - * the directory where to find the {@link Story} objects - * @param text - * the {@link OutputType} to use for non-image documents - * @param image - * the {@link OutputType} to use for image documents - */ - public LocalLibrary(File baseDir, OutputType text, OutputType image) { - this.baseDir = baseDir; - this.text = text; - this.image = image; - - this.lastId = 0; - this.stories = null; - this.sourceCovers = null; - - baseDir.mkdirs(); - } - - @Override - protected List getMetas(Progress pg) { - return new ArrayList(getStories(pg).keySet()); - } - - @Override - public File getFile(String luid, Progress pg) throws IOException { - Instance.getInstance().getTraceHandler().trace( - this.getClass().getSimpleName() + ": get file for " + luid); - - File file = null; - String mess = "no file found for "; - - MetaData meta = getInfo(luid); - if (meta != null) { - File[] files = getStories(pg).get(meta); - if (files != null) { - mess = "file retrieved for "; - file = files[1]; - } - } - - Instance.getInstance().getTraceHandler() - .trace(this.getClass().getSimpleName() + ": " + mess + luid - + " (" + meta.getTitle() + ")"); - - return file; - } - - @Override - public Image getCover(String luid) throws IOException { - MetaData meta = getInfo(luid); - if (meta != null) { - if (meta.getCover() != null) { - return meta.getCover(); - } - - File[] files = getStories(null).get(meta); - if (files != null) { - File infoFile = files[0]; - - try { - meta = InfoReader.readMeta(infoFile, true); - return meta.getCover(); - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - } - } - } - - return null; - } - - @Override - protected void updateInfo(MetaData meta) { - invalidateInfo(); - } - - @Override - protected void invalidateInfo(String luid) { - synchronized (lock) { - stories = null; - sourceCovers = null; - } - } - - @Override - protected String getNextId() { - getStories(null); // make sure lastId is set - - synchronized (lock) { - return String.format("%03d", ++lastId); - } - } - - @Override - protected void doDelete(String luid) throws IOException { - for (File file : getRelatedFiles(luid)) { - // TODO: throw an IOException if we cannot delete the files? - IOUtils.deltree(file); - file.getParentFile().delete(); - } - } - - @Override - protected Story doSave(Story story, Progress pg) throws IOException { - MetaData meta = story.getMeta(); - - File expectedTarget = getExpectedFile(meta); - expectedTarget.getParentFile().mkdirs(); - - BasicOutput it = BasicOutput.getOutput(getOutputType(meta), true, true); - it.process(story, expectedTarget.getPath(), pg); - - return story; - } - - @Override - protected synchronized void saveMeta(MetaData meta, Progress pg) - throws IOException { - File newDir = getExpectedDir(meta.getSource()); - if (!newDir.exists()) { - newDir.mkdirs(); - } - - List relatedFiles = getRelatedFiles(meta.getLuid()); - for (File relatedFile : relatedFiles) { - // TODO: this is not safe at all. - // We should copy all the files THEN delete them - // Maybe also adding some rollback cleanup if possible - if (relatedFile.getName().endsWith(".info")) { - try { - String name = relatedFile.getName().replaceFirst("\\.info$", - ""); - relatedFile.delete(); - InfoCover.writeInfo(newDir, name, meta); - relatedFile.getParentFile().delete(); - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - } - } else { - relatedFile.renameTo(new File(newDir, relatedFile.getName())); - relatedFile.getParentFile().delete(); - } - } - - updateInfo(meta); - } - - @Override - public Image getCustomSourceCover(String source) { - synchronized (lock) { - if (sourceCovers == null) { - sourceCovers = new HashMap(); - } - } - - synchronized (lock) { - Image img = sourceCovers.get(source); - if (img != null) { - return img; - } - } - - File coverDir = getExpectedDir(source); - if (coverDir.isDirectory()) { - File cover = new File(coverDir, ".cover.png"); - if (cover.exists()) { - InputStream in; - try { - in = new FileInputStream(cover); - try { - synchronized (lock) { - Image img = new Image(in); - if (img.getSize() == 0) { - img.close(); - throw new IOException( - "Empty image not accepted"); - } - sourceCovers.put(source, img); - } - } finally { - in.close(); - } - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - Instance.getInstance().getTraceHandler() - .error(new IOException( - "Cannot load the existing custom source cover: " - + cover, - e)); - } - } - } - - synchronized (lock) { - return sourceCovers.get(source); - } - } - - @Override - public Image getCustomAuthorCover(String author) { - synchronized (lock) { - if (authorCovers == null) { - authorCovers = new HashMap(); - } - } - - synchronized (lock) { - Image img = authorCovers.get(author); - if (img != null) { - return img; - } - } - - File cover = getAuthorCoverFile(author); - if (cover.exists()) { - InputStream in; - try { - in = new FileInputStream(cover); - try { - synchronized (lock) { - Image img = new Image(in); - if (img.getSize() == 0) { - img.close(); - throw new IOException( - "Empty image not accepted"); - } - authorCovers.put(author, img); - } - } finally { - in.close(); - } - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - Instance.getInstance().getTraceHandler() - .error(new IOException( - "Cannot load the existing custom author cover: " - + cover, - e)); - } - } - - synchronized (lock) { - return authorCovers.get(author); - } - } - - @Override - public void setSourceCover(String source, String luid) throws IOException { - setSourceCover(source, getCover(luid)); - } - - @Override - public void setAuthorCover(String author, String luid) throws IOException { - setAuthorCover(author, getCover(luid)); - } - - /** - * Set the source cover to the given story cover. - * - * @param source - * the source to change - * @param coverImage - * the cover image - */ - void setSourceCover(String source, Image coverImage) { - File dir = getExpectedDir(source); - dir.mkdirs(); - File cover = new File(dir, ".cover"); - try { - Instance.getInstance().getCache().saveAsImage(coverImage, cover, - true); - synchronized (lock) { - if (sourceCovers != null) { - sourceCovers.put(source, coverImage); - } - } - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - } - } - - /** - * Set the author cover to the given story cover. - * - * @param author - * the author to change - * @param coverImage - * the cover image - */ - void setAuthorCover(String author, Image coverImage) { - File cover = getAuthorCoverFile(author); - cover.getParentFile().mkdirs(); - try { - Instance.getInstance().getCache().saveAsImage(coverImage, cover, - true); - synchronized (lock) { - if (authorCovers != null) { - authorCovers.put(author, coverImage); - } - } - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - } - } - - @Override - public void imprt(BasicLibrary other, String luid, Progress pg) - throws IOException { - if (pg == null) { - pg = new Progress(); - } - - // Check if we can simply copy the files instead of the whole process - if (other instanceof LocalLibrary) { - LocalLibrary otherLocalLibrary = (LocalLibrary) other; - - MetaData meta = otherLocalLibrary.getInfo(luid); - String expectedType = "" - + (meta != null && meta.isImageDocument() ? image : text); - if (meta != null && meta.getType().equals(expectedType)) { - File from = otherLocalLibrary.getExpectedDir(meta.getSource()); - File to = this.getExpectedDir(meta.getSource()); - List relatedFiles = otherLocalLibrary - .getRelatedFiles(luid); - if (!relatedFiles.isEmpty()) { - pg.setMinMax(0, relatedFiles.size()); - } - - for (File relatedFile : relatedFiles) { - File target = new File(relatedFile.getAbsolutePath() - .replace(from.getAbsolutePath(), - to.getAbsolutePath())); - if (!relatedFile.equals(target)) { - target.getParentFile().mkdirs(); - InputStream in = null; - try { - in = new FileInputStream(relatedFile); - IOUtils.write(in, target); - } catch (IOException e) { - if (in != null) { - try { - in.close(); - } catch (Exception ee) { - } - } - - pg.done(); - throw e; - } - } - - pg.add(1); - } - - invalidateInfo(); - pg.done(); - return; - } - } - - super.imprt(other, luid, pg); - } - - /** - * Return the {@link OutputType} for this {@link Story}. - * - * @param meta - * the {@link Story} {@link MetaData} - * - * @return the type - */ - private OutputType getOutputType(MetaData meta) { - if (meta != null && meta.isImageDocument()) { - return image; - } - - return text; - } - - /** - * Return the default {@link OutputType} for this kind of {@link Story}. - * - * @param imageDocument - * TRUE for images document, FALSE for text documents - * - * @return the type - */ - public String getOutputType(boolean imageDocument) { - if (imageDocument) { - return image.toString(); - } - - return text.toString(); - } - - /** - * Get the target {@link File} related to the given .info - * {@link File} and {@link MetaData}. - * - * @param meta - * the meta - * @param infoFile - * the .info {@link File} - * - * @return the target {@link File} - */ - private File getTargetFile(MetaData meta, File infoFile) { - // Replace .info with whatever is needed: - String path = infoFile.getPath(); - path = path.substring(0, path.length() - ".info".length()); - String newExt = getOutputType(meta).getDefaultExtension(true); - - return new File(path + newExt); - } - - /** - * The target (full path) where the {@link Story} related to this - * {@link MetaData} should be located on disk for a new {@link Story}. - * - * @param key - * the {@link Story} {@link MetaData} - * - * @return the target - */ - private File getExpectedFile(MetaData key) { - String title = key.getTitle(); - if (title == null) { - title = ""; - } - title = title.replaceAll("[^a-zA-Z0-9._+-]", "_"); - if (title.length() > 40) { - title = title.substring(0, 40); - } - return new File(getExpectedDir(key.getSource()), - key.getLuid() + "_" + title); - } - - /** - * The directory (full path) where the new {@link Story} related to this - * {@link MetaData} should be located on disk. - * - * @param source - * the type (source) - * - * @return the target directory - */ - private File getExpectedDir(String source) { - String sanitizedSource = source.replaceAll("[^a-zA-Z0-9._+/-]", "_"); - - while (sanitizedSource.startsWith("/") - || sanitizedSource.startsWith("_")) { - if (sanitizedSource.length() > 1) { - sanitizedSource = sanitizedSource.substring(1); - } else { - sanitizedSource = ""; - } - } - - sanitizedSource = sanitizedSource.replace("/", File.separator); - - if (sanitizedSource.isEmpty()) { - sanitizedSource = "_EMPTY"; - } - - return new File(baseDir, sanitizedSource); - } - - /** - * Return the full path to the file to use for the custom cover of this - * author. - *

- * One or more of the parent directories MAY not exist. - * - * @param author - * the author - * - * @return the custom cover file - */ - private File getAuthorCoverFile(String author) { - File aDir = new File(baseDir, "_AUTHORS"); - String hash = HashUtils.md5(author); - String ext = Instance.getInstance().getConfig() - .getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER); - return new File(aDir, hash + "." + ext.toLowerCase()); - } - - /** - * Return the list of files/directories on disk for this {@link Story}. - *

- * If the {@link Story} is not found, and empty list is returned. - * - * @param luid - * the {@link Story} LUID - * - * @return the list of {@link File}s - * - * @throws IOException - * if the {@link Story} was not found - */ - private List getRelatedFiles(String luid) throws IOException { - List files = new ArrayList(); - - MetaData meta = getInfo(luid); - if (meta == null) { - throw new IOException("Story not found: " + luid); - } - - File infoFile = getStories(null).get(meta)[0]; - File targetFile = getStories(null).get(meta)[1]; - - files.add(infoFile); - files.add(targetFile); - - String readerExt = getOutputType(meta).getDefaultExtension(true); - String fileExt = getOutputType(meta).getDefaultExtension(false); - - String path = targetFile.getAbsolutePath(); - if (readerExt != null && !readerExt.equals(fileExt)) { - path = path.substring(0, path.length() - readerExt.length()) - + fileExt; - File relatedFile = new File(path); - - if (relatedFile.exists()) { - files.add(relatedFile); - } - } - - String coverExt = "." + Instance.getInstance().getConfig() - .getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase(); - File coverFile = new File(path + coverExt); - if (!coverFile.exists()) { - coverFile = new File( - path.substring(0, path.length() - fileExt.length()) - + coverExt); - } - - if (coverFile.exists()) { - files.add(coverFile); - } - - String summaryExt = ".summary"; - File summaryFile = new File(path + summaryExt); - if (!summaryFile.exists()) { - summaryFile = new File( - path.substring(0, path.length() - fileExt.length()) - + summaryExt); - } - - if (summaryFile.exists()) { - files.add(summaryFile); - } - - return files; - } - - /** - * Fill the list of stories by reading the content of the local directory - * {@link LocalLibrary#baseDir}. - *

- * Will use a cached list when possible (see - * {@link BasicLibrary#invalidateInfo()}). - * - * @param pg - * the optional {@link Progress} - * - * @return the list of stories (for each item, the first {@link File} is the - * info file, the second file is the target {@link File}) - */ - private Map getStories(Progress pg) { - if (pg == null) { - pg = new Progress(); - } else { - pg.setMinMax(0, 100); - } - - Map stories = this.stories; - if (stories == null) { - stories = getStoriesDo(pg); - synchronized (lock) { - if (this.stories == null) - this.stories = stories; - else - stories = this.stories; - } - } - - pg.done(); - return stories; - - } - - /** - * Actually do the work of {@link LocalLibrary#getStories(Progress)} (i.e., - * do not retrieve the cache). - * - * @param pg - * the optional {@link Progress} - * - * @return the list of stories (for each item, the first {@link File} is the - * info file, the second file is the target {@link File}) - */ - private synchronized Map getStoriesDo(Progress pg) { - if (pg == null) { - pg = new Progress(); - } else { - pg.setMinMax(0, 100); - } - - Map stories = new HashMap(); - - File[] dirs = baseDir.listFiles(new FileFilter() { - @Override - public boolean accept(File file) { - return file != null && file.isDirectory(); - } - }); - - if (dirs != null) { - Progress pgDirs = new Progress(0, 100 * dirs.length); - pg.addProgress(pgDirs, 100); - - for (File dir : dirs) { - Progress pgFiles = new Progress(); - pgDirs.addProgress(pgFiles, 100); - pgDirs.setName("Loading from: " + dir.getName()); - - addToStories(stories, pgFiles, dir); - - pgFiles.setName(null); - } - - pgDirs.setName("Loading directories"); - } - - pg.done(); - - return stories; - } - - private void addToStories(Map stories, Progress pgFiles, - File dir) { - File[] infoFilesAndSubdirs = dir.listFiles(new FileFilter() { - @Override - public boolean accept(File file) { - boolean info = file != null && file.isFile() - && file.getPath().toLowerCase().endsWith(".info"); - boolean dir = file != null && file.isDirectory(); - boolean isExpandedHtml = new File(file, "index.html").isFile(); - return info || (dir && !isExpandedHtml); - } - }); - - if (pgFiles != null) { - pgFiles.setMinMax(0, infoFilesAndSubdirs.length); - } - - for (File infoFileOrSubdir : infoFilesAndSubdirs) { - if (infoFileOrSubdir.isDirectory()) { - addToStories(stories, null, infoFileOrSubdir); - } else { - try { - MetaData meta = InfoReader.readMeta(infoFileOrSubdir, - false); - try { - int id = Integer.parseInt(meta.getLuid()); - if (id > lastId) { - lastId = id; - } - - stories.put(meta, new File[] { infoFileOrSubdir, - getTargetFile(meta, infoFileOrSubdir) }); - } catch (Exception e) { - // not normal!! - throw new IOException("Cannot understand the LUID of " - + infoFileOrSubdir + ": " + meta.getLuid(), e); - } - } catch (IOException e) { - // We should not have not-supported files in the - // library - Instance.getInstance().getTraceHandler().error( - new IOException("Cannot load file from library: " - + infoFileOrSubdir, e)); - } - } - - if (pgFiles != null) { - pgFiles.add(1); - } - } - } -} diff --git a/library/MetaResultList.java b/library/MetaResultList.java deleted file mode 100644 index 8b8a167..0000000 --- a/library/MetaResultList.java +++ /dev/null @@ -1,419 +0,0 @@ -package be.nikiroo.fanfix.library; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; - -import be.nikiroo.fanfix.data.MetaData; -import be.nikiroo.utils.StringUtils; - -public class MetaResultList { - /** Max number of items before splitting in [A-B] etc. for eligible items */ - static private final int MAX = 20; - - private List metas; - - // Lazy lists: - // TODO: sync-protect them? - private List sources; - private List authors; - private List tags; - - // can be null (will consider it empty) - public MetaResultList(List metas) { - if (metas == null) { - metas = new ArrayList(); - } - - Collections.sort(metas); - this.metas = metas; - } - - // not NULL - // sorted - public List getMetas() { - return metas; - } - - public List getSources() { - if (sources == null) { - sources = new ArrayList(); - for (MetaData meta : metas) { - if (!sources.contains(meta.getSource())) - sources.add(meta.getSource()); - } - sort(sources); - } - - return sources; - } - - // A -> (A), A/ -> (A, A/*) if we can find something for "*" - public List getSources(String source) { - List linked = new ArrayList(); - if (source != null && !source.isEmpty()) { - if (!source.endsWith("/")) { - linked.add(source); - } else { - linked.add(source.substring(0, source.length() - 1)); - for (String src : getSources()) { - if (src.startsWith(source)) { - linked.add(src); - } - } - } - } - - sort(linked); - return linked; - } - - /** - * List all the known types (sources) of stories, grouped by directory - * ("Source_1/a" and "Source_1/b" will be grouped into "Source_1"). - *

- * Note that an empty item in the list means a non-grouped source (type) -- - * e.g., you could have for Source_1: - *

    - *
  • : empty, so source is "Source_1"
  • - *
  • a: empty, so source is "Source_1/a"
  • - *
  • b: empty, so source is "Source_1/b"
  • - *
- * - * @return the grouped list - * - * @throws IOException - * in case of IOException - */ - public Map> getSourcesGrouped() throws IOException { - Map> map = new TreeMap>(); - for (String source : getSources()) { - String name; - String subname; - - int pos = source.indexOf('/'); - if (pos > 0 && pos < source.length() - 1) { - name = source.substring(0, pos); - subname = source.substring(pos + 1); - - } else { - name = source; - subname = ""; - } - - List list = map.get(name); - if (list == null) { - list = new ArrayList(); - map.put(name, list); - } - list.add(subname); - } - - return map; - } - - public List getAuthors() { - if (authors == null) { - authors = new ArrayList(); - for (MetaData meta : metas) { - if (!authors.contains(meta.getAuthor())) - authors.add(meta.getAuthor()); - } - sort(authors); - } - - return authors; - } - - /** - * Return the list of authors, grouped by starting letter(s) if needed. - *

- * If the number of authors is not too high, only one group with an empty - * name and all the authors will be returned. - *

- * If not, the authors will be separated into groups: - *

    - *
  • *: any author whose name doesn't contain letters nor numbers - *
  • - *
  • 0-9: any author whose name starts with a number
  • - *
  • A-C (for instance): any author whose name starts with - * A, B or C
  • - *
- * Note that the letters used in the groups can vary (except * and - * 0-9, which may only be present or not). - * - * @return the authors' names, grouped by letter(s) - * - * @throws IOException - * in case of IOException - */ - public Map> getAuthorsGrouped() throws IOException { - return group(getAuthors()); - } - - public List getTags() { - if (tags == null) { - tags = new ArrayList(); - for (MetaData meta : metas) { - for (String tag : meta.getTags()) { - if (!tags.contains(tag)) - tags.add(tag); - } - } - sort(tags); - } - - return tags; - } - - /** - * Return the list of tags, grouped by starting letter(s) if needed. - *

- * If the number of tags is not too high, only one group with an empty name - * and all the tags will be returned. - *

- * If not, the tags will be separated into groups: - *

    - *
  • *: any tag which name doesn't contain letters nor numbers - *
  • - *
  • 0-9: any tag which name starts with a number
  • - *
  • A-C (for instance): any tag which name starts with - * A, B or C
  • - *
- * Note that the letters used in the groups can vary (except * and - * 0-9, which may only be present or not). - * - * @return the tags' names, grouped by letter(s) - * - * @throws IOException - * in case of IOException - */ - public Map> getTagsGrouped() throws IOException { - return group(getTags()); - } - - // helper - public List filter(String source, String author, String tag) { - List sources = source == null ? null : Arrays.asList(source); - List authors = author == null ? null : Arrays.asList(author); - List tags = tag == null ? null : Arrays.asList(tag); - - return filter(sources, authors, tags); - } - - // null or empty -> no check, rest = must be included - // source: a source ending in "/" means "this or any source starting with - // this", - // i;e., to enable source hierarchy - // + sorted - public List filter(List sources, List authors, - List tags) { - if (sources != null && sources.isEmpty()) - sources = null; - if (authors != null && authors.isEmpty()) - authors = null; - if (tags != null && tags.isEmpty()) - tags = null; - - // Quick check - if (sources == null && authors == null && tags == null) { - return metas; - } - - // allow "sources/" hierarchy - if (sources != null) { - List folders = new ArrayList(); - List leaves = new ArrayList(); - for (String source : sources) { - if (source.endsWith("/")) { - if (!folders.contains(source)) - folders.add(source); - } else { - if (!leaves.contains(source)) - leaves.add(source); - } - } - - sources = leaves; - for (String folder : folders) { - for (String otherLeaf : getSources(folder)) { - if (!sources.contains(otherLeaf)) { - sources.add(otherLeaf); - } - } - } - } - - List result = new ArrayList(); - for (MetaData meta : metas) { - if (sources != null && !sources.contains(meta.getSource())) { - continue; - } - if (authors != null && !authors.contains(meta.getAuthor())) { - continue; - } - - if (tags != null) { - boolean keep = false; - for (String thisTag : meta.getTags()) { - if (tags.contains(thisTag)) - keep = true; - } - - if (!keep) - continue; - } - - result.add(meta); - } - - Collections.sort(result); - return result; - } - - /** - * Return the list of values, grouped by starting letter(s) if needed. - *

- * If the number of values is not too high, only one group with an empty - * name and all the values will be returned (see - * {@link MetaResultList#MAX}). - *

- * If not, the values will be separated into groups: - *

    - *
  • *: any value which name doesn't contain letters nor numbers - *
  • - *
  • 0-9: any value which name starts with a number
  • - *
  • A-C (for instance): any value which name starts with - * A, B or C
  • - *
- * Note that the letters used in the groups can vary (except * and - * 0-9, which may only be present or not). - * - * @param values - * the values to group - * - * @return the values, grouped by letter(s) - * - * @throws IOException - * in case of IOException - */ - private Map> group(List values) - throws IOException { - Map> groups = new TreeMap>(); - - // If all authors fit the max, just report them as is - if (values.size() <= MAX) { - groups.put("", values); - return groups; - } - - // Create groups A to Z, which can be empty here - for (char car = 'A'; car <= 'Z'; car++) { - groups.put(Character.toString(car), find(values, car)); - } - - // Collapse them - List keys = new ArrayList(groups.keySet()); - for (int i = 0; i + 1 < keys.size(); i++) { - String keyNow = keys.get(i); - String keyNext = keys.get(i + 1); - - List now = groups.get(keyNow); - List next = groups.get(keyNext); - - int currentTotal = now.size() + next.size(); - if (currentTotal <= MAX) { - String key = keyNow.charAt(0) + "-" - + keyNext.charAt(keyNext.length() - 1); - - List all = new ArrayList(); - all.addAll(now); - all.addAll(next); - - groups.remove(keyNow); - groups.remove(keyNext); - groups.put(key, all); - - keys.set(i, key); // set the new key instead of key(i) - keys.remove(i + 1); // remove the next, consumed key - i--; // restart at key(i) - } - } - - // Add "special" groups - groups.put("*", find(values, '*')); - groups.put("0-9", find(values, '0')); - - // Prune empty groups - keys = new ArrayList(groups.keySet()); - for (String key : keys) { - if (groups.get(key).isEmpty()) { - groups.remove(key); - } - } - - return groups; - } - - /** - * Get all the authors that start with the given character: - *
    - *
  • *: any author whose name doesn't contain letters nor numbers - *
  • - *
  • 0: any authors whose name starts with a number
  • - *
  • A (any capital latin letter): any author whose name starts - * with A
  • - *
- * - * @param values - * the full list of authors - * @param car - * the starting character, *, 0 or a capital - * letter - * - * @return the authors that fulfil the starting letter - */ - private List find(List values, char car) { - List accepted = new ArrayList(); - for (String value : values) { - char first = '*'; - for (int i = 0; first == '*' && i < value.length(); i++) { - String san = StringUtils.sanitize(value, true, true); - char c = san.charAt(i); - if (c >= '0' && c <= '9') { - first = '0'; - } else if (c >= 'a' && c <= 'z') { - first = (char) (c - 'a' + 'A'); - } else if (c >= 'A' && c <= 'Z') { - first = c; - } - } - - if (first == car) { - accepted.add(value); - } - } - - return accepted; - } - - /** - * Sort the given {@link String} values, ignoring case. - * - * @param values - * the values to sort - */ - private void sort(List values) { - Collections.sort(values, new Comparator() { - @Override - public int compare(String o1, String o2) { - return ("" + o1).compareToIgnoreCase("" + o2); - } - }); - } -} diff --git a/library/RemoteLibrary.java b/library/RemoteLibrary.java deleted file mode 100644 index 3e0e192..0000000 --- a/library/RemoteLibrary.java +++ /dev/null @@ -1,589 +0,0 @@ -package be.nikiroo.fanfix.library; - -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.List; - -import javax.net.ssl.SSLException; - -import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix.data.MetaData; -import be.nikiroo.fanfix.data.Story; -import be.nikiroo.utils.Image; -import be.nikiroo.utils.Progress; -import be.nikiroo.utils.Version; -import be.nikiroo.utils.serial.server.ConnectActionClientObject; - -/** - * This {@link BasicLibrary} will access a remote server to list the available - * stories, and download the ones you try to load to the local directory - * specified in the configuration. - *

- * This remote library uses a custom fanfix:// protocol. - * - * @author niki - */ -public class RemoteLibrary extends BasicLibrary { - interface RemoteAction { - public void action(ConnectActionClientObject action) throws Exception; - } - - class RemoteConnectAction extends ConnectActionClientObject { - public RemoteConnectAction() throws IOException { - super(host, port, key); - } - - @Override - public Object send(Object data) - throws IOException, NoSuchFieldException, NoSuchMethodException, - ClassNotFoundException { - Object rep = super.send(data); - if (rep instanceof RemoteLibraryException) { - RemoteLibraryException remoteEx = (RemoteLibraryException) rep; - throw remoteEx.unwrapException(); - } - - return rep; - } - } - - private String host; - private int port; - private final String key; - private final String subkey; - - // informative only (server will make the actual checks) - private boolean rw; - - /** - * Create a {@link RemoteLibrary} linked to the given server. - *

- * Note that the key is structured: - * xxx(|yyy(|wl)(|bl)(|rw) - *

- * Note that anything before the first pipe (|) character is - * considered to be the encryption key, anything after that character is - * called the subkey (including the other pipe characters and flags!). - *

- * This is important because the subkey (including the pipe characters and - * flags) must be present as-is in the server configuration file to be - * allowed. - *

    - *
  • xxx: the encryption key used to communicate with the - * server
  • - *
  • yyy: the secondary key
  • - *
  • rw: flag to allow read and write access if it is not the - * default on this server
  • - *
  • bl: flag to bypass the blacklist (if it exists)
  • - *
  • wl: flag to bypass the whitelist if it exists
  • - *
- *

- * Some examples: - *

    - *
  • my_key: normal connection, will take the default server - * options
  • - *
  • my_key|agzyzz|wl|bl: will ask to bypass the black list and the - * white list (if it exists)
  • - *
  • my_key|agzyzz|rw: will ask read-write access (if the default - * is read-only)
  • - *
  • my_key|agzyzz|wl|rw: will ask both read-write access and white - * list bypass
  • - *
- * - * @param key - * the key that will allow us to exchange information with the - * server - * @param host - * the host to contact or NULL for localhost - * @param port - * the port to contact it on - */ - public RemoteLibrary(String key, String host, int port) { - int index = -1; - if (key != null) { - index = key.indexOf('|'); - } - - if (index >= 0) { - this.key = key.substring(0, index); - this.subkey = key.substring(index + 1); - } else { - this.key = key; - this.subkey = ""; - } - - if (host.startsWith("fanfix://")) { - host = host.substring("fanfix://".length()); - } - - this.host = host; - this.port = port; - } - - @Override - public String getLibraryName() { - return (rw ? "[READ-ONLY] " : "") + "fanfix://" + host + ":" + port; - } - - @Override - public Status getStatus() { - Instance.getInstance().getTraceHandler() - .trace("Getting remote lib status..."); - Status status = getStatusDo(); - Instance.getInstance().getTraceHandler() - .trace("Remote lib status: " + status); - return status; - } - - private Status getStatusDo() { - final Status[] result = new Status[1]; - - result[0] = null; - try { - new RemoteConnectAction() { - @Override - public void action(Version serverVersion) throws Exception { - Object rep = send(new Object[] { subkey, "PING" }); - - if ("r/w".equals(rep)) { - rw = true; - result[0] = Status.READ_WRITE; - } else if ("r/o".equals(rep)) { - rw = false; - result[0] = Status.READ_ONLY; - } else { - result[0] = Status.UNAUTHORIZED; - } - } - - @Override - protected void onError(Exception e) { - if (e instanceof SSLException) { - result[0] = Status.UNAUTHORIZED; - } else { - result[0] = Status.UNAVAILABLE; - } - } - }.connect(); - } catch (UnknownHostException e) { - result[0] = Status.INVALID; - } catch (IllegalArgumentException e) { - result[0] = Status.INVALID; - } catch (Exception e) { - result[0] = Status.UNAVAILABLE; - } - - return result[0]; - } - - @Override - public Image getCover(final String luid) throws IOException { - final Image[] result = new Image[1]; - - connectRemoteAction(new RemoteAction() { - @Override - public void action(ConnectActionClientObject action) - throws Exception { - Object rep = action - .send(new Object[] { subkey, "GET_COVER", luid }); - result[0] = (Image) rep; - } - }); - - return result[0]; - } - - @Override - public Image getCustomSourceCover(final String source) throws IOException { - return getCustomCover(source, "SOURCE"); - } - - @Override - public Image getCustomAuthorCover(final String author) throws IOException { - return getCustomCover(author, "AUTHOR"); - } - - // type: "SOURCE" or "AUTHOR" - private Image getCustomCover(final String source, final String type) - throws IOException { - final Image[] result = new Image[1]; - - connectRemoteAction(new RemoteAction() { - @Override - public void action(ConnectActionClientObject action) - throws Exception { - Object rep = action.send(new Object[] { subkey, - "GET_CUSTOM_COVER", type, source }); - result[0] = (Image) rep; - } - }); - - return result[0]; - } - - @Override - public synchronized Story getStory(final String luid, Progress pg) - throws IOException { - final Progress pgF = pg; - final Story[] result = new Story[1]; - - connectRemoteAction(new RemoteAction() { - @Override - public void action(ConnectActionClientObject action) - throws Exception { - Progress pg = pgF; - if (pg == null) { - pg = new Progress(); - } - - Object rep = action - .send(new Object[] { subkey, "GET_STORY", luid }); - - MetaData meta = null; - if (rep instanceof MetaData) { - meta = (MetaData) rep; - if (meta.getWords() <= Integer.MAX_VALUE) { - pg.setMinMax(0, (int) meta.getWords()); - } - } - - List list = new ArrayList(); - for (Object obj = action.send(null); obj != null; obj = action - .send(null)) { - list.add(obj); - pg.add(1); - } - - result[0] = RemoteLibraryServer.rebuildStory(list); - pg.done(); - } - }); - - return result[0]; - } - - @Override - public synchronized Story save(final Story story, final String luid, - Progress pg) throws IOException { - - final String[] luidSaved = new String[1]; - Progress pgSave = new Progress(); - Progress pgRefresh = new Progress(); - if (pg == null) { - pg = new Progress(); - } - - pg.setMinMax(0, 10); - pg.addProgress(pgSave, 9); - pg.addProgress(pgRefresh, 1); - - final Progress pgF = pgSave; - - connectRemoteAction(new RemoteAction() { - @Override - public void action(ConnectActionClientObject action) - throws Exception { - Progress pg = pgF; - if (story.getMeta().getWords() <= Integer.MAX_VALUE) { - pg.setMinMax(0, (int) story.getMeta().getWords()); - } - - action.send(new Object[] { subkey, "SAVE_STORY", luid }); - - List list = RemoteLibraryServer.breakStory(story); - for (Object obj : list) { - action.send(obj); - pg.add(1); - } - - luidSaved[0] = (String) action.send(null); - - pg.done(); - } - }); - - // because the meta changed: - MetaData meta = getInfo(luidSaved[0]); - if (story.getMeta().getClass() != null) { - // If already available locally: - meta.setCover(story.getMeta().getCover()); - } else { - // If required: - meta.setCover(getCover(meta.getLuid())); - } - story.setMeta(meta); - - pg.done(); - - return story; - } - - @Override - public synchronized void delete(final String luid) throws IOException { - connectRemoteAction(new RemoteAction() { - @Override - public void action(ConnectActionClientObject action) - throws Exception { - action.send(new Object[] { subkey, "DELETE_STORY", luid }); - } - }); - } - - @Override - public void setSourceCover(final String source, final String luid) - throws IOException { - setCover(source, luid, "SOURCE"); - } - - @Override - public void setAuthorCover(final String author, final String luid) - throws IOException { - setCover(author, luid, "AUTHOR"); - } - - // type = "SOURCE" | "AUTHOR" - private void setCover(final String value, final String luid, - final String type) throws IOException { - connectRemoteAction(new RemoteAction() { - @Override - public void action(ConnectActionClientObject action) - throws Exception { - action.send(new Object[] { subkey, "SET_COVER", type, value, - luid }); - } - }); - } - - @Override - // Could work (more slowly) without it - public MetaData imprt(final URL url, Progress pg) throws IOException { - // Import the file locally if it is actually a file - - if (url == null || url.getProtocol().equalsIgnoreCase("file")) { - return super.imprt(url, pg); - } - - // Import it remotely if it is an URL - - if (pg == null) { - pg = new Progress(); - } - - final Progress pgF = pg; - final String[] luid = new String[1]; - - connectRemoteAction(new RemoteAction() { - @Override - public void action(ConnectActionClientObject action) - throws Exception { - Progress pg = pgF; - - Object rep = action.send( - new Object[] { subkey, "IMPORT", url.toString() }); - - while (true) { - if (!RemoteLibraryServer.updateProgress(pg, rep)) { - break; - } - - rep = action.send(null); - } - - pg.done(); - luid[0] = (String) rep; - } - }); - - if (luid[0] == null) { - throw new IOException("Remote failure"); - } - - pg.done(); - return getInfo(luid[0]); - } - - @Override - // Could work (more slowly) without it - protected synchronized void changeSTA(final String luid, - final String newSource, final String newTitle, - final String newAuthor, Progress pg) throws IOException { - - final Progress pgF = pg == null ? new Progress() : pg; - - connectRemoteAction(new RemoteAction() { - @Override - public void action(ConnectActionClientObject action) - throws Exception { - Progress pg = pgF; - - Object rep = action.send(new Object[] { subkey, "CHANGE_STA", - luid, newSource, newTitle, newAuthor }); - while (true) { - if (!RemoteLibraryServer.updateProgress(pg, rep)) { - break; - } - - rep = action.send(null); - } - } - }); - } - - @Override - public File getFile(final String luid, Progress pg) { - throw new java.lang.InternalError( - "Operation not supportorted on remote Libraries"); - } - - /** - * Stop the server. - * - * @throws IOException - * in case of I/O errors - * @throws SSLException - * when the key was not accepted - */ - public void stop() throws IOException, SSLException { - connectRemoteAction(new RemoteAction() { - @Override - public void action(ConnectActionClientObject action) - throws Exception { - action.send(new Object[] { subkey, "EXIT" }); - Thread.sleep(100); - } - }); - } - - @Override - public MetaData getInfo(String luid) throws IOException { - List metas = getMetasList(luid, null); - if (!metas.isEmpty()) { - return metas.get(0); - } - - return null; - } - - @Override - protected List getMetas(Progress pg) throws IOException { - return getMetasList("*", pg); - } - - @Override - protected void updateInfo(MetaData meta) { - // Will be taken care of directly server side - } - - @Override - protected void invalidateInfo(String luid) { - // Will be taken care of directly server side - } - - // The following methods are only used by Save and Delete in BasicLibrary: - - @Override - protected String getNextId() { - throw new java.lang.InternalError("Should not have been called"); - } - - @Override - protected void doDelete(String luid) throws IOException { - throw new java.lang.InternalError("Should not have been called"); - } - - @Override - protected Story doSave(Story story, Progress pg) throws IOException { - throw new java.lang.InternalError("Should not have been called"); - } - - // - - /** - * Return the meta of the given story or a list of all known metas if the - * luid is "*". - *

- * Will not get the covers. - * - * @param luid - * the luid of the story or * - * @param pg - * the optional progress - * - * @return the metas - * - * @throws IOException - * in case of I/O error or bad key (SSLException) - */ - private List getMetasList(final String luid, Progress pg) - throws IOException { - final Progress pgF = pg; - final List metas = new ArrayList(); - - connectRemoteAction(new RemoteAction() { - @Override - public void action(ConnectActionClientObject action) - throws Exception { - Progress pg = pgF; - if (pg == null) { - pg = new Progress(); - } - - Object rep = action - .send(new Object[] { subkey, "GET_METADATA", luid }); - - while (true) { - if (!RemoteLibraryServer.updateProgress(pg, rep)) { - break; - } - - rep = action.send(null); - } - - if (rep instanceof MetaData[]) { - for (MetaData meta : (MetaData[]) rep) { - metas.add(meta); - } - } else if (rep != null) { - metas.add((MetaData) rep); - } - } - }); - - return metas; - } - - private void connectRemoteAction(final RemoteAction runAction) - throws IOException { - final IOException[] err = new IOException[1]; - try { - final RemoteConnectAction[] array = new RemoteConnectAction[1]; - RemoteConnectAction ra = new RemoteConnectAction() { - @Override - public void action(Version serverVersion) throws Exception { - runAction.action(array[0]); - } - - @Override - protected void onError(Exception e) { - if (!(e instanceof IOException)) { - Instance.getInstance().getTraceHandler().error(e); - return; - } - - err[0] = (IOException) e; - } - }; - array[0] = ra; - ra.connect(); - } catch (Exception e) { - err[0] = (IOException) e; - } - - if (err[0] != null) { - throw err[0]; - } - } -} diff --git a/library/RemoteLibraryException.java b/library/RemoteLibraryException.java deleted file mode 100644 index 4cbb631..0000000 --- a/library/RemoteLibraryException.java +++ /dev/null @@ -1,100 +0,0 @@ -package be.nikiroo.fanfix.library; - -import java.io.IOException; - -/** - * Exceptions sent from remote to local. - * - * @author niki - */ -public class RemoteLibraryException extends IOException { - private static final long serialVersionUID = 1L; - - private boolean wrapped; - - @SuppressWarnings("unused") - private RemoteLibraryException() { - // for serialization purposes - } - - /** - * Wrap an {@link IOException} to allow it to pass across the network. - * - * @param cause - * the exception to wrap - * @param remote - * this exception is used to send the contained - * {@link IOException} to the other end of the network - */ - public RemoteLibraryException(IOException cause, boolean remote) { - this(null, cause, remote); - } - - /** - * Wrap an {@link IOException} to allow it to pass across the network. - * - * @param message - * the error message - * @param wrapped - * this exception is used to send the contained - * {@link IOException} to the other end of the network - */ - public RemoteLibraryException(String message, boolean wrapped) { - this(message, null, wrapped); - } - - /** - * Wrap an {@link IOException} to allow it to pass across the network. - * - * @param message - * the error message - * @param cause - * the exception to wrap - * @param wrapped - * this exception is used to send the contained - * {@link IOException} to the other end of the network - */ - public RemoteLibraryException(String message, IOException cause, - boolean wrapped) { - super(message, cause); - this.wrapped = wrapped; - } - - /** - * Return the actual exception we should return to the client code. It can - * be: - *

    - *
  • the cause if {@link RemoteLibraryException#isWrapped()} is - * TRUE
  • - *
  • this if {@link RemoteLibraryException#isWrapped()} is FALSE - * (
  • - *
  • this if the cause is NULL (so we never return NULL) - *
  • - *
- * It is never NULL. - * - * @return the unwrapped exception or this, never NULL - */ - public synchronized IOException unwrapException() { - Throwable ex = super.getCause(); - if (!isWrapped() || !(ex instanceof IOException)) { - ex = this; - } - - return (IOException) ex; - } - - /** - * This exception is used to send the contained {@link IOException} to the - * other end of the network. - *

- * In other words, do not use this exception in client code when it - * has reached the other end of the network, but use its cause instead (see - * {@link RemoteLibraryException#unwrapException()}). - * - * @return TRUE if it is - */ - public boolean isWrapped() { - return wrapped; - } -} diff --git a/library/RemoteLibraryServer.java b/library/RemoteLibraryServer.java deleted file mode 100644 index 59819bb..0000000 --- a/library/RemoteLibraryServer.java +++ /dev/null @@ -1,596 +0,0 @@ -package be.nikiroo.fanfix.library; - -import java.io.IOException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.net.ssl.SSLException; - -import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix.bundles.Config; -import be.nikiroo.fanfix.data.Chapter; -import be.nikiroo.fanfix.data.MetaData; -import be.nikiroo.fanfix.data.Paragraph; -import be.nikiroo.fanfix.data.Story; -import be.nikiroo.utils.Progress; -import be.nikiroo.utils.Progress.ProgressListener; -import be.nikiroo.utils.StringUtils; -import be.nikiroo.utils.Version; -import be.nikiroo.utils.serial.server.ConnectActionServerObject; -import be.nikiroo.utils.serial.server.ServerObject; - -/** - * Create a new remote server that will listen for orders on the given port. - *

- * The available commands are given as arrays of objects (first item is the - * command, the rest are the arguments). - *

- * All the commands are always prefixed by the subkey (which can be EMPTY if - * none). - *

- *

    - *
  • PING: will return the mode if the key is accepted (mode can be: "r/o" or - * "r/w")
  • - *
  • GET_METADATA *: will return the metadata of all the stories in the - * library (array)
  • * - *
  • GET_METADATA [luid]: will return the metadata of the story of LUID - * luid
  • - *
  • GET_STORY [luid]: will return the given story if it exists (or NULL if - * not)
  • - *
  • SAVE_STORY [luid]: save the story (that must be sent just after the - * command) with the given LUID, then return the LUID
  • - *
  • IMPORT [url]: save the story found at the given URL, then return the LUID - *
  • - *
  • DELETE_STORY [luid]: delete the story of LUID luid
  • - *
  • GET_COVER [luid]: return the cover of the story
  • - *
  • GET_CUSTOM_COVER ["SOURCE"|"AUTHOR"] [source]: return the cover for this - * source/author
  • - *
  • SET_COVER ["SOURCE"|"AUTHOR"] [value] [luid]: set the default cover for - * the given source/author to the cover of the story denoted by luid
  • - *
  • CHANGE_SOURCE [luid] [new source]: change the source of the story of LUID - * luid
  • - *
  • EXIT: stop the server
  • - *
- * - * @author niki - */ -public class RemoteLibraryServer extends ServerObject { - private Map commands = new HashMap(); - private Map times = new HashMap(); - private Map wls = new HashMap(); - private Map bls = new HashMap(); - private Map rws = new HashMap(); - - /** - * Create a new remote server (will not be active until - * {@link RemoteLibraryServer#start()} is called). - *

- * Note: the key we use here is the encryption key (it must not contain a - * subkey). - * - * @throws IOException - * in case of I/O error - */ - public RemoteLibraryServer() throws IOException { - super("Fanfix remote library", - Instance.getInstance().getConfig() - .getInteger(Config.SERVER_PORT), - Instance.getInstance().getConfig() - .getString(Config.SERVER_KEY)); - - setTraceHandler(Instance.getInstance().getTraceHandler()); - } - - @Override - protected Object onRequest(ConnectActionServerObject action, - Version clientVersion, Object data, long id) throws Exception { - long start = new Date().getTime(); - - // defaults are positive (as previous versions without the feature) - boolean rw = true; - boolean wl = true; - boolean bl = true; - - String subkey = ""; - String command = ""; - Object[] args = new Object[0]; - if (data instanceof Object[]) { - Object[] dataArray = (Object[]) data; - if (dataArray.length > 0) { - subkey = "" + dataArray[0]; - } - if (dataArray.length > 1) { - command = "" + dataArray[1]; - - args = new Object[dataArray.length - 2]; - for (int i = 2; i < dataArray.length; i++) { - args[i - 2] = dataArray[i]; - } - } - } - - List whitelist = Instance.getInstance().getConfig() - .getList(Config.SERVER_WHITELIST); - if (whitelist == null) { - whitelist = new ArrayList(); - } - List blacklist = Instance.getInstance().getConfig() - .getList(Config.SERVER_BLACKLIST); - if (blacklist == null) { - blacklist = new ArrayList(); - } - - if (whitelist.isEmpty()) { - wl = false; - } - - rw = Instance.getInstance().getConfig().getBoolean(Config.SERVER_RW, - rw); - if (!subkey.isEmpty()) { - List allowed = Instance.getInstance().getConfig() - .getList(Config.SERVER_ALLOWED_SUBKEYS); - if (allowed.contains(subkey)) { - if ((subkey + "|").contains("|rw|")) { - rw = true; - } - if ((subkey + "|").contains("|wl|")) { - wl = false; // |wl| = bypass whitelist - whitelist = new ArrayList(); - } - if ((subkey + "|").contains("|bl|")) { - bl = false; // |bl| = bypass blacklist - blacklist = new ArrayList(); - } - } - } - - String mode = display(wl, bl, rw); - - String trace = mode + "[ " + command + "] "; - for (Object arg : args) { - trace += arg + " "; - } - long now = System.currentTimeMillis(); - System.out.println(StringUtils.fromTime(now) + ": " + trace); - - Object rep = null; - try { - rep = doRequest(action, command, args, rw, whitelist, blacklist); - } catch (IOException e) { - rep = new RemoteLibraryException(e, true); - } - - commands.put(id, command); - wls.put(id, wl); - bls.put(id, bl); - rws.put(id, rw); - times.put(id, (new Date().getTime() - start)); - - return rep; - } - - private String display(boolean whitelist, boolean blacklist, boolean rw) { - String mode = ""; - if (!rw) { - mode += "RO: "; - } - if (whitelist) { - mode += "WL: "; - } - if (blacklist) { - mode += "BL: "; - } - - return mode; - } - - @Override - protected void onRequestDone(long id, long bytesReceived, long bytesSent) { - boolean whitelist = wls.get(id); - boolean blacklist = bls.get(id); - boolean rw = rws.get(id); - wls.remove(id); - bls.remove(id); - rws.remove(id); - - String rec = StringUtils.formatNumber(bytesReceived) + "b"; - String sent = StringUtils.formatNumber(bytesSent) + "b"; - long now = System.currentTimeMillis(); - System.out.println(StringUtils.fromTime(now) + ": " - + String.format("%s[>%s]: (%s sent, %s rec) in %d ms", - display(whitelist, blacklist, rw), commands.get(id), - sent, rec, times.get(id))); - - commands.remove(id); - times.remove(id); - } - - private Object doRequest(ConnectActionServerObject action, String command, - Object[] args, boolean rw, List whitelist, - List blacklist) throws NoSuchFieldException, - NoSuchMethodException, ClassNotFoundException, IOException { - if ("PING".equals(command)) { - return rw ? "r/w" : "r/o"; - } else if ("GET_METADATA".equals(command)) { - List metas = new ArrayList(); - - if ("*".equals(args[0])) { - Progress pg = createPgForwarder(action); - - for (MetaData meta : Instance.getInstance().getLibrary() - .getMetas(pg)) { - metas.add(removeCover(meta)); - } - - forcePgDoneSent(pg); - } else { - MetaData meta = Instance.getInstance().getLibrary() - .getInfo((String) args[0]); - MetaData light; - if (meta.getCover() == null) { - light = meta; - } else { - light = meta.clone(); - light.setCover(null); - } - - metas.add(light); - } - - for (int i = 0; i < metas.size(); i++) { - if (!isAllowed(metas.get(i), whitelist, blacklist)) { - metas.remove(i); - i--; - } - } - - return metas.toArray(new MetaData[0]); - - } else if ("GET_STORY".equals(command)) { - MetaData meta = Instance.getInstance().getLibrary() - .getInfo((String) args[0]); - if (meta == null || !isAllowed(meta, whitelist, blacklist)) { - return null; - } - - meta = meta.clone(); - meta.setCover(null); - - action.send(meta); - action.rec(); - - Story story = Instance.getInstance().getLibrary() - .getStory((String) args[0], null); - for (Object obj : breakStory(story)) { - action.send(obj); - action.rec(); - } - } else if ("SAVE_STORY".equals(command)) { - if (!rw) { - throw new RemoteLibraryException( - "Read-Only remote library: " + args[0], false); - } - - List list = new ArrayList(); - - action.send(null); - Object obj = action.rec(); - while (obj != null) { - list.add(obj); - action.send(null); - obj = action.rec(); - } - - Story story = rebuildStory(list); - Instance.getInstance().getLibrary().save(story, (String) args[0], - null); - return story.getMeta().getLuid(); - } else if ("IMPORT".equals(command)) { - if (!rw) { - throw new RemoteLibraryException( - "Read-Only remote library: " + args[0], false); - } - - Progress pg = createPgForwarder(action); - MetaData meta = Instance.getInstance().getLibrary() - .imprt(new URL((String) args[0]), pg); - forcePgDoneSent(pg); - return meta.getLuid(); - } else if ("DELETE_STORY".equals(command)) { - if (!rw) { - throw new RemoteLibraryException( - "Read-Only remote library: " + args[0], false); - } - - Instance.getInstance().getLibrary().delete((String) args[0]); - } else if ("GET_COVER".equals(command)) { - return Instance.getInstance().getLibrary() - .getCover((String) args[0]); - } else if ("GET_CUSTOM_COVER".equals(command)) { - if ("SOURCE".equals(args[0])) { - return Instance.getInstance().getLibrary() - .getCustomSourceCover((String) args[1]); - } else if ("AUTHOR".equals(args[0])) { - return Instance.getInstance().getLibrary() - .getCustomAuthorCover((String) args[1]); - } else { - return null; - } - } else if ("SET_COVER".equals(command)) { - if (!rw) { - throw new RemoteLibraryException( - "Read-Only remote library: " + args[0] + ", " + args[1], - false); - } - - if ("SOURCE".equals(args[0])) { - Instance.getInstance().getLibrary() - .setSourceCover((String) args[1], (String) args[2]); - } else if ("AUTHOR".equals(args[0])) { - Instance.getInstance().getLibrary() - .setAuthorCover((String) args[1], (String) args[2]); - } - } else if ("CHANGE_STA".equals(command)) { - if (!rw) { - throw new RemoteLibraryException( - "Read-Only remote library: " + args[0] + ", " + args[1], - false); - } - - Progress pg = createPgForwarder(action); - Instance.getInstance().getLibrary().changeSTA((String) args[0], - (String) args[1], (String) args[2], (String) args[3], pg); - forcePgDoneSent(pg); - } else if ("EXIT".equals(command)) { - if (!rw) { - throw new RemoteLibraryException( - "Read-Only remote library: EXIT", false); - } - - stop(10000, false); - } - - return null; - } - - @Override - protected void onError(Exception e) { - if (e instanceof SSLException) { - long now = System.currentTimeMillis(); - System.out.println(StringUtils.fromTime(now) + ": " - + "[Client connection refused (bad key)]"); - } else { - getTraceHandler().error(e); - } - } - - /** - * Break a story in multiple {@link Object}s for easier serialisation. - * - * @param story - * the {@link Story} to break - * - * @return the list of {@link Object}s - */ - static List breakStory(Story story) { - List list = new ArrayList(); - - story = story.clone(); - list.add(story); - - if (story.getMeta().isImageDocument()) { - for (Chapter chap : story) { - list.add(chap); - list.addAll(chap.getParagraphs()); - chap.setParagraphs(new ArrayList()); - } - story.setChapters(new ArrayList()); - } - - return list; - } - - /** - * Rebuild a story from a list of broke up {@link Story} parts. - * - * @param list - * the list of {@link Story} parts - * - * @return the reconstructed {@link Story} - */ - static Story rebuildStory(List list) { - Story story = null; - Chapter chap = null; - - for (Object obj : list) { - if (obj instanceof Story) { - story = (Story) obj; - } else if (obj instanceof Chapter) { - chap = (Chapter) obj; - story.getChapters().add(chap); - } else if (obj instanceof Paragraph) { - chap.getParagraphs().add((Paragraph) obj); - } - } - - return story; - } - - /** - * Update the {@link Progress} with the adequate {@link Object} received - * from the network via {@link RemoteLibraryServer}. - * - * @param pg - * the {@link Progress} to update - * @param rep - * the object received from the network - * - * @return TRUE if it was a progress event, FALSE if not - */ - static boolean updateProgress(Progress pg, Object rep) { - boolean updateProgress = false; - if (rep instanceof Integer[] && ((Integer[]) rep).length == 3) - updateProgress = true; - if (rep instanceof Object[] && ((Object[]) rep).length >= 5 - && "UPDATE".equals(((Object[]) rep)[0])) - updateProgress = true; - - if (updateProgress) { - Object[] a = (Object[]) rep; - - int offset = 0; - if (a[0] instanceof String) { - offset = 1; - } - - int min = (Integer) a[0 + offset]; - int max = (Integer) a[1 + offset]; - int progress = (Integer) a[2 + offset]; - - Object meta = null; - if (a.length > (3 + offset)) { - meta = a[3 + offset]; - } - - String name = null; - if (a.length > (4 + offset)) { - name = a[4 + offset] == null ? "" : a[4 + offset].toString(); - } - - if (min >= 0 && min <= max) { - pg.setName(name); - pg.setMinMax(min, max); - pg.setProgress(progress); - if (meta != null) { - pg.put("meta", meta); - } - - return true; - } - } - - return false; - } - - /** - * Create a {@link Progress} that will forward its progress over the - * network. - * - * @param action - * the {@link ConnectActionServerObject} to use to forward it - * - * @return the {@link Progress} - */ - private Progress createPgForwarder(final ConnectActionServerObject action) { - final Boolean[] isDoneForwarded = new Boolean[] { false }; - final Progress pg = new Progress() { - @Override - public boolean isDone() { - return isDoneForwarded[0]; - } - }; - - final Integer[] p = new Integer[] { -1, -1, -1 }; - final Object[] pMeta = new MetaData[1]; - final String[] pName = new String[1]; - final Long[] lastTime = new Long[] { new Date().getTime() }; - pg.addProgressListener(new ProgressListener() { - @Override - public void progress(Progress progress, String name) { - Object meta = pg.get("meta"); - if (meta instanceof MetaData) { - meta = removeCover((MetaData) meta); - } - - int min = pg.getMin(); - int max = pg.getMax(); - int rel = min + (int) Math - .round(pg.getRelativeProgress() * (max - min)); - - boolean samePg = p[0] == min && p[1] == max && p[2] == rel; - - // Do not re-send the same value twice over the wire, - // unless more than 2 seconds have elapsed (to maintain the - // connection) - if (!samePg || !same(pMeta[0], meta) || !same(pName[0], name) // - || (new Date().getTime() - lastTime[0] > 2000)) { - p[0] = min; - p[1] = max; - p[2] = rel; - pMeta[0] = meta; - pName[0] = name; - - try { - action.send(new Object[] { "UPDATE", min, max, rel, - meta, name }); - action.rec(); - } catch (Exception e) { - getTraceHandler().error(e); - } - - lastTime[0] = new Date().getTime(); - } - - isDoneForwarded[0] = (pg.getProgress() >= pg.getMax()); - } - }); - - return pg; - } - - private boolean same(Object obj1, Object obj2) { - if (obj1 == null || obj2 == null) - return obj1 == null && obj2 == null; - - return obj1.equals(obj2); - } - - // with 30 seconds timeout - private void forcePgDoneSent(Progress pg) { - long start = new Date().getTime(); - pg.done(); - while (!pg.isDone() && new Date().getTime() - start < 30000) { - try { - Thread.sleep(100); - } catch (InterruptedException e) { - getTraceHandler().error(e); - } - } - } - - private MetaData removeCover(MetaData meta) { - MetaData light = null; - if (meta != null) { - if (meta.getCover() == null) { - light = meta; - } else { - light = meta.clone(); - light.setCover(null); - } - } - - return light; - } - - private boolean isAllowed(MetaData meta, List whitelist, - List blacklist) { - MetaResultList one = new MetaResultList(Arrays.asList(meta)); - if (!whitelist.isEmpty()) { - if (one.filter(whitelist, null, null).isEmpty()) { - return false; - } - } - if (!blacklist.isEmpty()) { - if (!one.filter(blacklist, null, null).isEmpty()) { - return false; - } - } - - return true; - } -} diff --git a/library/Template.java b/library/Template.java deleted file mode 100644 index 3ec9873..0000000 --- a/library/Template.java +++ /dev/null @@ -1,105 +0,0 @@ -package be.nikiroo.fanfix.library; - -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.streams.ReplaceInputStream; - -public class Template { - private Class location; - private String name; - - private Map values = new HashMap(); - private Map valuesTemplate = new HashMap(); - private Map> valuesTemplateList = new HashMap>(); - - public Template(Class location, String name) { - this.location = location; - this.name = name; - } - - public synchronized InputStream read() throws IOException { - - String from[] = new String[values.size() + valuesTemplate.size() - + valuesTemplateList.size()]; - String to[] = new String[from.length]; - - int i = 0; - - for (String key : values.keySet()) { - from[i] = "${" + key + "}"; - to[i] = values.get(key); - - i++; - } - for (String key : valuesTemplate.keySet()) { - InputStream value = valuesTemplate.get(key).read(); - try { - from[i] = "${" + key + "}"; - to[i] = IOUtils.readSmallStream(value); - } finally { - value.close(); - } - - i++; - } - for (String key : valuesTemplateList.keySet()) { - List