Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* 
   * Copyright (C) 2012 FoxLabs
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.foxlabs.validation.constraint;
 
 import java.util.Map;
 import java.util.Set;
 
 import java.net.URI;
 
This class provides CheckConstraint implementation that checks whether a string is valid URI reference. Also the allowed set of schemes can be configured.

 
 public final class UriConstraint extends CheckConstraint<String> {
    
    
UriConstraint default instance initialized with empty set of schemes.
 
     public static final UriConstraint DEFAULT = new UriConstraint((String[]) null);
    
    
Set of allowed schemes (empty set means all schemes are allowed).
 
     private final Set<Stringschemes;
    
    
Constructs a new UriConstraint with the specified array of allowed schemes.

Parameters:
schemes Array of allowed schemes.
Throws:
java.lang.IllegalArgumentException if the specified array of schemes contains null or empty elements.
 
     UriConstraint(String[] schemes) {
         this. = schemes == null || schemes.length == 0
             ? Collections.<String>emptySet()
             : Collections.unmodifiableSet(Assert.noEmptyStringSet(schemes"schemes"));
     }
    
    
Constructs a new UriConstraint from the specified annotation.

Parameters:
annotation Constraint annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines array of schemes that contains empty elements.
 
     UriConstraint(Uri annotation) {
         this(annotation.schemes());
     }
    
    
Returns java.lang.String type.

Returns:
java.lang.String type.
 
     @Override
     public Class<?> getType() {
         return String.class;
     }
    
    
Returns set of allowed schemes (empty set means all schemes are allowed).

Returns:
Set of allowed schemes.
 
     public Set<StringgetSchemes() {
         return ;
     }
    
    
Appends schemes argument that contains set of allowed schemes.

Parameters:
context Validation context.
arguments Arguments to be substituted into the error message template.
Returns:
true.
    @Override
    public boolean appendMessageArguments(ValidationContext<?> contextMap<StringObjectarguments) {
        super.appendMessageArguments(contextarguments);
        if (.size() > 0)
            arguments.put("schemes");
        return true;
    }
    
    
Checks whether the specified string is valid URI reference with allowed scheme.

Parameters:
value URI string.
context Validation context.
Returns:
true if the specified string is valid URI reference with allowed scheme; false otherwise.
    @Override
    protected <T> boolean check(String valueValidationContext<T> context) {
        if (value == null)
            return true;
        try {
            URI uri = new URI(value);
            String scheme = uri.getScheme();
            return .isEmpty() || scheme != null && .contains(scheme.toLowerCase());
        } catch (URISyntaxException e) {}
        return false;
    }
    
New to GrepCode? Check out our FAQ X