Coverage Report - org.mule.providers.file.SimpleFilenameParser
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleFilenameParser
90%
9/10
75%
3/4
7
SimpleFilenameParser$1
94%
16/17
93%
13/14
7
 
 1  
 /*
 2  
  * $Id: SimpleFilenameParser.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 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  
 /**
 21  
  * <code>SimpleFilenameParser</code> understands a limited set of tokens, namely
 22  
  * <ul>
 23  
  * <li>${DATE} : the currrent date in the format dd-MM-yy_HH-mm-ss.SS</li>
 24  
  * <li>${DATE:yy-MM-dd} : the current date using the specified format</li>
 25  
  * <li>${SYSTIME} : The current system time milliseconds</li>
 26  
  * <li>${UUID} : A generated Universally unique id</li>
 27  
  * <li>${ORIGINALNAME} : The origial file name if the file being written was read
 28  
  * from another location</li>
 29  
  * <li>${COUNT} : An incremental counter</li>
 30  
  * <li>${<Message Property Name>} : A name of a property on the message</li>
 31  
  * </ul>
 32  
  * Note that square brackets can be used instead of curl brackets, this is useful
 33  
  * when defining the file output pattern in a Mule Url endpointUri where the curl
 34  
  * bracket is an invalid character.
 35  
  */
 36  
 
 37  60
 public class SimpleFilenameParser implements FilenameParser
 38  
 {
 39  
     public static final String DEFAULT_DATE_FORMAT = "dd-MM-yy_HH-mm-ss.SSS";
 40  
 
 41  52
     private final TemplateParser antParser = TemplateParser.createAntStyleParser();
 42  52
     private final TemplateParser squareParser = TemplateParser.createSquareBracesStyleParser();
 43  
 
 44  52
     private final AtomicLong count = new AtomicLong(0);
 45  
 
 46  
     public String getFilename(UMOMessageAdapter adapter, String pattern)
 47  
     {
 48  36
         if (pattern == null)
 49  
         {
 50  0
             return UUID.getUUID() + ".dat";
 51  
         }
 52  
         else
 53  
         {
 54  36
             if (pattern.indexOf('{') > -1)
 55  
             {
 56  18
                 return getFilename(adapter, pattern, antParser);
 57  
             }
 58  
             else
 59  
             {
 60  18
                 return getFilename(adapter, pattern, squareParser);
 61  
             }
 62  
         }
 63  
     }
 64  
 
 65  
     protected String getFilename(final UMOMessageAdapter adapter, String pattern, TemplateParser parser)
 66  
     {
 67  36
         return parser.parse(new TemplateParser.TemplateCallback()
 68  
         {
 69  36
             public Object match(String token)
 70  
             {
 71  36
                 if (token.equals("DATE"))
 72  
                 {
 73  4
                     return DateUtils.getTimeStamp(DEFAULT_DATE_FORMAT);
 74  
                 }
 75  32
                 else if (token.startsWith("DATE:"))
 76  
                 {
 77  4
                     token = token.substring(5);
 78  4
                     return DateUtils.getTimeStamp(token);
 79  
                 }
 80  28
                 else if (token.startsWith("UUID"))
 81  
                 {
 82  4
                     return UUID.getUUID();
 83  
                 }
 84  24
                 else if (token.startsWith("SYSTIME"))
 85  
                 {
 86  4
                     return String.valueOf(System.currentTimeMillis());
 87  
                 }
 88  20
                 else if (token.startsWith("COUNT"))
 89  
                 {
 90  8
                     return String.valueOf(count.getAndIncrement());
 91  
                 }
 92  12
                 else if (adapter != null)
 93  
                 {
 94  12
                     if (token.startsWith("ORIGINALNAME"))
 95  
                     {
 96  4
                         return adapter.getStringProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, null);
 97  
                     }
 98  
                     else
 99  
                     {
 100  8
                         return adapter.getStringProperty(token, null);
 101  
                     }
 102  
                 }
 103  0
                 return null;
 104  
 
 105  
             }
 106  
         }, pattern);
 107  
     }
 108  
 }