package gr.thmmy.mthmmy.utils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.ArrayList;
/**
* This class consists exclusively of static classes (enums) and methods (excluding methods of inner
* classes). It can be used to resolve a page's language and state or fix embedded videos html code.
*/
public class ParseHelpers {
/**
* Debug Tag for logging debug output to LogCat
*/
@SuppressWarnings("unused")
private static final String TAG = "ParseHelpers";
/**
* An enum describing a forum page's language by defining the types:
* - {@link #PAGE_INCOMPLETE}
* - {@link #UNDEFINED_LANGUAGE}
* - {@link #ENGLISH}
* - {@link #ENGLISH_GUEST}
* - {@link #GREEK}
*
*/
public enum Language {
/**
* Page language is greek.
*/
GREEK,
/**
* Page language is english.
*/
ENGLISH,
/**
* Page language is english and the user is guest.
*/
ENGLISH_GUEST,
/**
* Page is incomplete. Data are not enough to determine the language.
*/
PAGE_INCOMPLETE,
/**
* Page language is not (yet) supported.
*/
UNDEFINED_LANGUAGE;
/**
* Returns one of the supported forum languages.
*
* @param page {@link Document} object containing this page's source code
* @return language of this page
* @see org.jsoup.Jsoup Jsoup
*/
public static Language getLanguage(Document page) {
Element welcoming = page.select("h3").first();
if (welcoming == null) {
Element welcomingGuest = page.select("div[id=myuser]").first();
if (welcomingGuest != null) {
if (welcomingGuest.text().contains("Welcome")) return ENGLISH_GUEST;
}
return PAGE_INCOMPLETE;
} else if (welcoming.text().contains("Καλώς ορίσατε")) return GREEK;
else if (welcoming.text().contains("Hey")) return ENGLISH;
else return UNDEFINED_LANGUAGE;
}
/**
* This method defines a custom equality check for {@link Language} enums.
* Method returns true if parameter's Target is the same as the object and in the specific
* cases described below, false otherwise.