/// <summary> /// 创建新实例。 /// Initialize a new instance. /// </summary> /// <param name="translationStream"> /// 翻译流。 /// The stream of the translation. /// </param> /// <param name="leaveOpen"> /// 是否在读取后保存流开启。 /// Whether to keep the stream open after reading. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="translationStream"/> 为 <c>null</c> 。 /// <paramref name="translationStream"/> is <c>null</c>. /// </exception> /// <exception cref="CannotReadTranslationException"> /// 读取翻译失败。 /// Cannot read the translation. /// </exception> public Zhouyi(Stream translationStream, bool leaveOpen = false) { if (translationStream is null) { throw new ArgumentNullException(nameof(translationStream)); } TranslationFile?translation; try { using (var stream = new StreamReader(translationStream, null, true, -1, leaveOpen)) translation = JsonSerializer.Deserialize <TranslationFile>( stream.ReadToEnd(), this.jsonSerializerOptions); } catch (JsonException e) { throw new CannotReadTranslationException($"Invalid translation.", e); } if (translation is null || !translation.Check()) { throw new CannotReadTranslationException($"Invalid translation."); } this.patternsAndNumbers = new PatternsAndNumbers( translation.Patterns, translation.Numbers); this.trigramsAndHexagrams = new TrigramsAndHexagrams( translation.Hexagrams, translation.Trigrams); }
/// <summary> /// 创建一个不具有翻译的新实例。 /// 所有翻译将使用空字符串代替。 /// Initialize a new instance without translations. /// Empty strings will be used instead of the translations. /// </summary> public Zhouyi() { TranslationFile translation = TranslationFile.Empty; Debug.Assert(translation.Check()); this.patternsAndNumbers = new PatternsAndNumbers( translation.Patterns, translation.Numbers); this.trigramsAndHexagrams = new TrigramsAndHexagrams( translation.Hexagrams, translation.Trigrams); }
/// <summary> /// 创建新实例。 /// Initialize a new instance. /// </summary> /// <param name="translationFilePath"> /// 翻译文件路径。 /// Path of the translation file. /// </param> /// <exception cref="CannotReadTranslationException"> /// 读取翻译失败。 /// Cannot read the translation. /// </exception> /// <exception cref="ArgumentNullException"> /// <paramref name="translationFilePath"/> 为 <c>null</c> 。 /// <paramref name="translationFilePath"/> is <c>null</c>. /// </exception> public Zhouyi(string translationFilePath) { if (translationFilePath is null) { throw new ArgumentNullException(nameof(translationFilePath)); } TranslationFile?translation; try { using (var stream = new StreamReader(translationFilePath)) translation = JsonSerializer.Deserialize <TranslationFile>( stream.ReadToEnd(), this.jsonSerializerOptions); } catch (IOException e) { throw new CannotReadTranslationException($"Cannot read translation file: {translationFilePath}", e); } catch (JsonException e) { throw new CannotReadTranslationException($"Invalid translation file: {translationFilePath}", e); } catch (ArgumentException e) { throw new CannotReadTranslationException($"Cannot read translation file: {translationFilePath}", e); } if (translation is null || !translation.Check()) { throw new CannotReadTranslationException($"Invalid translation file: {translationFilePath}"); } this.patternsAndNumbers = new PatternsAndNumbers( translation.Patterns, translation.Numbers); this.trigramsAndHexagrams = new TrigramsAndHexagrams( translation.Hexagrams, translation.Trigrams); }