[Test]//@Test(expected = IllegalArgumentException.class) public void TestInvalidLanguageIllegalArgumentException() { Assert.Throws <ArgumentException>(() => Languages.GetInstance("thereIsNoSuchLanguage")); }
[Test]//@Test(expected = IllegalStateException.class) public void TestInvalidLangIllegalStateException() { Assert.Throws <InvalidOperationException>(() => Lang.LoadFromResource("thisIsAMadeUpResourceName", Languages.GetInstance(NameType.GENERIC))); }
/// <summary> /// Loads language rules from a resource. /// <para/> /// In normal use, you will obtain instances of Lang through the <see cref="GetInstance(NameType)"/> method. /// You will only need to call this yourself if you are developing custom language mapping rules. /// </summary> /// <param name="languageRulesResourceName">The fully-qualified or partially-qualified resource name to load.</param> /// <param name="languages">The languages that these rules will support.</param> /// <returns>A Lang encapsulating the loaded language-guessing rules.</returns> public static Lang LoadFromResource(string languageRulesResourceName, Languages languages) { IList <LangRule> rules = new List <LangRule>(); Stream lRulesIS = typeof(Lang).GetTypeInfo().Assembly.FindAndGetManifestResourceStream(typeof(Lang), languageRulesResourceName); if (lRulesIS == null) { throw new InvalidOperationException("Unable to resolve required resource:" + LANGUAGE_RULES_RN); } using (TextReader reader = new StreamReader(lRulesIS, ResourceConstants.ENCODING)) { bool inExtendedComment = false; string rawLine; while ((rawLine = reader.ReadLine()) != null) { string line = rawLine; if (inExtendedComment) { // check for closing comment marker, otherwise discard doc comment line if (line.EndsWith(ResourceConstants.EXT_CMT_END, StringComparison.Ordinal)) { inExtendedComment = false; } } else { if (line.StartsWith(ResourceConstants.EXT_CMT_START, StringComparison.Ordinal)) { inExtendedComment = true; } else { // discard comments int cmtI = line.IndexOf(ResourceConstants.CMT, StringComparison.Ordinal); if (cmtI >= 0) { line = line.Substring(0, cmtI - 0); } // trim leading-trailing whitespace line = line.Trim(); if (line.Length == 0) { continue; // empty lines can be safely skipped } // split it up string[] parts = WHITESPACE.Split(line).TrimEnd(); if (parts.Length != 3) { throw new ArgumentException("Malformed line '" + rawLine + "' in language resource '" + languageRulesResourceName + "'"); } Regex pattern = new Regex(parts[0], RegexOptions.Compiled); string[] langs = TOKEN.Split(parts[1]).TrimEnd(); bool accept = parts[2].Equals("true", StringComparison.Ordinal); rules.Add(new LangRule(pattern, new JCG.HashSet <string>(langs), accept)); } } } } return(new Lang(rules, languages)); }
private Lang(IList <LangRule> rules, Languages languages) { this.rules = rules.AsReadOnly(); this.languages = languages; }
private Lang(IList <LangRule> rules, Languages languages) { this.rules = Collections.UnmodifiableList(rules); this.languages = languages; }