/* * Created on Feb 13, 2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package webraveler.ravel; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Hashtable; /** * @author schmitzc * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class RavelerSetup { /** * Sets up the banner, virtual urls, users, and plugins for Raveler * * @param ravel */ public static void setupRaveler(Raveler ravel) { initializeBanner(ravel); } //setupRaveler(Raveler) /** * Sets up the Web Raveler banner * * @param ravel */ public static void initializeBanner(Raveler ravel) { initializeBannerCSS(ravel); initializeBannerHTML(ravel); initializeBannerScript(ravel); initializeBannerImages(ravel); } //initializeBanner(Raveler) /** * Sets up the style sheet for the Web Raveler banner * * @param ravel */ public static void initializeBannerCSS(Raveler ravel) { // Check if the file bannerCSS.dat exists, otherwise create a new one and // read each line from text file and add to the bannerCSS string System.out.println("RavelerSetup: Attempting to load bannerCSS.dat"); try { // Load bannerCSS string from bannerCSS.dat if it exists FileInputStream bannerCSSFileIn = new FileInputStream("bannerCSS.dat"); ObjectInputStream objInput = new ObjectInputStream(bannerCSSFileIn); ravel.setBannerCSS((String) objInput.readObject()); objInput.close(); } catch(FileNotFoundException fne1) { System.out.println("RavelerSetup: No bannerCSS.dat found. . . Attempting to create one."); try { // Open bannerCSS.txt file FileInputStream bannerCSSTextFileIn = new FileInputStream(Raveler.WEBRAVELER_HOME + "/data/bannerCSS.txt"); InputStreamReader fileInReader = new InputStreamReader(bannerCSSTextFileIn); BufferedReader buf = new BufferedReader(fileInReader); String ln = new String(); // Read in each line of the file and add it to the bannerCSS string while ((ln = buf.readLine()) != null) ravel.setBannerCSS(ravel.getBannerCSS() + ln + "\n"); // Create bannerCSS.dat file FileOutputStream outFileStream = new FileOutputStream(Raveler.WEBRAVELER_HOME + "/data/bannerCSS.dat"); ObjectOutputStream objOut = new ObjectOutputStream(outFileStream); objOut.writeObject(ravel.getBannerCSS()); objOut.flush(); objOut.close(); System.out.println("RavelerSetup: Successfully created bannerCSS.dat"); } catch (FileNotFoundException fnfe2) { System.out.println("RavelerSetup: No bannerCSS.txt file found."); fnfe2.printStackTrace(); } catch (IOException ioe1) { System.out.println("RavelerSetup: Problem encountered while writing bannerCSS.dat. Exiting."); ioe1.printStackTrace(); System.exit(1); } } catch (IOException ioe2) { System.out.println("RavelerSetup: Problem encountered while reading bannerCSS.dat. Exiting."); ioe2.printStackTrace(); System.exit(1); } catch (ClassNotFoundException cnfe) { System.out.println("RavelerSetup: Problem encountered while reading bannerCSS.dat. Exiting."); cnfe.printStackTrace(); System.exit(1); } } //initializeBannerCSS(Raveler) /** * Sets up the HTML for the Web Raveler Banner * * @param ravel */ public static void initializeBannerHTML(Raveler ravel) { ravel.bannerHTML = new String(""); // Check if the file bannerHTML.dat exists, otherwise create a new one and // read each line from text file and add to the bannerHTML string System.out.println("RavelerSetup: Attempting to load bannerHTML.dat"); try { // Attempt to load the bannerHTML string if bannerHTML.dat exists FileInputStream bannerHTMLFileIn = new FileInputStream(Raveler.WEBRAVELER_HOME + "/data/bannerHTML.dat"); ObjectInputStream objInput = new ObjectInputStream(bannerHTMLFileIn); ravel.bannerHTML = (String) objInput.readObject(); objInput.close(); } catch(FileNotFoundException fne) { System.out.println("RavelerSetup: No bannerHTML.dat found. . . Attempting to create one."); try { System.out.println("RavelerSetup: Attempting to load bannerHTML.txt"); // Open bannerHTML.txt file FileInputStream bannerHTMLTextFileIn = new FileInputStream(Raveler.WEBRAVELER_HOME + "/data/bannerHTML.txt"); InputStreamReader fileInReader = new InputStreamReader(bannerHTMLTextFileIn); BufferedReader buf = new BufferedReader(fileInReader); String ln = new String(); // Read in each line of the file and add it to the bannerHTML string while ((ln = buf.readLine()) != null) ravel.bannerHTML += ln + "\n"; // Create bannerScript.dat file FileOutputStream outFileStream = new FileOutputStream(Raveler.WEBRAVELER_HOME + "/data/bannerHTML.dat"); ObjectOutputStream objOut = new ObjectOutputStream(outFileStream); objOut.writeObject(ravel.bannerHTML); objOut.flush(); objOut.close(); System.out.println("RavelerSetup: Successfully created bannerHTML.dat"); } catch (FileNotFoundException fnfe2) { System.out.println("RavelerSetup: No bannerHTML.txt file found."); fnfe2.printStackTrace(); System.exit(1); } catch (IOException ioe1) { System.out.println("RavelerSetup: Problem encountered while writing bannerHTML.dat. Exiting."); ioe1.printStackTrace(); System.exit(1); } } catch (IOException ioe2) { System.out.println("RavelerSetup: Problem encountered while reading bannerHTML.dat. Exiting."); ioe2.printStackTrace(); System.exit(1); } catch (ClassNotFoundException cnfe) { System.out.println("RavelerSetup: Problem encountered while reading bannerHTML.dat. Exiting."); cnfe.printStackTrace(); System.exit(1); } } //initializeBannerHTML(Raveler) /** * Sets up the JavaScript for the Web Raveler banner * * @param ravel */ public static void initializeBannerScript(Raveler ravel) { ravel.bannerScript = new String(""); // Check if the file ravel.bannerScript.dat exists, otherwise create a new one and // read each line from text file and add to the bannerScript string System.out.println("RavelerSetup: Attempting to load bannerScript.dat"); try { // Attempt to load bannerScript string from bannerScript.dat if it exists FileInputStream bannerScriptFileIn = new FileInputStream(Raveler.WEBRAVELER_HOME + "/data/bannerScript.dat"); ObjectInputStream objInput = new ObjectInputStream(bannerScriptFileIn); ravel.bannerScript = (String) objInput.readObject(); objInput.close(); } catch (FileNotFoundException fnfe1) { System.out.println("RavelerSetup: No bannerScript.dat found. . . Attempting to create one."); try { System.out.println("RavelerSetup: Attempting to load bannerScript.txt"); // Open bannerScript.txt file FileInputStream bannerScriptTextFileIn = new FileInputStream(Raveler.WEBRAVELER_HOME + "/data/bannerScript.txt"); InputStreamReader fileInReader = new InputStreamReader(bannerScriptTextFileIn); BufferedReader buf = new BufferedReader(fileInReader); String ln = new String(); // Read in each line of the file and add it to the bannerScript string while ((ln = buf.readLine()) != null) ravel.bannerScript += ln + "\n"; // Create ravel.bannerScript.dat file FileOutputStream outFileStream = new FileOutputStream(Raveler.WEBRAVELER_HOME + "/data/bannerScript.dat"); ObjectOutputStream objOut = new ObjectOutputStream(outFileStream); objOut.writeObject(ravel.bannerScript); objOut.flush(); objOut.close(); System.out.println("RavelerSetup: Successfully created bannerScript.dat"); } catch (FileNotFoundException fnfe2) { System.out.println("RavelerSetup: No bannerScript.txt file found."); fnfe2.printStackTrace(); } catch (IOException ioe1) { System.out.println("RavelerSetup: Problem encountered while writing bannerScript.dat. Exiting."); ioe1.printStackTrace(); System.exit(1); } } catch (IOException ioe2) { System.out.println("RavelerSetup: Problem encountered while reading bannerScript.dat. Exiting."); ioe2.printStackTrace(); System.exit(1); } catch (ClassNotFoundException cnfe) { System.out.println("RavelerSetup: Problem encountered while reading bannerScript.dat. Exiting."); cnfe.printStackTrace(); System.exit(1); } } //initializeBannerScript(Raveler) /** * Sets up the images for the Web Raveler banner * * @param ravel */ public static void initializeBannerImages(Raveler ravel) { // Check if the file images.dat exists, otherwise create a new one and add // raveler banner images, then, read the images from file and create a hash System.out.println("RavelerSetup: Attempting to load images.dat"); FileInputStream imageFileIn = null; FileOutputStream imageFileOut = null; try { imageFileIn = new FileInputStream(Raveler.WEBRAVELER_HOME + "/data/images.dat"); ObjectInputStream objInput = new ObjectInputStream(imageFileIn); ravel.bannerImages = (Hashtable) objInput.readObject(); objInput.close(); } catch(FileNotFoundException fnfe1) { try { System.out.println("RavelerSetup: No images.dat found. . . Attempting to create one."); imageFileOut = new FileOutputStream(Raveler.WEBRAVELER_HOME + "/data/images.dat"); /* Put the images in a hash */ ravel.bannerImages = new Hashtable(); byte[] imageBytes = null; try { System.out.println("RavelerSetup: Creating images directory"); File imageDir = new File(Raveler.WEBRAVELER_HOME + "/data/images"); String imageFileList[] = imageDir.list(); String fileName; if (imageFileList == null) return; for ( int i=0,j=imageFileList.length; i < j; i++ ) { fileName = imageFileList[i]; if (fileName.endsWith(".gif")) { System.out.println("RavelerSetup: Found an image "+fileName); File image = new File(Raveler.WEBRAVELER_HOME + "/data/images/"+fileName); FileInputStream imageIn = new FileInputStream(image); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); int input; try { while( (input = imageIn.read()) != -1 ) { byteStream.write(input); /* write the input byte to the stream */ } imageBytes = byteStream.toByteArray(); ravel.bannerImages.put(fileName,imageBytes); imageBytes = null; } catch (Exception ex) { ex.printStackTrace(); imageBytes = null; } } //if } //for ObjectOutputStream objOut = new ObjectOutputStream(imageFileOut); objOut.writeObject(ravel.bannerImages); objOut.flush(); objOut.close(); System.out.println("RavelerSetup: Successfully created images.dat"); } catch (FileNotFoundException fnfe2) { fnfe2.printStackTrace(); } } catch(IOException ioe1) { System.out.println("RavelerSetup: Could neither open nor create images.dat in your WebRaveler2 directory. Exiting."); ioe1.printStackTrace(); System.exit(1); } } catch (IOException ioe2) { System.out.println("RavelerSetup: Problem encountered while reading images.dat. Exiting."); ioe2.printStackTrace(); System.exit(1); } catch (ClassNotFoundException cnfe) { System.out.println("RavelerSetup: Problem encountered while reading images.dat. Exiting."); cnfe.printStackTrace(); System.exit(1); } } //initializeBannerImages(Raveler) } //class RavelerSetup