Skip to content
Open

HW5 #12

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Main.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
61 changes: 61 additions & 0 deletions Translation.java
Original file line number Diff line number Diff line change
@@ -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("<text>") +6, content.indexOf("</text>"));
//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";
}
}
25 changes: 25 additions & 0 deletions src/FileSourceProvider.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
27 changes: 27 additions & 0 deletions src/SourceLoader.java
Original file line number Diff line number Diff line change
@@ -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<SourceProvider> sourceProviders = new ArrayList<SourceProvider>();

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);
}
}
13 changes: 13 additions & 0 deletions src/SourceProvider.java
Original file line number Diff line number Diff line change
@@ -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;
}
49 changes: 49 additions & 0 deletions src/URLSourceProvider.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}