Class ParsedValidatorRules


  • public class ParsedValidatorRules
    extends Object
    This wrapper class provides access to the validation rules.
    Author:
    nhant01, GeorgeLuo
    • Constructor Detail

      • ParsedValidatorRules

        public ParsedValidatorRules​(@Nonnull
                                    dev.amp.validator.ValidatorProtos.HtmlFormat.Code htmlFormat,
                                    @Nonnull
                                    AMPValidatorManager ampValidatorManager)
        Constructor.
        Parameters:
        htmlFormat - the HTML format.
        ampValidatorManager - the AMPValidatorManager instance.
    • Method Detail

      • findOrCreatePattern

        public Pattern findOrCreatePattern​(@Nonnull
                                           Map<String,​Pattern> regexMap,
                                           @Nonnull
                                           String regex,
                                           boolean isFullMatch)
        Find the regex pattern from the map. If found then returns. Otherwise create new pattern. In an event when a pattern syntax exception occurs, escape curly brace and recompile.
        Parameters:
        regexMap - the regex map
        regex - the regex
        isFullMatch - if we are looking for a full match
        Returns:
        the pattern
      • getFullMatchRegex

        public Pattern getFullMatchRegex​(@Nonnull
                                         String regex)
        Returns full match regex pattern.
        Parameters:
        regex - the regex.
        Returns:
        returns the full match regex pattern.
      • getFullMatchCaseiRegex

        public Pattern getFullMatchCaseiRegex​(@Nonnull
                                              String regex)
        Returns full match case insensitive regex pattern.
        Parameters:
        regex - case insensitive regex.
        Returns:
        returns the full match case insensitive regex pattern.
      • getPartialMatchCaseiRegex

        public Pattern getPartialMatchCaseiRegex​(@Nonnull
                                                 String regex)
        Returns the partial match case insensitive match regex pattern.
        Parameters:
        regex - the regex.
        Returns:
        returns the partial match case insensitive match regex pattern.
      • getReferencePointName

        public String getReferencePointName​(@Nonnull
                                            dev.amp.validator.ValidatorProtos.ReferencePoint referencePoint)
                                     throws TagValidationException
        Computes the name for a given reference point. Used in generating error strings.
        Parameters:
        referencePoint - the reference point.
        Returns:
        returns the compute name for a given reference point.
        Throws:
        TagValidationException - the TagValidationException.
      • getByTagSpecId

        public ParsedTagSpec getByTagSpecId​(String specName)
                                     throws TagValidationException
        Return the ParsedTagSpec given the reference point spec name.
        Parameters:
        specName - the spec name.
        Returns:
        return the ParsedTagSpec given the reference point spec name.
        Throws:
        TagValidationException - the TagValidationException.
      • getTagSpecIdBySpecName

        public Integer getTagSpecIdBySpecName​(@Nonnull
                                              String specName)
        Returns the spec id by spec name.
        Parameters:
        specName - the spec name.
        Returns:
        returns the spec id if exists.
      • getTagSpecIdByReferencePointTagSpecName

        public int getTagSpecIdByReferencePointTagSpecName​(@Nonnull
                                                           String tagName)
                                                    throws TagValidationException
        Returns the tag spec id by reference point tag spec name.
        Parameters:
        tagName - the reference point tag name.
        Returns:
        returns the tag spec id by reference point tag spec name.
        Throws:
        TagValidationException - the TagValidationException.
      • betterValidationResultThan

        public boolean betterValidationResultThan​(@Nonnull
                                                  dev.amp.validator.ValidatorProtos.ValidationResult.Builder resultA,
                                                  @Nonnull
                                                  dev.amp.validator.ValidatorProtos.ValidationResult.Builder resultB)
                                           throws ValidatorException
        Returns true iff resultA is a better result than resultB.
        Parameters:
        resultA - a validation result.
        resultB - a validation result.
        Returns:
        returns true iff resultA is a better result than resultB.
        Throws:
        ValidatorException - the ValidatorException.
      • isTypeIdentifier

        public boolean isTypeIdentifier​(@Nonnull
                                        String maybeTypeIdentifier)
        Checks if maybeTypeIdentifier is contained in rules' typeIdentifiers.
        Parameters:
        maybeTypeIdentifier - identifier to check
        Returns:
        true iff maybeTypeIdentifier is in typeIdentifiers.
      • validateTypeIdentifiers

        public void validateTypeIdentifiers​(@Nonnull
                                            Attributes attrs,
                                            @Nonnull
                                            List<String> formatIdentifiers,
                                            @Nonnull
                                            Context context,
                                            @Nonnull
                                            dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
        Validates type identifiers within a set of attributes, adding ValidationErrors as necessary, and sets type identifiers on ValidationResult.typeIdentifier.
        Parameters:
        attrs - sax Attributes object from tag.
        formatIdentifiers - html formats
        context - global context of document validation
        validationResult - status of document validation
      • validateHtmlTag

        public void validateHtmlTag​(@Nonnull
                                    ParsedHtmlTag htmlTag,
                                    @Nonnull
                                    Context context,
                                    @Nonnull
                                    dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
        Validates the HTML tag for type identifiers.
        Parameters:
        htmlTag - the html tag to validate.
        context - global context of document validation
        validationResult - status of document validation
      • specificity

        public int specificity​(@Nonnull
                               dev.amp.validator.ValidatorProtos.ValidationError.Code errorCode)
        Returns the error code specificity.
        Parameters:
        errorCode - the validation error code.
        Returns:
        returns the error code specificity.
      • maxSpecificity

        public int maxSpecificity​(@Nonnull
                                  List<dev.amp.validator.ValidatorProtos.ValidationError> errors)
                           throws ValidatorException
        A helper function which allows us to compare two candidate results in validateTag to report the results which have the most specific errors.
        Parameters:
        errors - a list of validation errors.
        Returns:
        returns maximum value of specificity found in all errors.
        Throws:
        ValidatorException - the TagValidationException.
      • isErrorSubset

        public boolean isErrorSubset​(@Nonnull
                                     List<dev.amp.validator.ValidatorProtos.ValidationError> errorsA,
                                     @Nonnull
                                     List<dev.amp.validator.ValidatorProtos.ValidationError> errorsB)
        Returns true iff the error codes in errorsB are a subset of the error codes in errorsA.
        Parameters:
        errorsA - a list of validation errors.
        errorsB - a list of validation errors.
        Returns:
        returns true iff the error codes in errorsB are a subset of the error codes in errorsA.
      • betterValidationStatusThan

        public boolean betterValidationStatusThan​(@Nonnull
                                                  dev.amp.validator.ValidatorProtos.ValidationResult.Status statusA,
                                                  @Nonnull
                                                  dev.amp.validator.ValidatorProtos.ValidationResult.Status statusB)
                                           throws ValidatorException
        Returns true iff statusA is a better status than statusB.
        Parameters:
        statusA - validation result status.
        statusB - validation result status.
        Returns:
        returns true iff statusA is a better status than statusB.
        Throws:
        ValidatorException - the ValidatorException.
      • dispatchForTagName

        public TagSpecDispatch dispatchForTagName​(@Nonnull
                                                  String tagName)
        Returns a TagSpecDispatch for a give tag name.
        Parameters:
        tagName - the tag name.
        Returns:
        returns a TagSpecDispatch if found.
      • getStylesSpecUrl

        public String getStylesSpecUrl()
        Returns a styles spec url.
        Returns:
        returns a styles spec url.
      • getTemplateSpecUrl

        public String getTemplateSpecUrl()
        Returns a template spec url.
        Returns:
        returns a template spec url.
      • getScriptSpecUrl

        public String getScriptSpecUrl()
        Returns the script spec url.
        Returns:
        returns the script spec url.
      • getCss

        public List<ParsedDocCssSpec> getCss()
        Returns the list of Css length spec.
        Returns:
        returns the list of Css length spec.
      • getDescendantTagLists

        public List<dev.amp.validator.ValidatorProtos.DescendantTagList> getDescendantTagLists()
        Returns the descendant tag lists.
        Returns:
        returns the descendant tag lists.
      • getCombinedDisallowedCdataRegex

        public String getCombinedDisallowedCdataRegex​(int tagSpecId)
        Returns a combined disallowed regex.
        Parameters:
        tagSpecId - tag spec id.
        Returns:
        returns a combined disallowed regex.
      • maybeEmitGlobalTagValidationErrors

        public void maybeEmitGlobalTagValidationErrors​(@Nonnull
                                                       Context context,
                                                       @Nonnull
                                                       dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
                                                throws TagValidationException
        Emits any validation errors which require a global view (mandatory tags, tags required by other tags, mandatory alternatives).
        Parameters:
        context - the Context.
        validationResult - the ValidationResult.
        Throws:
        TagValidationException - the TagValidationException.
      • maybeEmitValueSetMismatchErrors

        public void maybeEmitValueSetMismatchErrors​(@Nonnull
                                                    Context context,
                                                    @Nonnull
                                                    dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
                                             throws TagValidationException
        Emits errors when there is a ValueSetRequirement with no matching ValueSetProvision in the document.
        Parameters:
        context - the Context.
        validationResult - the ValidationResult.
        Throws:
        TagValidationException - the TagValidationException.
      • maybeEmitCssLengthSpecErrors

        public void maybeEmitCssLengthSpecErrors​(@Nonnull
                                                 Context context,
                                                 @Nonnull
                                                 dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
                                          throws TagValidationException
        Emits errors for css size limitations across entire document.
        Parameters:
        context - the Context.
        validationResult - the ValidationResult.
        Throws:
        TagValidationException - the TagValidationException.
      • maybeEmitMandatoryAlternativesSatisfiedErrors

        public void maybeEmitMandatoryAlternativesSatisfiedErrors​(@Nonnull
                                                                  Context context,
                                                                  @Nonnull
                                                                  dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
                                                           throws TagValidationException
        Emits errors for tags that are specified as mandatory alternatives. Returns false iff context.Progress(result).complete.
        Parameters:
        context - the Context.
        validationResult - the ValidationResult.
        Throws:
        TagValidationException - the TagValidationException.
      • maybeEmitMandatoryTagValidationErrors

        public void maybeEmitMandatoryTagValidationErrors​(@Nonnull
                                                          Context context,
                                                          @Nonnull
                                                          dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
                                                   throws TagValidationException
        Emits errors for tags that are specified to be mandatory.
        Parameters:
        context - the Context.
        validationResult - the ValidationResult.
        Throws:
        TagValidationException - the TagValidationException.
      • maybeEmitRequiresOrExcludesValidationErrors

        public void maybeEmitRequiresOrExcludesValidationErrors​(@Nonnull
                                                                Context context,
                                                                @Nonnull
                                                                dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
                                                         throws TagValidationException
        Emits errors for tags that specify that another tag is also required or a condition is required to be satisfied. Returns false iff context.Progress(result).complete.
        Parameters:
        context - the Context.
        validationResult - the ValidationResult.
        Throws:
        TagValidationException
      • maybeEmitDocSizeErrors

        public void maybeEmitDocSizeErrors​(@Nonnull
                                           Context context,
                                           @Nonnull
                                           dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
        Emits errors for doc size limitations across entire document.
        Parameters:
        context - the context to evaluate against
        validationResult - to populate
      • isDocSpecCorrectHtmlFormat

        public boolean isDocSpecCorrectHtmlFormat​(@Nonnull
                                                  dev.amp.validator.ValidatorProtos.DocSpec docSpec)
        Returns true if `spec` is usable for the HTML format these rules are built for.
        Parameters:
        docSpec - the docSpec to evaluate
        Returns:
        true if `spec` is usable for the HTML format these rules
      • isDocCssSpecCorrectHtmlFormat

        public boolean isDocCssSpecCorrectHtmlFormat​(@Nonnull
                                                     dev.amp.validator.ValidatorProtos.DocCssSpec docCssSpec)
        Returns true if `spec` is usable for the HTML format these rules are built for.
        Parameters:
        docCssSpec - the DocCssSpec.
        Returns:
        Returns true if `spec` is usable for the HTML format these rules are built for.
      • getDoc

        public List<ParsedDocSpec> getDoc()
        Getter for parsed doc
        Returns:
        this parsedDoc
      • getParsedAttrSpecs

        public ParsedAttrSpecs getParsedAttrSpecs()
        Returns:
        this ParsedAttrSpecs