您的位置:首页 > 其它

原有日记向Evernote迁移

2013-05-08 08:52 176 查看
目的: 以前用了一个很不知名的日记软件,现在想把旧的日记迁移到Evernote上去

现状: 我的原有的日记有两种格式

1) 格式如下:

2012/12/12

日记内容,XXXXX

日记内容,XXXXX

2012/12/13

另一篇日记内容,XXXXX

2) 格式如下:

有N个rtf文件,每个文件名都是类似于Diary110202.rtf

日记内容就是普通的文本

步骤:

1. 分析Evernote能导入的ENEX文件的基本格式(只支持文本,换行符,标题)

ENEX的文件格式主要是XML的形式,但是发现它的XML比较复杂,用到了诸多XML的特性,所以我决定用纯字符串形式的拼接来生成XML字符串,而非使用DOM和SAX.

大致格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export.dtd">
<en-export export-date="20130508T004627Z" application="Evernote/Windows"
	version="4.x">
	<note>
		<title>2013.5.6</title>
		<content>
			<![CDATA[<?xml version="1.0" encoding="UTF-8"?>
			<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
			<en-note style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
				2013.5.6
				<div>日记内容</div><div><br/></div>
			</en-note>]]>
		</content>
		<created>20130506T133325Z</created>
		<updated>20130506T133517Z</updated>
	</note>
</en-export>


2. 出现的问题:

主要的问题在于,日记内容里如果存在换行符的话,比如要被替换成<div>日记内容</div>,而且不能直接写上<br>,否则会出现一个很奇特的问题,Evernote能成功导入,但是却无法同步,未同步的日记被放到Unsynced Notes里去.

3. 我希望让所有日记的题目都是2012.1.1格式的时间,但是ENEX格式的日记有创建时间和更新时间,于是还要再做一层转换

所有源码如下(写的有点乱):

public class EverNotePorting {
	public static final String DELIMETER = "::::";
	public static final String HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE en-export SYSTEM \"http://xml.evernote.com/pub/evernote-export.dtd\"><en-export export-date=\"20130507T023920Z\" application=\"Evernote/Windows\" version=\"4.x\">";
	public static final String HEADER_END = "</en-export>";
	public static final String CDATA = "<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\"><en-note style=\"word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;\">";
	public static final String CDATA_END = "</en-note>]]>";
	List<String> noteList = new ArrayList<String>();

	public String newENEX() {
		return HEADER + newNoteList() + HEADER_END;
	}

	/**
	 * create a string like
	 * <p>
	 * <note>XXX</note>
	 * <p>
	 * <note>XXX</note>
	 * 
	 * @return
	 */
	private String newNoteList() {
		StringBuilder sb = new StringBuilder();
		for (String note : noteList) {
			sb.append(note);
		}
		return sb.toString();
	}

	public void newNote(String content, String create) {
		StringBuilder note = new StringBuilder();
		String time = addTag("created", adjustTimeUTC(create)) + addTag("updated", adjustTimeUTC(create));
		note.append(addTag("note", addTag("title", adjustTime(create)) + newContent(content, adjustTime(create)) + time));
		noteList.add(note.toString());
	}

	/**
	 * create a string like <content>XXX</content>, which also contains some
	 * META info
	 * 
	 * @param content
	 * @param time
	 * @return
	 */
	private String newContent(String content, String time) {
		String string = addTag("content", CDATA + time + content + CDATA_END);
		return string;
	}

	private static String addTag(String tag, String text) {
		return "<" + tag + ">" + text + "</" + tag + ">";
	}

	/**
	 * 2013/5/6 --> 20130506T133325Z
	 */
	public static String adjustTimeUTC(String time) {
		String[] strings = time.split("/");
		if (Integer.valueOf(strings[1]) < 10) {
			strings[1] = "0" + strings[1];
		}
		if (Integer.valueOf(strings[2]) < 10) {
			strings[2] = "0" + strings[2];
		}
		return strings[0] + strings[1] + strings[2] + "T120000Z";
	}

	/**
	 * 2013/5/6 -->2013.5.6
	 */
	public static String adjustTime(String time) {
		return time.replace("/", ".");
	}

	public static List<String> parseFile() throws IOException {
		List<String> list = new ArrayList<String>();
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				EverNotePorting.class.getResourceAsStream("diary")));
		String line = null;
		String time = "";
		StringBuilder content = new StringBuilder();
		while ((line = reader.readLine()) != null) {
			if (line.contains("2010/")) { // only contains 2010 dairy
				if (time.length() != 0) {
					list.add(time + DELIMETER + content.toString());
				}
				time = line;
				content = new StringBuilder();
			} else {
				content.append("<div>" + line + "</div>");
			}
		}
		list.add(time + DELIMETER + content.toString());
		return list;
	}

	public static String getFileContent(File file) throws Exception {
		InputStream iStream = new FileInputStream(file);

		long l = file.length();
		byte[] b = new byte[(int) l];
		iStream.read(b);
		RTFEditorKit rtfParser = new RTFEditorKit();
		Document document = rtfParser.createDefaultDocument();
		rtfParser.read(new ByteArrayInputStream(b), document, 0);
		String text = document.getText(0, document.getLength());
		StringBuilder sb = new StringBuilder();
		String[] lines = text.split("\n");
		for (String string : lines) {
			sb.append("<div>").append(string).append("</div>");
		}
		iStream.close();
		return sb.toString();
	}

	// convert Diary110202 to 2011/02/02
	public static String adjustFileNameToTime(String file) {
		int year = Integer.valueOf(file.substring(5, 7));
		int month = Integer.valueOf(file.substring(7, 9));
		int day = Integer.valueOf(file.substring(9, 11));
		return "20" + year + "/" + month + "/" + day;
	}

	public static void main(String[] args) throws Exception {
		/**
		 * this part is used to generate evenote database from a file which
		 * format is like:
		 * <p>
		 * 2012/12/12
		 * <p>
		 * this is a note
		 * <p>
		 * this is another line
		 * <p>
		 * 2012/12/13 this is another line
		 * <p>
		 */

		// List<String> list = parseFile();
		// EverNotePorting evernote = new EverNotePorting();
		// for (String string : list) {
		// String[] strings = string.split(DELIMETER);
		// evernote.newNote(strings[1], strings[0]);
		// }
		// System.out.println(evernote.newENEX());

		/**
		 * this part is used to generate evenote database from many files, whose
		 * name is Diary110202 and content is diary content:
		 */

		// EverNotePorting evernote = new EverNotePorting();
		// File file = new File("src/main/java/com/test");
		// File[] files = file.listFiles();
		// for (File string : files) {
		// if (string.getName().contains("Diary1")) {
		// evernote.newNote(getFileContent(string),
		// adjustFileNameToTime(string.getName()));
		// }
		// }
		// System.out.println(evernote.newENEX());
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: