#region コンストラクタ
/// <summary>
- /// ã\83\88ã\83©ã\83³ã\82¹ã\83¬ã\83¼ã\82¿ã\82\92ä½\9cæ\88\90。
+ /// ã\83\88ã\83©ã\83³ã\82¹ã\83¬ã\83¼ã\82¿ã\82¤ã\83³ã\82¹ã\82¿ã\83³ã\82¹ã\82\92ç\94\9fæ\88\90ã\81\99ã\82\8b。
/// </summary>
public Translator()
{
/// <summary>
/// ステータス管理用オブジェクト。
/// </summary>
- /// <exception cref="ArgumentNullException"><c>null</c>が指定された場合。</exception>
protected StatusManager<string> StatusManager
{
get;
#region 静的メソッド
/// <summary>
- /// 翻訳支援処理のインスタンスを作成。
+ /// 指定されたアプリケーション設定から翻訳支援処理のインスタンスを作成する。
/// </summary>
/// <param name="config">アプリケーション設定。</param>
/// <param name="from">翻訳元言語。</param>
/// <param name="to">翻訳先言語。</param>
/// <returns>生成したインスタンス。</returns>
+ /// <exception cref="NotImplementedException">
+ /// アプリケーション設定に指定されているトランスレータに引数無しのコンストラクタが存在しない場合。
+ /// </exception>
/// <remarks>
/// 設定は設定クラスより取得、無ければ一部自動生成する。
- /// インスタンス生成失敗時は例外を投げる。
+ /// インスタンス生成失敗時は各種例外を投げる。
/// </remarks>
public static Translator Create(Config config, string from, string to)
{
throw new InvalidOperationException("From or To is null");
}
- // å¤\89æ\95°ã\82\92å\88\9dæ\9c\9få\8c\96ã\80\81å\87¦ç\90\86æ\99\82é\96\93ã\82\92測å®\9aé\96\8bå§\8b
+ // å\88\9dæ\9c\9få\8c\96ã\82\84ã\82¹ã\83\88ã\83\83ã\83\97ã\82¦ã\82©ã\83\83ã\83\81ã\81®èµ·å\8b\95ã\81¨ã\81\84ã\81£ã\81\9få\89\8då\87¦ç\90\86ã\82\92å®\9fæ\96½
this.Initialize();
- this.Stopwatch.Start();
-
- // サーバー接続チェック
- string host = new Uri(this.From.Location).Host;
- if (!String.IsNullOrEmpty(host) && !Settings.Default.IgnoreError)
+ try
{
- if (!this.Ping(host))
+ // サーバー接続チェック
+ string host = new Uri(this.From.Location).Host;
+ if (!String.IsNullOrEmpty(host) && !Settings.Default.IgnoreError)
{
- throw new ApplicationException("ping failed");
+ if (!this.Ping(host))
+ {
+ throw new ApplicationException("ping failed");
+ }
}
- }
- // ここまでの間に終了要求が出ているかを確認
- this.ThrowExceptionIfCanceled();
+ // ここまでの間に終了要求が出ているかを確認
+ this.ThrowExceptionIfCanceled();
- // 翻訳支援処理実行部の本体を実行
- // ※以降の処理は、継承クラスにて定義
- try
- {
+ // 翻訳支援処理実行部の本体を実行
+ // ※以降の処理は、継承クラスにて定義
this.RunBody(name);
}
finally
{
- // 終了後は処理状態をクリア、処理時間を測定終了
- this.StatusManager.Clear();
- this.Stopwatch.Stop();
+ // 状態のクリアやストップウォッチの停止といった後処理を実施
+ this.Terminate();
}
}
#endregion
- #region 実装が必要なテンプレートメソッド
+ #region テンプレートメソッド
/// <summary>
/// 翻訳支援処理実行部の本体。
/// <remarks>テンプレートメソッド的な構造になっています。</remarks>
protected abstract void RunBody(string name);
+ /// <summary>
+ /// 翻訳支援処理実行時の初期化処理。
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// <see cref="RunBody"/>実行前の初期化のタイミングでコールされる。
+ /// 本メソッドで例外が発生した場合、<see cref="RunBody"/>, <see cref="Terminate"/>はコールされない。
+ /// </para>
+ /// <para>
+ /// オーバーライド時は必ず親のメソッドもコールすること。
+ /// </para>
+ /// </remarks>
+ protected virtual void Initialize()
+ {
+ // ロガーや処理状態などを初期化、処理時間の測定を開始
+ this.Logger.Clear();
+ this.StatusManager.Clear();
+ this.Stopwatch.Reset();
+ this.Text = String.Empty;
+ this.CancellationPending = false;
+ this.From.WebProxy.Referer = null;
+ this.To.WebProxy.Referer = null;
+ this.Stopwatch.Start();
+ }
+
+ /// <summary>
+ /// 翻訳支援処理実行時の終了処理。
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// <see cref="RunBody"/>実行後の後処理のタイミングでコールされる。
+ /// ただし、サーバー接続チェックに失敗した場合などでは、
+ /// <see cref="RunBody"/>が実行されること無く本メソッドが呼ばれる場合もある。
+ /// </para>
+ /// <para>
+ /// オーバーライド時は必ず親のメソッドもコールすること。
+ /// </para>
+ /// </remarks>
+ protected virtual void Terminate()
+ {
+ // 終了後は処理状態やRefererをクリア、処理時間を測定終了
+ this.StatusManager.Clear();
+ this.From.WebProxy.Referer = null;
+ this.To.WebProxy.Referer = null;
+ this.Stopwatch.Stop();
+ }
+
#endregion
#region 実装支援用メソッド
/// <param name="title">ページタイトル。</param>
/// <param name="page">取得したページ。ページが存在しない場合は <c>null</c> を返す。</param>
/// <returns>処理が成功した(404も含む)場合<c>true</c>、失敗した(通信エラーなど)の場合<c>false</c>。</returns>
- /// <exception cref="ApplicationException"><see cref="CancellationPending"/>が<c>true</c>の場合。</exception>
+ /// <exception cref="ApplicationException">
+ /// 想定外の例外が発生した場合でかつアプリケーション設定の<c>IgnoreError</c>が<c>false</c>の場合、
+ /// または<see cref="CancellationPending"/>が<c>true</c>の場合。
+ /// </exception>
/// <remarks>
/// 本メソッドは、大きく3パターンの動作を行う。
/// <list type="number">
/// <item><description>正常にページが取得できた → <c>true</c>でページを設定、ログ出力無し</description></item>
/// <item><description>404など想定内の例外でページが取得できなかった → <c>true</c>でページ無し、ログ出力無し</description></item>
/// <item><description>想定外の例外でページが取得できなかった → <c>false</c>でページ無し、ログ出力有り
- /// or <c>ApplicationException</c>で処理中断(アプリケーション設定のIgnoreErrorによる)。</description></item>
+ /// or <see cref="ApplicationException"/>で処理中断(アプリケーション設定の<c>IgnoreError</c>による)。</description></item>
/// </list>
/// また、実行中は処理状態をサーバー接続中に更新する。
/// 実行前後には終了要求のチェックも行う。
#region 内部処理用メソッド
/// <summary>
- /// 翻訳支援処理実行時の初期化処理。
- /// </summary>
- private void Initialize()
- {
- // 変数を初期化
- this.Logger.Clear();
- this.StatusManager.Clear();
- this.Stopwatch.Reset();
- this.Text = String.Empty;
- this.CancellationPending = false;
- }
-
- /// <summary>
/// サーバー接続チェック。
/// </summary>
/// <param name="server">サーバー名。</param>
- /// <returns><c>true</c> 接続成功。</returns>
+ /// <returns>接続成功の場合<c>true</c>。</returns>
/// <remarks>実行中は処理状態をサーバー接続中に更新する。</remarks>
private bool Ping(string server)
{
/// ログ出力によるエラー処理を含んだページ取得処理本体。
/// </summary>
/// <param name="title">ページタイトル。</param>
- /// <param name="page">取得したページ。ページが存在しない場合は <c>null</c> を返す。</param>
+ /// <param name="page">取得したページ。ページが存在しない場合は<c>null</c>を返す。</param>
/// <returns>処理が成功した(404も含む)場合<c>true</c>、失敗した(通信エラーなど)の場合<c>false</c>。</returns>
+ /// <exception cref="ApplicationException">
+ /// 想定外の例外が発生した場合でかつアプリケーション設定の<c>IgnoreError</c>が<c>false</c>の場合。
+ /// </exception>
/// <remarks>
/// 本メソッドは、大きく3パターンの動作を行う。
/// <list type="number">
/// <item><description>正常にページが取得できた → <c>true</c>でページを設定、ログ出力無し</description></item>
/// <item><description>404など想定内の例外でページが取得できなかった → <c>true</c>でページ無し、ログ出力無し</description></item>
/// <item><description>想定外の例外でページが取得できなかった → <c>false</c>でページ無し、ログ出力有り
- /// or <c>ApplicationException</c>で処理中断(アプリケーション設定のIgnoreError等による)。</description></item>
+ /// or <see cref="ApplicationException"/>で処理中断(アプリケーション設定の<c>IgnoreError</c>による)。</description></item>
/// </list>
/// </remarks>
private bool TryGetPageBody(string title, out Page page)