| 1 | /** |
| 2 | * BSD-style license; for more info see http://xradar.sourceforge.net/license.html |
| 3 | */ |
| 4 | package org.sourceforge.xradar.util; |
| 5 | |
| 6 | import java.util.logging.Logger; |
| 7 | |
| 8 | import org.sourceforge.xradar.statics.Report; |
| 9 | |
| 10 | /** |
| 11 | * @author Romain PELISSE, belaran@gmail.com |
| 12 | */ |
| 13 | public final class XRadarUtils { |
| 14 | |
| 15 | private static boolean isRunningOnWindows; |
| 16 | |
| 17 | // should not be instantiated |
| 18 | private XRadarUtils(){} |
| 19 | |
| 20 | /** |
| 21 | * On windows, some filename may have both '\' and '/' resulting to some |
| 22 | * bad concatenation. This method fixes the issues. |
| 23 | * |
| 24 | * @param filename |
| 25 | * @return |
| 26 | */ |
| 27 | public static String normalizeFileName(String file) { |
| 28 | Logger logger = Logger.getLogger(XRadarUtils.class.getSimpleName()); |
| 29 | logger.finest("Entering normalize filename with" + file); |
| 30 | XRadarUtils.isRunningOnWindows = XRadarUtils.isRunningOnWindowsOS(); |
| 31 | if ( isRunningOnWindows && file.contains("\\") ) //!&& file.contains("/") ) |
| 32 | { |
| 33 | logger.finest("Normalizing filename:" + file); |
| 34 | // We replace only the '\\' by '/' |
| 35 | file = file.replace('\\', '/'); |
| 36 | // // If this is an absolute path, we remove the drive |
| 37 | // Pattern pattern = Pattern.compile("^[A-Za-z]:.*$"); |
| 38 | // if ( pattern.matcher(file).matches() ) |
| 39 | // { |
| 40 | // file = file.substring(2,file.length()); |
| 41 | // logger.finest("Normalized to:" + file); |
| 42 | // } |
| 43 | // else |
| 44 | // logger.finest("Normalizing failed for:" + file); |
| 45 | // |
| 46 | } |
| 47 | return file; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * <p>Handy method to see if xradar is running on Windows or one real OS.</p> |
| 52 | * |
| 53 | * @return |
| 54 | */ |
| 55 | public static boolean isRunningOnWindowsOS() |
| 56 | { |
| 57 | boolean status = false; |
| 58 | String[] windows = { "Windows 98", "Windows ME", "Windows 2000", "Windows XP", "Windows Vista", "Windows 2003", "Windows 7" }; |
| 59 | |
| 60 | int idWindows; |
| 61 | String os = System.getProperty("os.name"); |
| 62 | if (os != null ) |
| 63 | for ( idWindows = 0; idWindows < windows.length; idWindows++ ) |
| 64 | if ( os.startsWith(windows[idWindows]) ) |
| 65 | status = true; |
| 66 | |
| 67 | return status; |
| 68 | } |
| 69 | |
| 70 | public static String removeDiskNameIfRunningOnWindows(String filename) { |
| 71 | String result = filename; |
| 72 | if ( XRadarUtils.isRunningOnWindowsOS()) { |
| 73 | result = filename.replaceFirst("^[A-Za-z]:", ""); |
| 74 | } |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | } |