private String createFileName ( final String title , final String contentType ) { if ( handle == null ) { throw new IllegalArgumentException ( "weblog handle cannot be null" ) ; } if ( contentType == null ) { throw new IllegalArgumentException ( "contentType cannot be null" ) ; } String fileName = null ; final SimpleDateFormat sdf = new SimpleDateFormat ( ) ; sdf . applyPattern ( "yyyyMMddHHssSSS" ) ; // Determine the extension based on the contentType. This is a hack. // The info we need to map from contentType to file extension is in // JRE/lib/content-type.properties, but Java Activation doesn't provide // a way to do a reverse mapping or to get at the data. final String [ ] typeTokens = contentType . split ( "/" ) ; final String ext = typeTokens [ 1 ] ; if ( title != null && ! title . trim ( ) . equals ( "" ) ) { // We've got a title, so use it to build file name final String base = Utilities . replaceNonAlphanumeric ( title , ' ' ) ; final StringTokenizer toker = new StringTokenizer ( base ) ; String tmp = null ; int count = 0 ; while ( toker . hasMoreTokens ( ) && count < 5 ) { String s = toker . nextToken ( ) ; s = s . toLowerCase ( ) ; tmp = tmp == null ? s : tmp + "_" + s ; count ++ ; } fileName = tmp + "-" + sdf . format ( new Date ( ) ) + "." + ext ; } else { // No title or text, so instead we'll use the item's date // in YYYYMMDD format to form the file name fileName = handle + "-" + sdf . format ( new Date ( ) ) + "." + ext ; } return fileName ; }
Creates a file name for a file based on a weblog handle title string and a content - type .