diff --git a/Main.java b/Main.java
new file mode 100644
index 0000000..8ce042c
--- /dev/null
+++ b/Main.java
@@ -0,0 +1,39 @@
+package com.company;
+
+import com.company.src.SourceLoader;
+import com.company.src.URLSourceProvider;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.UnknownHostException;
+import java.util.Scanner;
+
+public class Main {
+
+
+ public static void main(String[] args) throws IOException {
+
+ SourceLoader sourceLoader = new SourceLoader();
+ Translation translation = new Translation(new URLSourceProvider());
+
+ System.out.println("Write path to file or exit to stop app.");
+ Scanner scanner = new Scanner(System.in);
+ String command = scanner.next();
+ while(!"exit".equals(command)) {
+ try {
+ String source = sourceLoader.loadSource(command);
+ String translations = translation.translate(source);
+
+ System.out.println("Original: " + source);
+ System.out.println("Translation: " + translations);
+ } catch (UnknownHostException e) {
+ System.out.println("unable to connect: " + command);
+ } catch (FileNotFoundException e) {
+ System.out.println("file not found: " + command);
+ } catch (IOException e) {
+ System.out.println(e.getMessage());
+ }
+ command = scanner.next();
+ }
+ }
+}
diff --git a/Translation.java b/Translation.java
new file mode 100644
index 0000000..fb2b833
--- /dev/null
+++ b/Translation.java
@@ -0,0 +1,61 @@
+package com.company;
+
+import java.io.IOException;
+import com.company.src.URLSourceProvider;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
+/**
+ * Created by InnaBakum on 01.12.2015.
+ */
+public class Translation {
+ private URLSourceProvider urlSourceProvider;
+
+ private static final String YANDEX_API_KEY = "trnsl.1.1.20151208T101945Z.342811abacdd60a7.644872b5cb726286c7b5fbac3ecccc6e43df4a98";
+ private static final String TRANSLATION_DIRECTION = "ru";
+
+ public Translation(URLSourceProvider urlSourceProvider) {
+ this.urlSourceProvider = urlSourceProvider;
+ }
+
+
+ public String translate(String original) throws IOException {
+ StringBuilder stringBuilder = new StringBuilder();
+
+ if (original.contains(".")) {
+ for (String line : original.split("[.]")) {
+ stringBuilder.append(parseContent(urlSourceProvider.load(prepareURL(line)))).append(".");
+ }
+ } else {
+ stringBuilder.append(parseContent(urlSourceProvider.load(prepareURL(original))));
+ }
+ return stringBuilder.toString();
+ }
+
+
+ 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;
+ }
+
+
+ private String parseContent(String content) {
+ if (content.equals("CONNECTION FAIL")) {
+ return "[ lost text ]";
+ }
+ String text = content.substring(content.indexOf("") +6, content.indexOf(""));
+ //text = text.replace("**", "\n");
+ return text;
+ }
+
+
+ private String encodeText(String text) {
+ try {
+ return URLEncoder.encode(text, "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ }
+ return "error";
+ }
+}
diff --git a/src/FileSourceProvider.java b/src/FileSourceProvider.java
new file mode 100644
index 0000000..d9629da
--- /dev/null
+++ b/src/FileSourceProvider.java
@@ -0,0 +1,25 @@
+package com.company.src;
+
+import java.io.*;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.stream.Collectors;
+
+/**
+ * Created by InnaBakum on 01.12.2015.
+ */
+public class FileSourceProvider implements SourceProvider {
+
+ @Override
+ public boolean isAllowed(String pathToSource) {
+ File readFile = new File(pathToSource);
+ return readFile.isFile() && readFile.canRead() && readFile.exists();
+ }
+
+ @Override
+ public String load(String pathToSource) throws IOException {
+ BufferedReader reader = Files.newBufferedReader(Paths.get(pathToSource), StandardCharsets.UTF_8);
+ return reader.lines().map(line -> {return line;}).collect(Collectors.joining("\n"));
+ }
+}
diff --git a/src/SourceLoader.java b/src/SourceLoader.java
new file mode 100644
index 0000000..ba15a4c
--- /dev/null
+++ b/src/SourceLoader.java
@@ -0,0 +1,27 @@
+package com.company.src;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by InnaBakum on 01.12.2015.
+ */
+public class SourceLoader {
+ private List sourceProviders = new ArrayList();
+
+ public SourceLoader() {
+ sourceProviders.add(new FileSourceProvider());
+ sourceProviders.add(new URLSourceProvider());
+ }
+
+ public String loadSource(String pathToSource) throws IOException {
+ for (SourceProvider provider : sourceProviders) {
+ if (provider.isAllowed(pathToSource)) {
+ return provider.load(pathToSource);
+ }
+ }
+
+ throw new IOException("NOT FOUND " + pathToSource);
+ }
+}
diff --git a/src/SourceProvider.java b/src/SourceProvider.java
new file mode 100644
index 0000000..ef928e0
--- /dev/null
+++ b/src/SourceProvider.java
@@ -0,0 +1,13 @@
+package com.company.src;
+
+import java.io.IOException;
+
+/**
+ * Created by InnaBakum on 01.12.2015.
+ */
+public interface SourceProvider {
+
+ public boolean isAllowed(String pathToSource);
+
+ public String load(String pathToSource) throws IOException;
+}
\ No newline at end of file
diff --git a/src/URLSourceProvider.java b/src/URLSourceProvider.java
new file mode 100644
index 0000000..9e2dbf2
--- /dev/null
+++ b/src/URLSourceProvider.java
@@ -0,0 +1,49 @@
+package com.company.src;
+
+import java.net.MalformedURLException;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.stream.Collectors;
+
+/**
+ * Created by InnaBakum on 01.12.2015.
+ */
+public class URLSourceProvider implements SourceProvider {
+
+ private static final int HTTP_STATUS_OK = 200;
+
+ @Override
+ public boolean isAllowed(String pathToSource) {
+ try {
+ URL url = new URL(pathToSource);
+ url.openConnection();
+ return true;
+ } catch (MalformedURLException e) {
+ return false;
+ }catch (IOException ex){
+ return false;
+ }
+ }
+
+ @Override
+ public String load(String pathToSource) throws IOException {
+ URL url = new URL(pathToSource);
+ StringBuilder stringBuilder = new StringBuilder();
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ connection.setRequestProperty("Accept-Encoding", "identity");
+ if (HTTP_STATUS_OK != connection.getResponseCode()) {
+ throw new IOException();
+ }
+
+ BufferedReader reader = new BufferedReader(
+ new InputStreamReader(
+ connection.getInputStream()
+ ));
+
+ return reader.lines().map(line -> {return line;}
+ ).collect(Collectors.joining("\n"));
+ }
+}
\ No newline at end of file