Class Context


  • public class Context
    extends Object
    The Context keeps track of the line / column that the validator is in, as well as the mandatory tag specs that have already been validated. So, this constitutes the mutable state for the validator except for the validation result itself.
    Author:
    nhant01, GeorgeLuo
    • Constructor Detail

      • Context

        public Context​(@Nonnull
                       ParsedValidatorRules parsedValidatorRules,
                       int docByteSize)
        Constructor.
        Parameters:
        parsedValidatorRules - a ParsedValidationRules object.
        docByteSize - size of document
    • Method Detail

      • updateFromTagResults

        public void updateFromTagResults​(@Nonnull
                                         ParsedHtmlTag encounteredTag,
                                         @Nonnull
                                         ValidateTagResult referencePointResult,
                                         @Nonnull
                                         ValidateTagResult tagResult)
                                  throws TagValidationException
        Given the tagResult from validating a single tag, update the overall result as well as the Context state to affect later validation.
        Parameters:
        encounteredTag - the encountered tag.
        referencePointResult - the reference point result.
        tagResult - the tag result.
        Throws:
        TagValidationException - the TagValidationException.
      • markUrlSeenFromMatchingTagSpec

        public void markUrlSeenFromMatchingTagSpec​(@Nonnull
                                                   ParsedTagSpec parsedTagSpec)
        Records that a Tag was seen which contains an URL. Used to note issues with base href occurring in the document after an URL.
        Parameters:
        parsedTagSpec - parsed tag spec.
      • valueSetsProvided

        public Set<String> valueSetsProvided()
        Returns all the value set provisions so far, as a set of derived keys, as computed by keyFromValueSetProvision_().
        Returns:
        the value sets provided
      • valueSetsRequired

        public Map<String,​List<dev.amp.validator.ValidatorProtos.ValidationError>> valueSetsRequired()
        Returns all the value set requirements so far, keyed by derived keys, as computed by getValueSetProvisionKey().
        Returns:
        the map of value sets required.
      • satisfyMandatoryAlternativesFromTagSpec

        public void satisfyMandatoryAlternativesFromTagSpec​(@Nonnull
                                                            ParsedTagSpec parsedTagSpec)
        Record that this document contains a tag which is a member of a list of mandatory alternatives.
        Parameters:
        parsedTagSpec - parsed tag spec.
      • addBuiltError

        public void addBuiltError​(@Nonnull
                                  dev.amp.validator.ValidatorProtos.ValidationError error,
                                  @Nonnull
                                  dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
        Parameters:
        error - a ValidationError object.
        validationResult - a ValidationResult object.
      • addError

        public void addError​(@Nonnull
                             dev.amp.validator.ValidatorProtos.ValidationError.Code validationErrorCode,
                             @Nonnull
                             Locator lineCol,
                             List<String> params,
                             String specUrl,
                             @Nonnull
                             dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
        Add an error field to validationResult with severity ERROR.
        Parameters:
        validationErrorCode - Error code
        lineCol - a line / column pair.
        params - a list of params.
        specUrl - a link (URL) to the amphtml spec
        validationResult - a ValidationResult object.
      • addError

        public void addError​(@Nonnull
                             dev.amp.validator.ValidatorProtos.ValidationError.Code validationErrorCode,
                             int line,
                             int column,
                             List<String> params,
                             String specUrl,
                             @Nonnull
                             dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
        Add an error field to validationResult with severity ERROR.
        Parameters:
        validationErrorCode - Error code
        line - a line number.
        column - a column number.
        params - a list of params.
        specUrl - a link (URL) to the amphtml spec
        validationResult - a ValidationResult object.
      • addWarning

        public void addWarning​(@Nonnull
                               dev.amp.validator.ValidatorProtos.ValidationError.Code validationErrorCode,
                               @Nonnull
                               Locator lineCol,
                               List<String> params,
                               String specUrl,
                               @Nonnull
                               dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
        Add an error field to validationResult with severity WARNING.
        Parameters:
        validationErrorCode - Error code
        lineCol - a line / column pair.
        params - a list of params.
        specUrl - a link (URL) to the amphtml spec
        validationResult - a ValidationResult object.
      • getLineCol

        public Locator getLineCol()
        Returns a line/col pair.
        Returns:
        returns a line/col pair.
      • setLineCol

        public void setLineCol​(@Nonnull
                               Locator lineCol)
        Setting the LineCol.
        Parameters:
        lineCol - a pair line/col.
      • getRules

        public ParsedValidatorRules getRules()
        Returns the ParsedValidatorRules.
        Returns:
        returns the ParsedValidatorRules object.
      • getTagStack

        public TagStack getTagStack()
        Returns the tag stack.
        Returns:
        returns the tag stack.
      • recordTypeIdentifier

        public void recordTypeIdentifier​(@Nonnull
                                         String typeIdentifier)
        Record the type identifier in this document.
        Parameters:
        typeIdentifier - type identifier.
      • getTypeIdentifiers

        public List<String> getTypeIdentifiers()
        Returns the type identifiers in this document.
        Returns:
        returns the type identifiers.
      • isDocSpecValidForTypeIdentifiers

        public boolean isDocSpecValidForTypeIdentifiers​(@Nonnull
                                                        ParsedDocSpec spec)
        Returns true iff `spec` should be used for the type identifiers recorded in this context, as seen in the document so far. If called before type identifiers have been recorded, will always return false.
        Parameters:
        spec - to evaluate
        Returns:
        true iff `spec` should be used for the type identifiers
      • matchingDocSpec

        public ParsedDocSpec matchingDocSpec()
        Returns the first (there should be at most one) DocSpec which matches both the html format and type identifiers recorded so far in this context. If called before identifiers have been recorded, it may return an incorrect selection.
        Returns:
        first (there should be at most one) DocSpec which matches both the html format and type identifiers
      • getExtensions

        public ExtensionsContext getExtensions()
        Returns:
        returns the extensions of the current validation job.
      • getTagspecsValidated

        public Map<Integer,​Boolean> getTagspecsValidated()
        Returns the tag spec ids that have been validated. The return object should be treated as a set (the object keys), and the value should be ignored.
        Returns:
        returns validated tag specs.
      • hasTagspecsValidated

        public boolean hasTagspecsValidated​(int id)
        Returns the boolean value of true if exists.
        Parameters:
        id - tag spec id.
        Returns:
        returns the boolean value of true if exists.
      • addStyleTagByteSize

        public void addStyleTagByteSize​(int byteSize)
        Records how much of the document is used towards <style amp-custom>.
        Parameters:
        byteSize - byte size.
      • addInlineStyleByteSize

        public void addInlineStyleByteSize​(int byteSize)
        Records how much of the document is used towards inline style.
        Parameters:
        byteSize - integer to add to running inline style byte size.
      • getInlineStyleByteSize

        public int getInlineStyleByteSize()
        Returns the size of inline styles.
        Returns:
        returns running inline style byte size.
      • getStyleTagByteSize

        public int getStyleTagByteSize()
        Returns the size of style amp-custom.
        Returns:
        returns the size of style of amp-custom
      • isTransformed

        public boolean isTransformed()
        Returns true iff "transformed" is a type identifier in this document.
        Returns:
        returns true iff "transformed" is a type identifier in this document.
      • hasSeenUrl

        public boolean hasSeenUrl()
        Returns true iff the current context has observed a tag which contains an URL. This is set by calling markUrlSeen_ above.
        Returns:
        returns true if first url seen tag is not null.
      • satisfiesCondition

        public boolean satisfiesCondition​(@Nonnull
                                          String condition)
        Parameters:
        condition - the condition.
        Returns:
        returns true if condition exists.
      • firstSeenUrlTagName

        public String firstSeenUrlTagName()
        The TagSpecName of the first seen URL. Do not call unless HasSeenUrl returns true.
        Returns:
        returns TagSpecName of the first seen URL.
      • getMandatoryAlternativesSatisfied

        public List<String> getMandatoryAlternativesSatisfied()
        The mandatory alternatives that we've satisfied. This may contain duplicates (we'd have to filter them in record... above if we cared).
        Returns:
        returns the mandatory alternatives that we've satisfied.
      • matchingDocCssSpec

        public ParsedDocCssSpec matchingDocCssSpec()
        Returns the first (there should be at most one) DocCssSpec which matches both the html format and type identifiers recorded so far in this context. If called before identifiers have been recorded, it may return an incorrect selection.
        Returns:
        ParsedDocCssSpec
      • getDocByteSize

        public int getDocByteSize()
        Returns the document size from the document locator.
        Returns:
        size of document