📄 filestorage.java
字号:
/** * Storage.java * Created on 12.02.2003, 0:21:40 Alex * Package: net.sf.memoranda.util * * @author Alex V. Alishevskikh, alex@openmechanics.net * Copyright (c) 2003 Memoranda Team. http://memoranda.sf.net */package net.sf.memoranda.util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.URL;import javax.swing.text.html.HTMLDocument;import javax.swing.text.html.HTMLEditorKit;import net.sf.memoranda.EventsManager;import net.sf.memoranda.Note;import net.sf.memoranda.NoteList;import net.sf.memoranda.NoteListImpl;import net.sf.memoranda.Project;import net.sf.memoranda.ProjectManager;import net.sf.memoranda.ResourcesList;import net.sf.memoranda.ResourcesListImpl;import net.sf.memoranda.TaskList;import net.sf.memoranda.TaskListImpl;import net.sf.memoranda.date.CalendarDate;import net.sf.memoranda.ui.ExceptionDialog;import net.sf.memoranda.ui.htmleditor.AltHTMLWriter;import nu.xom.Builder;import nu.xom.DocType;import nu.xom.Document;/** * *//*$Id: FileStorage.java,v 1.15 2006/10/09 23:31:58 alexeya Exp $*/public class FileStorage implements Storage { public static String JN_DOCPATH = Util.getEnvDir(); private HTMLEditorKit editorKit = new HTMLEditorKit(); public FileStorage() { /*The 'MEMORANDA_HOME' key is an undocumented feature for hacking the default location (Util.getEnvDir()) of the memoranda storage dir. Note that memoranda.config file is always placed at fixed location (Util.getEnvDir()) anyway */ String mHome = (String) Configuration.get("MEMORANDA_HOME"); if (mHome.length() > 0) { JN_DOCPATH = mHome; /*DEBUG*/ System.out.println("[DEBUG]***Memoranda storage path has set to: " + JN_DOCPATH); } } public static void saveDocument(Document doc, String filePath) { /** * @todo: Configurable parameters */ try { /*The XOM bug: reserved characters are not escaped*/ //Serializer serializer = new Serializer(new FileOutputStream(filePath), "UTF-8"); //serializer.write(doc); OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8"); fw.write(doc.toXML()); fw.flush(); fw.close(); } catch (IOException ex) { new ExceptionDialog( ex, "Failed to write a document to " + filePath, ""); } } public static Document openDocument(InputStream in) throws Exception { Builder builder = new Builder(); return builder.build(new InputStreamReader(in, "UTF-8")); } public static Document openDocument(String filePath) { try { return openDocument(new FileInputStream(filePath)); } catch (Exception ex) { new ExceptionDialog( ex, "Failed to read a document from " + filePath, ""); } return null; } public static boolean documentExists(String filePath) { return new File(filePath).exists(); } /** * @see net.sf.memoranda.util.Storage#storeNote(net.sf.memoranda.Note) */ public void storeNote(Note note, javax.swing.text.Document doc) { String filename = JN_DOCPATH + note.getProject().getID() + File.separator; doc.putProperty( javax.swing.text.Document.TitleProperty, note.getTitle()); CalendarDate d = note.getDate(); filename += note.getId();//d.getDay() + "-" + d.getMonth() + "-" + d.getYear(); /*DEBUG*/System.out.println("[DEBUG] Save note: "+ filename); try { OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"); AltHTMLWriter writer = new AltHTMLWriter(fw, (HTMLDocument) doc); writer.write(); fw.flush(); fw.close(); //editorKit.write(new FileOutputStream(filename), doc, 0, doc.getLength()); //editorKit.write(fw, doc, 0, doc.getLength()); } catch (Exception ex) { new ExceptionDialog( ex, "Failed to write a document to " + filename, ""); } /*String filename = JN_DOCPATH + note.getProject().getID() + "/"; doc.putProperty(javax.swing.text.Document.TitleProperty, note.getTitle()); CalendarDate d = note.getDate(); filename += d.getDay() + "-" + d.getMonth() + "-" + d.getYear(); try { long t1 = new java.util.Date().getTime(); FileOutputStream ostream = new FileOutputStream(filename); ObjectOutputStream oos = new ObjectOutputStream(ostream); oos.writeObject((HTMLDocument)doc); oos.flush(); oos.close(); ostream.close(); long t2 = new java.util.Date().getTime(); System.out.println(filename+" save:"+ (t2-t1) ); } catch (Exception ex) { ex.printStackTrace(); }*/ } /** * @see net.sf.memoranda.util.Storage#openNote(net.sf.memoranda.Note) */ public javax.swing.text.Document openNote(Note note) { HTMLDocument doc = (HTMLDocument) editorKit.createDefaultDocument(); if (note == null) return doc; /* String filename = JN_DOCPATH + note.getProject().getID() + File.separator; CalendarDate d = note.getDate(); filename += d.getDay() + "-" + d.getMonth() + "-" + d.getYear(); */ String filename = getNotePath(note); try { /*DEBUG*/// Util.debug("Open note: " + filename);// Util.debug("Note Title: " + note.getTitle()); doc.setBase(new URL(getNoteURL(note))); editorKit.read( new InputStreamReader(new FileInputStream(filename), "UTF-8"), doc, 0); } catch (Exception ex) { //ex.printStackTrace(); // Do nothing - we've got a new empty document! } return doc; /*HTMLDocument doc = (HTMLDocument)editorKit.createDefaultDocument(); if (note == null) return doc; String filename = JN_DOCPATH + note.getProject().getID() + "/"; CalendarDate d = note.getDate(); filename += d.getDay() + "-" + d.getMonth() + "-" + d.getYear(); try { long t1 = new java.util.Date().getTime(); FileInputStream istream = new FileInputStream(filename); ObjectInputStream ois = new ObjectInputStream(istream); doc = (HTMLDocument)ois.readObject(); ois.close(); istream.close(); long t2 = new java.util.Date().getTime(); System.out.println(filename+" open:"+ (t2-t1) ); } catch (Exception ex) { ex.printStackTrace(); } return doc;*/ } public String getNoteURL(Note note) { return "file:" + JN_DOCPATH + note.getProject().getID() + "/" + note.getId(); } public String getNotePath(Note note) { String filename = JN_DOCPATH + note.getProject().getID() + File.separator;// CalendarDate d = note.getDate(); filename += note.getId();//d.getDay() + "-" + d.getMonth() + "-" + d.getYear(); return filename; } public void removeNote(Note note) { File f = new File(getNotePath(note)); /*DEBUG*/ System.out.println("[DEBUG] Remove note:" + getNotePath(note)); f.delete(); } /** * @see net.sf.memoranda.util.Storage#openProjectManager() */ public void openProjectManager() { if (!new File(JN_DOCPATH + ".projects").exists()) { ProjectManager._doc = null; return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -