PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/NCldrBuilder/NCldrBuilder.GetCultures.cs

https://github.com/GuySmithFerrier/NCLDR
C# | 1359 lines | 1116 code | 231 blank | 12 comment | 325 complexity | 39f7eb38bd08b57937cc9b4278c08988 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Xml.Linq;
  9. using NCldr.Types;
  10. namespace NCldr.Builder
  11. {
  12. public partial class NCldrBuilder
  13. {
  14. private static CultureData[] GetCultures()
  15. {
  16. if (options != null && !options.IncludeCultures)
  17. {
  18. return null;
  19. }
  20. List<CultureData> cultures = new List<CultureData>();
  21. string[] cldrCultureNames = GetFilenames(@"common\main");
  22. foreach (string cldrCultureName in cldrCultureNames)
  23. {
  24. if (IncludeCulture(cldrCultureName))
  25. {
  26. Progress("Adding culture", cldrCultureName);
  27. CultureData cultureData = GetCulture(cldrCultureName);
  28. cultures.Add(cultureData);
  29. Progress("Added culture", cldrCultureName, ProgressEventType.Added, cultureData);
  30. }
  31. }
  32. return cultures.ToArray();
  33. }
  34. private static bool IncludeCulture(string cldrCultureName)
  35. {
  36. if (options == null || options.CultureOptions == null ||
  37. options.CultureOptions.CultureSelection == CultureSelection.All)
  38. {
  39. return true;
  40. }
  41. else if (options.CultureOptions.CultureSelection == CultureSelection.IncludeOnly)
  42. {
  43. string cultureName = GetDotNetCultureName(cldrCultureName);
  44. return (from c in options.CultureOptions.IncludeCultures
  45. where c == cultureName
  46. select c).Any();
  47. }
  48. else if (options.CultureOptions.CultureSelection == CultureSelection.AllExceptExclude)
  49. {
  50. string cultureName = GetDotNetCultureName(cldrCultureName);
  51. return !(from c in options.CultureOptions.ExcludeCultures
  52. where c == cultureName
  53. select c).Any();
  54. }
  55. else if (options.CultureOptions.CultureSelection == CultureSelection.AllNew)
  56. {
  57. string cultureName = GetDotNetCultureName(cldrCultureName);
  58. return !IsDotNetCulture(cultureName);
  59. }
  60. return false;
  61. }
  62. private static bool IsDotNetCulture(string cultureName)
  63. {
  64. try
  65. {
  66. new System.Globalization.CultureInfo(cultureName);
  67. return true;
  68. }
  69. catch
  70. {
  71. return false;
  72. }
  73. }
  74. private static CultureData GetCulture(string cultureName)
  75. {
  76. XDocument document = GetXmlDocument(@"common\main\" + cultureName + ".xml");
  77. CultureData culture = new CultureData();
  78. culture.Identity = GetIdentity(document);
  79. culture.LanguageDisplayNames = GetDisplayNames<LanguageDisplayName>(document, "languages", "language");
  80. culture.RegionDisplayNames = GetDisplayNames<RegionDisplayName>(document, "territories", "territory");
  81. culture.ScriptDisplayNames = GetDisplayNames<ScriptDisplayName>(document, "scripts", "script");
  82. culture.Casing = GetCasing(cultureName);
  83. culture.Characters = GetCharacters(document);
  84. culture.Dates = GetDates(document);
  85. culture.Delimiters = GetDelimiters(document);
  86. culture.Layout = GetLayout(document);
  87. culture.ListPatterns = GetListPatterns(document);
  88. culture.Messages = GetMessages(document);
  89. culture.Numbers = GetNumbers(document, culture.Identity.Region);
  90. culture.RuleBasedNumberFormatting = GetRuleBasedNumberFormatting(cultureName);
  91. culture.UnitPatternSets = GetUnitPatternSets(document);
  92. return culture;
  93. }
  94. private static RuleBasedNumberFormatting GetRuleBasedNumberFormatting(string cultureName)
  95. {
  96. if (options != null && options.CultureOptions != null && !options.CultureOptions.IncludeRuleBasedNumberFormatting)
  97. {
  98. return null;
  99. }
  100. string filename = String.Format(@"common\rbnf\{0}.xml", cultureName);
  101. string path = Path.Combine(cldrPath, filename);
  102. if (!File.Exists(path))
  103. {
  104. return null;
  105. }
  106. XDocument rbnfDocument = GetXmlDocument(filename);
  107. List<XElement> ruleSetGroupingElements =
  108. (from i in rbnfDocument.Elements("ldml").Elements("rbnf").Elements("rulesetGrouping")
  109. select i).ToList();
  110. if (ruleSetGroupingElements == null || ruleSetGroupingElements.Count == 0)
  111. {
  112. return null;
  113. }
  114. RuleBasedNumberFormatting ruleBasedNumberFormatting = new RuleBasedNumberFormatting();
  115. ruleBasedNumberFormatting.SpelloutRuleSets = GetRuleBasedNumberFormattingRuleSet(ruleSetGroupingElements, "SpelloutRules");
  116. ruleBasedNumberFormatting.OrdinalRuleSets = GetRuleBasedNumberFormattingRuleSet(ruleSetGroupingElements, "OrdinalRules");
  117. return ruleBasedNumberFormatting;
  118. }
  119. private static RuleBasedNumberFormattingRuleSet[] GetRuleBasedNumberFormattingRuleSet(
  120. List<XElement> ruleSetGroupingElements, string ruleSetId)
  121. {
  122. XElement ruleSetGroupingElement = (from rs in ruleSetGroupingElements
  123. where rs.Attribute("type").Value.ToString() == ruleSetId
  124. select rs).FirstOrDefault();
  125. if (ruleSetGroupingElement == null)
  126. {
  127. return null;
  128. }
  129. List<RuleBasedNumberFormattingRuleSet> ruleSets = new List<RuleBasedNumberFormattingRuleSet>();
  130. List<XElement> ruleSetElements = (from rs in ruleSetGroupingElement.Elements("ruleset")
  131. select rs).ToList();
  132. foreach (XElement ruleSetElement in ruleSetElements)
  133. {
  134. RuleBasedNumberFormattingRuleSet ruleSet = new RuleBasedNumberFormattingRuleSet();
  135. ruleSet.Id = ruleSetElement.Attribute("type").Value.ToString();
  136. if (ruleSetElement.Attribute("access") != null)
  137. {
  138. ruleSet.Access = ruleSetElement.Attribute("access").Value.ToString();
  139. }
  140. List<RuleBasedNumberFormattingRule> rules = new List<RuleBasedNumberFormattingRule>();
  141. List<XElement> ruleElements = (from r in ruleSetElement.Elements("rbnfrule")
  142. select r).ToList();
  143. foreach (XElement ruleElement in ruleElements)
  144. {
  145. RuleBasedNumberFormattingRule rule = new RuleBasedNumberFormattingRule();
  146. rule.Value = ruleElement.Attribute("value").Value.ToString();
  147. rule.Rule = ruleElement.Value.ToString();
  148. rules.Add(rule);
  149. }
  150. ruleSet.RuleBasedNumberFormattingRules = rules.ToArray();
  151. ruleSets.Add(ruleSet);
  152. }
  153. return ruleSets.ToArray();
  154. }
  155. private static Casing GetCasing(string cultureName)
  156. {
  157. if (options != null && options.CultureOptions != null && !options.CultureOptions.IncludeCasing)
  158. {
  159. return null;
  160. }
  161. string filename = String.Format(@"common\casing\{0}.xml", cultureName);
  162. string path = Path.Combine(cldrPath, filename);
  163. if (!File.Exists(path))
  164. {
  165. return null;
  166. }
  167. XDocument casingDocument = GetXmlDocument(filename);
  168. List<XElement> casingElements = (from i in casingDocument.Elements("ldml").Elements("metadata").Elements("casingData")
  169. .Elements("casingItem")
  170. select i).ToList();
  171. if (casingElements == null || casingElements.Count == 0)
  172. {
  173. return null;
  174. }
  175. Casing casing = new Casing();
  176. casing.CalendarField = GetCasingType(casingElements, "calendar-field");
  177. casing.DayFormatExceptNarrow = GetCasingType(casingElements, "day-format-except-narrow");
  178. casing.DayNarrow = GetCasingType(casingElements, "day-narrow");
  179. casing.DayStandAloneExceptNarrow = GetCasingType(casingElements, "day-standalone-except-narrow");
  180. casing.DisplayName = GetCasingType(casingElements, "displayName");
  181. casing.DisplayNameCount = GetCasingType(casingElements, "displayName-count");
  182. casing.EraAbbr = GetCasingType(casingElements, "era-abbr");
  183. casing.EraName = GetCasingType(casingElements, "era-name");
  184. casing.EraNarrow = GetCasingType(casingElements, "era-narrow");
  185. casing.Key = GetCasingType(casingElements, "key");
  186. casing.Language = GetCasingType(casingElements, "language");
  187. casing.MetaZoneLong = GetCasingType(casingElements, "metazone-long");
  188. casing.MetaZoneShort = GetCasingType(casingElements, "metazone-short");
  189. casing.MonthFormatExceptNarrow = GetCasingType(casingElements, "month-format-except-narrow");
  190. casing.MonthNarrow = GetCasingType(casingElements, "month-narrow");
  191. casing.MonthStandAloneExceptNarrow = GetCasingType(casingElements, "day-standalone-except-narrow");
  192. casing.QuarterAbbreviated = GetCasingType(casingElements, "quarter-abbreviated");
  193. casing.Script = GetCasingType(casingElements, "script");
  194. casing.Symbol = GetCasingType(casingElements, "symbol");
  195. casing.Region = GetCasingType(casingElements, "territory");
  196. casing.Tense = GetCasingType(casingElements, "tense");
  197. casing.Type = GetCasingType(casingElements, "type");
  198. casing.ZoneExemplarCity = GetCasingType(casingElements, "zone-exemplarCity");
  199. return casing;
  200. }
  201. private static CasingType GetCasingType(List<XElement> casingElements, string casingElementName)
  202. {
  203. XElement element = (from c in casingElements
  204. where c.Attribute("type").Value.ToString() == casingElementName
  205. select c).FirstOrDefault();
  206. if (element == null)
  207. {
  208. return CasingType.None;
  209. }
  210. string casingType = element.Value;
  211. return (CasingType)Enum.Parse(typeof(CasingType), casingType, true);
  212. }
  213. private static Numbers GetNumbers(XDocument document, Region region)
  214. {
  215. if (options != null && options.CultureOptions != null && !options.CultureOptions.IncludeNumbers)
  216. {
  217. return null;
  218. }
  219. Numbers numbers = null;
  220. IEnumerable<XElement> numbersElements = document.Elements("ldml").Elements("numbers");
  221. if (numbersElements != null)
  222. {
  223. numbers = new Numbers();
  224. XElement defaultNumberingSystemElement = (from item in numbersElements.Elements("defaultNumberingSystem")
  225. select item).FirstOrDefault();
  226. if (defaultNumberingSystemElement != null)
  227. {
  228. numbers.DefaultNumberingSystemId = defaultNumberingSystemElement.Value.ToString();
  229. }
  230. List<XElement> otherNumberingSystemElements = (
  231. from item in numbersElements.Elements("otherNumberingSystems").Elements()
  232. select item).ToList();
  233. if (otherNumberingSystemElements != null && otherNumberingSystemElements.Count > 0)
  234. {
  235. List<NumberingSystem> numberingSystems = new List<NumberingSystem>();
  236. numbers.OtherNumberingSystems = new List<OtherNumberingSystem>();
  237. foreach (XElement otherNumberingSystemElement in otherNumberingSystemElements)
  238. {
  239. numbers.OtherNumberingSystems.Add(new OtherNumberingSystem()
  240. {
  241. Id = otherNumberingSystemElement.Name.ToString(),
  242. Value = otherNumberingSystemElement.Value.ToString()
  243. });
  244. numberingSystems.Add(new NumberingSystem() { Id = otherNumberingSystemElement.Value.ToString() });
  245. }
  246. // look to see if there is any data defined for each of the 'other' numbering systems
  247. foreach (NumberingSystem numberingSystem in numberingSystems)
  248. {
  249. numberingSystem.Symbols = GetNumberingSystemSymbols(numbersElements, numberingSystem.Id);
  250. numberingSystem.DecimalFormatPatternSets = GetDecimalFormatPatternSets(numbersElements, numberingSystem.Id);
  251. numberingSystem.CurrencySpacings = GetCurrencySpacings(numbersElements, numberingSystem.Id);
  252. numberingSystem.CurrencyFormatPattern = GetPattern(
  253. numbersElements, numberingSystem.Id, "currencyFormats", "currencyFormatLength", "currencyFormat");
  254. numberingSystem.DecimalFormatPattern = GetPattern(
  255. numbersElements, numberingSystem.Id, "decimalFormats", "decimalFormatLength", "decimalFormat");
  256. numberingSystem.PercentFormatPattern = GetPattern(
  257. numbersElements, numberingSystem.Id, "percentFormats", "percentFormatLength", "percentFormat");
  258. numberingSystem.ScientificFormatPattern = GetPattern(
  259. numbersElements, numberingSystem.Id, "scientificFormats", "scientificFormatLength", "scientificFormat");
  260. }
  261. // only save the numbering systems for which there was actually any data found
  262. numbers.NumberingSystems = (from ns in numberingSystems
  263. where ns.Symbols != null || ns.DecimalFormatPatternSets != null
  264. || ns.CurrencySpacings != null
  265. || ns.CurrencyFormatPattern != null || ns.DecimalFormatPattern != null
  266. || ns.PercentFormatPattern != null || ns.ScientificFormatPattern != null
  267. select ns).ToArray();
  268. }
  269. numbers.CurrencyDisplayNameSets = GetCurrencyDisplayNameSets(numbersElements);
  270. numbers.MiscellaneousPatternSets = GetMiscellaneousPatternSets(numbersElements);
  271. if (region != null)
  272. {
  273. numbers.CurrencyPeriods = GetCurrencyPeriods(region.Id);
  274. }
  275. }
  276. return numbers;
  277. }
  278. private static CurrencySpacings GetCurrencySpacings(IEnumerable<XElement> numbersElements, string numberingSystemId)
  279. {
  280. XElement formatsElement = (from item in numbersElements.Elements("currencyFormats")
  281. where item.Attribute("numberSystem") != null &&
  282. item.Attribute("numberSystem").Value.ToString() == numberingSystemId
  283. select item).FirstOrDefault();
  284. if (formatsElement != null)
  285. {
  286. XElement currencySpacingElement =
  287. (from item in formatsElement.Elements("currencySpacing")
  288. select item).FirstOrDefault();
  289. if (currencySpacingElement != null)
  290. {
  291. CurrencySpacings currencySpacings = new CurrencySpacings();
  292. XElement beforeCurrencyElement = (from p in currencySpacingElement
  293. .Elements("beforeCurrency")
  294. select p).FirstOrDefault();
  295. if (beforeCurrencyElement != null)
  296. {
  297. CurrencySpacing beforeCurrencySpacing = new CurrencySpacing();
  298. beforeCurrencySpacing.CurrencyMatch = beforeCurrencyElement.Element("currencyMatch").Value.ToString();
  299. beforeCurrencySpacing.SurroundingMatch = beforeCurrencyElement.Element("surroundingMatch").Value.ToString();
  300. beforeCurrencySpacing.InsertBetween = beforeCurrencyElement.Element("insertBetween").Value.ToString();
  301. currencySpacings.BeforeCurrency = beforeCurrencySpacing;
  302. }
  303. XElement afterCurrencyElement = (from p in currencySpacingElement
  304. .Elements("afterCurrency")
  305. select p).FirstOrDefault();
  306. if (afterCurrencyElement != null)
  307. {
  308. CurrencySpacing afterCurrencySpacing = new CurrencySpacing();
  309. afterCurrencySpacing.CurrencyMatch = afterCurrencyElement.Element("currencyMatch").Value.ToString();
  310. afterCurrencySpacing.SurroundingMatch = afterCurrencyElement.Element("surroundingMatch").Value.ToString();
  311. afterCurrencySpacing.InsertBetween = afterCurrencyElement.Element("insertBetween").Value.ToString();
  312. currencySpacings.AfterCurrency = afterCurrencySpacing;
  313. }
  314. return currencySpacings;
  315. }
  316. }
  317. return null;
  318. }
  319. private static CurrencyPeriod[] GetCurrencyPeriods(string regionId)
  320. {
  321. XElement regionElement = (from cp in supplementalDataDocument.Elements("supplementalData").Elements("currencyData")
  322. .Elements("region")
  323. where cp.Attribute("iso3166").Value.ToString() == regionId
  324. select cp).FirstOrDefault();
  325. if (regionElement != null)
  326. {
  327. List<CurrencyPeriod> currencyPeriods = new List<CurrencyPeriod>();
  328. foreach (XElement currencyPeriodElement in regionElement.Elements("currency"))
  329. {
  330. CurrencyPeriod currencyPeriod = new CurrencyPeriod();
  331. currencyPeriod.Id = currencyPeriodElement.Attribute("iso4217").Value.ToString();
  332. if (currencyPeriodElement.Attribute("from") != null)
  333. {
  334. currencyPeriod.From = ParseCldrDate(currencyPeriodElement.Attribute("from").Value.ToString());
  335. }
  336. if (currencyPeriodElement.Attribute("to") != null)
  337. {
  338. currencyPeriod.To = ParseCldrDate(currencyPeriodElement.Attribute("to").Value.ToString());
  339. }
  340. if (currencyPeriodElement.Attribute("tender") != null)
  341. {
  342. string tender = currencyPeriodElement.Attribute("tender").Value.ToString();
  343. currencyPeriod.IsTender = (tender == "true");
  344. }
  345. else
  346. {
  347. currencyPeriod.IsTender = true;
  348. }
  349. currencyPeriods.Add(currencyPeriod);
  350. }
  351. return currencyPeriods.ToArray();
  352. }
  353. return null;
  354. }
  355. private static CurrencyDisplayNameSet[] GetCurrencyDisplayNameSets(IEnumerable<XElement> numbersElements)
  356. {
  357. List<XElement> currencySetElements = (from item in numbersElements.Elements("currencies").Elements("currency")
  358. select item).ToList();
  359. if (currencySetElements != null && currencySetElements.Count > 0)
  360. {
  361. List<CurrencyDisplayNameSet> currencyDisplayNameSets = new List<CurrencyDisplayNameSet>();
  362. foreach (XElement currencySetElement in currencySetElements)
  363. {
  364. CurrencyDisplayNameSet currencyDisplayNameSet = new CurrencyDisplayNameSet();
  365. currencyDisplayNameSet.Id = currencySetElement.Attribute("type").Value.ToString();
  366. List<CurrencyDisplayName> currencyDisplayNames = new List<CurrencyDisplayName>();
  367. foreach (XElement currencyDisplayNameElement in currencySetElement.Elements("displayName"))
  368. {
  369. CurrencyDisplayName currencyDisplayName = new CurrencyDisplayName();
  370. if (currencyDisplayNameElement.Attribute("count") != null)
  371. {
  372. string id = currencyDisplayNameElement.Attribute("count").Value.ToString();
  373. id = id[0].ToString().ToUpper() + id.Substring(1);
  374. currencyDisplayName.Id = id;
  375. }
  376. currencyDisplayName.Name = currencyDisplayNameElement.Value.ToString();
  377. currencyDisplayNames.Add(currencyDisplayName);
  378. }
  379. currencyDisplayNameSet.CurrencyDisplayNames = currencyDisplayNames.ToArray();
  380. XElement currencySymbolElement = currencySetElement.Elements("symbol").FirstOrDefault();
  381. if (currencySymbolElement != null)
  382. {
  383. currencyDisplayNameSet.Symbol = currencySymbolElement.Value.ToString();
  384. }
  385. currencyDisplayNameSets.Add(currencyDisplayNameSet);
  386. }
  387. return currencyDisplayNameSets.ToArray();
  388. }
  389. return null;
  390. }
  391. private static MiscellaneousPatternSet[] GetMiscellaneousPatternSets(IEnumerable<XElement> numbersElements)
  392. {
  393. List<XElement> miscellanousPatternSetElements = (from item in numbersElements.Elements("miscPatterns")
  394. select item).ToList();
  395. if (miscellanousPatternSetElements != null && miscellanousPatternSetElements.Count > 0)
  396. {
  397. List<MiscellaneousPatternSet> miscellaneousPatternSets = new List<MiscellaneousPatternSet>();
  398. foreach (XElement miscellanousPatternSetElement in miscellanousPatternSetElements)
  399. {
  400. MiscellaneousPatternSet miscellaneousPatternSet = new MiscellaneousPatternSet();
  401. miscellaneousPatternSet.Id = miscellanousPatternSetElement.Attribute("numberSystem").Value.ToString();
  402. List<MiscellaneousPattern> miscellaneousPatterns = new List<MiscellaneousPattern>();
  403. foreach (XElement miscellanousPatternElement in miscellanousPatternSetElement.Elements("pattern"))
  404. {
  405. MiscellaneousPattern miscellaneousPattern = new MiscellaneousPattern();
  406. if (miscellanousPatternElement.Attribute("type") != null)
  407. {
  408. string id = miscellanousPatternElement.Attribute("type").Value.ToString();
  409. miscellaneousPattern.Id = id;
  410. }
  411. miscellaneousPattern.Pattern = miscellanousPatternElement.Value.ToString();
  412. miscellaneousPatterns.Add(miscellaneousPattern);
  413. }
  414. miscellaneousPatternSet.MiscellaneousPatterns = miscellaneousPatterns.ToArray();
  415. miscellaneousPatternSets.Add(miscellaneousPatternSet);
  416. }
  417. return miscellaneousPatternSets.ToArray();
  418. }
  419. return null;
  420. }
  421. private static string GetPattern(
  422. IEnumerable<XElement> numbersElements,
  423. string numberingSystemId,
  424. string formatsName,
  425. string formatLengthName,
  426. string formatName)
  427. {
  428. XElement formatsElement = (from item in numbersElements.Elements(formatsName)
  429. where item.Attribute("numberSystem") != null &&
  430. item.Attribute("numberSystem").Value.ToString() == numberingSystemId
  431. select item).FirstOrDefault();
  432. if (formatsElement != null)
  433. {
  434. XElement formatLengthElement =
  435. (from item in formatsElement.Elements(formatLengthName)
  436. where item.Attribute("type") == null
  437. select item).FirstOrDefault();
  438. if (formatLengthElement != null)
  439. {
  440. XElement patternElement = (from p in formatLengthElement
  441. .Elements(formatName).Elements("pattern")
  442. select p).FirstOrDefault();
  443. if (patternElement != null)
  444. {
  445. return patternElement.Value.ToString();
  446. }
  447. }
  448. }
  449. return null;
  450. }
  451. private static DecimalFormatPatternSet[] GetDecimalFormatPatternSets(IEnumerable<XElement> numbersElements, string numberingSystemId)
  452. {
  453. XElement decimalFormatsElement = (from item in numbersElements.Elements("decimalFormats")
  454. where item.Attribute("numberSystem") != null &&
  455. item.Attribute("numberSystem").Value.ToString() == numberingSystemId
  456. select item).FirstOrDefault();
  457. if (decimalFormatsElement != null)
  458. {
  459. List<XElement> decimalFormatLengthElements =
  460. (from item in decimalFormatsElement.Elements("decimalFormatLength")
  461. where item.Attribute("type") != null
  462. select item).ToList();
  463. if (decimalFormatLengthElements.Count > 0)
  464. {
  465. List<DecimalFormatPatternSet> decimalFormatPatternSets = new List<DecimalFormatPatternSet>();
  466. foreach (XElement decimalFormatLengthElement in decimalFormatLengthElements)
  467. {
  468. DecimalFormatPatternSet decimalFormatPatternSet = new DecimalFormatPatternSet();
  469. decimalFormatPatternSet.Id = decimalFormatLengthElement.Attribute("type").Value.ToString();
  470. List<XElement> patternElements = (from p in decimalFormatLengthElement
  471. .Elements("decimalFormat").Elements("pattern")
  472. select p).ToList();
  473. List<DecimalFormatPattern> patterns = new List<DecimalFormatPattern>();
  474. foreach (XElement patternElement in patternElements)
  475. {
  476. DecimalFormatPattern pattern = new DecimalFormatPattern();
  477. pattern.Id = patternElement.Attribute("type").Value.ToString();
  478. pattern.Pattern = patternElement.Value.ToString();
  479. pattern.Count = GetPatternCount(patternElement.Attribute("count").Value.ToString());
  480. patterns.Add(pattern);
  481. }
  482. decimalFormatPatternSet.Patterns = patterns.ToArray();
  483. decimalFormatPatternSets.Add(decimalFormatPatternSet);
  484. }
  485. return decimalFormatPatternSets.ToArray();
  486. }
  487. }
  488. return null;
  489. }
  490. private static PatternCount GetPatternCount(string patternCount)
  491. {
  492. return (PatternCount)Enum.Parse(typeof(PatternCount), patternCount, true);
  493. }
  494. private static NumberingSystemSymbols GetNumberingSystemSymbols(IEnumerable<XElement> numbersElements, string numberingSystemId)
  495. {
  496. NumberingSystemSymbols symbols = null;
  497. XElement symbolElement = (from item in numbersElements.Elements("symbols")
  498. where item.Attribute("numberSystem") != null &&
  499. item.Attribute("numberSystem").Value.ToString() == numberingSystemId
  500. select item).FirstOrDefault();
  501. if (symbolElement != null)
  502. {
  503. symbols = new NumberingSystemSymbols();
  504. if (symbolElement.Element("decimal") != null)
  505. {
  506. symbols.Decimal = symbolElement.Element("decimal").Value.ToString();
  507. }
  508. if (symbolElement.Element("group") != null)
  509. {
  510. symbols.Group = symbolElement.Element("group").Value.ToString();
  511. }
  512. if (symbolElement.Element("list") != null)
  513. {
  514. symbols.List = symbolElement.Element("list").Value.ToString();
  515. }
  516. if (symbolElement.Element("percentSign") != null)
  517. {
  518. symbols.PercentSign = symbolElement.Element("percentSign").Value.ToString();
  519. }
  520. if (symbolElement.Element("plusSign") != null)
  521. {
  522. symbols.PlusSign = symbolElement.Element("plusSign").Value.ToString();
  523. }
  524. if (symbolElement.Element("minusSign") != null)
  525. {
  526. symbols.MinusSign = symbolElement.Element("minusSign").Value.ToString();
  527. }
  528. if (symbolElement.Element("exponential") != null)
  529. {
  530. symbols.Exponential = symbolElement.Element("exponential").Value.ToString();
  531. }
  532. if (symbolElement.Element("superscriptingExponent") != null)
  533. {
  534. symbols.SuperScriptingExponent = symbolElement.Element("superscriptingExponent").Value.ToString();
  535. }
  536. if (symbolElement.Element("perMille") != null)
  537. {
  538. symbols.PerMille = symbolElement.Element("perMille").Value.ToString();
  539. }
  540. if (symbolElement.Element("infinity") != null)
  541. {
  542. symbols.Infinity = symbolElement.Element("infinity").Value.ToString();
  543. }
  544. if (symbolElement.Element("nan") != null)
  545. {
  546. symbols.Nan = symbolElement.Element("nan").Value.ToString();
  547. }
  548. }
  549. return symbols;
  550. }
  551. private static MessageSet GetMessages(XDocument document)
  552. {
  553. if (options != null && options.CultureOptions != null && !options.CultureOptions.IncludeMessages)
  554. {
  555. return null;
  556. }
  557. MessageSet messages = null;
  558. IEnumerable<XElement> ldmlElements = document.Elements("ldml");
  559. List<XElement> messageDatas = (from item in ldmlElements.Elements("posix")
  560. .Elements("messages").Elements()
  561. select item).ToList();
  562. if (messageDatas != null && messageDatas.Count > 0)
  563. {
  564. List<Message> messageList = new List<Message>();
  565. foreach(XElement messageData in messageDatas)
  566. {
  567. messageList.Add(new Message()
  568. {
  569. Id = messageData.Name.ToString(),
  570. Text = messageData.Value.ToString()
  571. });
  572. }
  573. messages = new MessageSet();
  574. messages.Messages = messageList.ToArray();
  575. }
  576. return messages;
  577. }
  578. private static Characters GetCharacters(XDocument document)
  579. {
  580. if (options != null && options.CultureOptions != null && !options.CultureOptions.IncludeCharacters)
  581. {
  582. return null;
  583. }
  584. Characters characters = null;
  585. IEnumerable<XElement> ldmlElements = document.Elements("ldml");
  586. List<XElement> characterDatas = (from item in ldmlElements.Elements("characters")
  587. select item).ToList();
  588. if (characterDatas != null && characterDatas.Count > 0)
  589. {
  590. characters = new Characters();
  591. characters.ExemplarCharacters = GetCharacterArray(characterDatas, "exemplarCharacters", null);
  592. characters.AuxiliaryExemplarCharacters = GetCharacterArray(characterDatas, "exemplarCharacters", "auxiliary");
  593. characters.PunctuationExemplarCharacters = GetCharacterArray(characterDatas, "exemplarCharacters", "punctuation");
  594. characters.IndexExemplarCharacters = GetCharacterArray(characterDatas, "exemplarCharacters", "index");
  595. characters.FinalEllipsis = GetString(characterDatas, "ellipsis", "final");
  596. characters.InitialEllipsis = GetString(characterDatas, "ellipsis", "initial");
  597. characters.MedialEllipsis = GetString(characterDatas, "ellipsis", "medial");
  598. characters.MoreInformation = GetString(characterDatas, "moreInformation", null);
  599. }
  600. return characters;
  601. }
  602. private static string[] GetCharacterArray(List<XElement> characterDatas, string elementName, string typeName)
  603. {
  604. string stringValue = GetString(characterDatas, elementName, typeName);
  605. if (String.IsNullOrEmpty(stringValue))
  606. {
  607. return null;
  608. }
  609. // strip off the leading and trailing square brackets
  610. stringValue = stringValue.Substring(1, stringValue.Length - 2);
  611. return stringValue.Split(' ');
  612. }
  613. private static string GetString(List<XElement> characterDatas, string elementName, string typeName)
  614. {
  615. XElement element;
  616. if (typeName == null)
  617. {
  618. element = (from cd in characterDatas.Elements(elementName)
  619. where cd.Attribute("type") == null
  620. select cd).FirstOrDefault();
  621. }
  622. else
  623. {
  624. element = (from cd in characterDatas.Elements(elementName)
  625. where cd.Attribute("type") != null && cd.Attribute("type").Value.ToString() == typeName
  626. select cd).FirstOrDefault();
  627. }
  628. if (element == null)
  629. {
  630. return null;
  631. }
  632. return element.Value.ToString();
  633. }
  634. private static Delimiters GetDelimiters(XDocument document)
  635. {
  636. if (options != null && options.CultureOptions != null && !options.CultureOptions.IncludeDelimiters)
  637. {
  638. return null;
  639. }
  640. Delimiters delimiters = null;
  641. IEnumerable<XElement> ldmlElements = document.Elements("ldml");
  642. List<XElement> delimiterDatas = (from item in ldmlElements.Elements("delimiters")
  643. select item).ToList();
  644. if (delimiterDatas != null && delimiterDatas.Count > 0)
  645. {
  646. delimiters = new Delimiters();
  647. delimiters.QuotationStart = GetString(delimiterDatas, "quotationStart", null);
  648. delimiters.QuotationEnd = GetString(delimiterDatas, "quotationEnd", null);
  649. delimiters.AlternateQuotationStart = GetString(delimiterDatas, "alternateQuotationStart", null);
  650. delimiters.AlternateQuotationEnd = GetString(delimiterDatas, "alternateQuotationEnd", null);
  651. }
  652. return delimiters;
  653. }
  654. private static UnitPatternSet[] GetUnitPatternSets(XDocument document)
  655. {
  656. if (options != null && options.CultureOptions != null && !options.CultureOptions.IncludeUnitPatternSets)
  657. {
  658. return null;
  659. }
  660. IEnumerable<XElement> ldmlElements = document.Elements("ldml");
  661. List<XElement> unitDatas = (from item in ldmlElements.Elements("units").Elements("unit")
  662. select item).ToList();
  663. if (unitDatas != null && unitDatas.Count > 0)
  664. {
  665. List<UnitPatternSet> unitPatternSets = new List<UnitPatternSet>();
  666. foreach (XElement unitData in unitDatas)
  667. {
  668. UnitPatternSet unitPatternSet = new UnitPatternSet();
  669. unitPatternSet.Id = unitData.Attribute("type").Value.ToString();
  670. List<UnitPattern> unitPatterns = new List<UnitPattern>();
  671. foreach (XElement unitPatternData in unitData.Elements("unitPattern"))
  672. {
  673. UnitPattern unitPattern = new UnitPattern();
  674. unitPattern.Count = GetPatternCount(unitPatternData.Attribute("count").Value.ToString());
  675. unitPattern.Pattern = unitPatternData.Value.ToString();
  676. if (unitPatternData.Attribute("alt") != null)
  677. {
  678. unitPattern.Alt = unitPatternData.Attribute("alt").Value.ToString();
  679. }
  680. unitPatterns.Add(unitPattern);
  681. }
  682. unitPatternSet.UnitPatterns = unitPatterns.ToArray();
  683. unitPatternSets.Add(unitPatternSet);
  684. }
  685. return unitPatternSets.ToArray();
  686. }
  687. return null;
  688. }
  689. private static Dates GetDates(XDocument document)
  690. {
  691. if (options != null && options.CultureOptions != null && !options.CultureOptions.IncludeDates)
  692. {
  693. return null;
  694. }
  695. Dates dates = null;
  696. IEnumerable<XElement> ldmlElements = document.Elements("ldml");
  697. List<XElement> calendarDatas = (from item in ldmlElements.Elements("dates")
  698. .Elements("calendars").Elements("calendar")
  699. select item).ToList();
  700. XElement defaultData = (from item in ldmlElements.Elements("dates")
  701. .Elements("calendars")
  702. select item.Element("default")).FirstOrDefault();
  703. if ((calendarDatas != null && calendarDatas.Count > 0) || defaultData != null)
  704. {
  705. dates = new Dates();
  706. DatesDisplayNames datesDisplayNames;
  707. if (defaultData != null)
  708. {
  709. dates.DefaultCalendarId = defaultData.Attribute("choice").Value.ToString();
  710. }
  711. if (calendarDatas != null && calendarDatas.Count > 0)
  712. {
  713. List<Calendar> calendars = new List<Calendar>();
  714. foreach (XElement calendarData in calendarDatas)
  715. {
  716. Calendar calendar = new Calendar();
  717. calendar.Id = calendarData.Attribute("type").Value.ToString();
  718. // Look for DatesDisplayNames in the "calendar" element (compatibility with release 22.1 and earlier).
  719. // Prior to release 23 the DatesDisplayNames can be found in the "calendar" element.
  720. // Starting with release 23 the DatesDisplayNames moved up to the "dates" element.
  721. datesDisplayNames = GetDatesDisplayNames(calendarData);
  722. if (datesDisplayNames != null)
  723. {
  724. dates.DisplayNames = datesDisplayNames;
  725. }
  726. calendar.MonthNameSets =
  727. GetNameSets<MonthName, MonthNameSet>(calendarData, "months", "monthContext", "monthWidth", "month");
  728. calendar.DayNameSets =
  729. GetNameSets<DayName, DayNameSet>(calendarData, "days", "dayContext", "dayWidth", "day");
  730. calendar.DayPeriodNameSets =
  731. GetNameSets<DayPeriodName, DayPeriodNameSet>(calendarData, "dayPeriods", "dayPeriodContext", "dayPeriodWidth", "dayPeriod");
  732. calendar.EraNameSets =
  733. GetNameSets<EraName, EraNameSet>(calendarData, "eras", "eraAbbr", "era");
  734. calendar.DateFormats = GetDateFormats(calendarData);
  735. calendar.TimeFormats = GetTimeFormats(calendarData);
  736. calendars.Add(calendar);
  737. }
  738. dates.Calendars = calendars.ToArray();
  739. }
  740. // Look for DatesDisplayNames in the "dates" element (release 23 and later).
  741. datesDisplayNames = GetDatesDisplayNames(ldmlElements.Elements("dates").FirstOrDefault());
  742. if (datesDisplayNames != null)
  743. {
  744. dates.DisplayNames = datesDisplayNames;
  745. }
  746. }
  747. return dates;
  748. }
  749. private static Layout GetLayout(XDocument document)
  750. {
  751. if (options != null && options.CultureOptions != null && !options.CultureOptions.IncludeLayout)
  752. {
  753. return null;
  754. }
  755. Layout layout = null;
  756. IEnumerable<XElement> ldmlElements = document.Elements("ldml");
  757. List<XElement> layoutDatas = (from item in ldmlElements.Elements("layout")
  758. select item).ToList();
  759. if (layoutDatas != null && layoutDatas.Count > 0)
  760. {
  761. XElement orientationData = layoutDatas.Elements("orientation").FirstOrDefault();
  762. if (orientationData != null)
  763. {
  764. layout = new Layout();
  765. layout.Orientation = new Orientation();
  766. // "characters" is deprecated and is left here for compatibility with Release 22.1 and earlier
  767. if (orientationData.Attribute("characters") != null)
  768. {
  769. layout.Orientation.CharacterOrder = orientationData.Attribute("characters").Value.ToString();
  770. }
  771. // "characterOrder" was introduced in Release 23 and takes precedence over "characters"
  772. if (orientationData.Element("characterOrder") != null)
  773. {
  774. layout.Orientation.CharacterOrder = orientationData.Element("characterOrder").Value.ToString();
  775. }
  776. // "lines" is deprecated and is left here for compatibility with Release 22.1 and earlier
  777. if (orientationData.Attribute("lines") != null)
  778. {
  779. layout.Orientation.LineOrder = orientationData.Attribute("lines").Value.ToString();
  780. }
  781. // "lineOrder" was introduced in Release 23 and takes precedence over "characters"
  782. if (orientationData.Element("lineOrder") != null)
  783. {
  784. layout.Orientation.LineOrder = orientationData.Element("lineOrder").Value.ToString();
  785. }
  786. }
  787. }
  788. return layout;
  789. }
  790. private static ListPattern[] GetListPatterns(XDocument document)
  791. {
  792. if (options != null && options.CultureOptions != null && !options.CultureOptions.IncludeListPatterns)
  793. {
  794. return null;
  795. }
  796. IEnumerable<XElement> ldmlElements = document.Elements("ldml");
  797. List<XElement> listPatternDatas = (from item in ldmlElements.Elements("listPatterns")
  798. .Elements("listPattern").Elements("listPatternPart")
  799. select item).ToList();
  800. if (listPatternDatas != null && listPatternDatas.Count > 0)
  801. {
  802. List<ListPattern> listPatterns = new List<ListPattern>();
  803. foreach (XElement listPatternData in listPatternDatas)
  804. {
  805. ListPattern listPattern = new ListPattern();
  806. listPattern.Id = listPatternData.Attribute("type").Value.ToString();
  807. listPattern.Pattern = listPatternData.Value.ToString();
  808. listPatterns.Add(listPattern);
  809. }
  810. return listPatterns.ToArray();
  811. }
  812. return null;
  813. }
  814. private static DateFormat[] GetDateFormats(XElement calendarData)
  815. {
  816. List<DateFormat> dateFormats = new List<DateFormat>();
  817. List<XElement> dateFormatDatas = (from item in calendarData.Elements("dateFormats")
  818. .Elements("dateFormatLength")
  819. select item).ToList();
  820. if (dateFormatDatas.Count == 0)
  821. {
  822. return null;
  823. }
  824. foreach (XElement dateFormatData in dateFormatDatas)
  825. {
  826. XElement patternData = (from p in dateFormatData.Elements("dateFormat").Elements("pattern")
  827. select p).FirstOrDefault();
  828. if (patternData != null)
  829. {
  830. DateFormat dateFormat = new DateFormat();
  831. dateFormat.Id = dateFormatData.Attribute("type").Value.ToString();
  832. dateFormat.Pattern = Sanitize(patternData.Value.ToString());
  833. dateFormats.Add(dateFormat);
  834. }
  835. }
  836. return dateFormats.ToArray();
  837. }
  838. private static string Sanitize(string s)
  839. {
  840. // remove erroneous 8207 characters in CLDR data
  841. return s.Replace(((char)8207).ToString(), String.Empty);
  842. }
  843. private static TimeFormat[] GetTimeFormats(XElement calendarData)
  844. {
  845. List<TimeFormat> timeFormats = new List<TimeFormat>();
  846. List<XElement> timeFormatDatas = (from item in calendarData.Elements("timeFormats")
  847. .Elements("timeFormatLength")
  848. select item).ToList();
  849. if (timeFormatDatas.Count == 0)
  850. {
  851. return null;
  852. }
  853. foreach (XElement timeFormatData in timeFormatDatas)
  854. {
  855. XElement patternData = (from p in timeFormatData.Elements("timeFormat").Elements("pattern")
  856. select p).FirstOrDefault();
  857. if (patternData != null)
  858. {
  859. TimeFormat timeFormat = new TimeFormat();
  860. timeFormat.Id = timeFormatData.Attribute("type").Value.ToString();
  861. timeFormat.Pattern = Sanitize(patternData.Value.ToString());
  862. timeFormats.Add(timeFormat);
  863. }
  864. }
  865. return timeFormats.ToArray();
  866. }
  867. private static DatesDisplayNames GetDatesDisplayNames(XElement data)
  868. {
  869. List<XElement> fieldDatas = (from item in data.Elements("fields")
  870. .Elements("field")
  871. select item).ToList();
  872. if (fieldDatas == null || fieldDatas.Count == 0)
  873. {
  874. return null;
  875. }
  876. DatesDisplayNames datesDisplayNames = new DatesDisplayNames();
  877. foreach (XElement fieldData in fieldDatas)
  878. {
  879. string type = fieldData.Attribute("type").Value.ToString();
  880. string displayName =
  881. fieldData.Element("displayName") == null ? null : fieldData.Element("displayName").Value.ToString();
  882. if (type == "day")
  883. {
  884. datesDisplayNames.Day = displayName;
  885. datesDisplayNames.Yesterday = (from r in fieldData.Elements("relative")
  886. where r.Attribute("type").Value.ToString() == "-1"
  887. select r.Value.ToString()).FirstOrDefault();
  888. datesDisplayNames.Today = (from r in fieldData.Elements("relative")
  889. where r.Attribute("type").Value.ToString() == "0"
  890. select r.Value.ToString()).FirstOrDefault();
  891. datesDisplayNames.Tomorrow = (from r in fieldData.Elements("relative")
  892. where r.Attribute("type").Value.ToStri

Large files files are truncated, but you can click here to view the full file