diff --git a/api/.gitignore b/api/.gitignore new file mode 100644 index 0000000..2a91258 --- /dev/null +++ b/api/.gitignore @@ -0,0 +1,201 @@ + +# Created by https://www.gitignore.io/api/java,maven,eclipse,intellij+all,visualstudiocode +# Edit at https://www.gitignore.io/?templates=java,maven,eclipse,intellij+all,visualstudiocode + +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# CDT- autotools +.autotools + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +# Annotation Processing +.apt_generated/ + +# Scala IDE specific (Scala & Java development for Eclipse) +.cache-main +.scala_dependencies +.worksheet + +# Eclipse Core +.project + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Annotation Processing +.apt_generated + +.sts4-cache/ + +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +# Ignores the whole .idea folder and all .iml files +# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 + +.idea/ + +# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 + +*.iml +modules.xml +.idea/misc.xml +*.ipr + +# Sonarlint plugin +.idea/sonarlint + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +.flattened-pom.xml + +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json + +# Ignore all local history of files +.history + +# End of https://www.gitignore.io/api/java,maven,eclipse,intellij+all,visualstudiocode \ No newline at end of file diff --git a/api/README.md b/api/README.md new file mode 100644 index 0000000..e84f94c --- /dev/null +++ b/api/README.md @@ -0,0 +1 @@ +# api \ No newline at end of file diff --git a/api/pom.xml b/api/pom.xml new file mode 100644 index 0000000..c328865 --- /dev/null +++ b/api/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + sjpapi.api + api + 1.0-SNAPSHOT + jar + + api + https://github.com/GHRik/SjpAPI + + + UTF-8 + 11 + ${java.version} + ${java.version} + ${java.version} + + 5.8.1 + + + 3.2.2 + 3.1.0 + 3.1.0 + 3.8.1 + 3.0.0-M5 + 3.2.0 + 3.0.0-M1 + + + + + + org.json + json + 20230227 + + + + + org.junit.jupiter + junit-jupiter-api + ${junit} + test + + + + + + + org.apache.maven.plugins + maven-shade-plugin + ${maven.shade} + + + package + + shade + + + + + sjpapi.api.SjpAPI + + + + + + + + + \ No newline at end of file diff --git a/src/CurlHelper.java b/api/src/main/java/sjpapi/api/CurlHelper.java similarity index 50% rename from src/CurlHelper.java rename to api/src/main/java/sjpapi/api/CurlHelper.java index 8c97b96..bdd6b90 100644 --- a/src/CurlHelper.java +++ b/api/src/main/java/sjpapi/api/CurlHelper.java @@ -4,11 +4,13 @@ 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 { + public static String getOutputFromCurl(String word) { if (word.trim().isEmpty()){ word = "empty"; @@ -16,19 +18,13 @@ public static String getOutputFromCurl(String word) throws IOException { String outputFromCurl = readCurlFromSJP(word); - if (!validator.isCurlValidate(outputFromCurl)){ + if (Boolean.FALSE.equals(validator.isCurlValidate(outputFromCurl))){ return ""; } return outputFromCurl; } - private static String readCurlFromSJP(String word) throws IOException { + private static String readCurlFromSJP(String word) { return reader.getCurlOutput("https://sjp.pl/"+word); } - - - - - - } diff --git a/src/CurlReader.java b/api/src/main/java/sjpapi/api/CurlReader.java similarity index 70% rename from src/CurlReader.java rename to api/src/main/java/sjpapi/api/CurlReader.java index 608d9ec..ac6ed93 100644 --- a/src/CurlReader.java +++ b/api/src/main/java/sjpapi/api/CurlReader.java @@ -6,27 +6,25 @@ 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(); @@ -34,8 +32,9 @@ public void run() { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); + Thread.currentThread().interrupt(); } - return readed; + return readResult; } @@ -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())); } @@ -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(); } - - } diff --git a/api/src/main/java/sjpapi/api/CurlValidator.java b/api/src/main/java/sjpapi/api/CurlValidator.java new file mode 100644 index 0000000..b85f68e --- /dev/null +++ b/api/src/main/java/sjpapi/api/CurlValidator.java @@ -0,0 +1,23 @@ +package sjpapi.api; + +public class CurlValidator { + + public Boolean isCurlValidate( String curlOutput ) { + + return (isResponseOK(curlOutput) || isResponseNotFound(curlOutput)) && isBodySection(curlOutput); + } + + private Boolean isResponseOK( String curlOutput ) { + + return curlOutput.startsWith("200"); + } + + private Boolean isResponseNotFound( String curlOutput ) { + + return curlOutput.startsWith("400"); + } + + private Boolean isBodySection( String curlOutput ) { + return curlOutput.contains("") && curlOutput.contains(""); + } +} diff --git a/src/SjpAPI.java b/api/src/main/java/sjpapi/api/SjpAPI.java similarity index 87% rename from src/SjpAPI.java rename to api/src/main/java/sjpapi/api/SjpAPI.java index d88f0e7..f90cbf1 100644 --- a/src/SjpAPI.java +++ b/api/src/main/java/sjpapi/api/SjpAPI.java @@ -6,6 +6,10 @@ public class SjpAPI { + private SjpAPI() { + } + + @SuppressWarnings("unused") public static String getWord(String word) throws IOException, JSONException { String json; String wordWithoutSpecialChar; diff --git a/src/SjpHelper.java b/api/src/main/java/sjpapi/api/SjpHelper.java similarity index 94% rename from src/SjpHelper.java rename to api/src/main/java/sjpapi/api/SjpHelper.java index 363e615..3420a53 100644 --- a/src/SjpHelper.java +++ b/api/src/main/java/sjpapi/api/SjpHelper.java @@ -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"; diff --git a/src/SjpValidator.java b/api/src/main/java/sjpapi/api/SjpValidator.java similarity index 100% rename from src/SjpValidator.java rename to api/src/main/java/sjpapi/api/SjpValidator.java diff --git a/src/SjpWrapper.java b/api/src/main/java/sjpapi/api/SjpWrapper.java similarity index 93% rename from src/SjpWrapper.java rename to api/src/main/java/sjpapi/api/SjpWrapper.java index a6656b4..62aae70 100644 --- a/src/SjpWrapper.java +++ b/api/src/main/java/sjpapi/api/SjpWrapper.java @@ -20,7 +20,7 @@ public String getWrapped (String curlOutput) throws JSONException, UnsupportedEn private List> regexForInfo (String curlOutput) { List> wrappedCurl = new ArrayList<>(); - if (isInDictionary(curlOutput)) { + if (Boolean.TRUE.equals(isInDictionary(curlOutput))) { final String regex = "]*>(.+?)<\\/h1>.]*>(.+?)<.+?(?=.*)href=\"\\/(.+?)\".+?znaczenie.+?]*>(.+?)<\\/p>"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); @@ -59,7 +59,7 @@ private String translateArrayToJSON(List> 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))); } @@ -68,7 +68,7 @@ private String translateArrayToJSON(List> wordsFromCurlOutput ) thr return allWrappedWord; } - private Boolean TranslateCanBeUsed(String oneParamOfWordFromCurl){ + private Boolean translateCanBeUsed(String oneParamOfWordFromCurl){ return !oneParamOfWordFromCurl.startsWith("niedopuszczalne"); } diff --git a/src/StringUtils.java b/api/src/main/java/sjpapi/api/StringUtils.java similarity index 94% rename from src/StringUtils.java rename to api/src/main/java/sjpapi/api/StringUtils.java index 4f1cb6f..c8b5b64 100644 --- a/src/StringUtils.java +++ b/api/src/main/java/sjpapi/api/StringUtils.java @@ -57,11 +57,14 @@ public class StringUtils { htmlEntities.put("€", "\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) { @@ -86,7 +89,7 @@ public static String htmlToPolishLetter(String htmlWithBadCoding) throws Unsuppo } public static boolean hasSpecyficHTMLTags(String text){ - String HTML_PATTERN = "]*>.+?<\\/a>|]*.+?<\\/span>|]*.+?<\\/p>"; + final String HTML_PATTERN = "]*>.+?<\\/a>|]*.+?<\\/span>|]*.+?<\\/p>"; Pattern pattern = Pattern.compile(HTML_PATTERN); Matcher matcher = pattern.matcher(text); return matcher.find(); diff --git a/test/CurlReaderTest.java b/api/src/test/java/sjpapi/api/CurlReaderTest.java similarity index 96% rename from test/CurlReaderTest.java rename to api/src/test/java/sjpapi/api/CurlReaderTest.java index c1419d9..e2f84e8 100644 --- a/test/CurlReaderTest.java +++ b/api/src/test/java/sjpapi/api/CurlReaderTest.java @@ -1,19 +1,21 @@ package sjpapi.api; -import junit.framework.TestCase; +import org.junit.jupiter.api.Test; import java.io.IOException; -import java.net.MalformedURLException; -public class CurlReaderTest extends TestCase { +import static org.junit.jupiter.api.Assertions.assertEquals; - private CurlReader reader = new CurlReader(); +class CurlReaderTest { - //If this test fails it is means sjp.pl changed something - // in the structure of the page, it is possible that + private final CurlReader reader = new CurlReader(); + + //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 { + @Test + void testIsSJPHaveSameSite() throws IOException { String mockCurl = "200 Słownik SJP.PL - Słownik języka polskiego, słownik do gier
SJP
SŁOWNIK SJP
X

