Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2013, Francis Galiegue <fgaliegue@gmail.com>
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the Lesser GNU General Public License as
   * published by the Free Software Foundation, either version 3 of the
   * License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * Lesser GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package com.github.fge.jsonschema.processors.format;
 
 
 import java.util.List;
 import java.util.Map;
 
 import static com.github.fge.jsonschema.messages.FormatMessages.*;

Format attribute handler

This processor is run after com.github.fge.jsonschema.processors.build.ValidatorBuilder if and only if the user has chosen to perform format validation (it is enabled by default).

It will append a specific com.github.fge.jsonschema.keyword.validator.KeywordValidator to the list of already existing validators if and only if:

  • there is a format keyword in the current schema;
  • the specified format attribute is supported;
  • the instance type is supported by this format attribute.

Note that it will warn if the format attribute is not recognized.

 
 public final class FormatProcessor
     implements Processor<ValidatorListValidatorList>
 {
     private final Map<StringFormatAttributeattributes;
 
     public FormatProcessor(final Library library)
     {
          = library.getFormatAttributes().entries();
     }
 
     public FormatProcessor(final Dictionary<FormatAttributedict)
     {
          = dict.entries();
     }
 
     @Override
     public ValidatorList process(final ProcessingReport report,
         final ValidatorList input)
         throws ProcessingException
     {
         final SchemaContext context = input.getContext();
         final JsonNode node = context.getSchema().getNode().get("format");
 
         if (node == null)
             return input;
 
         final String fmt = node.textValue();
         final FormatAttribute attr = .get(fmt);
 
         if (attr == null) {
             report.warn(input.newMessage().put("domain""validation")
                 .put("keyword""format").message()
                 .put("attribute"fmt));
             return input;
         }
 
         final NodeType type = context.getInstanceType();
 
         if (!attr.supportedTypes().contains(type))
             return input;
 
         final List<KeywordValidatorvalidators = Lists.newArrayList(input);
        validators.add(formatValidator(attr));
        return new ValidatorList(contextvalidators);
    }
    private static KeywordValidator formatValidator(final FormatAttribute attr)
    {
        return new KeywordValidator()
        {
            @Override
            public void validate(
                final Processor<FullDataFullDataprocessor,
                final ProcessingReport reportfinal FullData data)
                throws ProcessingException
            {
                attr.validate(reportdata);
            }
        };
    }
    @Override
    public String toString()
    {
        return "format attribute handler";
    }
New to GrepCode? Check out our FAQ X