-
Notifications
You must be signed in to change notification settings - Fork 1
/
XmlDocument.java
228 lines (194 loc) · 10.4 KB
/
XmlDocument.java
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.io.*;
import java.nio.file.Files;
import java.time.ZonedDateTime;
import java.util.HashMap;
public class XmlDocument {
private static final String W3_SCHEMA = "http://www.w3.org/2001/XMLSchema-instance";
private static final String PKP_HOME = "https://pkp.sfu.ca";
private final Volume volume;
private final HashMap<String, Integer> authorIDs;
private final String dateCreated;
private final String document;
/*
* Constructor fro XMLDocument
* assigns volume & authorIDs paramters to respective fields of the class
* assigns initDateCreated() value to dateCreated field
* assigns buildDocument() value to document field
* takes in: Volume obj volume, HashMap<Author, AuthorID> obj authorIDs
*/
public XmlDocument(Volume volume, HashMap<String, Integer> authorIDs) {
this.volume = volume;
this.authorIDs = authorIDs;
this.dateCreated = initDateCreated();
this.document = buildDocument();
}
// returns volume field data
public int getVolume() {
return this.volume.getVolume();
}
/*
* create a new file @ path from String path, create parent directories & write document field data to file
* takes in: String path
*/
public void saveToFile(String path) throws IOException {
File file = new File(path);
file.getParentFile().mkdirs();
file.createNewFile();
Files.writeString(file.toPath(), this.document);
}
/*
* Overrides Java toString()
* return val of document field as String; XML doc to String
*/
@Override
public String toString() {
return this.document;
}
/*
* assign ZonedDateTime obj now to current Date & Time
* returns string of Date in format YYYY-MM-DD
*/
private String initDateCreated() {
ZonedDateTime now = ZonedDateTime.now();
return String.format("%d-%02d-%02d",
now.getYear(),
now.getMonthValue(),
now.getDayOfMonth());
}
/*
* append XML details, schema instance & schema location to appropriately set up root elem of XML doc to doc StringBuilder
* assign volume's articles and fileIDs to Article[] and fileID int respectively
* loop through article objects in article & append XML block created upon calling buildXmlBlock() and passing the current article, fileID & sequence in volume
* add a new line string after every created XML block then increment
* delete last elem of doc and replace with closing XML tag </articles>
* returns: Stringified doc
*/
private String buildDocument() {
StringBuilder doc = new StringBuilder(192000);
doc.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
doc.append(String.format("<articles xmlns:xsi=\"%s\" ", W3_SCHEMA));
doc.append(String.format("xsi:schemaLocation=\"%s native.xsd\">\n", PKP_HOME));
Article[] articles = this.volume.getArticles();
int fileID = this.volume.getFileIDs()[0];
int i = 0;
for (Article article : articles) {
doc.append(buildXmlBlock(article, fileID + i, i)).append("\n");
i++;
}
doc.delete(doc.length() - 1, doc.length()).append("</articles>");
return doc.toString();
}
/*
* append various metadata elems (i.e. authors, keywords, publication info, etc) based on XML best practices for OJS to a stringBuilder `sb`
* takes in: Article obj article, Int fileID, Int seqInVol
* returns: XML String
*/
private String buildXmlBlock(Article article, int fileID, int seqInVol) {
String[] authors = article.getAuthors();
StringBuilder block = new StringBuilder(3200);
block.append(String.format("\s\s<article xmlns=\"%s\" ", PKP_HOME))
.append(String.format("xmlns:xsi=\"%s\" locale=\"en\" ", W3_SCHEMA))
.append(String.format("date_submitted=\"%s\" status=\"3\" ", dateCreated))
.append("submission_progress=\"\" current_publication_id=\"1\" stage=\"production\" ")
.append(String.format("xsi:schemaLocation=\"%s native.xsd\">\n", PKP_HOME));
block.append("\s\s\s\s<id type=\"internal\" advice=\"ignore\">")
.append(String.format("%d</id>\n", fileID));
block.append("\s\s\s\s<submission_file ")
.append(String.format("xmlns:xsi=\"%s\" id=\"%d\"", W3_SCHEMA, fileID))
.append(String.format("created_at=\"%s\" date_created=\"\" ", dateCreated))
.append(String.format("file_id=\"%d\" stage=\"submission\" ", fileID))
.append(String.format("updated_at=\"%s\" viewable=\"true\" ", dateCreated))
.append("genre=\"Article Text\" ")
.append(String.format("source_submission_file_id=\"%d\" ", fileID))
.append("uploader=\"admin\" ")
.append(String.format("xsi:schemaLocation=\"%s native.xsd\">\n", PKP_HOME));
block.append("\s\s\s\s\s\s<name locale=\"en\">")
.append(String.format("%s</name>\n", article.getName()));
block.append(String.format("\s\s\s\s\s\s<file id=\"%d\" ", fileID))
.append(String.format("filesize=\"%d\" ", article.getFileSize()))
.append("extension=\"pdf\">\n");
block.append("\s\s\s\s\s\s\s\s<href src=")
.append(String.format("\"%s\"/>\n", article.getPdfSource()));
block.append("\s\s\s\s\s\s</file>\n");
block.append("\s\s\s\s</submission_file>\n");
block.append(String.format("\s\s\s\s<publication xmlns:xsi=\"%s\" ", W3_SCHEMA))
.append("version=\"1\" status=\"3\" ")
.append("primary_contact_id=")
.append(String.format("\"%d\" url_path=\"\" ", this.authorIDs.get(authors[0])))
.append(String.format("seq=\"%d\" access_status=\"0\" ", seqInVol))
.append(String.format("date_published=\"%s\" section_ref=\"ART\" ", dateCreated))
.append(String.format("xsi:schemaLocation=\"%s native.xsd\">\n", PKP_HOME));
block.append("\s\s\s\s\s\s<id type=\"internal\" advice=\"ignore\">")
.append(String.format("%d</id>\n", fileID));
block.append("\s\s\s\s\s\s<id type=\"doi\" advice=\"update\">10.1119/5.0158200</id>\n");
block.append("\s\s\s\s\s\s<title locale=\"en\">")
.append(String.format("%s</title>\n", article.getTitle()));
block.append("\s\s\s\s\s\s<abstract locale=\"en\">")
.append(String.format("%s</abstract>\n", article.getAbstract()));
block.append("\s\s\s\s\s\s<licenseURL>http://www.tac.mta.ca/tac/consent.html")
.append("</licenseURL>\n");
block.append("\s\s\s\s\s\s<copyrightHolder locale=\"en\">author</copyrightHolder>\n");
block.append("\s\s\s\s\s\s<copyrightYear>")
.append(String.format("%d</copyrightYear>\n", this.volume.getYear()));
block.append("\s\s\s\s\s\s<keywords locale=\"en\">\n");
for (String keyword : article.getKeywords()) {
block.append(String.format("\s\s\s\s\s\s\s\s<keyword>%s</keyword>\n", keyword));
}
block.append("\s\s\s\s\s\s</keywords>\n");
block.append(String.format("\s\s\s\s\s\s<authors xmlns:xsi=\"%s\" ", W3_SCHEMA))
.append(String.format("xsi:schemaLocation=\"%s native.xsd\">\n", PKP_HOME));
int i = 0;
for (String author : authors) {
Author a = separateAuthorNames(author);
block.append("\s\s\s\s\s\s\s\s<author include_in_browse=\"true\" ")
.append(String.format("user_group_ref=\"Author\" seq=\"%d\" ", i++))
.append(String.format("id=\"%d\">\n", this.authorIDs.get(author)));
block.append("\s\s\s\s\s\s\s\s\s\s<givenname>")
.append(String.format("%s</givenname locale=\"en\">\n", a.givenName()));
block.append("\s\s\s\s\s\s\s\s\s<familyname locale=\"en\">")
.append(String.format("%s</surname>\n", a.familyName()));
block.append("\s\s\s\s\s\s\s\s\s\s<email>madeup@email.org</email>\n");
block.append("\s\s\s\s\s\s\s\s</author>\n");
}
block.append("\s\s\s\s\s\s</authors>\n");
block.append("\s\s\s\s\s\s<article_galley ")
.append(String.format("xmlns:xsi=\"%s\" locale=\"en\" url_path=\"\" ", W3_SCHEMA))
.append("approved=\"false\" ")
.append(String.format("xsi:schemaLocation=\"%s native.xsd\">\n", PKP_HOME));
block.append("\s\s\s\s\s\s\s\s<id type=\"internal\" advice=\"ignore\">")
.append(String.format("%d</id>\n", fileID));
block.append("\s\s\s\s\s\s\s\s<name locale=\"en\">PDF</name>\n");
block.append(String.format("\s\s\s\s\s\s\s\s<seq>%d</seq>\n", seqInVol));
block.append("\s\s\s\s\s\s\s\s<submission_file_ref ")
.append(String.format("id=\"%d\"/>\n", fileID));
block.append("\s\s\s\s\s\s</article_galley>\n");
block.append("\s\s\s\s\s\s<issue_identification>\n");
block.append("\s\s\s\s\s\s\s\s<volume>")
.append(String.format("%d</volume>\n", this.volume.getVolume()));
block.append("\s\s\s\s\s\s\s\s<year>")
.append(String.format("%d</year>\n", this.volume.getYear()));
String volumeTitle = this.volume.getTitle();
if (volumeTitle != null) {
block.append("\s\s\s\s\s\s\s\s<title locale=\"en\">")
.append(String.format("%s</title>\n", volumeTitle));
}
block.append("\s\s\s\s\s\s</issue_identification>\n");
block.append("\s\s\s\s\s\s<pages>")
.append(String.format("%d-%d", article.getStartPage(), article.getEndPage()))
.append("</pages>\n");
block.append("\s\s\s\s</publication>\n");
block.append("\s\s</article>\n");
return block.toString();
}
/*
* assign int i to last index in author where there's a ' '
* takes in: String author
* return Author record; givenName -> index 0 to i, familyName - > char after i to end
*/
private Author separateAuthorNames(String author) {
int i = author.lastIndexOf(' ');
return new Author(author.substring(0, i), author.substring(i + 1));
}
}
// Author record to store givenName and familyName
record Author(String givenName, String familyName) {}