forked from comixed/comixed
/
ComicRackBackupAdaptor.java
189 lines (168 loc) · 6.64 KB
/
ComicRackBackupAdaptor.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
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2019, 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.comixed.importer.adaptors;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.apache.commons.lang3.time.DateUtils;
import org.comixed.model.library.Comic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* <code>ComicRackBackupAdaptor</code> handles reading individual book entries from a ComicRack
* database.
*
* @author Darryl L. Pierce
*/
@Component
public class ComicRackBackupAdaptor {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
/**
* @param filename
* @return a list of comic files
* @throws ImportAdaptorException if an error occurs
*/
public List<Comic> load(
File filename, Map<String, String> currentPages, Map<String, String> booksguids)
throws ImportAdaptorException {
try {
this.logger.debug("Opening file for reading");
FileInputStream istream = new FileInputStream(filename);
this.logger.debug("Processing file content");
List<Comic> result = this.loadFromXml(istream, currentPages, booksguids);
this.logger.debug("Closing file");
istream.close();
this.logger.debug("Returning {} comic entries", result.size());
return result;
} catch (IOException | XMLStreamException | ParseException error) {
throw new ImportAdaptorException("unable to read file", error);
}
}
private List<Comic> loadFromXml(
InputStream istream, Map<String, String> currentPages, Map<String, String> booksguids)
throws XMLStreamException, ParseException {
List<Comic> result = new ArrayList<>();
final XMLStreamReader xmlInputReader = this.xmlInputFactory.createXMLStreamReader(istream);
this.logger.debug("Reading contents of XML file");
Comic comic = null;
while (xmlInputReader.hasNext()) {
if (xmlInputReader.isStartElement()) {
String tagName = xmlInputReader.getLocalName();
switch (tagName) {
case "Book":
{
this.logger.debug("Starting new comic file");
comic = new Comic();
String filename = xmlInputReader.getAttributeValue(null, "File");
this.logger.debug("Filename: {}", filename);
comic.setFilename(filename);
result.add(comic);
booksguids.put(xmlInputReader.getAttributeValue(null, "Id"), filename);
}
break;
case "Added":
{
this.logger.debug("Setting added date");
Date date =
DateUtils.parseDate(
xmlInputReader.getElementText(), "yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
this.logger.debug("Added date: {}", date.toString());
comic.setDateAdded(date);
}
break;
case "CurrentPage":
{
this.logger.debug("Setting current page");
String currentPage = xmlInputReader.getElementText();
this.logger.debug("Added current page: {}", currentPage);
currentPages.put(comic.getFilename(), currentPage);
}
break;
default:
// this.logger.debug("Unsupported tag");
break;
}
}
xmlInputReader.next();
}
return result;
}
/**
* @param filename
* @return a list of comic files
* @throws ImportAdaptorException if an error occurs
*/
public Map<String, List> loadLists(File filename, Map<String, String> booksguids)
throws ImportAdaptorException {
try {
this.logger.debug("Opening file for reading");
FileInputStream istream = new FileInputStream(filename);
this.logger.debug("Processing file content");
Map<String, List> result = this.loadListsFromXml(istream, booksguids);
this.logger.debug("Closing file");
istream.close();
this.logger.debug("Returning {} comic entries", result.size());
return result;
} catch (IOException | XMLStreamException | ParseException error) {
throw new ImportAdaptorException("unable to read file", error);
}
}
private Map<String, List> loadListsFromXml(InputStream istream, Map<String, String> booksguids)
throws XMLStreamException, ParseException {
Map<String, List> result = new HashMap<>();
final XMLStreamReader xmlInputReader = this.xmlInputFactory.createXMLStreamReader(istream);
this.logger.debug("Reading lists of XML file");
while (xmlInputReader.hasNext()) {
if (xmlInputReader.isStartElement()
&& xmlInputReader.getLocalName().equalsIgnoreCase("Item")
&& xmlInputReader
.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance", "type")
.equalsIgnoreCase("ComicIdListItem")) {
String listName = xmlInputReader.getAttributeValue(null, "Name");
List<String> comics = new ArrayList<>();
while (xmlInputReader.hasNext()) {
if (xmlInputReader.isStartElement()
&& xmlInputReader.getLocalName().equalsIgnoreCase("guid")) {
String guid = xmlInputReader.getElementText();
String filename = booksguids.get(guid);
if (filename == null) this.logger.debug("Book not found for guid {}", guid);
else comics.add(filename);
}
xmlInputReader.next();
if (xmlInputReader.isStartElement()
&& xmlInputReader.getLocalName().equalsIgnoreCase("Item")) break;
}
result.put(listName, comics);
}
xmlInputReader.next();
}
return result;
}
}