Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/cybertivity/powerjournal/ExportAgent.java


1   package com.cybertivity.powerjournal;
2   import java.io.BufferedReader;
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileNotFoundException;
6   import java.io.FileOutputStream;
7   import java.io.FileReader;
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.util.ArrayList;
11  import java.util.HashMap;
12  import javax.swing.JFileChooser;
13  import javax.swing.JFrame;
14  import com.cybertivity.powerjournal.database.DBException;
15  import com.cybertivity.powerjournal.framework.*;
16  
17  /**
18   * Title:        PowerJournal
19   * Description:  $Id: ExportAgent.java,v 1.2 2001/12/26 22:39:44 arrowood Exp $
20   * Copyright:    Copyright (c) 2001
21   * Company:      <A HREF="http://www.cybertivity.com">Cybertivity</A>
22   *
23   * @author <A HREF="mailto:chris.arrowood@cybertivity.com">Chris Arrowood</A>
24   * @created December 16, 2001
25   * @version 1.0
26   */
27  
28  public class ExportAgent {
29  
30    public final static String HTML_PATH = "/com/cybertivity/powerjournal/resources/html";
31    private File indexTemplate = null;
32    private File entryTemplate = null;
33  //  private File allEntriesTemplate = null;
34    private File linkTableTemplate = null;
35    public final static String FILE_SEPARATOR = System.getProperty("file.separator");
36    public String templateDirPath = "";
37    public String powerJournalDir = null;
38    public final static String ERROR_TITLE_EXPORT = "Export Error";
39    public JFrame content = null;
40    private final static String entryPageNamePrefix = "entry_";
41    private final static String entryPageNameSuffix = ".html";
42    public final static String TAG_JOURNAL_NAME = "@@JOURNAL_NAME@@";
43    public final static String TAG_LINK_PREVIOUS = "@@PREVIOUS_LINK@@";
44    public final static String TAG_LINK_NEXT = "@@NEXT_LINK@@";
45    public final static String TAG_ENTRY_LIST = "@@ENTRYLIST@@";
46    public final static String TAG_ENTRY_ID = "@@ENTRY_ID@@";
47    public final static String TAG_ENTRY_PRIVATE = "@@ENTRY_PRIVATE@@";
48    public final static String TAG_ENTRY_DATE = "@@ENTRY_DATE@@";
49    public final static String TAG_ENTRY_DESCRIPTION = "@@ENTRY_DESCRIPTION@@";
50    public final static String TAG_ENTRY_CONTENTS = "@@ENTRY_CONTENT@@";
51    public final static String TAG_LINK_INDEX = "@@INDEX_LINK@@";
52    private final static String TAG_HTML_TITLE_BEGIN = "<title>";
53    private final static String TAG_HTML_TITLE_END = "</title>";
54    private final static String TEXT_CR_LF = "\r\n";
55    private final static String TEXT_LF = "\n";
56    private final static String HTML_ANCHOR_OPEN_BEGIN = "<a href=\"";
57    private final static String HTML_ANCHOR_OPEN_END = "\">";
58    private final static String HTML_ANCHOR_CLOSE = "</a>";
59    private final static String HTML_BREAK = "<br>";
60    private final static String DIALOG_SUCCESS_TITLE = "Success";
61    private final static String DIALOG_SUCCESS_MSG = "Export Complete!";
62    private final static String DIALOG_CANCELED_TITLE = "Canceled";
63    private final static String DIALOG_CANCELED_MSG = "Export Canceled!";
64  
65  
66    public ExportAgent(String powerJournalDirArg, JFrame parent) {
67      content = parent;
68      powerJournalDir = powerJournalDirArg;
69      templateDirPath = powerJournalDir + FILE_SEPARATOR + "templates";
70      exportTemplates();
71    }
72  
73  
74  
75    private File getExportDir(String filename, boolean dirsOnly) {
76      JFileChooser fc = new JFileChooser();
77      if (dirsOnly) {
78        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
79      } else {
80        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
81      }
82      fc.setSelectedFile(new File(powerJournalDir + FILE_SEPARATOR + filename));
83      int returnVal = fc.showSaveDialog(content);
84      if (returnVal == JFileChooser.APPROVE_OPTION) {
85        return fc.getSelectedFile();
86      } else {
87        return null;
88      }
89    }
90  
91  
92    private void exportTemplates() {
93      //make sure template dir exists
94      File templateDir = new File(templateDirPath);
95      if (!templateDir.exists()) {
96        templateDir.mkdir();
97      }
98  
99      //make sure templates exist on disk
100     indexTemplate = new File(templateDirPath + FILE_SEPARATOR + "index.html");
101     createTemplate(indexTemplate, HtmlLib.INDEX_HTML);
102 
103     entryTemplate = new File(templateDirPath + FILE_SEPARATOR + "entry.html");
104     createTemplate(entryTemplate, HtmlLib.SINGLE_ENTRY_HTML);
105 
106 //    allEntriesTemplate = new File(templateDirPath + FILE_SEPARATOR + "index-all.html");
107 //    createTemplate(allEntriesTemplate, HtmlLib.INDEX_ALL_HTML);
108 
109     linkTableTemplate = new File(templateDirPath + FILE_SEPARATOR + "link-table.html");
110     createTemplate(linkTableTemplate, HtmlLib.LINK_TABLE_HTML);
111   }
112 
113 
114   private void createTemplate(File template, String templateContents) {
115     try {
116       if (!template.exists()) {
117         FileOutputStream out = new FileOutputStream(template.getAbsolutePath(), false);
118         out.write(templateContents.getBytes());
119         out.close();
120       }
121     } catch (FileNotFoundException e) {
122     } catch (IOException e) {
123     }
124   }
125 
126 
127   private String getTemplateContents(String templatePath) {
128     String line;
129     String fileContents = "";
130     try {
131       BufferedReader bufferedreader = new BufferedReader(new FileReader(templatePath));
132       while ((line = bufferedreader.readLine()) != null) {
133         fileContents += line + UtilLib.NEW_LINE;
134       }
135       bufferedreader.close();
136     } catch (FileNotFoundException e) {
137       return null;
138     } catch (IOException e) {
139       return null;
140     }
141     return fileContents;
142   }
143 
144 
145 
146   public void exportHTML(ArrayList entries, String currentJournalName) {
147     try {
148       StringBuffer sb = new StringBuffer();
149       String exportSubDirPath = "entries";
150       File exportDir = getExportDir("export", true);
151       if (exportDir == null) {
152         Dialogs.showInformation(content, DIALOG_CANCELED_TITLE, DIALOG_CANCELED_MSG);
153       } else {
154         //get templates from disk
155         HashMap templateContents = loadTemplateContents();
156         String indexTemplateContents = (String) templateContents.get(indexTemplate);
157 //        String indexAllTemplateContents = (String) templateContents.get(allEntriesTemplate);
158         String entriesTemplateContents = (String) templateContents.get(entryTemplate);
159         //both index and index-all will use the linkTableTemplateContents
160         String linkTableTemplateContents = (String) templateContents.get(linkTableTemplate);
161 
162         //make subdir
163         File exportSubDir = makeExportSubDir(exportDir.getCanonicalPath() + FILE_SEPARATOR + exportSubDirPath);
164 
165         //loop the entries
166         Integer previousEntryNumber = null;
167         Integer nextEntryNumber = null;
168         String linkTableForIndex = "";
169         for (int i = 0; i < entries.size(); i++) {
170           Entry entry = (Entry) entries.get(i);
171           String html = entriesTemplateContents;
172           String nextLinkText = getNextLinkText(i, entries);
173           html = parseExportTags(html, previousEntryNumber, nextLinkText, currentJournalName, entry);
174           previousEntryNumber = new Integer(entry.getEntryNumber());
175 
176           //create index for the index.html page
177           linkTableForIndex += linkTableTemplateContents;
178           linkTableForIndex = (makeLinkTableForIndex(linkTableForIndex, exportSubDirPath, linkTableTemplateContents, currentJournalName, entry));
179 
180           //write entry page
181           FileOutputStream out = new FileOutputStream(exportSubDir.getAbsolutePath() + FILE_SEPARATOR + entryPageNamePrefix + entry.getEntryNumber() + entryPageNameSuffix, false);
182           out.write(html.getBytes());
183           out.close();
184         }
185         //write index.html page
186         indexTemplateContents = UtilLib.replaceAllOccurances(indexTemplateContents, TAG_JOURNAL_NAME, currentJournalName);
187         indexTemplateContents = UtilLib.replaceAllOccurances(indexTemplateContents, TAG_ENTRY_LIST, linkTableForIndex);
188         FileOutputStream out = new FileOutputStream(exportDir.getAbsolutePath() + FILE_SEPARATOR + "index.html", false);
189         out.write(indexTemplateContents.getBytes());
190         out.close();
191 
192         Dialogs.showInformation(content, DIALOG_SUCCESS_TITLE, DIALOG_SUCCESS_MSG);
193       }
194     } catch (FileNotFoundException e) {
195       Dialogs.showError(content, ERROR_TITLE_EXPORT, e.getMessage());
196     } catch (IOException e) {
197       Dialogs.showError(content, ERROR_TITLE_EXPORT, e.getMessage());
198     }
199   }
200 
201 
202   private String makeLinkTableForIndex(String html, String exportSubDirPath, String linkTableTemplateContents, String currentJournalName, Entry entry) {
203     if (html.length() <= 0) {
204       html = linkTableTemplateContents;
205     }
206     String link = "<a name=\""+entry.getEntryNumber()+"\"></a>"
207          + HTML_ANCHOR_OPEN_BEGIN + exportSubDirPath + "/"
208          + entryPageNamePrefix + entry.getEntryNumber()
209          + entryPageNameSuffix + HTML_ANCHOR_OPEN_END
210          + entry.getEntryNumber() + HTML_ANCHOR_CLOSE;
211     html = UtilLib.replaceAllOccurances(html, TAG_ENTRY_ID, link);
212     html = parseExportTags(html, null, "", currentJournalName, entry);
213     return html;
214   }
215 
216 
217   private File makeExportSubDir(String exportDir) {
218     //make sure export dir exists
219     File exportSubDir = new File(exportDir);
220     if (!exportSubDir.exists()) {
221       exportSubDir.mkdir();
222     }
223     return exportSubDir;
224   }
225 
226 
227   private HashMap loadTemplateContents() {
228     HashMap html = new HashMap();
229     String indexTemplateContents = getTemplateContents(indexTemplate.getAbsolutePath());
230 //    String allEntriesTemplateContents = getTemplateContents(allEntriesTemplate.getAbsolutePath());
231     String entryTemplateContents = getTemplateContents(entryTemplate.getAbsolutePath());
232     String linkTableTemplateContents = getTemplateContents(linkTableTemplate.getAbsolutePath());
233 
234     //make sure we didnt encounter error loading them
235     if (indexTemplateContents == null) {
236       indexTemplateContents = HtmlLib.INDEX_HTML;
237     }
238 //    if (allEntriesTemplateContents == null) {
239 //      allEntriesTemplateContents = HtmlLib.INDEX_ALL_HTML;
240 //    }
241     if (entryTemplateContents == null) {
242       entryTemplateContents = HtmlLib.SINGLE_ENTRY_HTML;
243     }
244     if (linkTableTemplate == null) {
245       linkTableTemplateContents = HtmlLib.LINK_TABLE_HTML;
246     }
247     html.put(indexTemplate, indexTemplateContents);
248 //    html.put(allEntriesTemplate, allEntriesTemplateContents);
249     html.put(entryTemplate, entryTemplateContents);
250     html.put(linkTableTemplate, linkTableTemplateContents);
251     return html;
252   }
253 
254 
255   private String getNextLinkText(int index, ArrayList entries) {
256     if (index + 1 < entries.size()) {
257       Entry nextEntry = (Entry) entries.get(index + 1);
258       return HTML_ANCHOR_OPEN_BEGIN + entryPageNamePrefix + nextEntry.getEntryNumber() + entryPageNameSuffix + HTML_ANCHOR_OPEN_END + "Next" + HTML_ANCHOR_CLOSE;
259     } else {
260       return "Next";
261     }
262   }
263 
264 
265   private String parseExportTags(String html, Integer previousEntryNumber,
266       String nextLinkText, String currentJournalName, Entry entry) {
267     //fix newlines
268     String entryContents = UtilLib.replaceAllOccurances(entry.getEntryContents(), TEXT_CR_LF, HTML_BREAK);
269     entryContents = UtilLib.replaceAllOccurances(entryContents, TEXT_LF, HTML_BREAK);
270     String entryDesc = UtilLib.replaceAllOccurances(entry.getEntryDescription(), TEXT_CR_LF, HTML_BREAK);
271     entryDesc = UtilLib.replaceAllOccurances(entryDesc, TEXT_CR_LF, HTML_BREAK);
272     //replace tags
273     if (previousEntryNumber == null) {
274       html = UtilLib.replaceAllOccurances(html, TAG_LINK_PREVIOUS, "Previous");
275     } else {
276       html = UtilLib.replaceAllOccurances(html, TAG_LINK_PREVIOUS, HTML_ANCHOR_OPEN_BEGIN + entryPageNamePrefix + previousEntryNumber.intValue() + entryPageNameSuffix + HTML_ANCHOR_OPEN_END + "Previous" + HTML_ANCHOR_CLOSE);
277     }
278     html = UtilLib.replaceAllOccurances(html, TAG_LINK_NEXT, nextLinkText);
279     html = UtilLib.replaceAllOccurances(html, TAG_JOURNAL_NAME, currentJournalName);
280     html = UtilLib.replaceAllOccurances(html, TAG_ENTRY_ID, "" + entry.getEntryNumber());
281     html = UtilLib.replaceAllOccurances(html, TAG_ENTRY_PRIVATE, new Boolean(entry.getEntryPrivate()).toString());
282     html = UtilLib.replaceAllOccurances(html, TAG_ENTRY_DATE, entry.getEntryDateAsString());
283     html = UtilLib.replaceAllOccurances(html, TAG_ENTRY_DESCRIPTION, entryDesc);
284     html = UtilLib.replaceAllOccurances(html, TAG_ENTRY_CONTENTS, entryContents);
285     html = UtilLib.replaceAllOccurances(html, TAG_HTML_TITLE_BEGIN + TAG_HTML_TITLE_END, TAG_HTML_TITLE_BEGIN + currentJournalName + " - Entry # " + entry.getEntryNumber() + TAG_HTML_TITLE_END);
286     html = UtilLib.replaceAllOccurances(html, TAG_LINK_INDEX, HTML_ANCHOR_OPEN_BEGIN + "../index.html#" + entry.getEntryNumber() + HTML_ANCHOR_OPEN_END + "Back To Index" + HTML_ANCHOR_CLOSE);
287     return html;
288   }
289 
290 
291 
292   public void exportCSV(ArrayList entries, String currentJournalName) {
293     try {
294       StringBuffer sb = new StringBuffer();
295       String fileNameAndPath = "";
296       File exportDir = getExportDir(currentJournalName + ".csv", false);
297       if (exportDir == null) {
298         Dialogs.showInformation(content, DIALOG_CANCELED_TITLE, DIALOG_CANCELED_MSG);
299       } else {
300         fileNameAndPath = exportDir.getAbsolutePath();
301         for (int i = 0; i < entries.size(); i++) {
302           Entry entry = (Entry) entries.get(i);
303           ArrayList values = new ArrayList();
304           values.add("" + entry.getEntryNumber());
305           values.add(entry.getEntryDateAsString());
306           values.add(new Boolean(entry.getEntryPrivate()).toString());
307           values.add(entry.getEntryDescription());
308           values.add(entry.getEntryContents());
309           sb.append(UtilLib.convertToCSV(values, "<newline>", "<comma>"));
310         }
311         FileOutputStream out = new FileOutputStream(fileNameAndPath, false);
312         out.write(sb.toString().getBytes());
313         out.close();
314         Dialogs.showInformation(content, DIALOG_SUCCESS_TITLE, DIALOG_SUCCESS_MSG);
315       }
316     } catch (FileNotFoundException e) {
317       Dialogs.showError(content, ERROR_TITLE_EXPORT, e.getMessage());
318     } catch (IOException e) {
319       Dialogs.showError(content, ERROR_TITLE_EXPORT, e.getMessage());
320     }
321   }
322 
323 
324   public void exportText(ArrayList entries, String currentJournalName) {
325     try {
326       StringBuffer sb = new StringBuffer();
327       String fileNameAndPath = "";
328       File exportDir = getExportDir(currentJournalName + ".txt", false);
329       if (exportDir == null) {
330         Dialogs.showInformation(content, DIALOG_CANCELED_TITLE, DIALOG_CANCELED_MSG);
331       } else {
332         fileNameAndPath = exportDir.getAbsolutePath();
333         for (int i = 0; i < entries.size(); i++) {
334           Entry entry = (Entry) entries.get(i);
335           sb.append("ENTRY:  " + entry.getEntryNumber() + UtilLib.NEW_LINE);
336           sb.append("DATE:  " + entry.getEntryDateAsString() + UtilLib.NEW_LINE);
337           sb.append("PRIVATE:  " + new Boolean(entry.getEntryPrivate()).toString() + UtilLib.NEW_LINE);
338           sb.append("DESC:  " + UtilLib.fixNewLines(entry.getEntryDescription()) + UtilLib.NEW_LINE);
339           sb.append("CONTENTS:  " + UtilLib.fixNewLines(entry.getEntryContents()) + UtilLib.NEW_LINE);
340           sb.append(UtilLib.NEW_LINE);
341           sb.append("----------------------------------------------------------------------");
342           sb.append(UtilLib.NEW_LINE + UtilLib.NEW_LINE);
343         }
344         FileOutputStream out = new FileOutputStream(fileNameAndPath, false);
345         out.write(sb.toString().getBytes());
346         out.close();
347         Dialogs.showInformation(content, DIALOG_SUCCESS_TITLE, DIALOG_SUCCESS_MSG);
348       }
349     } catch (FileNotFoundException e) {
350       Dialogs.showError(content, ERROR_TITLE_EXPORT, e.getMessage());
351     } catch (IOException e) {
352       Dialogs.showError(content, ERROR_TITLE_EXPORT, e.getMessage());
353     }
354   }
355 
356 }