Class AttributeSpecUtils


  • public final class AttributeSpecUtils
    extends Object
    Methods to handle attribute spec validation.
    Author:
    nhant01, GeorgeLuo
    • Method Detail

      • isUsedForTypeIdentifiers

        public static boolean isUsedForTypeIdentifiers​(@Nonnull
                                                       List<String> typeIdentifiers,
                                                       @Nonnull
                                                       List<String> enabledBys,
                                                       @Nonnull
                                                       List<String> disabledBys)
        Returns true if this spec should be used for the given type identifiers based on the spec's disabled_by or enabled_by fields.
        Parameters:
        typeIdentifiers - type identifiers.
        enabledBys - enabled bys.
        disabledBys - disabled bys.
        Returns:
        returns true if type identifiers are used.
      • validateAttributes

        public static void validateAttributes​(@Nonnull
                                              ParsedTagSpec parsedTagSpec,
                                              @Nonnull
                                              ParsedTagSpec bestMatchReferencePoint,
                                              @Nonnull
                                              Context context,
                                              @Nonnull
                                              ParsedHtmlTag encounteredTag,
                                              @Nonnull
                                              ValidateTagResult result)
                                       throws TagValidationException,
                                              IOException,
                                              CssValidationException
        Validates whether the attributes set on |encountered_tag| conform to this tag specification. All mandatory attributes must appear. Only attributes explicitly mentioned by this tag spec may appear. Returns true iff the validation is successful.
        Parameters:
        parsedTagSpec - the parsed tag spec.
        bestMatchReferencePoint - best match reference point.
        context - the context.
        encounteredTag - encountered tag.
        result - validation result.
        Throws:
        TagValidationException - tag validation exception.
        IOException - IO Exception
        CssValidationException - Css Validation Exception
      • validateAttrRequiredExtensions

        public static void validateAttrRequiredExtensions​(@Nonnull
                                                          ParsedAttrSpec parsedAttrSpec,
                                                          @Nonnull
                                                          Context context,
                                                          @Nonnull
                                                          dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
        If this attribute requires an extension and we have processed all extensions, report an error if that extension has not been loaded.
        Parameters:
        parsedAttrSpec - the parsed Attr spec.
        context - context
        validationResult - validationResult
      • validateAttrDeclaration

        public static void validateAttrDeclaration​(@Nonnull
                                                   ParsedAttrSpec parsedAttrSpec,
                                                   @Nonnull
                                                   Context context,
                                                   @Nonnull
                                                   String tagSpecName,
                                                   @Nonnull
                                                   String attrName,
                                                   @Nonnull
                                                   String attrValue,
                                                   @Nonnull
                                                   dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
                                            throws IOException,
                                                   CssValidationException
        Helper method for ValidateAttributes.
        Parameters:
        parsedAttrSpec - the parsed Attr spec.
        context - context.
        tagSpecName - tag spec name.
        attrName - attr Name.
        attrValue - attr Value
        validationResult - validationResult.
        Throws:
        IOException - IO Exception
        CssValidationException - Css Validation Exception
      • attrValueHasTemplateSyntax

        public static boolean attrValueHasTemplateSyntax​(String value)
        Returns true if |value| contains mustache template syntax.
        Parameters:
        value - value.
        Returns:
        {boolean}
      • validateNonTemplateAttrValueAgainstSpec

        public static void validateNonTemplateAttrValueAgainstSpec​(@Nonnull
                                                                   ParsedAttrSpec parsedAttrSpec,
                                                                   @Nonnull
                                                                   Context context,
                                                                   @Nonnull
                                                                   String attrName,
                                                                   @Nonnull
                                                                   String attrValue,
                                                                   @Nonnull
                                                                   dev.amp.validator.ValidatorProtos.TagSpec tagSpec,
                                                                   @Nonnull
                                                                   dev.amp.validator.ValidatorProtos.ValidationResult.Builder result)
        This is the main validation procedure for attributes, operating with a ParsedAttrSpec instance.
        Parameters:
        parsedAttrSpec - parsed attribute spec.
        context - the context.
        attrName - attribute name.
        attrValue - attribute value.
        tagSpec - the tag spec.
        result - the validation result.
      • validateAttrValueProperties

        public static void validateAttrValueProperties​(@Nonnull
                                                       ParsedValueProperties parsedValueProperties,
                                                       @Nonnull
                                                       Context context,
                                                       @Nonnull
                                                       String attrName,
                                                       @Nonnull
                                                       String attrValue,
                                                       @Nonnull
                                                       dev.amp.validator.ValidatorProtos.TagSpec tagSpec,
                                                       @Nonnull
                                                       dev.amp.validator.ValidatorProtos.ValidationResult.Builder result)
        Helper method for validateNonTemplateAttrValueAgainstSpec.
        Parameters:
        parsedValueProperties - value properties.
        context - the context.
        attrName - the attribute name.
        attrValue - the attribute value.
        tagSpec - the tag spec.
        result - validation result.
      • validateAttrValueUrl

        public static void validateAttrValueUrl​(@Nonnull
                                                ParsedAttrSpec parsedAttrSpec,
                                                @Nonnull
                                                Context context,
                                                @Nonnull
                                                String attrName,
                                                @Nonnull
                                                String attrValue,
                                                @Nonnull
                                                dev.amp.validator.ValidatorProtos.TagSpec tagSpec,
                                                @Nonnull
                                                dev.amp.validator.ValidatorProtos.ValidationResult.Builder result)
        Helper method for validateNonTemplateAttrValueAgainstSpec.
        Parameters:
        parsedAttrSpec - the parsed attribute spec.
        context - the context.
        attrName - the attribute name.
        attrValue - the attribute value.
        tagSpec - the tag spec.
        result - validation result.
      • validateUrlAndProtocol

        public static void validateUrlAndProtocol​(@Nonnull
                                                  ParsedUrlSpec parsedUrlSpec,
                                                  @Nonnull
                                                  UrlErrorAdapter adapter,
                                                  @Nonnull
                                                  Context context,
                                                  @Nonnull
                                                  String urlStr,
                                                  @Nonnull
                                                  dev.amp.validator.ValidatorProtos.TagSpec tagSpec,
                                                  @Nonnull
                                                  dev.amp.validator.ValidatorProtos.ValidationResult.Builder result)
        Parameters:
        parsedUrlSpec - parsed url spec.
        adapter - UrlErrorAdaptor interface.
        context - the context.
        urlStr - url string.
        tagSpec - tag spec.
        result - validation result.
      • validateLayout

        public static void validateLayout​(@Nonnull
                                          ParsedTagSpec parsedTagSpec,
                                          @Nonnull
                                          Context context,
                                          @Nonnull
                                          ParsedHtmlTag encounteredTag,
                                          @Nonnull
                                          dev.amp.validator.ValidatorProtos.ValidationResult.Builder result)
                                   throws TagValidationException
        Validates the layout for the given tag. This involves checking the layout, width, height, sizes attributes with AMP specific logic.
        Parameters:
        parsedTagSpec - the parsed tag spec.
        context - the context.
        encounteredTag - encountered tag.
        result - validation result.
        Throws:
        TagValidationException - the tag validation exception.
      • validateAttrInExtension

        public static boolean validateAttrInExtension​(@Nonnull
                                                      dev.amp.validator.ValidatorProtos.TagSpec tagSpec,
                                                      @Nonnull
                                                      String attrName,
                                                      @Nonnull
                                                      String attrValue)
                                               throws TagValidationException
        Validates whether an encountered attribute is validated by an ExtensionSpec. ExtensionSpec's validate the 'custom-element', 'custom-template', and 'host-service' attributes. If an error is found, it is added to the |result|. The return value indicates whether or not the provided attribute is explained by this validation function.
        Parameters:
        tagSpec - tag spec.
        attrName - attribute name.
        attrValue - attribute value.
        Returns:
        returns value indicates whether or not the provided attribute is explained by validation function.
        Throws:
        TagValidationException - the tag validation exception.
      • getExtensionNameAttribute

        public static String getExtensionNameAttribute​(@Nonnull
                                                       dev.amp.validator.ValidatorProtos.ExtensionSpec extensionSpec)
        Determines the name of the attribute where you find the name of this sort of extension. Typically, this will return 'custom-element'.
        Parameters:
        extensionSpec - extensionSpec
        Returns:
        returns the name of the attribute where you find the name of this sort of extension.
      • validateAttrNotFoundInSpec

        public static void validateAttrNotFoundInSpec​(@Nonnull
                                                      ParsedTagSpec parsedTagSpec,
                                                      @Nonnull
                                                      Context context,
                                                      @Nonnull
                                                      String attrName,
                                                      @Nonnull
                                                      dev.amp.validator.ValidatorProtos.ValidationResult.Builder result)
        Helper method for validateAttributes, for when an attribute is encountered which is not specified by the validator.protoascii specification.
        Parameters:
        parsedTagSpec - the parsed tag spec.
        context - the context.
        attrName - attribute name.
        result - validation result.
      • validateAttrValueBelowTemplateTag

        public static void validateAttrValueBelowTemplateTag​(@Nonnull
                                                             ParsedTagSpec parsedTagSpec,
                                                             @Nonnull
                                                             Context context,
                                                             @Nonnull
                                                             String attrName,
                                                             @Nonnull
                                                             String attrValue,
                                                             @Nonnull
                                                             dev.amp.validator.ValidatorProtos.ValidationResult.Builder result)
        Specific checks for attribute values descending from a template tag.
        Parameters:
        parsedTagSpec - the parsed tag spec.
        context - the context.
        attrName - attribute name.
        attrValue - attribute value.
        result - the validation result.
      • attrValueHasPartialsTemplateSyntax

        public static boolean attrValueHasPartialsTemplateSyntax​(@Nonnull
                                                                 String value)
        Returns true if |value| contains a mustache partials template syntax.
        Parameters:
        value - a test value.
        Returns:
        Returns true if |value| contains a mustache partials template syntax.
      • attrValueHasUnescapedTemplateSyntax

        public static boolean attrValueHasUnescapedTemplateSyntax​(@Nonnull
                                                                  String value)
        Returns true if |value| contains a mustache unescaped template syntax.
        Parameters:
        value - a test value.
        Returns:
        returns true if |value| contains a mustache unescaped template syntax.