diff --git a/src/Translator.java b/src/Translator.java new file mode 100644 index 0000000..7d4039b --- /dev/null +++ b/src/Translator.java @@ -0,0 +1,81 @@ +import source.URLSourceProvider; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +/** + * Provides utilities for translating texts to russian language.
+ * Uses Yandex Translate API, more information at http://api.yandex.ru/translate/
+ * Depends on {@link URLSourceProvider} for accessing Yandex Translator API service + */ +public class Translator { + private URLSourceProvider urlSourceProvider; + /** + * Yandex Translate API key could be obtained at http://api.yandex.ru/key/form.xml?service=trnsl + * to do that you have to be authorized. + */ + private static final String YANDEX_API_KEY = "trnsl.1.1.20151128T202424Z.ba24693e3e370fd2.f4409e090e433b470028aecc1b1caea821e34386"; + private static final String TRANSLATION_DIRECTION = "ru"; + private static final String TAG_TEXT = ""; + private static final String CLOSE_TAG_TEXT = ""; + + public Translator(URLSourceProvider urlSourceProvider) { + this.urlSourceProvider = urlSourceProvider; + } + + /** + * Translates text to russian language + * + * @param original text to translate + * @return translated text + * @throws IOException + */ + public String translate(String original) throws IOException { + URLSourceProvider urlTranslator = new URLSourceProvider(); + String url = prepareURL(original); + + String translateText = urlTranslator.load(url); + return parseContent(translateText); + } + + /** + * Prepares URL to invoke Yandex Translate API service for specified text + * + * @param text to translate + * @return url for translation specified text + */ + private String prepareURL(String text) { + return "https://translate.yandex.net/api/v1.5/tr/translate?key=" + YANDEX_API_KEY + "&text=" + encodeText(text) + "&lang=" + TRANSLATION_DIRECTION; + } + + /** + * Parses content returned by Yandex Translate API service. Removes all tags and system texts. Keeps only translated text. + * + * @param content that was received from Yandex Translate API by invoking prepared URL + * @return translated text + */ + private String parseContent(String content) { + //TODO: implement me + int copyFrom = content.indexOf(TAG_TEXT)+TAG_TEXT.length(); + int copyTo = content.indexOf(CLOSE_TAG_TEXT); + + return content.substring(copyFrom, copyTo); + } + + /** + * Encodes text that need to be translated to put it as URL parameter + * + * @param text to be translated + * @return encoded text + */ + private String encodeText(String text) { + //TODO: implement me + try { + return URLEncoder.encode(text, "UTF-8"); + } catch (UnsupportedEncodingException e){ + return text; + } + + } +} diff --git a/src/TranslatorController.java b/src/TranslatorController.java new file mode 100644 index 0000000..a73d010 --- /dev/null +++ b/src/TranslatorController.java @@ -0,0 +1,33 @@ +import source.SourceLoader; +import source.URLSourceProvider; + +import java.io.IOException; +import java.util.Scanner; + +public class TranslatorController { + + public static void main(String[] args) { + //initialization + SourceLoader sourceLoader = new SourceLoader(); + Translator translator = new Translator(new URLSourceProvider()); + + Scanner scanner = new Scanner(System.in); + String command = scanner.next(); + while(!"exit".equals(command)) { + //TODO: add exception handling here to let user know about it and ask him to enter another path to translation + // So, the only way to stop the application is to do that manually or type "exit" + try { + String source = sourceLoader.loadSource(command); + System.out.println("Original: " + source); + String translation = translator.translate(source); + System.out.println("Translation: " + translation); + } + catch (IOException e) { + System.out.println("Imput new path."); + } + + + command = scanner.next(); + } + } +} diff --git a/src/source/FileSourceProvider.java b/src/source/FileSourceProvider.java new file mode 100644 index 0000000..67a4614 --- /dev/null +++ b/src/source/FileSourceProvider.java @@ -0,0 +1,39 @@ +package source; + +import java.io.*; +import java.nio.file.Files; + +/** + * Implementation for loading content from local file system. + * This implementation supports absolute paths to local file system without specifying file:// protocol. + * Examples c:/1.txt or d:/pathToFile/file.txt + */ +public class FileSourceProvider implements SourceProvider { + + @Override + public boolean isAllowed(String pathToSource) { + //TODO: implement me + File file = new File(pathToSource); + if (file.exists()&& file.canRead()){ + return true; + } + + return false; + } + + @Override + public String load(String pathToSource) throws IOException { + //TODO: implement me + BufferedReader br = null; + + String currentLine; + + br = new BufferedReader(new FileReader(pathToSource)); + StringBuilder result = new StringBuilder(); + while ((currentLine = br.readLine()) != null) { + result.append(currentLine); + + } + return result.toString(); + } +} diff --git a/src/source/SourceLoader.java b/src/source/SourceLoader.java new file mode 100644 index 0000000..536b2ae --- /dev/null +++ b/src/source/SourceLoader.java @@ -0,0 +1,26 @@ +package source; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * SourceLoader should contains all implementations of SourceProviders to be able to load different sources. + */ +public class SourceLoader { + private List sourceProviders = new ArrayList<>(); + + public SourceLoader() { + sourceProviders.addAll(Arrays.asList(new FileSourceProvider(),new URLSourceProvider())); + } + + public String loadSource(String pathToSource) throws IOException { + for (SourceProvider sourceProvider : sourceProviders) { + if (sourceProvider.isAllowed(pathToSource)) { + return sourceProvider.load(pathToSource); + } + } + throw new IOException(); + } +} diff --git a/src/source/SourceProvider.java b/src/source/SourceProvider.java new file mode 100644 index 0000000..c8a25d4 --- /dev/null +++ b/src/source/SourceProvider.java @@ -0,0 +1,23 @@ +package source; + +import java.io.IOException; + +/** + * Base interface to access different sources.
+ * isAllowed method should protect and help to determine can we load resource for specified path ot not. + */ +public interface SourceProvider { + /** + * Determines can current implementation load source by provided pathToSource + * @param pathToSource absolute path to the source + * @return whether current implementation load the source for specified pathToSource + */ + public boolean isAllowed(String pathToSource); + + /** + * Loads text from specified path. + * @param pathToSource absolute path to the source + * @return content of the source for specified pathToSource + */ + public String load(String pathToSource) throws IOException; +} diff --git a/src/source/URLSourceProvider.java b/src/source/URLSourceProvider.java new file mode 100644 index 0000000..8da2dbd --- /dev/null +++ b/src/source/URLSourceProvider.java @@ -0,0 +1,56 @@ +package source; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Implementation for loading content from specified URL.
+ * Valid paths to load are http://someurl.com, https://secureurl.com, ftp://frpurl.com etc. + */ +public class URLSourceProvider implements SourceProvider { + + @Override + public boolean isAllowed(String pathToSource) { + //TODO: implement me + try { + URL url = new URL(pathToSource); + + if (url.getProtocol().equalsIgnoreCase("ftp")) { + return true; + } + + HttpURLConnection http = (HttpURLConnection)url.openConnection(); + if (http.getResponseCode()==200){ + return true; + }else { + return false; + } + } catch (Exception e) { + return false; + } + } + + @Override + public String load(String pathToSource) throws IOException { + //TODO: implement me + URL url = new URL(pathToSource); + BufferedReader in = new BufferedReader( + new InputStreamReader(url.openStream())); + + String currentLine; + StringBuilder result = new StringBuilder(); + while ((currentLine = in.readLine()) != null) { + result.append(currentLine); + } + in.close(); + return result.toString(); + } +}