/** * Parses a language tag array from the specified string values. * * @param values The string values. May be {@code null}. * * @return The language tag array, or {@code null} if the parsed string * array is null. * * @throws LangTagException If parsing failed. */ public static LangTag[] parseLangTagArray(final String ... values) throws LangTagException { if (values == null) return null; LangTag[] out = new LangTag[values.length]; for (int i=0; i < values.length; i++) { out[i] = LangTag.parse(values[i]); } return out; }
/** * Overrides {@code Object.hashCode()}. * * @return The object hash code. */ @Override public int hashCode() { return toString().hashCode(); }
/** * Sets the region. * * <p>See RFC 5646 section 2.2.4. * * @param region The region, as a two-letter ISO 3166-1 code or a three- * digit UN M.49 code. {@code null} if not defined. * * @throws LangTagException If the region syntax is invalid. */ public void setRegion(final String region) throws LangTagException { if (region == null) { this.region = null; return; } ensureMaxLength(region); if (! isRegion(region)) throw new LangTagException("Invalid region subtag: Must be a two-letter ISO 3166-1 code or a three-digit UN M.49 code"); this.region = region.toUpperCase(); }
/** * Sets the private use. * * <p>See RFC 5646 section 2.2.7. * * @param privateUse The private use. {@code null} if not defined. * * @throws LangTagException If the extension syntax is invalid. */ public void setPrivateUse(final String privateUse) throws LangTagException { if (privateUse == null) { this.privateUse = null; return; } ensureMaxLength(privateUse); if (! isPrivateUse(privateUse)) throw new LangTagException("Invalid private use subtag"); this.privateUse = privateUse.toLowerCase(); }
List<String> extLangSubtags = new LinkedList<String>(); if (isPrimaryLanguage(subtags[0])) primaryLang = subtags[pos++]; while (pos < subtags.length && isExtendedLanguageSubtag(subtags[pos])) extLangSubtags.add(subtags[pos++]); LangTag langTag = new LangTag(primaryLang, extLangSubtags.toArray(new String[]{})); if (pos < subtags.length && isScript(subtags[pos])) langTag.setScript(subtags[pos++]); if (pos < subtags.length && isRegion(subtags[pos])) langTag.setRegion(subtags[pos++]); while (pos < subtags.length && isVariant(subtags[pos])) variantSubtags.add(subtags[pos++]); langTag.setVariants(variantSubtags.toArray(new String[]{})); while (pos < subtags.length && isExtensionSingleton(subtags[pos])) { langTag.setExtensions(extSubtags.toArray(new String[]{})); throw new LangTagException("Invalid private use subtag"); langTag.setPrivateUse("x-" + subtags[pos++]);
/** * Sets the extended language subtags. * * <p>See RFC 5646 section 2.2.2. * * @param languageSubtags The extended language subtags, as three-letter * ISO 639-3 codes. {@code null} if none. */ private void setExtendedLanguageSubtags(final String... languageSubtags) throws LangTagException { if (languageSubtags == null || languageSubtags.length == 0) { this.languageSubtags = null; return; } this.languageSubtags = new String[languageSubtags.length]; for (int i=0; i < languageSubtags.length; i++) { ensureMaxLength(languageSubtags[i]); if (! isExtendedLanguageSubtag(languageSubtags[i])) throw new LangTagException("Invalid extended language subtag: Must be a three-letter ISO 639-3 code"); this.languageSubtags[i] = languageSubtags[i].toLowerCase(); } }
/** * Sets the primary language subtag. * * <p>See RFC 5646 section 2.2.1. * * @param primaryLanguage The primary language, as the shortest two or * three-letter ISO 639 code. May be * {@code null}. * * @throws LangTagException If the primary language syntax is invalid. */ private void setPrimaryLanguage(final String primaryLanguage) throws LangTagException { if (primaryLanguage == null) { this.primaryLanguage = null; return; } ensureMaxLength(primaryLanguage); if (! isPrimaryLanguage(primaryLanguage)) throw new LangTagException("Invalid primary language subtag: Must be a two or three-letter ISO 639 code"); this.primaryLanguage = primaryLanguage.toLowerCase(); }
/** * Sets the extensions. * * <p>See RFC 5646 section 2.2.6. * * @param extensions The extensions. {@code null} if not defined. * * @throws LangTagException If the extension syntax is invalid. */ public void setExtensions(final String... extensions) throws LangTagException { if (extensions == null || extensions.length == 0) { this.extensions = null; return; } this.extensions = new String[extensions.length]; for (int i=0; i < extensions.length; i++) { ensureMaxLength(extensions[i]); if (! isExtension(extensions[i])) throw new LangTagException("Invalid extension subtag"); this.extensions[i] = extensions[i].toLowerCase(); } }
/** * Sets the variants. * * <p>See RFC 5646 section 2.2.5. * * @param variants The variants. {@code null} if not defined. * * @throws LangTagException If the variant syntax is invalid. */ public void setVariants(final String... variants) throws LangTagException { if (variants == null || variants.length == 0) { this.variants = null; return; } this.variants = new String[variants.length]; for (int i=0; i < variants.length; i++) { ensureMaxLength(variants[i]); if (! isVariant(variants[i])) throw new LangTagException("Invalid variant subtag"); this.variants[i] = variants[i].toLowerCase(); } }
/** * Sets the script. * * <p>See RFC 5646 section 2.2.3. * * @param script The script, as a four-letter ISO 15924 code. * {@code null} if not defined. * * @throws LangTagException If the script syntax is invalid. */ public void setScript(final String script) throws LangTagException { if (script == null) { this.script = null; return; } ensureMaxLength(script); if (! isScript(script)) throw new LangTagException("Invalid script subtag: Must be a four-letter ISO 15924 code"); this.script = script.substring(0, 1).toUpperCase() + script.substring(1).toLowerCase(); }
@Override public String toString() { StringBuilder sb = new StringBuilder(getLanguage());
/** * Parses a language tag list from the specified string values. * * @param values The string values. May be {@code null}. * * @return The language tag list, or {@code null} if the parsed string * array is null. * * @throws LangTagException If parsing failed. */ public static List<LangTag> parseLangTagList(final String ... values) throws LangTagException { if (values == null) return null; List<LangTag> out = new ArrayList<LangTag>(values.length); for (String s: values) { out.add(LangTag.parse(s)); } return out; }
/** * Returns a string array representation of the specified language tags * collection. * * @param langTags The language tags list. May be {@code null}. * * @return The string list, or {@code null} if the original list is * {@code null}. */ public static String[] toStringArray(final Collection<LangTag> langTags) { if (langTags == null) return null; String[] out = new String[langTags.size()]; int i=0; for (LangTag lt: langTags) { out[i++] = lt.toString(); } return out; }
/** * Parses a language tag list from the specified string collection. * * @param collection The string collection. May be {@code null}. * * @return The language tag list, or {@code null} if the parsed string * collection is null. * * @throws LangTagException If parsing failed. */ public static List<LangTag> parseLangTagList(final Collection<String> collection) throws LangTagException { if (collection == null) return null; List<LangTag> out = new ArrayList<LangTag>(collection.size()); for (String s: collection) { out.add(LangTag.parse(s)); } return out; }
/** * Overrides {@code Object.equals()}. * * @param object The object to compare to. * * @return {@code true} if the objects have the same value, otherwise * {@code false}. */ @Override public boolean equals(Object object) { return object != null && object instanceof LangTag && this.toString().equals(object.toString()); }
/** * Extracts the language tag, if any is found, from the specified * string. * * <p>Example: * * <pre> * "name#bg-BG" => "bg-BG" * "name#" => null * "name" => null * </pre> * * @param s The string. May contain a language tag. May be * {@code null}. * * @return The extracted language tag, {@code null} if not found. * * @throws LangTagException If the language tag is invalid. */ public static LangTag extract(final String s) throws LangTagException { if (s == null) return null; final int pos = s.indexOf('#'); if (pos < 0 || s.length() < pos + 1) return null; return LangTag.parse(s.substring(pos + 1)); }
/** * Returns a string list representation of the specified language tags * collection. * * @param langTags The language tags list. May be {@code null}. * * @return The string list, or {@code null} if the original list is * {@code null}. */ public static List<String> toStringList(final Collection<LangTag> langTags) { if (langTags == null) return null; List<String> out = new ArrayList<String>(langTags.size()); for (LangTag lt: langTags) { out.add(lt.toString()); } return out; }
langTag = LangTag.parse(parts[1]);
ldapAttributeName += ";lang-" + langTag.toString();
langTag = LangTag.parse(opt.substring("lang-".length())); break;