Permalink
Browse files

Add FacesConfigXml#getSupportedLocales() which returns an ordered list

  • Loading branch information...
1 parent 15a5705 commit 465a78dab39611033e8ef140225416834de86cca @BalusC BalusC committed Aug 26, 2015
@@ -16,18 +16,21 @@
import static org.omnifaces.util.Faces.hasContext;
import static org.omnifaces.util.Xml.createDocument;
import static org.omnifaces.util.Xml.getNodeList;
+import static org.omnifaces.util.Xml.getTextContent;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
+import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.webapp.FacesServlet;
import javax.servlet.Filter;
@@ -37,6 +40,7 @@
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
+import org.omnifaces.util.Utils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -87,6 +91,10 @@
"META-INF/faces-config.xml";
private static final String XPATH_RESOURCE_BUNDLE =
"application/resource-bundle";
+ private static final String XPATH_DEFAULT_LOCALE =
+ "application/locale-config/default-locale";
+ private static final String XPATH_SUPPORTED_LOCALE =
+ "application/locale-config/supported-locale";
private static final String XPATH_VAR =
"var";
private static final String XPATH_BASE_NAME =
@@ -100,6 +108,7 @@
private final AtomicBoolean initialized = new AtomicBoolean();
private Map<String, String> resourceBundles;
+ private List<Locale> supportedLocales;
// Init -----------------------------------------------------------------------------------------------------------
@@ -123,6 +132,7 @@ public FacesConfigXml init(ServletContext servletContext) {
Element facesConfigXml = loadFacesConfigXml(servletContext).getDocumentElement();
XPath xpath = XPathFactory.newInstance().newXPath();
resourceBundles = parseResourceBundles(facesConfigXml, xpath);
+ supportedLocales = parseSupportedLocales(facesConfigXml, xpath);
}
catch (Exception e) {
initialized.set(false);
@@ -145,6 +155,20 @@ public FacesConfigXml init(ServletContext servletContext) {
return resourceBundles;
}
+ /**
+ * Returns an ordered list of all supported locales on this application, with the default locale as the first
+ * item, if any. This will return an empty list if there are no locales definied in <code>faces-config.xml</code>.
+ * @return An ordered list of all supported locales on this application, with the default locale as the first
+ * item, if any.
+ * @see Application#getDefaultLocale()
+ * @see Application#getSupportedLocales()
+ * @since 2.2
+ */
+ public List<Locale> getSupportedLocales() {
+ checkInitialized();
+ return supportedLocales;
+ }
+
private void checkInitialized() {
// This init() call is performed here instead of in constructor, because WebLogic loads this enum as a CDI
// managed bean (in spite of having a VetoAnnotatedTypeExtension) which in turn implicitly invokes the enum
@@ -192,4 +216,29 @@ private static Document loadFacesConfigXml(ServletContext context) throws IOExce
return Collections.unmodifiableMap(resourceBundles);
}
+ /**
+ * Create and return a list of default locale and all supported locales in same order as in the given document.
+ * @throws XPathExpressionException
+ */
+ private static List<Locale> parseSupportedLocales(Element facesConfigXml, XPath xpath) throws XPathExpressionException {
+ List<Locale> supportedLocales = new ArrayList<>();
+ String defaultLocale = xpath.compile(XPATH_DEFAULT_LOCALE).evaluate(facesConfigXml).trim();
+
+ if (!Utils.isEmpty(defaultLocale)) {
+ supportedLocales.add(new Locale(defaultLocale));
+ }
+
+ NodeList supportedLocaleNodes = getNodeList(facesConfigXml, xpath, XPATH_SUPPORTED_LOCALE);
+
+ for (int i = 0; i < supportedLocaleNodes.getLength(); i++) {
+ Locale supportedLocale = new Locale(getTextContent(supportedLocaleNodes.item(i)));
+
+ if (!supportedLocales.contains(supportedLocale)) {
+ supportedLocales.add(supportedLocale);
+ }
+ }
+
+ return Collections.unmodifiableList(supportedLocales);
+ }
+
}
@@ -55,6 +55,7 @@
import javax.servlet.http.HttpSession;
import org.omnifaces.component.ParamHolder;
+import org.omnifaces.config.FacesConfigXml;
/**
* <p>
@@ -571,9 +572,10 @@ public static Locale getDefaultLocale() {
}
/**
- * Returns a list of all supported locales on this application, with the default locale as the first item, if any.
- * This will return an empty list if there are no locales definied in <code>faces-config.xml</code>.
- * @return A list of all supported locales on this application, with the default locale as the first item, if any.
+ * Returns an unordered list of all supported locales on this application, with the default locale as the first
+ * item, if any. This will return an empty list if there are no locales definied in <code>faces-config.xml</code>.
+ * @return An unordered list of all supported locales on this application, with the default locale as the first
+ * item, if any. If you need an ordered list, use {@link FacesConfigXml#getSupportedLocales()} instead.
* @see Application#getDefaultLocale()
* @see Application#getSupportedLocales()
*/

0 comments on commit 465a78d

Please sign in to comment.