forked from comixed/comixed
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ComicInfoEntryAdaptor.java
208 lines (193 loc) · 7.42 KB
/
ComicInfoEntryAdaptor.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
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2017, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/
package org.comixedproject.adaptors;
import java.io.*;
import java.text.MessageFormat;
import java.util.*;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.comixedproject.loaders.AbstractEntryLoader;
import org.comixedproject.loaders.EntryLoaderException;
import org.comixedproject.model.comic.Comic;
import org.comixedproject.model.comic.Credit;
import org.springframework.stereotype.Component;
/**
* * <code>ComicInfoEntryAdaptor</code> loads data from the ComicInfo.xml file created by ComicRack.
*
* @author Darryl L. Pierce
*/
@Component
@Log4j2
public class ComicInfoEntryAdaptor extends AbstractEntryLoader {
private final XMLInputFactory xmlInputFactory;
public ComicInfoEntryAdaptor() {
this.xmlInputFactory = XMLInputFactory.newInstance();
}
@Override
public void loadContent(
final Comic comic, final String filename, final byte[] content, final boolean ignoreMetadata)
throws EntryLoaderException {
if (ignoreMetadata) {
log.debug("Ignoring metadata");
return;
}
try {
this.loadXmlData(new ByteArrayInputStream(content), comic);
} catch (final XMLStreamException error) {
throw new EntryLoaderException(error);
}
}
protected void loadXmlData(InputStream istream, Comic comic) throws XMLStreamException {
final var xmlInputReader = this.xmlInputFactory.createXMLStreamReader(istream);
int publishedYear = -1;
int publishedMonth = -1;
while (xmlInputReader.hasNext()) {
if (xmlInputReader.isStartElement()) {
final String tagName = xmlInputReader.getLocalName();
log.debug("Processing tag: " + tagName);
switch (tagName) {
case "Publisher":
comic.setPublisher(xmlInputReader.getElementText());
break;
case "Series":
comic.setSeries(xmlInputReader.getElementText());
break;
case "Volume":
comic.setVolume(xmlInputReader.getElementText());
break;
case "Title":
comic.setTitle(xmlInputReader.getElementText());
break;
case "Number":
comic.setIssueNumber(xmlInputReader.getElementText());
break;
case "Summary":
comic.setDescription(xmlInputReader.getElementText());
break;
case "Year":
publishedYear = Integer.valueOf(xmlInputReader.getElementText());
break;
case "Month":
publishedMonth = Integer.valueOf(xmlInputReader.getElementText());
break;
case "Web":
break;
case "PageCount":
break;
case "Characters":
List<String> characters = this.commandSeparatedList(xmlInputReader.getElementText());
for (String character : characters) {
comic.getCharacters().add(character);
}
break;
case "Teams":
List<String> teams = this.commandSeparatedList(xmlInputReader.getElementText());
for (String team : teams) {
comic.getTeams().add(team);
}
break;
case "Locations":
List<String> locations = this.commandSeparatedList(xmlInputReader.getElementText());
for (String location : locations) {
comic.getLocations().add(location);
}
break;
case "Writer":
case "Editor":
case "Penciller":
case "Inker":
case "Colorist":
case "Letterer":
case "CoverArtist":
{
List<String> names = this.commandSeparatedList(xmlInputReader.getElementText());
for (String name : names) {
String role = tagName.toLowerCase();
if (role.equals("coverartist")) {
role = "cover";
}
log.debug("Adding role: {}={}", role, name);
comic.getCredits().add(new Credit(comic, name, role));
}
}
break;
default:
log.debug("Unrecognized tag");
break;
}
}
xmlInputReader.next();
}
// if we have the published year and/or month then set them
if (publishedYear > -1) {
GregorianCalendar gc =
(publishedMonth > -1)
? new GregorianCalendar(publishedYear, publishedMonth - 1, 1)
: new GregorianCalendar(publishedYear, 0, 1);
comic.setCoverDate(gc.getTime());
}
}
private List<String> commandSeparatedList(String text) {
List<String> result = new ArrayList<>();
var tokens = new StringTokenizer(text, ",");
while (tokens.hasMoreTokens()) {
result.add(tokens.nextToken().trim());
}
return result;
}
/**
* Writes the content of the provided comic as a ComicInfo.xml file to the provided output stream.
*
* @param comic the comic
* @return the content of the file
* @throws IOException if an error occurs
*/
public byte[] saveContent(Comic comic) throws IOException {
log.debug("Generating comic info data from comic");
var result = new ByteArrayOutputStream();
var writer = new PrintWriter(new OutputStreamWriter(result));
writer.write(
"<?xml version=\"1.0\"?><ComicInfo xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
this.writeEntry(writer, "Publisher", comic.getPublisher());
this.writeEntry(writer, "Series", comic.getSeries());
this.writeEntry(writer, "Volume", comic.getVolume());
this.writeEntry(writer, "Number", comic.getIssueNumber());
this.writeEntry(writer, "Title", comic.getTitle());
this.writeEntry(writer, "Summary", comic.getDescription());
if (comic.getCoverDate() != null) {
var gc = Calendar.getInstance();
gc.setTime(comic.getCoverDate());
this.writeEntry(writer, "Year", String.valueOf(gc.get(Calendar.YEAR)));
this.writeEntry(writer, "Month", String.valueOf(gc.get(Calendar.MONTH) + 1));
}
this.writeEntry(writer, "PageCount", String.valueOf(comic.getPageCount()));
this.writeEntry(writer, "Characters", StringUtils.join(comic.getCharacters(), ","));
this.writeEntry(writer, "Teams", StringUtils.join(comic.getTeams(), ","));
this.writeEntry(writer, "Locations", StringUtils.join(comic.getLocations(), ","));
writer.write("</ComicInfo>");
writer.flush();
return result.toByteArray();
}
private void writeEntry(PrintWriter writer, String tagName, String value) {
if (value != null) {
writer.write(MessageFormat.format("<{0}>{1}</{0}>", tagName, value));
}
}
}