@SuppressWarnings("unchecked") public DateProcessor create(Map<String, Processor.Factory> registry, String processorTag, Map<String, Object> config) throws Exception { String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field", DEFAULT_TARGET_FIELD); String timezoneString = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "timezone"); DateTimeZone timezone = timezoneString == null ? DateTimeZone.UTC : DateTimeZone.forID(timezoneString); String localeString = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "locale"); Locale locale = Locale.ENGLISH; if (localeString != null) { try { locale = (new Locale.Builder()).setLanguageTag(localeString).build(); } catch (IllformedLocaleException e) { throw new IllegalArgumentException("Invalid language tag specified: " + localeString); } } List<String> formats = ConfigurationUtils.readList(TYPE, processorTag, config, "formats"); return new DateProcessor(processorTag, timezone, locale, field, formats, targetField); }
/** * Returns an instance of Locale used for service look up. * The result Locale has no extensions except for ja_JP_JP * and th_TH_TH * * @param locale the locale * @return the locale used for service look up */ static Locale getLookupLocale(Locale locale) { Locale lookupLocale = locale; if (locale.hasExtensions() && !locale.equals(JRELocaleConstants.JA_JP_JP) && !locale.equals(JRELocaleConstants.TH_TH_TH)) { // remove extensions Builder locbld = new Builder(); try { locbld.setLocale(locale); locbld.clearExtensions(); lookupLocale = locbld.build(); } catch (IllformedLocaleException e) { // A Locale with non-empty extensions // should have well-formed fields except // for ja_JP_JP and th_TH_TH. Therefore, // it should never enter in this catch clause. config(LocaleServiceProviderPool.class, "A locale(" + locale + ") has non-empty extensions, but has illformed fields."); // Fallback - script field will be lost. lookupLocale = new Locale(locale.getLanguage(), locale.getCountry(), locale.getVariant()); } } return lookupLocale; }
private boolean filterOutUnsupportedTags(byte[] b) { List<Locale> locales; try { locales = Arrays.asList(new String(b).split(" ")).stream() .filter(tag -> !tag.isEmpty()) .map(IncludeLocalesPlugin::tagToLocale) .collect(Collectors.toList()); } catch (IllformedLocaleException ile) { // Seems not an available locales string literal. return false; } byte[] filteredBytes = filterLocales(locales).stream() .collect(Collectors.joining(" ")) .getBytes(); System.arraycopy(filteredBytes, 0, b, 0, filteredBytes.length); Arrays.fill(b, filteredBytes.length, b.length, (byte)' '); return true; }
public LiteralImpl(final String literal, final String languageTag) { this.lexicalForm = Objects.requireNonNull(literal); this.languageTag = Objects.requireNonNull(lowerCase(languageTag)); if (languageTag.isEmpty()) { // TODO: Check against // http://www.w3.org/TR/n-triples/#n-triples-grammar throw new IllegalArgumentException("Language tag can't be null"); } try { new Locale.Builder().setLanguageTag(languageTag); } catch (final IllformedLocaleException ex) { throw new IllegalArgumentException("Invalid languageTag: " + languageTag, ex); } // System.out.println(aLocale); this.dataType = Types.RDF_LANGSTRING; }
public static void main(String...a) { try { Locale.Builder locBuild = new Locale.Builder(); //locBuild.setLanguageTag("de-CH-x-phonebk"); locBuild.setLanguageTag("zh-cn-a-myext-x-private"); Locale lc = locBuild.build(); System.out.println("L:"+lc.getLanguage()); System.out.println("S:"+lc.getScript()); System.out.println("C:"+lc.getCountry()); System.out.println("V:"+lc.getVariant()); System.out.println(lc.toLanguageTag()); LangTag lang3 = LangTagParser.parse("zh-cn-a-myext-x-private") ; System.out.println(); System.out.println(lang3.asString()); LangTag lang2 = LangTagParserAlt.parse("zh-cn-a-myext-x-private") ; System.out.println(); System.out.println(lang2.asString()); } catch (IllformedLocaleException ex) { ex.printStackTrace(); } }
@Override public Locale resolveLocale(HttpServletRequest request) { Locale defaultLocale = Locale.getDefault(); String language = request.getParameter(LANGUAGE_QUERY_PARAMETER_NAME); if (defaultLocale != null && language == null) { return defaultLocale; } Locale resolvedLocale; try { resolvedLocale = new Locale.Builder().setLanguageTag(language).build(); } catch (IllformedLocaleException e) { resolvedLocale = Locale.getDefault(); } return resolvedLocale; }
/** * Returns an instance of Locale used for service look up. * The result Locale has no extensions except for ja_JP_JP * and th_TH_TH * * @param locale the locale * @return the locale used for service look up */ private static Locale getLookupLocale(Locale locale) { Locale lookupLocale = locale; Set<Character> extensions = locale.getExtensionKeys(); if (!extensions.isEmpty() && !locale.equals(locale_ja_JP_JP) && !locale.equals(locale_th_TH_TH)) { // remove extensions Builder locbld = new Builder(); try { locbld.setLocale(locale); locbld.clearExtensions(); lookupLocale = locbld.build(); } catch (IllformedLocaleException e) { // A Locale with non-empty extensions // should have well-formed fields except // for ja_JP_JP and th_TH_TH. Therefore, // it should never enter in this catch clause. config("A locale(" + locale + ") has non-empty extensions, but has illformed fields."); // Fallback - script field will be lost. lookupLocale = new Locale(locale.getLanguage(), locale.getCountry(), locale.getVariant()); } } return lookupLocale; }
private Locale resolveApplicationLocale(String configuredLocale) { Locale locale; if (StringUtils.isNotBlank(configuredLocale)) { try { locale = new Locale.Builder().setLanguageTag(configuredLocale).build(); if (LocaleUtils.isAvailableLocale(locale)) { LOGGER.debug("Setting application locale to configured {}", locale); } else { locale = getDefaultLocale(); LOGGER.debug("Locale {} is not available. Defaulting to {}", configuredLocale, getDefaultLocale()); } } catch (IllformedLocaleException e) { locale = getDefaultLocale(); LOGGER.warn( "Error parsing configured application locale: {}. Defaulting to {}. Locale must be in the form \"en-US\" or \"fr-FR\"", configuredLocale, getDefaultLocale()); } } else { locale = getDefaultLocale(); LOGGER.debug("Setting application locale to default value {}", getDefaultLocale()); } LOGGER.info("Application locale set to: '{}'", locale); return locale; }
@Override public DateIndexNameProcessor create(Map<String, Processor.Factory> registry, String tag, Map<String, Object> config) throws Exception { String localeString = ConfigurationUtils.readOptionalStringProperty(TYPE, tag, config, "locale"); String timezoneString = ConfigurationUtils.readOptionalStringProperty(TYPE, tag, config, "timezone"); DateTimeZone timezone = timezoneString == null ? DateTimeZone.UTC : DateTimeZone.forID(timezoneString); Locale locale = Locale.ENGLISH; if (localeString != null) { try { locale = (new Locale.Builder()).setLanguageTag(localeString).build(); } catch (IllformedLocaleException e) { throw new IllegalArgumentException("Invalid language tag specified: " + localeString); } } List<String> dateFormatStrings = ConfigurationUtils.readOptionalList(TYPE, tag, config, "date_formats"); if (dateFormatStrings == null) { dateFormatStrings = Collections.singletonList("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); } List<Function<String, DateTime>> dateFormats = new ArrayList<>(dateFormatStrings.size()); for (String format : dateFormatStrings) { DateFormat dateFormat = DateFormat.fromString(format); dateFormats.add(dateFormat.getFunction(format, timezone, locale)); } String field = ConfigurationUtils.readStringProperty(TYPE, tag, config, "field"); String indexNamePrefix = ConfigurationUtils.readStringProperty(TYPE, tag, config, "index_name_prefix", ""); String dateRounding = ConfigurationUtils.readStringProperty(TYPE, tag, config, "date_rounding"); String indexNameFormat = ConfigurationUtils.readStringProperty(TYPE, tag, config, "index_name_format", "yyyy-MM-dd"); return new DateIndexNameProcessor(tag, field, dateFormats, timezone, indexNamePrefix, dateRounding, indexNameFormat); }
private boolean filterOutUnsupportedTags(byte[] b) { List<Locale> locales; List<String> originalTags = Arrays.asList(new String(b).split(" ")); try { locales = originalTags.stream() .filter(tag -> !tag.isEmpty()) .map(IncludeLocalesPlugin::tagToLocale) .collect(Collectors.toList()); } catch (IllformedLocaleException ile) { // Seems not an available locales string literal. return false; } byte[] filteredBytes = filterLocales(locales).stream() // Make sure the filtered language tags do exist in the // original supported tags for compatibility codes, e.g., "iw" .filter(originalTags::contains) .collect(Collectors.joining(" ")) .getBytes(); if (filteredBytes.length > b.length) { throw new InternalError("Size of filtered locales is bigger than the original one"); } System.arraycopy(filteredBytes, 0, b, 0, filteredBytes.length); Arrays.fill(b, filteredBytes.length, b.length, (byte)' '); return true; }
/** * Get a valid locale from the cookie value. * * @param localeCookieValue * @return a valid locale. */ String getValidLocaleFromCookie(String localeCookieValue) { String validLocale; try { Locale localeFromCookie = new Locale.Builder().setLanguageTag(localeCookieValue).build(); validLocale = localeFromCookie.toLanguageTag(); } catch (NullPointerException | IllformedLocaleException e) { logger.debug("Invalid localeCookieValue, fallback to en"); validLocale = "en"; } return validLocale; }
public static LangTag parse(String x) { try { locBuild.clear(); locBuild.setLanguageTag(x); return asLangTag(locBuild); } catch (IllformedLocaleException ex) { return null; } }
public static String canonical(String str) { try { // Does not do conversion of language for ISO 639 codes that have changed. return locBuild.setLanguageTag(str).build().toLanguageTag(); } catch (IllformedLocaleException ex) { return str; } }
/** {@inheritDoc} */ public boolean run(final Context context, final Result proto, final Map<String, Object> data) { Result nr = new Result(proto); nr.setCode("content"); nr.addNode("lang"); nr.setDocument("rfc7483"); nr.setReference("4.4"); String lang = Utils.getStringAttribute(context, nr, "lang", null, data); if (lang == null) { return false; } Result vr = new Result(nr); boolean res = true; try { new Locale.Builder().setLanguageTag(lang); } catch (IllformedLocaleException e) { vr.setStatus(Status.Failure); vr.setInfo("invalid: " + e.toString()); res = false; } if (res) { vr.setStatus(Status.Success); vr.setInfo("valid"); } context.addResult(vr); return res; }
public KiWiHandler(KiWiStore store, KiWiLoaderConfiguration config) { this.config = config; this.store = store; this.localeCache = CacheBuilder.newBuilder() .maximumSize(100) .build(new CacheLoader<String, Locale>() { @Override public Locale load(String lang) throws Exception { try { Locale.Builder builder = new Locale.Builder(); builder.setLanguageTag(lang); return builder.build(); } catch (IllformedLocaleException ex) { log.warn("malformed language literal (language: {})", lang); return null; } } }); if(config.isStatementExistanceCheck()) { switch (store.getPersistence().getConfiguration().getRegistryStrategy()) { case DATABASE: log.info("KiWi Loader: database registry"); registry = new DBTripleRegistry(store); break; case CACHE: log.info("KiWi Loader: cache registry"); registry = new CacheTripleRegistry(store.getPersistence().getCacheManager()); break; case LOCAL: log.info("KiWi Loader: in-memory registry"); registry = new LocalTripleRegistry(); break; default: log.info("KiWi Loader: in-memory registry"); registry = new LocalTripleRegistry(); } } log.info("KiWi Loader: namespaces {}", config.isIgnoreNamespaces() ? "ignored" : "enabled"); }
BuilderILE(String... args) { super(IllformedLocaleException.class); this.args = args; run(); }