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