001 /* 002 * CRIMSON 003 * Copyright (c) 2006, Stephen Fisher, Susan Davidson, and Junhyong Kim, 004 * University of Pennsylvania. 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU General Public License as 008 * published by the Free Software Foundation; either version 2 of the 009 * License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, but 012 * WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 * 02110-1301 USA. 020 * 021 * @(#)HTMLFile.java 022 */ 023 024 package edu.upenn.crimson.io; 025 026 import edu.upenn.crimson.CrimsonUtils; 027 import edu.upenn.crimson.gui.GUIUtils; 028 import javax.swing.*; 029 import java.net.URL; 030 import java.io.*; 031 import javax.swing.filechooser.FileFilter; 032 033 /** 034 * Save URL, HTML text, or InputStream as HTML file. 035 * 036 * @XXX This may not work for URL or InputStreams 037 * 038 * @author Stephen Fisher 039 * @version $Id: HTMLFile.java,v 1.4 2007/05/16 18:55:58 fisher Exp $ 040 */ 041 042 public class HTMLFile { 043 public static void saveURL(URL source) { 044 saveURL(source, "", true); 045 } 046 047 public static void saveURL(URL source, String filename, boolean overwrite) { 048 // if no filename, then get filename 049 if (filename.length() == 0) filename = saveFileChooser(); 050 051 // check again because saveFileChooser() might return empty 052 // filename 053 if (filename.length() == 0) return; 054 055 // add ".htm" filename extension, if necessary 056 if ((! filename.endsWith(".html")) && (! filename.endsWith(".htm"))) { 057 filename += ".htm"; 058 } 059 060 /* 061 try { 062 htmlText.setPage((URL) source); 063 } catch (IOException e) { 064 CrimsonUtils.printMsg("Invalid URL to be saved", CrimsonUtils.ERROR); 065 return; 066 } 067 */ 068 } 069 070 public static void saveInputStream(InputStream source) { 071 saveInputStream(source, "", true); 072 } 073 074 public static void saveInputStream(InputStream source, String filename, boolean overwrite) { 075 // if no filename, then get filename 076 if (filename.length() == 0) filename = saveFileChooser(); 077 078 // check again because saveFileChooser() might return empty 079 // filename 080 if (filename.length() == 0) return; 081 082 // add ".htm" filename extension, if necessary 083 if ((! filename.endsWith(".html")) && (! filename.endsWith(".htm"))) { 084 filename += ".htm"; 085 } 086 087 /* 088 try { 089 htmlText.read((InputStream) source, null); 090 } catch (IOException e) { 091 CrimsonUtils.printMsg("Invalid InputStream to be saved", CrimsonUtils.ERROR); 092 return; 093 } 094 */ 095 } 096 097 /** 098 * Save the html text to a file. This will overwrite any existing 099 * file. 100 * 101 * @XXX need to throw FileIO exceptions, rather than just print 102 * errors. 103 */ 104 public static void saveText(String source) { 105 saveText(source, "", true); 106 } 107 108 /** 109 * Save the html text to a file. 110 * 111 * @XXX need to throw FileIO exceptions, rather than just print 112 * errors. 113 */ 114 public static void saveText(String source, String filename, boolean overwrite) { 115 // if no filename, then get filename 116 if (filename.length() == 0) filename = saveFileChooser(); 117 118 // check again because saveFileChooser() might return empty 119 // filename 120 if (filename.length() == 0) return; 121 122 // add ".htm" filename extension, if necessary 123 if ((! filename.endsWith(".html")) && (! filename.endsWith(".htm"))) { 124 filename += ".htm"; 125 } 126 127 File file = new File(filename); 128 // if the file already exists and not supposed to overwrite 129 // it, then return on error. 130 if (file.exists() && (! overwrite)) { 131 CrimsonUtils.printMsg("ERROR: file \"" + filename + "\" already exists."); 132 return; 133 } 134 135 try { 136 FileWriter fWriter = new FileWriter(file); 137 BufferedWriter bWriter = new BufferedWriter(fWriter); 138 139 bWriter.write(source); 140 // bWriter.newLine(); 141 bWriter.flush(); 142 bWriter.close(); 143 } catch (FileNotFoundException e) { 144 // problem with FileOutputStream 145 CrimsonUtils.printError("File \"" + filename + "\" can not be opened."); 146 CrimsonUtils.printError(e.getMessage()); 147 } catch (IOException e) { 148 // problem with ObjectOutputStream. XXX do we need to 149 // close bWriter()? 150 CrimsonUtils.printError("Error writting html file \"" + filename + "\"."); 151 CrimsonUtils.printError(e.getMessage()); 152 } 153 } 154 155 /** 156 * Use a JFileChooser the get the file info for saving HTML to a file. 157 */ 158 private static String saveFileChooser() { 159 // use the current working directory 160 JFileChooser fileChooser = new JFileChooser(); 161 162 // set the title 163 fileChooser.setDialogTitle("Save HTML File"); 164 // set the filter, if present 165 fileChooser.setAcceptAllFileFilterUsed(true); 166 FileFilter filter = new HTMLFilter(); 167 fileChooser.addChoosableFileFilter(filter); 168 fileChooser.setFileFilter(filter); 169 170 // launch the file chooser 171 int status = fileChooser.showSaveDialog(null); 172 if (status == JFileChooser.APPROVE_OPTION) { 173 File file = fileChooser.getSelectedFile(); 174 String filename = GUIUtils.getFilename(file); 175 176 String[] ext = {".htm", ".html"}; 177 boolean notFound = true; 178 int i = 0; 179 while (notFound && (i < ext.length)) { 180 if (filename.endsWith(ext[i])) notFound = false; 181 i++; 182 } 183 184 // the filename doesn't end with a valid ext, so loop 185 // through the extensions to see if we can find a file 186 // with one of the valid extensions. we could just take 187 // the first ext that isn't a valid file but it's assumed 188 // that users will be consistent in their use of 189 // extensions and thus if ".html" matches but ".htm" 190 // doesn't, we assume that ".html" is actually what the 191 // user wants to use. 192 if (notFound) { 193 i = 0; 194 // we're overloading 'notFound' by using it 195 // here as well as above. 196 while (notFound && (i < ext.length)) { 197 file = new File(filename + ext[i]); 198 if (file.exists()) notFound = false; 199 else i++; // don't increment if found 200 } 201 // if no file extensions match a file, then 202 // just use the first one in the list. 203 if (notFound) filename += ext[0]; 204 else filename += ext[i]; 205 } 206 207 if (! file.exists()) return filename; 208 209 // the file exist, so check if want to overwrite the file 210 String msg = "The file \"" + file.getPath() + "\" already exists.\n"; 211 msg += "Do you want to overwrite the file?"; 212 Object[] options = {"Overwrite", "Cancel"}; 213 int flag = JOptionPane.showOptionDialog(null, msg, 214 "Overwrite Confirmation", 215 JOptionPane.YES_NO_OPTION, 216 JOptionPane.QUESTION_MESSAGE, 217 null, 218 options, 219 options[1]); 220 if (flag == JOptionPane.YES_OPTION) { // "Overwrite" 221 return filename; 222 } else { 223 return saveFileChooser(); 224 } 225 } 226 227 return ""; 228 } 229 230 231 /** HTML FileFilter. */ 232 private static class HTMLFilter extends FileFilter { 233 public boolean accept(File f) { 234 // accept directories 235 if (f.isDirectory()) return true; 236 237 // accept all files 238 if ((f.getName()).endsWith(".html")) return true; 239 if ((f.getName()).endsWith(".htm")) return true; 240 241 return false; 242 } 243 244 // set the filter's description 245 public String getDescription() { return "HTML files (*.htm; *.html)"; } 246 } 247 248 } // HTMLFile.java