Package dev.amp.validator
Class ParsedValidatorRules
- java.lang.Object
-
- dev.amp.validator.ParsedValidatorRules
-
public class ParsedValidatorRules extends Object
This wrapper class provides access to the validation rules.- Author:
- nhant01, GeorgeLuo
-
-
Constructor Summary
Constructors Constructor Description ParsedValidatorRules(dev.amp.validator.ValidatorProtos.HtmlFormat.Code htmlFormat, AMPValidatorManager ampValidatorManager)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanbetterValidationResultThan(dev.amp.validator.ValidatorProtos.ValidationResult.Builder resultA, dev.amp.validator.ValidatorProtos.ValidationResult.Builder resultB)Returns true iff resultA is a better result than resultB.booleanbetterValidationStatusThan(dev.amp.validator.ValidatorProtos.ValidationResult.Status statusA, dev.amp.validator.ValidatorProtos.ValidationResult.Status statusB)Returns true iff statusA is a better status than statusB.TagSpecDispatchdispatchForTagName(String tagName)Returns a TagSpecDispatch for a give tag name.PatternfindOrCreatePattern(Map<String,Pattern> regexMap, String regex, boolean isFullMatch)Find the regex pattern from the map.ParsedTagSpecgetByTagSpecId(int id)Returns the ParsedTagSpec given the tag spec id.ParsedTagSpecgetByTagSpecId(String specName)Return the ParsedTagSpec given the reference point spec name.StringgetCombinedDisallowedCdataRegex(int tagSpecId)Returns a combined disallowed regex.List<ParsedDocCssSpec>getCss()Returns the list of Css length spec.List<dev.amp.validator.ValidatorProtos.DescendantTagList>getDescendantTagLists()Returns the descendant tag lists.List<ParsedDocSpec>getDoc()Getter for parsed docPatterngetFullMatchCaseiRegex(String regex)Returns full match case insensitive regex pattern.PatterngetFullMatchRegex(String regex)Returns full match regex pattern.ParsedAttrSpecsgetParsedAttrSpecs()PatterngetPartialMatchCaseiRegex(String regex)Returns the partial match case insensitive match regex pattern.StringgetReferencePointName(dev.amp.validator.ValidatorProtos.ReferencePoint referencePoint)Computes the name for a given reference point.StringgetScriptSpecUrl()Returns the script spec url.StringgetStylesSpecUrl()Returns a styles spec url.intgetTagSpecIdByReferencePointTagSpecName(String tagName)Returns the tag spec id by reference point tag spec name.IntegergetTagSpecIdBySpecName(String specName)Returns the spec id by spec name.StringgetTemplateSpecUrl()Returns a template spec url.booleanisDocCssSpecCorrectHtmlFormat(dev.amp.validator.ValidatorProtos.DocCssSpec docCssSpec)Returns true if `spec` is usable for the HTML format these rules are built for.booleanisDocSpecCorrectHtmlFormat(dev.amp.validator.ValidatorProtos.DocSpec docSpec)Returns true if `spec` is usable for the HTML format these rules are built for.booleanisErrorSubset(List<dev.amp.validator.ValidatorProtos.ValidationError> errorsA, List<dev.amp.validator.ValidatorProtos.ValidationError> errorsB)Returns true iff the error codes in errorsB are a subset of the error codes in errorsA.booleanisTypeIdentifier(String maybeTypeIdentifier)Checks if maybeTypeIdentifier is contained in rules' typeIdentifiers.intmaxSpecificity(List<dev.amp.validator.ValidatorProtos.ValidationError> errors)A helper function which allows us to compare two candidate results in validateTag to report the results which have the most specific errors.voidmaybeEmitCssLengthSpecErrors(Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Emits errors for css size limitations across entire document.voidmaybeEmitDocSizeErrors(Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Emits errors for doc size limitations across entire document.voidmaybeEmitGlobalTagValidationErrors(Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Emits any validation errors which require a global view (mandatory tags, tags required by other tags, mandatory alternatives).voidmaybeEmitMandatoryAlternativesSatisfiedErrors(Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Emits errors for tags that are specified as mandatory alternatives.voidmaybeEmitMandatoryTagValidationErrors(Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Emits errors for tags that are specified to be mandatory.voidmaybeEmitRequiresOrExcludesValidationErrors(Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Emits errors for tags that specify that another tag is also required or a condition is required to be satisfied.voidmaybeEmitValueSetMismatchErrors(Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Emits errors when there is a ValueSetRequirement with no matching ValueSetProvision in the document.intspecificity(dev.amp.validator.ValidatorProtos.ValidationError.Code errorCode)Returns the error code specificity.voidvalidateHtmlTag(ParsedHtmlTag htmlTag, Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Validates the HTML tag for type identifiers.voidvalidateTypeIdentifiers(Attributes attrs, List<String> formatIdentifiers, Context context, 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.
-
-
-
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 mapregex- the regex- 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.
-
getByTagSpecId
public ParsedTagSpec getByTagSpecId(int id) throws TagValidationException
Returns the ParsedTagSpec given the tag spec id.- Parameters:
id- tag spec id.- Returns:
- returns the ParsedTagSpec.
- Throws:
TagValidationException- the TagValidationException.
-
getTagSpecIdByReferencePointTagSpecName
public int getTagSpecIdByReferencePointTagSpecName(@Nonnull String tagName) throws TagValidationExceptionReturns 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 ValidatorExceptionReturns 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 formatscontext- global context of document validationvalidationResult- 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 validationvalidationResult- 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 ValidatorExceptionA 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 ValidatorExceptionReturns 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 TagValidationExceptionEmits 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 TagValidationExceptionEmits 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 TagValidationExceptionEmits 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 TagValidationExceptionEmits 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 TagValidationExceptionEmits 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 TagValidationExceptionEmits 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- the 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 againstvalidationResult- 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
-
-