From: Marco Zanon Date: Sun, 25 Mar 2012 13:07:49 +0000 (+0000) Subject: Renamed the 'translation' package to 'text' (and its exceptions accordingly). X-Git-Tag: SVN-to-Git~147 X-Git-Url: https://gitweb.marcozanon.com/?a=commitdiff_plain;h=3e9924d0834e82e223864de8527dd6e7267b5dab;p=Macaco Renamed the 'translation' package to 'text' (and its exceptions accordingly). --- diff --git a/src/java/com/marcozanon/macaco/text/MTranslation.java b/src/java/com/marcozanon/macaco/text/MTranslation.java new file mode 100644 index 0000000..01c0889 --- /dev/null +++ b/src/java/com/marcozanon/macaco/text/MTranslation.java @@ -0,0 +1,158 @@ +/** + * Macaco + * Copyright (c) 2009-2012 Marco Zanon . + * Released under MIT license (see LICENSE for details). + */ + +package com.marcozanon.macaco.text; + +import com.marcozanon.macaco.MInformation; +import com.marcozanon.macaco.MObject; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStreamReader; +import java.io.IOException; +import java.io.LineNumberReader; +import java.io.UnsupportedEncodingException; +import java.util.LinkedHashMap; + +public class MTranslation extends MObject { + + protected String language = null; + + protected LinkedHashMap> messages = new LinkedHashMap>(); + + /* */ + + public MTranslation(String file, String language) throws MTranslationFileParsingTextException { + super(); + // + if ((null == language) || ("".equals(language))) { + throw new IllegalArgumentException("Invalid 'language': null or empty."); + } + // + this.language = language; + this.parseFile(file); + } + + /* Language */ + + public String getLanguage() { + return this.language; + } + + /* Strings management */ + + protected LinkedHashMap> getMessagesReference() { + return this.messages; + } + + public void parseFile(String file) throws MTranslationFileParsingTextException { + if ((null == file) || ("".equals(file))) { + throw new IllegalArgumentException("Invalid 'file': null or empty."); + } + // + LineNumberReader buffer = null; + try { + buffer = new LineNumberReader(new InputStreamReader(new FileInputStream(file), MInformation.TEXT_ENCODING)); + } + catch (FileNotFoundException exception) { + throw new MTranslationFileParsingTextException("Could not open file.", exception); + } + catch (UnsupportedEncodingException exception) { // cannot happen + } + String message = null; + String line = null; + synchronized (this.getMessagesReference()) { + while (true) { + try { + line = buffer.readLine(); + } + catch (IOException exception) { + throw new MTranslationFileParsingTextException("Could not read file.", exception); + } + if (null == line) { + break; + } + line = line.trim(); + if ("".equals(line)) { + message = null; + continue; + } + else if (null == message) { + message = line; + if (this.getMessagesReference().containsKey(message)) { + throw new MTranslationFileParsingTextException(String.format("Invalid line: %s: duplicated message: %s.", buffer.getLineNumber(), message)); + } + this.getMessagesReference().put(message, new LinkedHashMap()); + } + else { + int a = line.indexOf("="); + if (-1 == a) { + throw new MTranslationFileParsingTextException(String.format("Invalid line: %s: string malformed: %s.", buffer.getLineNumber(), line)); + } + String language = line.substring(0, a).trim(); + String translation = line.substring(a + 1).trim(); + if (this.getMessagesReference().get(message).containsKey(language)) { + throw new MTranslationFileParsingTextException(String.format("Invalid line: %s: duplicated translation for message: %s.", buffer.getLineNumber(), message)); + } + this.getMessagesReference().get(message).put(language, translation); + } + } + } + try { + buffer.close(); + } + catch (IOException exception) { + throw new MTranslationFileParsingTextException("Could not close file.", exception); + } + } + + public void clear() { + synchronized (this.getMessagesReference()) { + this.getMessagesReference().clear(); + } + } + + public String getLenientTranslation(String message, String language) { + String translation = null; + try { + translation = this.getTranslation(message, language, false); + } + catch (MTranslationValueNotFoundTextException exception) { // cannot happen + } + return translation; + } + + public String getStrictTranslation(String message, String language) throws MTranslationValueNotFoundTextException { + return this.getTranslation(message, language, true); + } + + protected String getTranslation(String message, String language, boolean strictMode) throws MTranslationValueNotFoundTextException { + if ((null == message) || ("".equals(message))) { + throw new IllegalArgumentException("Invalid 'message': null or empty."); + } + if ((null == language) || ("".equals(language))) { + throw new IllegalArgumentException("Invalid 'language': null or empty."); + } + // + if (!this.getMessagesReference().containsKey(message)) { + if (strictMode) { + throw new MTranslationValueNotFoundTextException(String.format("Invalid 'message': %s: not available.", message)); + } + return message; + } + if (this.getLanguage().equals(language)) { + return message; + } + LinkedHashMap messageTranslations = this.getMessagesReference().get(message); + if (!messageTranslations.containsKey(language)) { + if (strictMode) { + throw new MTranslationValueNotFoundTextException(String.format("Invalid 'language': %s: translation not available for message: %s.", language, message)); + } + return message; + } + return messageTranslations.get(language); + } + +} diff --git a/src/java/com/marcozanon/macaco/text/MTranslationException.java b/src/java/com/marcozanon/macaco/text/MTranslationException.java new file mode 100644 index 0000000..14ee47d --- /dev/null +++ b/src/java/com/marcozanon/macaco/text/MTranslationException.java @@ -0,0 +1,31 @@ +/** + * Macaco + * Copyright (c) 2009-2012 Marco Zanon . + * Released under MIT license (see LICENSE for details). + */ + +package com.marcozanon.macaco.text; + +public abstract class MTranslationException extends MTextException { + + private static final long serialVersionUID = 0L; + + /* */ + + public MTranslationException() { + super(); + } + + public MTranslationException(String message) { + super(message); + } + + public MTranslationException(Throwable error) { + super(error); + } + + public MTranslationException(String message, Throwable error) { + super(message, error); + } + +} diff --git a/src/java/com/marcozanon/macaco/text/MTranslationFileParsingTextException.java b/src/java/com/marcozanon/macaco/text/MTranslationFileParsingTextException.java new file mode 100644 index 0000000..5289e75 --- /dev/null +++ b/src/java/com/marcozanon/macaco/text/MTranslationFileParsingTextException.java @@ -0,0 +1,31 @@ +/** + * Macaco + * Copyright (c) 2009-2012 Marco Zanon . + * Released under MIT license (see LICENSE for details). + */ + +package com.marcozanon.macaco.text; + +public class MTranslationFileParsingTextException extends MTranslationException { + + private static final long serialVersionUID = 0L; + + /* */ + + public MTranslationFileParsingTextException() { + super(); + } + + public MTranslationFileParsingTextException(String message) { + super(message); + } + + public MTranslationFileParsingTextException(Throwable error) { + super(error); + } + + public MTranslationFileParsingTextException(String message, Throwable error) { + super(message, error); + } + +} diff --git a/src/java/com/marcozanon/macaco/text/MTranslationValueNotFoundTextException.java b/src/java/com/marcozanon/macaco/text/MTranslationValueNotFoundTextException.java new file mode 100644 index 0000000..0236c50 --- /dev/null +++ b/src/java/com/marcozanon/macaco/text/MTranslationValueNotFoundTextException.java @@ -0,0 +1,31 @@ +/** + * Macaco + * Copyright (c) 2009-2012 Marco Zanon . + * Released under MIT license (see LICENSE for details). + */ + +package com.marcozanon.macaco.text; + +public class MTranslationValueNotFoundTextException extends MTranslationException { + + private static final long serialVersionUID = 0L; + + /* */ + + public MTranslationValueNotFoundTextException() { + super(); + } + + public MTranslationValueNotFoundTextException(String message) { + super(message); + } + + public MTranslationValueNotFoundTextException(Throwable error) { + super(error); + } + + public MTranslationValueNotFoundTextException(String message, Throwable error) { + super(message, error); + } + +} diff --git a/src/java/com/marcozanon/macaco/translation/MFileParsingTranslationException.java b/src/java/com/marcozanon/macaco/translation/MFileParsingTranslationException.java deleted file mode 100644 index 9ff3e99..0000000 --- a/src/java/com/marcozanon/macaco/translation/MFileParsingTranslationException.java +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Macaco - * Copyright (c) 2009-2012 Marco Zanon . - * Released under MIT license (see LICENSE for details). - */ - -package com.marcozanon.macaco.translation; - -public class MFileParsingTranslationException extends MTranslationException { - - private static final long serialVersionUID = 0L; - - /* */ - - public MFileParsingTranslationException() { - super(); - } - - public MFileParsingTranslationException(String message) { - super(message); - } - - public MFileParsingTranslationException(Throwable error) { - super(error); - } - - public MFileParsingTranslationException(String message, Throwable error) { - super(message, error); - } - -} diff --git a/src/java/com/marcozanon/macaco/translation/MTranslation.java b/src/java/com/marcozanon/macaco/translation/MTranslation.java deleted file mode 100644 index 9d6d47f..0000000 --- a/src/java/com/marcozanon/macaco/translation/MTranslation.java +++ /dev/null @@ -1,158 +0,0 @@ -/** - * Macaco - * Copyright (c) 2009-2012 Marco Zanon . - * Released under MIT license (see LICENSE for details). - */ - -package com.marcozanon.macaco.translation; - -import com.marcozanon.macaco.MInformation; -import com.marcozanon.macaco.MObject; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStreamReader; -import java.io.IOException; -import java.io.LineNumberReader; -import java.io.UnsupportedEncodingException; -import java.util.LinkedHashMap; - -public class MTranslation extends MObject { - - protected String language = null; - - protected LinkedHashMap> messages = new LinkedHashMap>(); - - /* */ - - public MTranslation(String file, String language) throws MFileParsingTranslationException { - super(); - // - if ((null == language) || ("".equals(language))) { - throw new IllegalArgumentException("Invalid 'language': null or empty."); - } - // - this.language = language; - this.parseFile(file); - } - - /* Language */ - - public String getLanguage() { - return this.language; - } - - /* Strings management */ - - protected LinkedHashMap> getMessagesReference() { - return this.messages; - } - - public void parseFile(String file) throws MFileParsingTranslationException { - if ((null == file) || ("".equals(file))) { - throw new IllegalArgumentException("Invalid 'file': null or empty."); - } - // - LineNumberReader buffer = null; - try { - buffer = new LineNumberReader(new InputStreamReader(new FileInputStream(file), MInformation.TEXT_ENCODING)); - } - catch (FileNotFoundException exception) { - throw new MFileParsingTranslationException("Could not open file.", exception); - } - catch (UnsupportedEncodingException exception) { // cannot happen - } - String message = null; - String line = null; - synchronized (this.getMessagesReference()) { - while (true) { - try { - line = buffer.readLine(); - } - catch (IOException exception) { - throw new MFileParsingTranslationException("Could not read file.", exception); - } - if (null == line) { - break; - } - line = line.trim(); - if ("".equals(line)) { - message = null; - continue; - } - else if (null == message) { - message = line; - if (this.getMessagesReference().containsKey(message)) { - throw new MFileParsingTranslationException(String.format("Invalid line: %s: duplicated message: %s.", buffer.getLineNumber(), message)); - } - this.getMessagesReference().put(message, new LinkedHashMap()); - } - else { - int a = line.indexOf("="); - if (-1 == a) { - throw new MFileParsingTranslationException(String.format("Invalid line: %s: string malformed: %s.", buffer.getLineNumber(), line)); - } - String language = line.substring(0, a).trim(); - String translation = line.substring(a + 1).trim(); - if (this.getMessagesReference().get(message).containsKey(language)) { - throw new MFileParsingTranslationException(String.format("Invalid line: %s: duplicated translation for message: %s.", buffer.getLineNumber(), message)); - } - this.getMessagesReference().get(message).put(language, translation); - } - } - } - try { - buffer.close(); - } - catch (IOException exception) { - throw new MFileParsingTranslationException("Could not close file.", exception); - } - } - - public void clear() { - synchronized (this.getMessagesReference()) { - this.getMessagesReference().clear(); - } - } - - public String getLenientTranslation(String message, String language) { - String translation = null; - try { - translation = this.getTranslation(message, language, false); - } - catch (MValueNotFoundTranslationException exception) { // cannot happen - } - return translation; - } - - public String getStrictTranslation(String message, String language) throws MValueNotFoundTranslationException { - return this.getTranslation(message, language, true); - } - - protected String getTranslation(String message, String language, boolean strictMode) throws MValueNotFoundTranslationException { - if ((null == message) || ("".equals(message))) { - throw new IllegalArgumentException("Invalid 'message': null or empty."); - } - if ((null == language) || ("".equals(language))) { - throw new IllegalArgumentException("Invalid 'language': null or empty."); - } - // - if (!this.getMessagesReference().containsKey(message)) { - if (strictMode) { - throw new MValueNotFoundTranslationException(String.format("Invalid 'message': %s: not available.", message)); - } - return message; - } - if (this.getLanguage().equals(language)) { - return message; - } - LinkedHashMap messageTranslations = this.getMessagesReference().get(message); - if (!messageTranslations.containsKey(language)) { - if (strictMode) { - throw new MValueNotFoundTranslationException(String.format("Invalid 'language': %s: translation not available for message: %s.", language, message)); - } - return message; - } - return messageTranslations.get(language); - } - -} diff --git a/src/java/com/marcozanon/macaco/translation/MTranslationException.java b/src/java/com/marcozanon/macaco/translation/MTranslationException.java deleted file mode 100644 index 0d31b03..0000000 --- a/src/java/com/marcozanon/macaco/translation/MTranslationException.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Macaco - * Copyright (c) 2009-2012 Marco Zanon . - * Released under MIT license (see LICENSE for details). - */ - -package com.marcozanon.macaco.translation; - -import com.marcozanon.macaco.MException; - -public abstract class MTranslationException extends MException { - - private static final long serialVersionUID = 0L; - - /* */ - - public MTranslationException() { - super(); - } - - public MTranslationException(String message) { - super(message); - } - - public MTranslationException(Throwable error) { - super(error); - } - - public MTranslationException(String message, Throwable error) { - super(message, error); - } - -} diff --git a/src/java/com/marcozanon/macaco/translation/MValueNotFoundTranslationException.java b/src/java/com/marcozanon/macaco/translation/MValueNotFoundTranslationException.java deleted file mode 100644 index eb572d7..0000000 --- a/src/java/com/marcozanon/macaco/translation/MValueNotFoundTranslationException.java +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Macaco - * Copyright (c) 2009-2012 Marco Zanon . - * Released under MIT license (see LICENSE for details). - */ - -package com.marcozanon.macaco.translation; - -public class MValueNotFoundTranslationException extends MTranslationException { - - private static final long serialVersionUID = 0L; - - /* */ - - public MValueNotFoundTranslationException() { - super(); - } - - public MValueNotFoundTranslationException(String message) { - super(message); - } - - public MValueNotFoundTranslationException(Throwable error) { - super(error); - } - - public MValueNotFoundTranslationException(String message, Throwable error) { - super(message, error); - } - -}