--- /dev/null
+/**
+ * Macaco
+ * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
+ * 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<String, LinkedHashMap<String, String>> messages = new LinkedHashMap<String, LinkedHashMap<String, String>>();
+
+ /* */
+
+ 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<String, LinkedHashMap<String, String>> 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<String, String>());
+ }
+ 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<String, String> 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);
+ }
+
+}
--- /dev/null
+/**
+ * Macaco
+ * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
+ * 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);
+ }
+
+}
--- /dev/null
+/**
+ * Macaco
+ * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
+ * 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);
+ }
+
+}
--- /dev/null
+/**
+ * Macaco
+ * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
+ * 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);
+ }
+
+}
+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * 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);
- }
-
-}
+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * 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<String, LinkedHashMap<String, String>> messages = new LinkedHashMap<String, LinkedHashMap<String, String>>();
-
- /* */
-
- 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<String, LinkedHashMap<String, String>> 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<String, String>());
- }
- 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<String, String> 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);
- }
-
-}
+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * 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);
- }
-
-}
+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * 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);
- }
-
-}