Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.devbliss.doctest.utils;
  
  import java.io.File;
  import java.io.FileWriter;
  import java.io.Writer;
 import java.util.List;
 
Defines some general methods to write a file and defines its name. Each com.devbliss.doctest.renderer.ReportRenderer must use this class to write the file and build its correct name.

Author(s):
bmary
 
 public class FileHelper {

    
By convention we are using the maven project structure. Therefore doctest will be written into ./target/doctests/.
 
     public static final String OUTPUT_DIRECTORY = new File("").getAbsolutePath()
             + "/target/site/doctests/";

    
This writes out the file and retries if some other task has locked the file. This could cause a StackOverflowException, but I cannot think of any real case where this happens...

Parameters:
nameOfFile
Throws:
InvalidReportException
 
     public void writeFile(String nameCompletePathString finalDoc) {
         // make sure the directory exists
         new File(nameCompletePath).getParentFile().mkdirs();
         writeOutFile(nameCompletePathfinalDoc);
     }
 
     private void writeOutFile(String nameOfFileString content) {
         Writer fw = null;
         if (content != null) {
             try {
                 fw = new FileWriter(nameOfFile);
                 fw.write(content);
             } catch (IOException e) {
                 try {
                     Thread.sleep(200);
                     writeOutFile(nameOfFilecontent);
                 } catch (InterruptedException err2) {
                     writeOutFile(nameOfFilecontent);
                 }
             } finally {
                 closeFileWriter(fw);
             }
         }
     }
 
     private void closeFileWriter(Writer fw) {
         if (fw != null) {
             try {
                 fw.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
 
     public String getCompleteFileName(String nameString extension) {
         return  + name + extension;
     }
 
     public String readFile(File fileToUploadthrows IOException {
         FileInputStream stream = new FileInputStream(fileToUpload);
         try {
             FileChannel fc = stream.getChannel();
             MappedByteBuffer bb = fc.map(.., 0, fc.size());
             /* Instead of using default, pass in a decoder. */
             String body = Charset.forName("UTF-8").decode(bb).toString();
             return body.replaceAll("\n""<br/>");
         } finally {
             stream.close();
         }
     }
 
     public List<LinkDocItemgetListOfFile(String fileName) {
         File[] files = fetchFilesInDirectory(fileName);
         List<LinkDocItemlist = new ArrayList<LinkDocItem>();
        // fetch neither the file itself nor the hidden files
        for (File file : files) {
            if (!file.getPath().equals(fileName)) {
                if (!file.isHidden()) {
                    list.add(new LinkDocItem(file.getName(), file.getName()));
                }
            }
        }
        return list;
    }

    
Fetch all the files of the doctests directory. Each file corresponds to a test case.

Parameters:
indexFileWithCompletePath
Returns:
    private File[] fetchFilesInDirectory(String indexFileWithCompletePath) {
        return new File(indexFileWithCompletePath).getParentFile().listFiles();
    }

    
Returns true if the name of the file already exists its directory

Parameters:
fileName
Returns:
    private boolean isFileNameAlreadyTaken(String fileName) {
        String completeName =  + fileName;
        File[] files = fetchFilesInDirectory(completeName);
        if (files != null) {
            for (File file : files) {
                if (completeName.equals(file.getPath())) {
                    return true;
                }
            }
        }
        return false;
    }
    public void validateFileName(String fileNamethrows AssertionError {
        if (fileName == null) {
            throw new AssertionError("The file name can not be null.");
        }
        if (isFileNameAlreadyTaken(fileName)) {
            throw new AssertionError("The file name " + fileName
                    + " already exists. Please choose a new one!");
        }
    }
New to GrepCode? Check out our FAQ X