Repository navigation
Expand file tree
/
Copy pathEverNotePorting.java
More file actions
182 lines (166 loc) · 5.41 KB
/
Copy pathEverNotePorting.java
File metadata and controls
182 lines (166 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* This is used to batch evernote diary with simple interface
*
* @author terry zhu
*
*/
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;
}
/**
* Usage:
* <p>
* EverNotePorting evernote = new EverNotePorting();
* <p>
* evernote.newNote(content, createTime);
* <p>
* System.out.println(evernote.newENEX());
* <p>
* The output is a xml format, we could copy it to a txt file and rename it
* with *.enex and imported by evernote
*/
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());
}
}