/** * 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(); }
/** * 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(); }
/** * 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 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 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(); } }