Follow treslines by email clicking Here!

Thursday, March 20, 2014

Normalize file names to conform android's convertions

Hi there!

Today i'm gonna show how to normalize resource file names coming from your designers saving you a lot of time while renaming it. It is more like an util class. It may help you like it helped me. NOTE: I'm using the apache-common-io lib for that.

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
/**
 * Normalizes incorrect file names to conform android's conventions.
 * @author Ricardo Ferreira
 * @version 1.0
 * @since 20/03/2014
 */
public class FileNameNormalizer {

   
    /**
     * Normalizes incorrect file names like "hello kit", "bruno & Mars" or "what-is-that" into "hello_kit", 
     * "bruno_Mars" and "what_is_that". Here an example on how to use it:
     * 

     *  String dir1 = "D:/dir1/";
     *  String dir2 = "D:/dir2/";
     *  String[] fromDirectories = new String[]{dir1,dir2};
     *  String[] withExtensions = new String[]{"gif", "png", "bmp" };
     *  String toDestination = "D:/destination/";
     *  String[] regularExpression =  new String[]{" & "," ","-" };
     *  normalize(fromDirectories, withExtensions, toDestination,regularExpression);
     * 
* @param fromDirectories * @param withExtensions * @param toDestination * @param regularExpression */ public void normalize(String[] fromDirectories, final String[] withExtensions, final String toDestination,String[] regularExpression){//NOPMD for (String fromDirectory : fromDirectories) { normalize(fromDirectory, withExtensions, toDestination,regularExpression); } } private void normalize(String fromDirectory, final String[] withExtensions, final String toDestination, String[] regularExpression){//NOPMD fromDirectory=ensureCorrectDirectory(fromDirectory); String destination = ensureCorrectDirectory(toDestination); File dir = new File(fromDirectory); FilenameFilter filenameFilter = new FilenameFilter() { @Override public boolean accept(final File dir, final String name) { for (final String ext : withExtensions) { if (name.endsWith("." + ext)) { return (true); } } return (false); } }; if (dir.isDirectory()) { for (final File f : dir.listFiles(filenameFilter)) { normalizeAndMoveTo(f.getAbsolutePath(), destination,regularExpression); } } } private String ensureCorrectDirectory(String dir){ if(!dir.endsWith("/")){ dir+="/"; } return dir; } private void normalizeAndMoveTo(String toNomalize, String destination, String[] regularExpression){ File oldFile = new File(toNomalize); File temp = new File(normalizePng(toNomalize,regularExpression)); String newFilePath = destination + temp.getName(); File newFile = new File(newFilePath); try { FileUtils.moveFile(oldFile, newFile); } catch (IOException e) { e.printStackTrace(); } } private String normalizePng(String toNormalize, String[] regularExpression){ String replacement = "_"; String newname = toNormalize; for (String regex : regularExpression) { newname = newname.replaceAll(regex, replacement); } return newname; } }

No comments:

Post a Comment