/// -------------------------------------------------------------------------------- /// <summary> /// Initializes a new instance of the <see cref="ValidCharsWrapper"/> class. /// </summary> /// <param name="validCharacters">An instance of the valid characters class.</param> /// -------------------------------------------------------------------------------- public ValidCharsWrapper(ValidCharacters validCharacters) { m_validChars = validCharacters; }
/// ------------------------------------------------------------------------------------ /// <summary> /// Verifies the default word forming characters. /// </summary> /// <param name="validChars">The valid chars.</param> /// ------------------------------------------------------------------------------------ private static void VerifyDefaultWordFormingCharacters(ValidCharacters validChars) { string[] expectedWordFormingChars = (string[])ReflectionHelper.GetField( typeof(ValidCharacters), "s_defaultWordformingChars"); ValidCharsWrapper validCharsW = new ValidCharsWrapper(validChars); Assert.AreEqual(expectedWordFormingChars, validCharsW.WordFormingCharacters.ToArray(), "We expect the load method to have a fallback to the default word-forming characters"); Assert.AreEqual(0, validCharsW.NumericCharacters.Count); Assert.AreEqual(0, validCharsW.OtherCharacters.Count); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Loads the specified XML source to initialize a new instance of the /// <see cref="ValidCharacters"/> class. /// </summary> /// <param name="xmlSrc">The XML source representation.</param> /// <param name="wsName">The name of the writing system that is being loaded</param> /// <param name="ws">The writing system</param> /// <param name="exceptionHandler">The exception handler to use if valid character data /// cannot be loaded.</param> /// <returns></returns> /// ------------------------------------------------------------------------------------ public static ValidCharacters Load(string xmlSrc, string wsName, IWritingSystem ws, LoadExceptionDelegate exceptionHandler) { Exception e; var validChars = XmlSerializationHelper.DeserializeFromString <ValidCharacters>(xmlSrc, out e); bool fTryOldStyleList = false; if (validChars != null) { validChars.LoadException += exceptionHandler; } else { validChars = new ValidCharacters(); validChars.LoadException += exceptionHandler; if (e != null) { fTryOldStyleList = !DataAppearsToBeMalFormedXml(xmlSrc); } if (!fTryOldStyleList && !String.IsNullOrEmpty(xmlSrc)) { var bldr = new StringBuilder(); bldr.AppendFormat("Invalid ValidChars field while loading the {0} writing system:", wsName); bldr.Append(Environment.NewLine); bldr.Append("\t"); bldr.Append(xmlSrc); validChars.ReportError(new ArgumentException(bldr.ToString(), "xmlSrc", e)); } } List <string> invalidChars = validChars.Init(); if (fTryOldStyleList) { e = null; List <string> list = ParseCharString(xmlSrc, " ", validChars.m_cpe, out invalidChars); validChars.AddCharacters(list); } if (invalidChars.Count > 0) { var bldr = new StringBuilder(); bldr.AppendFormat("Invalid ValidChars field while loading the {0} writing system. The following characters are invalid:", wsName); foreach (string chr in invalidChars) { bldr.Append(Environment.NewLine); bldr.Append("\t"); bldr.AppendFormat("{0} (U+{1:X4}", chr, (int)chr[0]); for (int ich = 1; ich < chr.Length; ich++) { bldr.AppendFormat(", U+{0:X4}", (int)chr[ich]); } bldr.Append(")"); } validChars.ReportError(new ArgumentException(bldr.ToString(), "xmlSrc")); } if ((e != null || invalidChars.Count > 0) && validChars.m_WordFormingCharacters.Count == 0) { validChars.AddDefaultWordformingCharOverrides(); } return(validChars); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Verifies the sort order of characters added to the specified valid characters /// object. /// </summary> /// ------------------------------------------------------------------------------------ private void VerifySortOrder(ValidCharacters validChars) { ValidCharsWrapper validCharsW = new ValidCharsWrapper(validChars); Assert.AreEqual("b", validCharsW.WordFormingCharacters[0]); Assert.AreEqual("c", validCharsW.WordFormingCharacters[1]); Assert.AreEqual("t", validCharsW.WordFormingCharacters[2]); Assert.AreEqual("z", validCharsW.WordFormingCharacters[3]); validChars.AddCharacter("8"); validChars.AddCharacter("7"); validChars.AddCharacter("6"); validChars.AddCharacter("5"); Assert.AreEqual("5", validCharsW.NumericCharacters[0]); Assert.AreEqual("6", validCharsW.NumericCharacters[1]); Assert.AreEqual("7", validCharsW.NumericCharacters[2]); Assert.AreEqual("8", validCharsW.NumericCharacters[3]); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Loads the specified XML source to initialize a new instance of the /// <see cref="ValidCharacters"/> class. /// </summary> /// <param name="xmlSrc">The XML source representation.</param> /// <param name="wsName">The name of the writing system that is being loaded</param> /// <param name="ws">The writing system</param> /// <param name="exceptionHandler">The exception handler to use if valid character data /// cannot be loaded.</param> /// <param name="legacyOverridesFile"></param> /// <returns></returns> /// ------------------------------------------------------------------------------------ public static ValidCharacters Load(string xmlSrc, string wsName, IWritingSystem ws, LoadExceptionDelegate exceptionHandler, string legacyOverridesFile) { Exception e; var validChars = XmlSerializationHelper.DeserializeFromString<ValidCharacters>(xmlSrc, out e); bool fTryOldStyleList = false; if (validChars != null) { validChars.LoadException += exceptionHandler; } else { validChars = new ValidCharacters(); validChars.LoadException += exceptionHandler; if (e != null) fTryOldStyleList = !DataAppearsToBeMalFormedXml(xmlSrc); if (!fTryOldStyleList && !String.IsNullOrEmpty(xmlSrc)) { var bldr = new StringBuilder(); bldr.AppendFormat("Invalid ValidChars field while loading the {0} writing system:", wsName); bldr.Append(Environment.NewLine); bldr.Append("\t"); bldr.Append(xmlSrc); validChars.ReportError(new ArgumentException(bldr.ToString(), "xmlSrc", e)); } } validChars.m_legacyOverridesFile = legacyOverridesFile; List<string> invalidChars = validChars.Init(); if (fTryOldStyleList) { e = null; List<string> list = ParseCharString(xmlSrc, " ", validChars.m_cpe, out invalidChars); validChars.AddCharacters(list); } if (invalidChars.Count > 0) { var bldr = new StringBuilder(); bldr.AppendFormat("Invalid ValidChars field while loading the {0} writing system. The following characters are invalid:", wsName); foreach (string chr in invalidChars) { bldr.Append(Environment.NewLine); bldr.Append("\t"); bldr.AppendFormat("{0} (U+{1:X4}", chr, (int)chr[0]); for (int ich = 1; ich < chr.Length; ich++) bldr.AppendFormat(", U+{0:X4}", (int)chr[ich]); bldr.Append(")"); } validChars.ReportError(new ArgumentException(bldr.ToString(), "xmlSrc")); } if ((e != null || invalidChars.Count > 0) && validChars.m_WordFormingCharacters.Count == 0) validChars.AddDefaultWordformingCharOverrides(); return validChars; }