|
| 1 | +package org.audit4j.core.handler.file; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.Calendar; |
| 5 | +import java.util.Date; |
| 6 | + |
| 7 | +import org.audit4j.core.AuditUtil; |
| 8 | +import org.audit4j.core.CoreConstants; |
| 9 | +import org.audit4j.core.TroubleshootManager; |
| 10 | + |
| 11 | +public final class FileHandlerUtil { |
| 12 | + private FileHandlerUtil() { |
| 13 | + } |
| 14 | + |
| 15 | + /** |
| 16 | + * Generate output file path. |
| 17 | + * |
| 18 | + * @return the string |
| 19 | + */ |
| 20 | + @Deprecated |
| 21 | + public static String generateOutputFilePath(String path) { |
| 22 | + String tempPath = separatorsToSystem(path); |
| 23 | + tempPath = tempPath + File.separatorChar + generateAuditFileName(); |
| 24 | + return tempPath; |
| 25 | + } |
| 26 | + /** |
| 27 | + * Generate output file path. |
| 28 | + * |
| 29 | + * @return the string |
| 30 | + */ |
| 31 | + public static String generateOutputFilePath(String path, String fileName) { |
| 32 | + String tempPath = separatorsToSystem(path); |
| 33 | + tempPath = tempPath + File.separatorChar + fileName; |
| 34 | + return tempPath; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Generate file name. |
| 39 | + * |
| 40 | + * @return the string |
| 41 | + */ |
| 42 | + public static String generatePreviousPreviousFileName() { |
| 43 | + StringBuffer name = new StringBuffer(); |
| 44 | + name.append("Audit_Log-").append(AuditUtil.dateToString(new Date(), "yyyy-MM-dd")) |
| 45 | + .append(CoreConstants.AUDIT_EXTENTION); |
| 46 | + return name.toString(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Generate file name. |
| 51 | + * |
| 52 | + * @return the string |
| 53 | + */ |
| 54 | + public static String generateAuditFileName() { |
| 55 | + StringBuffer name = new StringBuffer(); |
| 56 | + name.append("Audit_Log-").append(AuditUtil.dateToString(new Date(), "yyyy-MM-dd")) |
| 57 | + .append(CoreConstants.AUDIT_EXTENTION); |
| 58 | + return name.toString(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Generate file name. |
| 63 | + * |
| 64 | + * @return the string |
| 65 | + */ |
| 66 | + public static String generateAuditArchiveFileName(Date date) { |
| 67 | + StringBuffer name = new StringBuffer(); |
| 68 | + name.append("Audit_Log-").append(AuditUtil.dateToString(date, "yyyy-MM-dd")) |
| 69 | + .append(".auditarchive"); |
| 70 | + return name.toString(); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + /** |
| 75 | + * Generate file name. |
| 76 | + * |
| 77 | + * @return the string |
| 78 | + */ |
| 79 | + public static String generateAuditFileName(Date date) { |
| 80 | + StringBuffer name = new StringBuffer(); |
| 81 | + name.append("Audit_Log-").append(AuditUtil.dateToString(date, "yyyy-MM-dd")) |
| 82 | + .append(CoreConstants.AUDIT_EXTENTION); |
| 83 | + return name.toString(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Generate file name. |
| 88 | + * |
| 89 | + * @return the string |
| 90 | + */ |
| 91 | + public static String generateCommonFileName(Date date) { |
| 92 | + StringBuffer name = new StringBuffer(); |
| 93 | + name.append("Audit_Log-").append(AuditUtil.dateToString(date, "yyyy-MM-dd")); |
| 94 | + return name.toString(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Converts all separators to the Unix separator of forward slash. |
| 99 | + * |
| 100 | + * @param path |
| 101 | + * the path to be changed, null ignored |
| 102 | + * @return the updated path |
| 103 | + */ |
| 104 | + public static String separatorsToUnix(String path) { |
| 105 | + if (path == null || path.indexOf(CoreConstants.WINDOWS_SEPARATOR) == -1) { |
| 106 | + return path; |
| 107 | + } |
| 108 | + return path.replace(CoreConstants.WINDOWS_SEPARATOR, CoreConstants.UNIX_SEPARATOR); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Converts all separators to the Windows separator of backslash. |
| 113 | + * |
| 114 | + * @param path |
| 115 | + * the path to be changed, null ignored |
| 116 | + * @return the updated path |
| 117 | + */ |
| 118 | + public static String separatorsToWindows(String path) { |
| 119 | + if (path == null || path.indexOf(CoreConstants.UNIX_SEPARATOR) == -1) { |
| 120 | + return path; |
| 121 | + } |
| 122 | + return path.replace(CoreConstants.UNIX_SEPARATOR, CoreConstants.WINDOWS_SEPARATOR); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Converts all separators to the system separator. |
| 127 | + * |
| 128 | + * @param path |
| 129 | + * the path to be changed, null ignored |
| 130 | + * @return the updated path |
| 131 | + */ |
| 132 | + public static String separatorsToSystem(String path) { |
| 133 | + if (path == null) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + if (TroubleshootManager.isWindows()) { |
| 137 | + return separatorsToWindows(path); |
| 138 | + } else { |
| 139 | + return separatorsToUnix(path); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Checks if is file already exists. |
| 145 | + * |
| 146 | + * @param path |
| 147 | + * the path |
| 148 | + * @return true, if is file already exists |
| 149 | + */ |
| 150 | + public static boolean isFileAlreadyExists(String path) { |
| 151 | + File file = new File(path); |
| 152 | + if (file.exists()) { |
| 153 | + return true; |
| 154 | + } else { |
| 155 | + return false; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Add days. |
| 161 | + * |
| 162 | + * @param date |
| 163 | + * the date |
| 164 | + * @param different |
| 165 | + * the different |
| 166 | + * @param unit |
| 167 | + * the unit |
| 168 | + * @return the date |
| 169 | + */ |
| 170 | + public static Date addDate(final Date date, final Integer different) { |
| 171 | + final Calendar cal = Calendar.getInstance(); |
| 172 | + cal.setTime(date); |
| 173 | + cal.add(Calendar.DATE, different); |
| 174 | + return cal.getTime(); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Substract days. |
| 179 | + * |
| 180 | + * @param date |
| 181 | + * the date |
| 182 | + * @param different |
| 183 | + * the different |
| 184 | + * @param unit |
| 185 | + * the unit |
| 186 | + * @return the date |
| 187 | + */ |
| 188 | + public static Date substractDate(final Date date, final Integer different) { |
| 189 | + |
| 190 | + return addDate(date, -different); |
| 191 | + } |
| 192 | +} |
0 commit comments