Słownik SJP.PL

Słownik języka polskiego, ortograficzny, wyrazów obcych i słownik do gier w jednym.

Słownik jest rozwijany z myślą o zastosowaniu do sprawdzania pisowni w programach open-source, do gier słownych (np. literaki) i do użytku online jako kilka rodzajów słowników w jednym.

Redakcją słownika zajmują się hobbyści.

Słownik jest udostępniany na otwartych licencjach (różnych w zależności od wersji).

Pierwsza wersja (słownik alternatywny) powstała na bazie dawnego słownika do ispella.

Kontakt w sprawach technicznych: sjpslownik@gmail.com

© Zespół SJP.PL

"; assertEquals(mockCurl,reader.getCurlOutput("https://www.sjp.pl")); } diff --git a/test/CurlValidatorTest.java b/api/src/test/java/sjpapi/api/CurlValidatorTest.java similarity index 78% rename from test/CurlValidatorTest.java rename to api/src/test/java/sjpapi/api/CurlValidatorTest.java index 37750a3..3763b1f 100644 --- a/test/CurlValidatorTest.java +++ b/api/src/test/java/sjpapi/api/CurlValidatorTest.java @@ -1,12 +1,16 @@ package sjpapi.api; -import junit.framework.TestCase; +import org.junit.jupiter.api.Test; -public class CurlValidatorTest extends TestCase { +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class CurlValidatorTest { private CurlValidator validator = new CurlValidator(); - public void testIsHTTPNotFound() { + @Test + void testIsHTTPNotFound() { String mockCurl = "404 Not Found\n" + "Server: nginx\n" + "Date: Thu, 18 Feb 2021 08:56:46 GMT\n" + @@ -16,7 +20,8 @@ public void testIsHTTPNotFound() { assertFalse(validator.isCurlValidate(mockCurl)); } - public void testIsHTTP200() { + @Test + void testIsHTTP200() { String mockCurl = "200 OK\n" + "Server: nginx\n" + "Date: Thu, 18 Feb 2021 08:56:46 GMT\n" + @@ -26,7 +31,8 @@ public void testIsHTTP200() { assertTrue(validator.isCurlValidate(mockCurl)); } - public void testIsHTTP200WithoutBody() { + @Test + void testIsHTTP200WithoutBody() { String mockCurl = "200 OK\n" + "Server: nginx\n" + "Date: Thu, 18 Feb 2021 08:56:46 GMT\n" + @@ -34,5 +40,4 @@ public void testIsHTTP200WithoutBody() { "Transfer-Encoding: chunked"; assertFalse(validator.isCurlValidate(mockCurl)); } - } \ No newline at end of file diff --git a/api/src/test/java/sjpapi/api/SjpValidatorTest.java b/api/src/test/java/sjpapi/api/SjpValidatorTest.java new file mode 100644 index 0000000..9a1f1c6 --- /dev/null +++ b/api/src/test/java/sjpapi/api/SjpValidatorTest.java @@ -0,0 +1,24 @@ +package sjpapi.api; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class SjpValidatorTest { + + private final SjpValidator validator = new SjpValidator(); + + @Test + void testIsCurlHaveH1Tag() { + String mockCurl = "dsa"; + assertTrue(validator.isCurlWithWordValidate(mockCurl)); + } + + @Test + void testIsCurlDONTHaveH1Tag() { + String mockCurl = "das asdas dd dasdas daas ddsomething"; + assertFalse(validator.isCurlWithWordValidate(mockCurl)); + } + +} \ No newline at end of file diff --git a/src/CurlValidator.java b/src/CurlValidator.java deleted file mode 100644 index 89fbb49..0000000 --- a/src/CurlValidator.java +++ /dev/null @@ -1,21 +0,0 @@ -package sjpapi.api; - -public class CurlValidator { - - public Boolean isCurlValidate( String curlOutput ) { - - return isResponseOK(curlOutput) && isBodySection(curlOutput); - } - - private Boolean isResponseOK( String curlOutput ) { - - return curlOutput.startsWith("200") || curlOutput.startsWith("404"); - } - - private Boolean isBodySection( String curlOutput ) { - if (curlOutput.contains("") && curlOutput.contains("")){ - return true; - } - return false; - } -} diff --git a/test/SjpValidatorTest.java b/test/SjpValidatorTest.java deleted file mode 100644 index 5151e86..0000000 --- a/test/SjpValidatorTest.java +++ /dev/null @@ -1,19 +0,0 @@ -package sjpapi.api; - -import junit.framework.TestCase; - -public class SjpValidatorTest extends TestCase { - - private SjpValidator validator = new SjpValidator(); - - - public void testIsCurlHaveH1Tag() { - String mockCurl = "dsa"; - assertTrue(validator.isCurlWithWordValidate(mockCurl)); - } - public void testIsCurlDONTHaveH1Tag() { - String mockCurl = "das asdas dd dasdas daas ddsomething"; - assertFalse(validator.isCurlWithWordValidate(mockCurl)); - } - -} \ No newline at end of file