Package dev.amp.validator
Class Context
- java.lang.Object
-
- dev.amp.validator.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 Summary
Constructors Constructor Description Context(ParsedValidatorRules parsedValidatorRules)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddBuiltError(dev.amp.validator.ValidatorProtos.ValidationError error, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)voidaddError(dev.amp.validator.ValidatorProtos.ValidationError.Code validationErrorCode, int line, int column, List<String> params, String specUrl, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Add an error field to validationResult with severity ERROR.voidaddError(dev.amp.validator.ValidatorProtos.ValidationError.Code validationErrorCode, Locator lineCol, List<String> params, String specUrl, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Add an error field to validationResult with severity ERROR.voidaddInlineStyleByteSize(int byteSize)Records how much of the document is used towards inline style.voidaddStyleTagByteSize(int byteSize)Records how much of the document is used towards <style amp-custom>.voidaddWarning(dev.amp.validator.ValidatorProtos.ValidationError.Code validationErrorCode, Locator lineCol, List<String> params, String specUrl, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Add an error field to validationResult with severity WARNING.StringfirstSeenUrlTagName()The TagSpecName of the first seen URL.ExtensionsContextgetExtensions()intgetInlineStyleByteSize()Returns the size of inline styles.LocatorgetLineCol()Returns a line/col pair.List<String>getMandatoryAlternativesSatisfied()The mandatory alternatives that we've satisfied.ParsedValidatorRulesgetRules()Returns the ParsedValidatorRules.ExtensionsUtils.ScriptReleaseVersiongetScriptReleaseVersion()getter for scriptReleaseVersionintgetStyleTagByteSize()Returns the size of style amp-custom.Map<Integer,Boolean>getTagspecsValidated()Returns the tag spec ids that have been validated.TagStackgetTagStack()Returns the tag stack.List<String>getTypeIdentifiers()Returns the type identifiers in this document.booleanhasSeenUrl()Returns true iff the current context has observed a tag which contains an URL.booleanhasTagspecsValidated(int id)Returns the boolean value of true if exists.booleanisTransformed()Returns true iff "transformed" is a type identifier in this document.voidmarkUrlSeenFromMatchingTagSpec(ParsedTagSpec parsedTagSpec)Records that a Tag was seen which contains an URL.ParsedDocCssSpecmatchingDocCssSpec()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.voidrecordTypeIdentifier(String typeIdentifier)Record the type identifier in this document.booleansatisfiesCondition(String condition)voidsatisfyMandatoryAlternativesFromTagSpec(ParsedTagSpec parsedTagSpec)Record that this document contains a tag which is a member of a list of mandatory alternatives.voidsetLineCol(Locator lineCol)Setting the LineCol.voidupdateFromTagResults(ParsedHtmlTag encounteredTag, ValidateTagResult referencePointResult, ValidateTagResult tagResult)Given the tagResult from validating a single tag, update the overall result as well as the Context state to affect later validation.Set<String>valueSetsProvided()Returns all the value set provisions so far, as a set of derived keys, as computed by keyFromValueSetProvision_().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().
-
-
-
Constructor Detail
-
Context
public Context(@Nonnull ParsedValidatorRules parsedValidatorRules)Constructor.- Parameters:
parsedValidatorRules- a ParsedValidationRules object.
-
-
Method Detail
-
updateFromTagResults
public void updateFromTagResults(@Nonnull ParsedHtmlTag encounteredTag, @Nonnull ValidateTagResult referencePointResult, @Nonnull ValidateTagResult tagResult) throws TagValidationExceptionGiven 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 codelineCol- a line / column pair.params- a list of params.specUrl- a link (URL) to the amphtml specvalidationResult- 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 codeline- a line number.column- a column number.params- a list of params.specUrl- a link (URL) to the amphtml specvalidationResult- 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 codelineCol- a line / column pair.params- a list of params.specUrl- a link (URL) to the amphtml specvalidationResult- 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.
-
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.
-
getScriptReleaseVersion
public ExtensionsUtils.ScriptReleaseVersion getScriptReleaseVersion()
getter for scriptReleaseVersion- Returns:
- the associated script release version.
-
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
-
-