Exemple #1
0
        /// <summary>
        /// Updates the message from the given message event arguments.
        /// </summary>
        /// <param name="messageEventArgs">message event arguments</param>
        private void UpdateMessage(MessageEventArgs messageEventArgs)
        {
            NumberVerifierMessageData messageData = (NumberVerifierMessageData)messageEventArgs.ExtendedData;

            tb_ErrorDetails.Text = messageEventArgs.Level.ToString() + "\r\n" + messageEventArgs.Message;
            tb_SourceIssues.Text = messageData.SourceIssues;
            tb_TargetIssues.Text = messageData.TargetIssues;
        }
        /// <summary>
        /// The following member performs the actual verification. It traverses the segment pairs of the current document,
        /// and checks whether a particular segment has any numbers. It then determines whether
        /// the target and the source contains the same numbers.
        /// If not, a warning message will be generated, which is then displayed between the source and target segments,
        /// and in the Messages window of SDL Trados Studio.
        /// </summary>
        /// <param name="paragraphUnit"></param>
        private void CheckParagraphUnit(IParagraphUnit paragraphUnit)
        {

            var sourceNumberList = new List<string>();
            var targetNumberList = new List<string>();
            var sourceNormalizedNumberList = new List<string>();
            var targetNormalizedNumberList = new List<string>();

            // loop through the whole paragraph unit
            foreach (var segmentPair in paragraphUnit.SegmentPairs.Where(FilterSegmentPairs))
            {
                var sourceText = GetSegmentText(segmentPair.Source);
                var targetText = GetSegmentText(segmentPair.Target);
               

                // find all alphanumeric names in source and add to list
                var sourceAlphanumericsList = GetAlphanumericList(sourceText);

                // find all alphanumeric names in target and add to list
                var targetAlphanumericsList = GetAlphanumericList(targetText);

                // remove alphanumeric names found both in source and target from respective list
                RemoveMatchingAlphanumerics(sourceAlphanumericsList, targetAlphanumericsList);

                // find all numbers in source and add to list
                sourceNumberList.Clear();
                sourceNormalizedNumberList.Clear();
                NormalizeAlphanumerics(sourceText, sourceNumberList, sourceNormalizedNumberList,_sourceThousandSeparators,_sourceDecimalSeparators, VerificationSettings.SourceNoSeparator);

                // find all numbers in target and add to list
                targetNumberList.Clear();
                targetNormalizedNumberList.Clear();
                NormalizeAlphanumerics(targetText, targetNumberList, targetNormalizedNumberList,_targetThousandSeparators,_targetDecimalSeparators, VerificationSettings.TargetNoSeparator);

                // remove identical numbers found both in source and target from respective list
                RemoveIdenticalNumbers(sourceNumberList, targetNumberList, targetNormalizedNumberList, sourceNormalizedNumberList);

                // remove numbers found both in source and target from respective list disregarding difference in thousands and decimal separators
                RemoveNumbersIgnoreThousandsAndDecimalSeparators(sourceNumberList, targetNormalizedNumberList, sourceNormalizedNumberList, targetNumberList);

                // remove numbers found both in source and target from respective list disregarding difference when thousands and decimal separators are undefined due to ambiguity 
                RemoveNumbersUndefinedThousandsAndDecimalSeparator(targetNumberList, sourceNumberList, sourceNormalizedNumberList, targetNormalizedNumberList);



                var errorLevel = ErrorLevel.Unspecified;
                var errorMessage = String.Empty;
                var extendedErrorMessage = String.Empty;

                // check if numbers have been modified and should be reported
                if (sourceNumberList.Count > 0 && targetNumberList.Count > 0 && VerificationSettings.ReportModifiedNumbers.Value)
                {
                    switch (VerificationSettings.ModifiedNumbersErrorType.Value)
                    {
                        case "Error":
                            errorLevel = ErrorLevel.Error;
                            break;
                        case "Warning":
                            errorLevel = ErrorLevel.Warning;
                            break;
                        default:
                            errorLevel = ErrorLevel.Note;
                            break;
                    }
                    errorMessage = errorMessage + PluginResources.Error_NumbersNotIdentical;
                }

                // check if numbers have been added and should be reported
                if (sourceNumberList.Count < targetNumberList.Count && VerificationSettings.ReportAddedNumbers.Value)
                {
                    if (VerificationSettings.AddedNumbersErrorType.Value == "Error")
                    {
                        errorLevel = ErrorLevel.Error;
                    }
                    else if (VerificationSettings.AddedNumbersErrorType.Value == "Warning" && errorLevel != ErrorLevel.Error)
                    {
                        errorLevel = ErrorLevel.Warning;
                    }
                    else if (errorLevel != ErrorLevel.Error && errorLevel != ErrorLevel.Warning)
                    {
                        errorLevel = ErrorLevel.Note;
                    }
                    errorMessage = errorMessage + PluginResources.Error_NumbersAdded;
                }

                // check if numbers have been removed and should be reported
                if (sourceNumberList.Count > targetNumberList.Count && VerificationSettings.ReportRemovedNumbers.Value)
                {
                    if (VerificationSettings.RemovedNumbersErrorType.Value == "Error")
                    {
                        errorLevel = ErrorLevel.Error;
                    }
                    else if (VerificationSettings.RemovedNumbersErrorType.Value == "Warning" && errorLevel != ErrorLevel.Error)
                    {
                        errorLevel = ErrorLevel.Warning;
                    }
                    else if (errorLevel != ErrorLevel.Error && errorLevel != ErrorLevel.Warning)
                    {
                        errorLevel = ErrorLevel.Note;
                    }
                    errorMessage = errorMessage + PluginResources.Error_NumbersRemoved;
                }

                // check if alphanumeric names are mismatched and should be reported
                if ((targetAlphanumericsList.Count > 0 || sourceAlphanumericsList.Count > 0) && VerificationSettings.ReportModifiedAlphanumerics.Value)
                {
                    if (VerificationSettings.ModifiedAlphanumericsErrorType.Value == "Error")
                    {
                        errorLevel = ErrorLevel.Error;
                    }
                    else if (VerificationSettings.ModifiedAlphanumericsErrorType.Value == "Warning" && errorLevel != ErrorLevel.Error)
                    {
                        errorLevel = ErrorLevel.Warning;
                    }
                    else if (errorLevel != ErrorLevel.Error && errorLevel != ErrorLevel.Warning)
                    {
                        errorLevel = ErrorLevel.Note;
                    }
                    errorMessage = errorMessage + PluginResources.Error_AlphanumericsModified;
                }


                // if there are any mismatched numbers or alphanumerics to report, output a warning message
                if (errorMessage == String.Empty) continue;

                // collate remaining numbers and put into string variables for reporting of details
                var sourceNumberIssues = String.Empty;
                if (sourceNumberList.Count > 0 && (VerificationSettings.ReportAddedNumbers.Value || VerificationSettings.ReportRemovedNumbers.Value || VerificationSettings.ReportModifiedNumbers.Value))
                {
                    sourceNumberIssues = sourceNumberList.Aggregate(sourceNumberIssues, (current, t) => current + (t + " \r\n"));
                }

                if (sourceAlphanumericsList.Count > 0 && VerificationSettings.ReportModifiedAlphanumerics.Value)
                {
                    sourceNumberIssues = sourceAlphanumericsList.Aggregate(sourceNumberIssues, (current, t) => current + (t + " \r\n"));
                }

                var targetNumberIssues = String.Empty;
                if (targetNumberList.Count > 0 && (VerificationSettings.ReportAddedNumbers.Value || VerificationSettings.ReportRemovedNumbers.Value || VerificationSettings.ReportModifiedNumbers.Value))
                {
                    targetNumberIssues = targetNumberList.Aggregate(targetNumberIssues, (current, t) => current + (t + " \r\n"));
                }

                if (targetAlphanumericsList.Count > 0 && VerificationSettings.ReportModifiedAlphanumerics.Value)
                {
                    targetNumberIssues = targetAlphanumericsList.Aggregate(targetNumberIssues, (current, t) => current + (t + " \r\n"));
                }


                if (VerificationSettings.ReportExtendedMessages == true)
                {
                    extendedErrorMessage = "\r\n SOURCE: " + TextGeneratorProcessor.GetPlainText(segmentPair.Source, !VerificationSettings.ExcludeTagText.Value) + " \r\n TARGET: " + TextGeneratorProcessor.GetPlainText(segmentPair.Target, !VerificationSettings.ExcludeTagText.Value);
                }


                #region ReportingMessage

                var extendedMessageReporter = MessageReporter as IBilingualContentMessageReporterWithExtendedData;
                if (extendedMessageReporter != null)
                {
                    #region CreateExtendedData
                    var extendedData = new NumberVerifierMessageData(sourceNumberIssues, targetNumberIssues, segmentPair.Target);
                    #endregion

                    #region ReportingMessageWithExtendedData
                    extendedMessageReporter.ReportMessage(this, PluginResources.Plugin_Name,
                        errorLevel, errorMessage + extendedErrorMessage,
                        new TextLocation(new Location(segmentPair.Target, true), 0),
                        new TextLocation(new Location(segmentPair.Target, false), segmentPair.Target.ToString().Length - 1),
                        extendedData);
                    #endregion

                }
                else
                {
                    #region ReportingMessageWithoutExtendedData
                    MessageReporter.ReportMessage(this, PluginResources.Plugin_Name,
                        errorLevel, errorMessage + extendedErrorMessage,
                        new TextLocation(new Location(segmentPair.Target, true), 0),
                        new TextLocation(new Location(segmentPair.Target, false), segmentPair.Target.ToString().Length - 1));
                    #endregion
                }
            }
        }