/** * Use Java's built in support for bidi text to determine the base directionality of the element's text. The * response to this only indicates the *base* directionality, it does not indicate whether or not there are any RTL * characters in the text. According to the Atom Bidi spec, if the dir attribute is set explicitly, we should not do * language guessing. This restriction can be bypassed by setting ignoredir to true. */ public static <T extends Element> Direction guessDirectionFromJavaBidi(T element, boolean ignoredir) { if (!ignoredir && hasDirection(element)) return getDirection(element); return Bidi.guessDirectionFromJavaBidi(element.getText()); }
/** * Attempt to guess the base direction of an element using an analysis of the directional properties of the * characters used. This is a brute-force style approach that can achieve fairly reasonable results when the element * text consists primarily of characters with the same bidi properties. This approach is implemented by the Snarfer * feed reader as is documented at http://www.xn--8ws00zhy3a.com/blog/2006/12/right-to-left-rss According to the * Atom Bidi spec, if the dir attribute is set explicitly, we should not do language guessing. This restriction can * be bypassed by setting ignoredir to true. */ public static <T extends Element> Direction guessDirectionFromTextProperties(T element, boolean ignoredir) { if (!ignoredir && hasDirection(element)) return getDirection(element); return Bidi.guessDirectionFromTextProperties(element.getText()); }
/** * Attempt to guess the base direction using the charset encoding. This is a bit of a last resort approach */ @SuppressWarnings("unchecked") public static <T extends Element> Direction guessDirectionFromEncoding(T element, boolean ignoredir) { if (!ignoredir && hasDirection(element)) return getDirection(element); Document doc = element.getDocument(); if (doc == null) return Direction.UNSPECIFIED; return Bidi.guessDirectionFromEncoding(doc.getCharset()); }
/** * Attempt to guess the base direction using the in-scope language. Implements the method used by Internet Explorer * 7's feed view documented here: * http://blogs.msdn.com/rssteam/archive/2007/05/17/reading-feeds-in-right-to-left-order.aspx. This algorithm * differs slightly from the method documented in that the primary language tag is case insensitive. If the language * tag is not specified, then the default Locale is used to determine the direction. According to the Atom Bidi * spec, if the dir attribute is set explicitly, we should not do language guessing. This restriction can be * bypassed by setting ignoredir to true. */ public static <T extends Element> Direction guessDirectionFromLanguage(T element, boolean ignoredir) { if (!ignoredir && hasDirection(element)) return getDirection(element); String language = element.getLanguage(); Lang lang = language != null ? new Lang(language) : new Lang(Locale.getDefault()); return Bidi.guessDirectionFromLanguage(lang); }