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; } }

😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘‡

Be sure to read, it will change your life!
Show your work by Austin Kleonhttps://amzn.to/34NVmwx

This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practiceshttps://amzn.to/30WQSm2

Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv

This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp

Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy

😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘†