D:\NewZebra\JavaApplications\DesignPatternsProject\src\structural_patterns\pdf_facade_pattern\TextObjectProducer.java
  1 /* 
  2  * TextObjectProducer.java
  3  *
  4  * Disclaimer:
  5  * ZebraSoft.com disclaims all warrantees with regard to documents/programs, including 
  6  * all implied warranties. In no event shall ZebraSoft.com be liable for indirect or 
  7  * consequential damages or any damages whatsoever resulting from loss of use, data or 
  8  * profits arising out of the use of documents/programs.
  9  * 
 10  * All content included on this site, such as text, graphics, logos, button icons, 
 11  * images, audio clips, video clips, digital downloads, programs, data compilations, 
 12  * and software, is the property of ZebraSoft.com or its content suppliers and 
 13  * protected by United States and international copyright laws. 
 14  *
 15  * This site or any portion of this site may not be reproduced, duplicated, 
 16  * copied, sold, resold, visited, or otherwise exploited for any commercial 
 17  * purpose without express written consent of ZebraSoft.com.
 18  */
 19 
 20 package structural_patterns.pdf_facade_pattern;
 21 
 22 /**
 23  *  
 24  * @author Sam Eldin
 25  */
 26 
 27 import java.io.*;
 28 import java.text.*;
 29 import java.util.*;
 30 
 31 public class TextObjectProducer extends TextImageBaseAssembler { 
 32     
 33     TextBaseProduct     textBaseProductHandle   = null;
 34     String              path               = null; 
 35     String              fileName           = null; 
 36     
 37     /** Creates a new instance of TextObjectProducer */
 38     public TextObjectProducer(String passedPath, String passedFileName) {
 39         path               = passedPath; 
 40         fileName           = passedFileName; 
 41         factoryMethod();
 42     }
 43    /*
 44     *
 45     */ 
 46    public String who_am_I(){
 47        return("TextObjectProducer - it creates a Java Object from a Text file");
 48        
 49    }
 50    /*
 51     *
 52     */ 
 53    public void factoryMethod(){
 54         try
 55         {
 56             int         index;
 57             String      textfileName;
 58             String      objectFileName;
 59             
 60             textfileName = path 
 61                                 + PDF_Base.FILE_Separator
 62                                 + fileName; 
 63             textBaseProductHandle = new TextBaseProduct();
 64            //========================================
 65            BufferedReader  bufferedFilePointer = openBufferedReaderFile(textfileName); 
 66            if(null == bufferedFilePointer)
 67                         return;
 68            textBaseProductHandle.linesOfTextVecxtor = new Vector();
 69            String inputLine = bufferedFilePointer.readLine();
 70           // load the vector one line of text at the time
 71            while(inputLine != null)
 72            {
 73                 textBaseProductHandle.linesOfTextVecxtor.addElement(inputLine);
 74                 inputLine = bufferedFilePointer.readLine();
 75            }
 76            bufferedFilePointer.close();
 77            //=====================================
 78            fileName = fileName.toLowerCase(); 
 79            String tempObjectFileName = fileName;
 80            int lastIndex = fileName.lastIndexOf(PDF_Base.TEXT_FILE_EXTENSION); 
 81            if(lastIndex > 0)
 82             tempObjectFileName = fileName.substring(0, lastIndex);
 83            objectFileName = path 
 84                                 + PDF_Base.FILE_Separator
 85                                 + tempObjectFileName 
 86                                 + PDF_Base.OBJECT_FILE_EXTENSION; 
 87            FileOutputStream storageFile4Objects = new FileOutputStream(objectFileName);
 88            // Write object with ObjectOutputStream
 89            ObjectOutputStream objectStorageHandle = new  ObjectOutputStream (storageFile4Objects);
 90            objectStorageHandle.writeObject(textBaseProductHandle); 
 91            objectStorageHandle.flush();
 92            objectStorageHandle.close();
 93         }
 94         catch(IOException eIOException)
 95         {
 96             System.out.println("ProductionFactory eIOException");
 97             System.out.println("eIOException: " + eIOException.getMessage());        
 98         }           
 99     }
100     /*
101      *
102      */
103     public  BufferedReader  openBufferedReaderFile(String passedFileName)
104     {
105       FileReader    fileReaderHandle;
106 
107       try
108       {
109         fileReaderHandle = new FileReader(passedFileName);
110       }
111       catch (FileNotFoundException MyFileNotFoundException)
112       {
113           System.out.println("\n" + passedFileName  + " Can not find input file.");
114           return(null);
115       }
116       BufferedReader bufferedReaderHandle = new BufferedReader(fileReaderHandle);
117       return(bufferedReaderHandle);
118     }    
119     /**
120      *
121      */
122      public static void main (String[] args)
123     {
124 //         ProductionFactory productionFactoryHandle   = new ProductionFactory();
125          TextObjectProducer TextObjectProducerHandle   = new TextObjectProducer(PDF_Base.FOLDER_PATH, "Command.txt");
126          TextObjectProducer TextObjectProducerHandle_2   = new TextObjectProducer(PDF_Base.FOLDER_PATH, "Singleton.txt");
127     }    
128 }
129 
130