Skip to content

Commit 7354d2d

Browse files
- relaxed the conditions for package images a bit - missing binary data now returns null instead of throwing an exception
- lots of dependency updates
1 parent 96d0679 commit 7354d2d

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>io.frictionlessdata</groupId>
66
<artifactId>datapackage-java</artifactId>
7-
<version>0.9.8-SNAPSHOT</version>
7+
<version>0.9.9-SNAPSHOT</version>
88
<packaging>jar</packaging>
99
<issueManagement>
1010
<url>https://github.com/frictionlessdata/datapackage-java/issues</url>
@@ -23,7 +23,7 @@
2323
<maven.compiler.source>${java.version}</maven.compiler.source>
2424
<maven.compiler.target>${java.version}</maven.compiler.target>
2525
<maven.compiler.compiler>${java.version}</maven.compiler.compiler>
26-
<tableschema-java-version>0.9.8</tableschema-java-version>
26+
<tableschema-java-version>0.9.9</tableschema-java-version>
2727
<junit.version>6.0.0</junit.version>
2828
<slf4j-simple.version>2.0.17</slf4j-simple.version>
2929
<apache-commons-collections4.version>4.5.0</apache-commons-collections4.version>

src/main/java/io/frictionlessdata/datapackage/JSONBase.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -336,33 +336,38 @@ private static ZipEntry findZipEntry(ZipFile zipFile, String fileName) {
336336
return null;
337337
}
338338

339-
protected static String getZipFileContentAsString(Path inFilePath, String fileName) throws IOException {
339+
protected static String getZipFileContentAsString(Path inFilePath, String fileName, boolean strict) throws IOException {
340340
// Read in memory the file inside the zip.
341341
ZipFile zipFile = new ZipFile(inFilePath.toFile());
342342
ZipEntry entry = findZipEntry(zipFile, fileName);
343343

344-
// Throw exception if expected datapackage.json file not found.
345-
if(entry == null){
346-
throw new DataPackageException("The zip file does not contain the expected file: " + fileName);
344+
if (entry == null) {
345+
if (strict) {
346+
throw new DataPackageException("The zip file does not contain the expected file: " + fileName);
347+
} else {
348+
return null;
349+
}
347350
}
348351

349352
String content;
350-
// Read the datapackage.json file inside the zip
351353
try (InputStream stream = zipFile.getInputStream(entry)) {
352354
content = getFileContentAsString(stream);
353355
}
354356
zipFile.close();
355357
return content;
356358
}
357359

358-
protected static byte[] getZipFileContentAsByteArray(Path inFilePath, String fileName) throws IOException {
360+
protected static byte[] getZipFileContentAsByteArray(Path inFilePath, String fileName, boolean strict) throws IOException {
359361
// Read in memory the file inside the zip.
360362
ZipFile zipFile = new ZipFile(inFilePath.toFile());
361363
ZipEntry entry = findZipEntry(zipFile, fileName);
362364

363-
// Throw exception if expected datapackage.json file not found.
364-
if(entry == null){
365-
throw new DataPackageException("The zip file does not contain the expected file: " + fileName);
365+
if (entry == null) {
366+
if (strict) {
367+
throw new DataPackageException("The zip file does not contain the expected file: " + fileName);
368+
} else {
369+
return null;
370+
}
366371
}
367372
try (InputStream inputStream = zipFile.getInputStream(entry);
368373
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
@@ -380,7 +385,7 @@ public static ObjectNode dereference(File fileObj, Path basePath, boolean isArch
380385
if (File.separator.equals("\\")) {
381386
filePath = filePath.replaceAll("\\\\", "/");
382387
}
383-
jsonContentString = getZipFileContentAsString(basePath, filePath);
388+
jsonContentString = getZipFileContentAsString(basePath, filePath, true);
384389
} else {
385390
/* If reference is file path.
386391
from the spec: "SECURITY: / (absolute path) and ../ (relative parent path)

src/main/java/io/frictionlessdata/datapackage/Package.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public Package(Path descriptorFile, boolean strict) throws Exception {
213213
if (isArchive(descriptorFile.toFile())) {
214214
isArchivePackage = true;
215215
basePath = descriptorFile;
216-
sourceJsonNode = createNode(JSONBase.getZipFileContentAsString(descriptorFile, DATAPACKAGE_FILENAME));
216+
sourceJsonNode = createNode(JSONBase.getZipFileContentAsString(descriptorFile, DATAPACKAGE_FILENAME, true));
217217
} else {
218218
basePath = descriptorFile.getParent();
219219
String sourceJsonString = getFileContentAsString(descriptorFile);
@@ -299,7 +299,9 @@ public String getImagePath() {
299299

300300
/**
301301
* Returns the image data if the image is stored inside the data package, null if {@link #getImagePath()}
302-
* would return a URL
302+
* would return a URL.
303+
*
304+
* If the referenced file does not exist in the package, return null
303305
*
304306
* @return binary image data
305307
*/
@@ -309,7 +311,7 @@ public byte[] getImage() throws IOException {
309311
return imageData;
310312
if (!StringUtils.isEmpty(image)) {
311313
if (isArchivePackage) {
312-
return getZipFileContentAsByteArray((Path)basePath, image);
314+
return getZipFileContentAsByteArray((Path)basePath, image, false);
313315
} else {
314316
File imgFile = new File (((Path)basePath).toFile(), image);
315317
try (InputStream inputStream = new FileInputStream(imgFile);

src/main/java/io/frictionlessdata/datapackage/resource/FilebasedResource.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public File getBasePath() {
9696
byte[] getRawData(File input) throws IOException {
9797
if (this.isInArchive) {
9898
String fileName = input.getPath().replaceAll("\\\\", "/");
99-
return getZipFileContentAsString (basePath.toPath(), fileName).getBytes(charset);
99+
return getZipFileContentAsString (basePath.toPath(), fileName, true).getBytes(charset);
100100
} else {
101101
File file = new File(this.basePath, input.getPath());
102102
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
@@ -133,24 +133,24 @@ public String[] getHeaders() throws Exception{
133133
List<Table> readData () throws Exception{
134134
List<Table> tables;
135135
if (this.isInArchive) {
136-
tables = readfromZipFile();
136+
tables = readFromZipFile();
137137
} else {
138-
tables = readfromOrdinaryFile();
138+
tables = readFromOrdinaryFile();
139139
}
140140
return tables;
141141
}
142142

143-
private List<Table> readfromZipFile() throws IOException {
143+
private List<Table> readFromZipFile() throws IOException {
144144
List<Table> tables = new ArrayList<>();
145145
for (File file : paths) {
146146
String fileName = file.getPath().replaceAll("\\\\", "/");
147-
String content = getZipFileContentAsString (basePath.toPath(), fileName);
147+
String content = getZipFileContentAsString (basePath.toPath(), fileName, true);
148148
Table table = Table.fromSource(content, schema, getCsvFormat());
149149
tables.add(table);
150150
}
151151
return tables;
152152
}
153-
private List<Table> readfromOrdinaryFile() throws IOException {
153+
private List<Table> readFromOrdinaryFile() throws IOException {
154154
List<Table> tables = new ArrayList<>();
155155
for (File file : paths) {
156156
/* from the spec: "SECURITY: / (absolute path) and ../ (relative parent path)

0 commit comments

Comments
 (0)