public bool Validate(string textSource, string marker, ITargetRule targetRule) { if (textSource == null) { throw new ArgumentNullException("textSource"); } if (marker == null) { throw new ArgumentNullException("matchingText"); } if (IsNegative(targetRule.CharacterLength)) { throw new CustomExceptions.TextExtractorException(Constant.ErrorMessages.CHARACTER_LENGTH_IS_NEGATIVE); } if (IsNegative(targetRule.Occurrence)) { throw new CustomExceptions.TextExtractorException(Constant.ErrorMessages.OCCURRENCE_LENGTH_IS_NEGATIVE); } if (targetRule.MarkerEnum == Constant.MarkerEnum.PlainText && marker.Length > textSource.Length) { throw new CustomExceptions.TextExtractorException(Constant.ErrorMessages.MATCHING_TEXT_LENGTH_GREATER_THAN_TEXT_SOURCE_LENGTH); } return(true); }
public string ExtractText(string textSource, string startMarker, string stopMarker, ITargetRule targetRule) { this.TargetRule = targetRule; this.StopMarker = stopMarker; this.extractionsEnd = targetRule.MaximumExtractions + targetRule.Occurrence; if (targetRule.CharacterLength > Constant.Sizes.EXTRACTOR_TARGET_TEXT_CHARACTERS_MAXIMUM || targetRule.CharacterLength < Constant.Sizes.EXTRACTOR_TARGET_TEXT_CHARACTERS_MINIMUM) { throw new CustomExceptions.TextExtractorException(string.Format("Number of Characters is not between {0} and {1}.", Constant.Sizes.EXTRACTOR_TARGET_TEXT_CHARACTERS_MINIMUM, Constant.Sizes.EXTRACTOR_TARGET_TEXT_CHARACTERS_MAXIMUM)); } if (targetRule.Occurrence > Constant.Sizes.EXTRACTOR_TARGET_TEXT_OCCURENCE_MAXIMUM || targetRule.Occurrence < Constant.Sizes.EXTRACTOR_TARGET_TEXT_OCCURENCE_MINIMUM) { throw new CustomExceptions.TextExtractorException(string.Format("Occurrence is not between {0} and {1}.", Constant.Sizes.EXTRACTOR_TARGET_TEXT_OCCURENCE_MINIMUM, Constant.Sizes.EXTRACTOR_TARGET_TEXT_OCCURENCE_MAXIMUM)); } try { //validate inputs TextExtractionValidator.Validate(textSource, startMarker, targetRule); ExtractTextByMarker(textSource, startMarker); } catch (Exception ex) { throw new CustomExceptions.TextExtractorException(Constant.ErrorMessages.DEFAULT_TEXT_EXTRACTION_ERROR_MESSAGE, ex); } string result = null; if (this.textExtracted.Count >= targetRule.MinimumExtractions) { result = String.Join(targetRule.CustomDelimiter, this.textExtracted); this.IsMarkerFound = true; } else { this.IsMarkerFound = false; } return(result); }