Skip to content
Open
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
12 changes: 4 additions & 8 deletions src/CurlHelper.java → src/main/java/sjpapi/api/CurlHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

public class CurlHelper {

private final static CurlReader reader = new CurlReader();
private final static CurlValidator validator = new CurlValidator();
private static final CurlReader reader = new CurlReader();
private static final CurlValidator validator = new CurlValidator();

private CurlHelper() {
}

public static String getOutputFromCurl(String word) throws IOException {

Expand All @@ -25,10 +27,4 @@ public static String getOutputFromCurl(String word) throws IOException {
private static String readCurlFromSJP(String word) throws IOException {
return reader.getCurlOutput("https://sjp.pl/"+word);
}






}
43 changes: 18 additions & 25 deletions src/CurlReader.java → src/main/java/sjpapi/api/CurlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,35 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;

import static java.util.logging.Level.SEVERE;

public class CurlReader {

private String readed = "";
private String readResult = "";
private static final Logger logger = Logger.getLogger(CurlReader.class.getName());

public String getCurlOutput(final String https) {


Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
try {
readed = CurlReader.this.read(https);
} catch (MalformedURLException e) {
e.printStackTrace();
}

} catch (Exception e) {
e.printStackTrace();
}
Thread thread = new Thread(() -> {
try {
readResult = read(https);
} catch (MalformedURLException e) {
logger.log(SEVERE, "MalformedURLException");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
return readed;
return readResult;
}


Expand All @@ -57,12 +56,9 @@ private String read(String https) throws IOException {

BufferedReader br;
String response = curlOutput.toString();
if ("404".equals(response))
{
if ("404".equals(response)) {
br = new BufferedReader(new InputStreamReader(urlConnection.getErrorStream()));
}
else
{
} else {
br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
}

Expand All @@ -72,13 +68,10 @@ private String read(String https) throws IOException {
}
br.close();
urlConnection.disconnect();
}
catch(Exception e) {
} catch (Exception e) {
curlOutput = new StringBuilder();
}

return curlOutput.toString();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ private Boolean isResponseOK( String curlOutput ) {
}

private Boolean isBodySection( String curlOutput ) {
if (curlOutput.contains("<body>") && curlOutput.contains("</body>")){
return true;
}
return false;
return curlOutput.contains("<body>") && curlOutput.contains("</body>");
}
}
3 changes: 3 additions & 0 deletions src/SjpAPI.java → src/main/java/sjpapi/api/SjpAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

public class SjpAPI {

private SjpAPI() {
}

public static String getWord(String word) throws IOException, JSONException {
String json;
String wordWithoutSpecialChar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public class SjpHelper {
private static final SjpValidator validator = new SjpValidator();
private static final SjpWrapper wrapper = new SjpWrapper();

private SjpHelper() {
}

public static String translateFromCurlToJSON(String curl ) throws JSONException, UnsupportedEncodingException {

String json = "EMPTY";
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public String getWrapped (String curlOutput) throws JSONException, UnsupportedEn
private List<List<String>> regexForInfo (String curlOutput) {

List<List<String>> wrappedCurl = new ArrayList<>();
if (isInDictionary(curlOutput)) {
if (Boolean.TRUE.equals(isInDictionary(curlOutput))) {

final String regex = "<h1[^>]*>(.+?)<\\/h1>.<p[^>]*>(.+?)<.+?(?=.*)href=\"\\/(.+?)\".+?znaczenie.+?<p[^>]*>(.+?)<\\/p>";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
Expand Down Expand Up @@ -59,7 +59,7 @@ private String translateArrayToJSON(List<List<String>> wordsFromCurlOutput ) thr
jsonObject.put("name",wordsFromCurlOutput.get(0).get(0));
jsonObject.put("count",wordsFromCurlOutput.size());
for( int i = 0; i < wordsFromCurlOutput.size(); i++){
jsonObject.put("canBeUsed"+"["+i+"]",TranslateCanBeUsed(wordsFromCurlOutput.get(i).get(1)));
jsonObject.put("canBeUsed"+"["+i+"]", translateCanBeUsed(wordsFromCurlOutput.get(i).get(1)));
jsonObject.put("variant"+"["+i+"]", StringUtils.htmlToPolishLetter(wordsFromCurlOutput.get(i).get(2)));
jsonObject.put("meaning"+"["+i+"]", wrappedDescription(wordsFromCurlOutput.get(i).get(3)));
}
Expand All @@ -68,7 +68,7 @@ private String translateArrayToJSON(List<List<String>> wordsFromCurlOutput ) thr
return allWrappedWord;
}

private Boolean TranslateCanBeUsed(String oneParamOfWordFromCurl){
private Boolean translateCanBeUsed(String oneParamOfWordFromCurl){
return !oneParamOfWordFromCurl.startsWith("niedopuszczalne");

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ public class StringUtils {
htmlEntities.put("&euro;", "\u20a0");
}

private StringUtils() {
}

// Function from
// https://www.rgagnon.com/javadetails/java-0307.html
public static String unescapeHTML(String source, int start) {
int startOfHtmlSpecialCharCombination, closeOfHtmlSpecialCharCombination;
int startOfHtmlSpecialCharCombination;
int closeOfHtmlSpecialCharCombination;

startOfHtmlSpecialCharCombination = source.indexOf("&", start);
if (startOfHtmlSpecialCharCombination > -1) {
Expand All @@ -86,7 +89,7 @@ public static String htmlToPolishLetter(String htmlWithBadCoding) throws Unsuppo
}

public static boolean hasSpecyficHTMLTags(String text){
String HTML_PATTERN = "<a[^>]*>.+?<\\/a>|<span[^>]*.+?<\\/span>|<p[^>]*.+?<\\/p>";
final String HTML_PATTERN = "<a[^>]*>.+?<\\/a>|<span[^>]*.+?<\\/span>|<p[^>]*.+?<\\/p>";
Pattern pattern = Pattern.compile(HTML_PATTERN);
Matcher matcher = pattern.matcher(text);
return matcher.find();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package sjpapi.api;

import junit.framework.TestCase;
import sjpapi.api.CurlReader;

import java.io.IOException;
import java.net.MalformedURLException;

public class CurlReaderTest extends TestCase {

private CurlReader reader = new CurlReader();

//If this test fails it is means sjp.pl changed something
// in the structure of the page, it is possible that
//If this test fails, it means sjp.pl changed something
// in the structure of the page; it is possible that
// SjpAPI has stopped working.
// If it still works, you can ignore this test.
public void testIsSJPHaveSameSite() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sjpapi.api;

import junit.framework.TestCase;
import sjpapi.api.CurlValidator;

public class CurlValidatorTest extends TestCase {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package sjpapi.api;

import junit.framework.TestCase;
import sjpapi.api.SjpValidator;

public class SjpValidatorTest extends TestCase {

private SjpValidator validator = new SjpValidator();
private final SjpValidator validator = new SjpValidator();


public void testIsCurlHaveH1Tag() {
Expand Down