1 10 11 package org.mule.providers.file; 12 13 import org.mule.umo.provider.UMOMessageAdapter; 14 import org.mule.util.DateUtils; 15 import org.mule.util.TemplateParser; 16 import org.mule.util.UUID; 17 18 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong; 19 20 36 37 public class SimpleFilenameParser implements FilenameParser 38 { 39 public static final String DEFAULT_DATE_FORMAT = "dd-MM-yy_HH-mm-ss.SSS"; 40 41 private final TemplateParser antParser = TemplateParser.createAntStyleParser(); 42 private final TemplateParser squareParser = TemplateParser.createSquareBracesStyleParser(); 43 44 private final AtomicLong count = new AtomicLong(0); 45 46 public String getFilename(UMOMessageAdapter adapter, String pattern) 47 { 48 if (pattern == null) 49 { 50 return UUID.getUUID() + ".dat"; 51 } 52 else 53 { 54 if (pattern.indexOf('{') > -1) 55 { 56 return getFilename(adapter, pattern, antParser); 57 } 58 else 59 { 60 return getFilename(adapter, pattern, squareParser); 61 } 62 } 63 } 64 65 protected String getFilename(final UMOMessageAdapter adapter, String pattern, TemplateParser parser) 66 { 67 return parser.parse(new TemplateParser.TemplateCallback() 68 { 69 public Object match(String token) 70 { 71 if (token.equals("DATE")) 72 { 73 return DateUtils.getTimeStamp(DEFAULT_DATE_FORMAT); 74 } 75 else if (token.startsWith("DATE:")) 76 { 77 token = token.substring(5); 78 return DateUtils.getTimeStamp(token); 79 } 80 else if (token.startsWith("UUID")) 81 { 82 return UUID.getUUID(); 83 } 84 else if (token.startsWith("SYSTIME")) 85 { 86 return String.valueOf(System.currentTimeMillis()); 87 } 88 else if (token.startsWith("COUNT")) 89 { 90 return String.valueOf(count.getAndIncrement()); 91 } 92 else if (adapter != null) 93 { 94 if (token.startsWith("ORIGINALNAME")) 95 { 96 return adapter.getStringProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, null); 97 } 98 else 99 { 100 return adapter.getStringProperty(token, null); 101 } 102 } 103 return null; 104 105 } 106 }, pattern); 107 } 108 } 109 | Popular Tags |