PathFormat.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.baidu.ueditor;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. public class PathFormat {
  7. private static final String TIME = "time";
  8. private static final String FULL_YEAR = "yyyy";
  9. private static final String YEAR = "yy";
  10. private static final String MONTH = "mm";
  11. private static final String DAY = "dd";
  12. private static final String HOUR = "hh";
  13. private static final String MINUTE = "ii";
  14. private static final String SECOND = "ss";
  15. private static final String RAND = "rand";
  16. private static Date currentDate = null;
  17. public static String parse ( String input ) {
  18. Pattern pattern = Pattern.compile( "\\{([^\\}]+)\\}", Pattern.CASE_INSENSITIVE );
  19. Matcher matcher = pattern.matcher(input);
  20. PathFormat.currentDate = new Date();
  21. StringBuffer sb = new StringBuffer();
  22. while ( matcher.find() ) {
  23. matcher.appendReplacement(sb, PathFormat.getString( matcher.group( 1 ) ) );
  24. }
  25. matcher.appendTail(sb);
  26. return sb.toString();
  27. }
  28. /**
  29. * 格式化路径, 把windows路径替换成标准路径
  30. * @param input 待格式化的路径
  31. * @return 格式化后的路径
  32. */
  33. public static String format ( String input ) {
  34. return input.replace( "\\", "/" );
  35. }
  36. public static String parse ( String input, String filename ) {
  37. Pattern pattern = Pattern.compile( "\\{([^\\}]+)\\}", Pattern.CASE_INSENSITIVE );
  38. Matcher matcher = pattern.matcher(input);
  39. String matchStr = null;
  40. PathFormat.currentDate = new Date();
  41. StringBuffer sb = new StringBuffer();
  42. while ( matcher.find() ) {
  43. matchStr = matcher.group( 1 );
  44. if ( matchStr.indexOf( "filename" ) != -1 ) {
  45. filename = filename.replace( "$", "\\$" ).replaceAll( "[\\/:*?\"<>|]", "" );
  46. matcher.appendReplacement(sb, filename );
  47. } else {
  48. matcher.appendReplacement(sb, PathFormat.getString( matchStr ) );
  49. }
  50. }
  51. matcher.appendTail(sb);
  52. return sb.toString();
  53. }
  54. private static String getString ( String pattern ) {
  55. pattern = pattern.toLowerCase();
  56. // time 处理
  57. if ( pattern.indexOf( PathFormat.TIME ) != -1 ) {
  58. return PathFormat.getTimestamp();
  59. } else if ( pattern.indexOf( PathFormat.FULL_YEAR ) != -1 ) {
  60. return PathFormat.getFullYear();
  61. } else if ( pattern.indexOf( PathFormat.YEAR ) != -1 ) {
  62. return PathFormat.getYear();
  63. } else if ( pattern.indexOf( PathFormat.MONTH ) != -1 ) {
  64. return PathFormat.getMonth();
  65. } else if ( pattern.indexOf( PathFormat.DAY ) != -1 ) {
  66. return PathFormat.getDay();
  67. } else if ( pattern.indexOf( PathFormat.HOUR ) != -1 ) {
  68. return PathFormat.getHour();
  69. } else if ( pattern.indexOf( PathFormat.MINUTE ) != -1 ) {
  70. return PathFormat.getMinute();
  71. } else if ( pattern.indexOf( PathFormat.SECOND ) != -1 ) {
  72. return PathFormat.getSecond();
  73. } else if ( pattern.indexOf( PathFormat.RAND ) != -1 ) {
  74. return PathFormat.getRandom( pattern );
  75. }
  76. return pattern;
  77. }
  78. private static String getTimestamp () {
  79. return System.currentTimeMillis() + "";
  80. }
  81. private static String getFullYear () {
  82. return new SimpleDateFormat( "yyyy" ).format( PathFormat.currentDate );
  83. }
  84. private static String getYear () {
  85. return new SimpleDateFormat( "yy" ).format( PathFormat.currentDate );
  86. }
  87. private static String getMonth () {
  88. return new SimpleDateFormat( "MM" ).format( PathFormat.currentDate );
  89. }
  90. private static String getDay () {
  91. return new SimpleDateFormat( "dd" ).format( PathFormat.currentDate );
  92. }
  93. private static String getHour () {
  94. return new SimpleDateFormat( "HH" ).format( PathFormat.currentDate );
  95. }
  96. private static String getMinute () {
  97. return new SimpleDateFormat( "mm" ).format( PathFormat.currentDate );
  98. }
  99. private static String getSecond () {
  100. return new SimpleDateFormat( "ss" ).format( PathFormat.currentDate );
  101. }
  102. private static String getRandom ( String pattern ) {
  103. int length = 0;
  104. pattern = pattern.split( ":" )[ 1 ].trim();
  105. length = Integer.parseInt( pattern );
  106. return ( Math.random() + "" ).replace( ".", "" ).substring( 0, length );
  107. }
  108. public static void main(String[] args) {
  109. // TODO Auto-generated method stub
  110. }
  111. }