All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xpertss.json.schema.format.helpers.IPv4FormatAttribute Maven / Gradle / Ivy

The newest version!
package xpertss.json.schema.format.helpers;

import com.github.fge.jackson.NodeType;
import xpertss.json.schema.core.exceptions.ProcessingException;
import xpertss.json.schema.core.report.ProcessingReport;
import xpertss.json.schema.format.AbstractFormatAttribute;
import xpertss.json.schema.processors.data.FullData;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.google.common.net.InetAddresses;

/**
 * Validator for both the {@code ip-address} (draft v3) and {@code ipv4} (draft
 * v4) format attributes.
 *
 * 

This uses Guava's {@link InetAddresses} to do the job.

*/ public final class IPv4FormatAttribute extends AbstractFormatAttribute { private static final int IPV4_LENGTH = 4; public IPv4FormatAttribute(String fmt) { super(fmt, NodeType.STRING); } @Override public void validate(ProcessingReport report, MessageBundle bundle, FullData data) throws ProcessingException { String ipaddr = data.getInstance().getNode().textValue(); if (InetAddresses.isInetAddress(ipaddr) && InetAddresses.forString(ipaddr).getAddress().length == IPV4_LENGTH) return; report.error(newMsg(data, bundle, "err.format.invalidIPv4Address") .putArgument("value", ipaddr)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy