Coverage Report - net.fckeditor.tool.UtilsFile
 
Classes in this File Line Coverage Branch Coverage Complexity
UtilsFile
61%
19/31
65%
13/20
3.429
 
 1  
 /*
 2  
  * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 3  
  * Copyright (C) 2003-2008 Frederico Caldeira Knabben
 4  
  * 
 5  
  * == BEGIN LICENSE ==
 6  
  * 
 7  
  * Licensed under the terms of any of the following licenses at your
 8  
  * choice:
 9  
  * 
 10  
  *  - GNU General Public License Version 2 or later (the "GPL")
 11  
  *    http://www.gnu.org/licenses/gpl.html
 12  
  * 
 13  
  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 14  
  *    http://www.gnu.org/licenses/lgpl.html
 15  
  * 
 16  
  *  - Mozilla Public License Version 1.1 or later (the "MPL")
 17  
  *    http://www.mozilla.org/MPL/MPL-1.1.html
 18  
  * 
 19  
  * == END LICENSE ==
 20  
  */
 21  
 package net.fckeditor.tool;
 22  
 
 23  
 import java.io.File;
 24  
 import java.io.InputStream;
 25  
 
 26  
 import org.apache.commons.io.FilenameUtils;
 27  
 import org.devlib.schmidt.imageinfo.ImageInfo;
 28  
 import org.slf4j.Logger;
 29  
 import org.slf4j.LoggerFactory;
 30  
 
 31  
 import net.fckeditor.handlers.ConnectorHandler;
 32  
 
 33  
 /**
 34  
  * Some static helper methods in conjunction with files.
 35  
  *
 36  
  * @version $Id: UtilsFile.java 1690 2008-03-13 13:08:42Z mosipov $
 37  
  */
 38  0
 public class UtilsFile {
 39  
         
 40  2
         private static final Logger logger = LoggerFactory.getLogger(UtilsFile.class);
 41  
 
 42  
         /**
 43  
      * Do a cleanup of the file name to avoid possible problems. <br>
 44  
      * The force single Extension property will be respected!
 45  
      * 
 46  
      * @param fileName
 47  
      * @return folder name where \ / | : ? * &quot; &lt; &gt; 'control chars' replaced by '_'
 48  
      */
 49  
     public static String sanitizeFileName(final String fileName) {
 50  3
             if (fileName == null)
 51  0
                     return null;
 52  3
             if (fileName.equals(""))
 53  0
                     return "";
 54  
     
 55  3
             String name = (ConnectorHandler.isForceSingleExtension()) ? UtilsFile.forceSingleExtension(fileName)
 56  
                     : fileName;
 57  
     
 58  
             // Remove \ / | : ? * " < > with _
 59  3
             return name.replaceAll("\\/|\\/|\\||:|\\?|\\*|\"|<|>|\\p{Cntrl}", "_");
 60  
     }
 61  
 
 62  
         /**
 63  
      * Do a cleanup of the folder name to avoid possible problems.
 64  
      * 
 65  
      * @param folderName
 66  
      * @return folder name where . \ / | : ? * &quot; &lt; &gt; 'control chars' replaced by '_'
 67  
      */
 68  
     public static String sanitizeFolderName(final String folderName) {
 69  2
             if (folderName == null)
 70  0
                     return null;
 71  2
             if (folderName.equals(""))
 72  0
                     return "";
 73  
     
 74  
             // Remove . \ / | : ? * " < > with _
 75  2
             return folderName.replaceAll("\\.|\\/|\\/|\\||:|\\?|\\*|\"|<|>|\\p{Cntrl}", "_");
 76  
     }
 77  
 
 78  
         /**
 79  
          * Checks if the underlying file of the InputStrem is an image.
 80  
          * 
 81  
          * @param in
 82  
          * @return <code>True</code>, if the underlying file is an image, or <code>false</code>.
 83  
          */
 84  
         public static boolean isImage(final InputStream in) {
 85  0
             ImageInfo ii = new ImageInfo();
 86  0
             ii.setInput(in);
 87  0
             return ii.check();
 88  
     }
 89  
 
 90  
         /**
 91  
      * TODO - document me!
 92  
      * 
 93  
      * @param path
 94  
      * @return <code>true</code> if path corresponds to rules or
 95  
      *         <code>false</code>.
 96  
      */
 97  
     public static boolean isValidPath(final String path) {
 98  12
             if (Utils.isEmpty(path))
 99  1
                     return false;
 100  11
             if (!path.startsWith("/"))
 101  1
                     return false;
 102  10
             if (!path.endsWith("/"))
 103  3
                     return false;
 104  
             
 105  7
             if (!path.equals(FilenameUtils.separatorsToUnix(FilenameUtils
 106  
                             .normalize(path))))
 107  5
                     return false;
 108  
             
 109  2
             return true;
 110  
     }
 111  
 
 112  
         /**
 113  
      * TODO - document me!
 114  
      * 
 115  
      * @param filename
 116  
      * @return string with a single dot only
 117  
      */
 118  
     public static String forceSingleExtension(final String filename) {
 119  7
             return filename.replaceAll("\\.(?![^.]+$)", "_");
 120  
     }
 121  
 
 122  
         /**
 123  
      * TODO - document me!
 124  
      * 
 125  
      * @param filename
 126  
      * @return <code>true</code> if filename contains severals dots else
 127  
      *         <code>false</code>
 128  
      */
 129  
     public static boolean isSingleExtension(final String filename) {
 130  5
             return filename.matches("[^\\.]+\\.[^\\.]+");
 131  
     }
 132  
     
 133  
     /**
 134  
          * TODO - document me!
 135  
          * @param dir
 136  
          */
 137  
         public static void checkDirAndCreate(File dir) {
 138  0
                 if (!dir.exists()) {
 139  0
                         dir.mkdirs();
 140  0
                         logger.debug("Dir '{}' successfully created", dir);
 141  
                 }
 142  0
         }
 143  
 
 144  
 }