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.converter;
 
 import java.io.File;
 
This class provides Converter implementation for the java.io.File type.

 
 public final class FileConverter extends AbstractConverter<File> {
    
    
FileConverter default instance.
 
     public static final FileConverter DEFAULT = new FileConverter((Stringnull);
    
    
Parent directory pathname.
 
     private final String directory;
    
    
Constructs a new FileConverter with the specified parent directory pathname.

Parameters:
directory Parent directory pathname.
 
     FileConverter(String directory) {
         this. = directory;
     }
    
    
Constructs a new FileConverter from the specified annotation.

Parameters:
annotation Converter annotation.
 
     FileConverter(ParentDirectory annotation) {
         this(annotation.path());
     }
    
    
Returns java.io.File type.

Returns:
java.io.File type.
 
     @Override
     public Class<FilegetType() {
         return File.class;
     }
    
    
FileConverter has no error message template.

Parameters:
context Validation context.
Returns:
null.
 
     @Override
     public String getMessageTemplate(ValidationContext<?> context) {
         return null;
     }
    
    
Returns java.io.File object for the specified path.

This method simply creates a java.io.File object and is not responsible for verifying the existence of a file.

Parameters:
value File pathname.
context Validation context.
Returns:
java.io.File object for the specified path.
 
     @Override
     protected <T> File doDecode(String valueValidationContext<T> context) {
         return new File(value);
     }
    
    
Returns pathname for the specified java.io.File object.

Parameters:
value java.io.File object to be encoded.
context Validation context.
Returns:
Pathname for the specified java.io.File object.
    @Override
    protected <T> String doEncode(File valueValidationContext<T> context) {
        String path = value.getPath();
        if ( != null) {
            File file = value;
            File parent = new File();
            while (!(file == null || parent.equals(file)))
                file = file.getParentFile();
            if (file != null)
                path = file.equals(value) ? "" : path.substring(parent.getPath().length() + 1);
        }
        return path;
    }
    
New to GrepCode? Check out our FAQ X