16
16
package nl .knaw .dans .dvingest .core .dansbag .deposit ;
17
17
18
18
import gov .loc .repository .bagit .domain .Bag ;
19
+ import gov .loc .repository .bagit .reader .BagReader ;
19
20
import nl .knaw .dans .dvingest .core .dansbag .exception .InvalidDepositException ;
20
- import nl .knaw .dans .dvingest .core .dansbag .service .BagDataManager ;
21
21
import nl .knaw .dans .dvingest .core .dansbag .service .ManifestHelper ;
22
+ import nl .knaw .dans .dvingest .core .dansbag .service .ManifestHelperImpl ;
23
+ import nl .knaw .dans .dvingest .core .dansbag .service .XPathEvaluator ;
22
24
import nl .knaw .dans .dvingest .core .dansbag .service .XmlReader ;
23
25
import org .apache .commons .configuration2 .Configuration ;
26
+ import org .apache .commons .configuration2 .FileBasedConfiguration ;
27
+ import org .apache .commons .configuration2 .PropertiesConfiguration ;
28
+ import org .apache .commons .configuration2 .builder .FileBasedConfigurationBuilder ;
29
+ import org .apache .commons .configuration2 .builder .fluent .Parameters ;
30
+ import org .apache .commons .configuration2 .ex .ConfigurationException ;
24
31
import org .apache .commons .lang3 .StringUtils ;
25
32
import org .w3c .dom .Document ;
33
+ import org .w3c .dom .Node ;
26
34
import org .xml .sax .SAXException ;
27
35
28
36
import javax .xml .parsers .ParserConfigurationException ;
32
40
import java .time .OffsetDateTime ;
33
41
import java .util .List ;
34
42
import java .util .Optional ;
43
+ import java .util .stream .Collectors ;
44
+
45
+ import static nl .knaw .dans .dvingest .core .dansbag .service .XPathConstants .FILES_FILE ;
35
46
36
47
public class DepositReaderImpl implements DepositReader {
48
+ private static final String DEPOSIT_PROPERTIES_FILENAME = "deposit.properties" ;
49
+
37
50
private final XmlReader xmlReader ;
38
51
private final BagDirResolver bagDirResolver ;
39
- private final BagDataManager bagDataManager ;
40
- private final DepositFileLister depositFileLister ;
52
+ private final BagReader bagReader ;
41
53
42
54
private final ManifestHelper manifestHelper ;
43
55
44
- public DepositReaderImpl (XmlReader xmlReader , BagDirResolver bagDirResolver , BagDataManager bagDataManager , DepositFileLister depositFileLister , ManifestHelper manifestHelper ) {
56
+ public DepositReaderImpl (XmlReader xmlReader , BagDirResolver bagDirResolver , BagReader bagReader , ManifestHelper manifestHelper ) {
45
57
this .xmlReader = xmlReader ;
46
58
this .bagDirResolver = bagDirResolver ;
47
- this .bagDataManager = bagDataManager ;
48
- this .depositFileLister = depositFileLister ;
59
+ this .bagReader = bagReader ;
49
60
this .manifestHelper = manifestHelper ;
50
61
}
51
62
@@ -54,18 +65,18 @@ public DansBagDeposit readDeposit(Path depositDir) throws InvalidDepositExceptio
54
65
try {
55
66
var bagDir = bagDirResolver .getBagDir (depositDir );
56
67
57
- var config = bagDataManager . readDepositProperties (depositDir );
58
- var bag = bagDataManager . readBag (bagDir );
68
+ var config = readDepositProperties (depositDir );
69
+ var bag = bagReader . read (bagDir );
59
70
manifestHelper .ensureSha1ManifestPresent (bag );
60
71
61
72
var deposit = mapToDeposit (depositDir , bagDir , config , bag );
62
73
63
74
deposit .setBag (bag );
64
- deposit .setDdm (readOptionalXmlFile (deposit .getDdmPath ()));
65
- deposit .setFilesXml (readOptionalXmlFile (deposit .getFilesXmlPath ()));
75
+ deposit .setDdm (readRequiredXmlFile (deposit .getDdmPath ()));
76
+ deposit .setFilesXml (readRequiredXmlFile (deposit .getFilesXmlPath ()));
66
77
deposit .setAmd (readOptionalXmlFile (deposit .getAmdPath ()));
67
78
68
- deposit .setFiles (depositFileLister . getDepositFiles (deposit ));
79
+ deposit .setFiles (getDepositFiles (deposit ));
69
80
70
81
return deposit ;
71
82
}
@@ -74,15 +85,54 @@ public DansBagDeposit readDeposit(Path depositDir) throws InvalidDepositExceptio
74
85
}
75
86
}
76
87
77
- Document readOptionalXmlFile (Path path ) throws ParserConfigurationException , IOException , SAXException {
88
+
89
+ private Configuration readDepositProperties (Path depositDir ) throws ConfigurationException {
90
+ var propertiesFile = depositDir .resolve (DEPOSIT_PROPERTIES_FILENAME );
91
+ var params = new Parameters ();
92
+ var paramConfig = params .properties ()
93
+ .setFileName (propertiesFile .toString ());
94
+
95
+ var builder = new FileBasedConfigurationBuilder <FileBasedConfiguration >
96
+ (PropertiesConfiguration .class , null , true )
97
+ .configure (paramConfig );
98
+
99
+ return builder .getConfiguration ();
100
+ }
101
+
102
+ private List <DepositFile > getDepositFiles (DansBagDeposit dansBagDeposit ) throws IOException {
103
+ var bag = dansBagDeposit .getBag ();
104
+ var filePathToSha1 = ManifestHelperImpl .getFilePathToSha1 (bag );
105
+
106
+ return XPathEvaluator .nodes (dansBagDeposit .getFilesXml (), FILES_FILE )
107
+ .map (node -> {
108
+ var filePath = Optional .ofNullable (node .getAttributes ().getNamedItem ("filepath" ))
109
+ .map (Node ::getTextContent )
110
+ .map (Path ::of )
111
+ .orElseThrow (() -> new IllegalArgumentException ("File element without filepath attribute" ));
112
+
113
+ var sha1 = filePathToSha1 .get (filePath );
114
+
115
+ return new DepositFile (filePath , sha1 , node );
116
+ })
117
+ .collect (Collectors .toList ());
118
+ }
119
+
120
+ private Document readRequiredXmlFile (Path path ) throws ParserConfigurationException , IOException , SAXException {
121
+ if (!Files .exists (path )) {
122
+ throw new IllegalArgumentException ("Required file not found: " + path );
123
+ }
124
+ return xmlReader .readXmlFile (path );
125
+ }
126
+
127
+ private Document readOptionalXmlFile (Path path ) throws ParserConfigurationException , IOException , SAXException {
78
128
if (Files .exists (path )) {
79
129
return xmlReader .readXmlFile (path );
80
130
}
81
131
82
132
return null ;
83
133
}
84
134
85
- DansBagDeposit mapToDeposit (Path path , Path bagDir , Configuration config , Bag bag ) {
135
+ private DansBagDeposit mapToDeposit (Path path , Path bagDir , Configuration config , Bag bag ) {
86
136
var deposit = new DansBagDeposit ();
87
137
deposit .setBagDir (bagDir );
88
138
deposit .setDir (path );
0 commit comments