package com.marcoa.utility; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.FilenameFilter; import java.util.Vector; import java.util.*; public class BannerChooser { @SuppressWarnings("unchecked") /** * Returns a random banner file name */ public static String getImg(String directory) { //Create Random object Random generator = new Random(); //Create an array to hold image file names String[] bannerFiles = getFiles(directory); //Get a random index number int randomIndex = generator.nextInt(bannerFiles.length); //Return the element at the random index return bannerFiles[randomIndex]; } /** * Returns the html path for a random banner or filler banner */ public static String imagePath() { String imgFile = ""; String path = ""; boolean bannerDir = hasFiles(); //check that there are banner files in the directory if( bannerDir ) { //if there are then get a random image from BANNER_LOC imgFile = getImg(MagicNumber.BANNER_LOC); //append the path to banner location path = "/ads/dog-jpg/banners/" + imgFile; } else { //if there are no images in the directory //get a random image from the filler banner directory imgFile = getImg(MagicNumber.FILLERBANNER_LOC); //append the path to the filler banners path = "/images/web-ads/banners/" + imgFile; } return path; } /** * Returns the correct html link path for the banner ad * returned by imagePath() */ public static String linkPath(String imgPath) { String path = ""; String imgFile = ""; String contractNum = ""; StringTokenizer st = new StringTokenizer(imgPath, "/"); int tokenCount = st.countTokens(); String[] tokens = new String[tokenCount]; int i = 0; while( st.hasMoreTokens() ) { tokens[i] = st.nextToken(); i++; } //Get the file name from imgPath imgFile = tokens[ tokenCount-1 ]; boolean bannerDir = hasFiles(); //If there are paid banners then the imgFile is a contract number if( bannerDir ) { //Get rid of extention, keep only the contract number contractNum = imgFile.substring(0, (imgFile.length() - 4) ); //Append contract number to DisplayBannerAd.do path = "DisplayBannerAd.do?id=" + contractNum; } else { //Else imgFile is the file name of a filler ad //Append file name to /images/web-ads/fullAds/ path = "/images/web-ads/fullAds/" + imgFile; } return path; } /** * Checks to make sure there are files in the banner directory */ public static boolean hasFiles() { String[] bannerFiles = getFiles(MagicNumber.BANNER_LOC); if ( bannerFiles.length != 0 ) { return true; } else { return false; } } /** * Takes in a directory and looks for *.jpg files and returns a list of * them. * * @param directory * The directory to be looked through for files. * @return An array of filenames as Strings. */ public static String[] getFiles(String directory) { /* Open the directory */ File dir = new File(directory); /* get a list of all filenames that are children of the above directory */ String[] children = dir.list(); // It is also possible to filter the list of returned files. // This example only returns files that end with '.jpg'. FilenameFilter filter = new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".jpg"); } }; children = dir.list(filter); // The list of files can also be retrieved as File objects (Commented // out at the moment, because it isn't needed) /* File[] files = dir.listFiles(filter); */ return children; } }