Skip to content

Commit d7a45da

Browse files
Better support and test for non-tabular resources. Added spotbugs maven plugin
1 parent e246fb3 commit d7a45da

File tree

9 files changed

+448
-40
lines changed

9 files changed

+448
-40
lines changed

pom.xml

Lines changed: 21 additions & 30 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.8.1-SNAPSHOT</version>
7+
<version>0.8.3-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.8.1</tableschema-java-version>
26+
<tableschema-java-version>0.8.3</tableschema-java-version>
2727
<junit.version>5.12.0</junit.version>
2828
<slf4j-simple.version>2.0.17</slf4j-simple.version>
2929
<apache-commons-collections4.version>4.4</apache-commons-collections4.version>
@@ -40,6 +40,7 @@
4040
<coveralls-maven-plugin.version>4.3.0</coveralls-maven-plugin.version>
4141
<dependency-check-maven.version>12.1.1</dependency-check-maven.version>
4242
<jacoco-maven-plugin.version>0.8.13</jacoco-maven-plugin.version>
43+
<spotbugs-maven-plugin.version>4.9.3.0</spotbugs-maven-plugin.version>
4344
</properties>
4445

4546
<repositories>
@@ -125,33 +126,6 @@
125126
</executions>
126127
</plugin>
127128

128-
<plugin>
129-
<groupId>org.apache.maven.plugins</groupId>
130-
<artifactId>maven-gpg-plugin</artifactId>
131-
<version>${maven-gpg-plugin.version}</version>
132-
<executions>
133-
<execution>
134-
<id>sign-artifacts</id>
135-
<phase>verify</phase>
136-
<goals>
137-
<goal>sign</goal>
138-
</goals>
139-
</execution>
140-
</executions>
141-
</plugin>
142-
143-
<plugin>
144-
<groupId>org.sonatype.plugins</groupId>
145-
<artifactId>nexus-staging-maven-plugin</artifactId>
146-
<version>${nexus-staging-maven-plugin.version}</version>
147-
<extensions>true</extensions>
148-
<configuration>
149-
<serverId>ossrh</serverId>
150-
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
151-
<autoReleaseAfterClose>true</autoReleaseAfterClose>
152-
</configuration>
153-
</plugin>
154-
155129
<plugin>
156130
<groupId>org.jacoco</groupId>
157131
<artifactId>jacoco-maven-plugin</artifactId>
@@ -188,7 +162,6 @@
188162
</executions>
189163
</plugin>
190164

191-
192165
<plugin>
193166
<groupId>org.apache.maven.plugins</groupId>
194167
<artifactId>maven-dependency-plugin</artifactId>
@@ -206,6 +179,24 @@
206179
</execution>
207180
</executions>
208181
</plugin>
182+
183+
<plugin>
184+
<groupId>com.github.spotbugs</groupId>
185+
<artifactId>spotbugs-maven-plugin</artifactId>
186+
<version>${spotbugs-maven-plugin.version}</version>
187+
<configuration>
188+
<effort>Max</effort>
189+
<threshold>High</threshold>
190+
<xmlOutput>false</xmlOutput>
191+
</configuration>
192+
<executions>
193+
<execution>
194+
<goals>
195+
<goal>check</goal>
196+
</goals>
197+
</execution>
198+
</executions>
199+
</plugin>
209200
</plugins>
210201
<resources>
211202
<resource>

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141
"skipInitialSpace"
4242
})
4343
@JsonIgnoreProperties(ignoreUnknown = true)
44-
public class Dialect {
44+
public class Dialect implements Cloneable {
4545

4646
private FileReference<?> reference;
4747

4848
// we construct one instance that will always keep the default values
49-
public static Dialect DEFAULT = new Dialect(){
49+
public static final Dialect DEFAULT = new Dialect(){
5050
private JsonNode JsonNode;
5151

5252
public String getJson() {
@@ -141,7 +141,9 @@ public void setReference (FileReference<?> ref){
141141
reference = ref;
142142
}
143143

144-
public Dialect clone() {
144+
@Override
145+
public Dialect clone() throws CloneNotSupportedException {
146+
super.clone();
145147
Dialect retVal = new Dialect();
146148
retVal.delimiter = this.delimiter;
147149
retVal.escapeChar = this.escapeChar;
@@ -239,7 +241,7 @@ public void writeJson (File outputFile) throws IOException{
239241
}
240242

241243
public void writeJson (OutputStream output) throws IOException{
242-
try (BufferedWriter file = new BufferedWriter(new OutputStreamWriter(output))) {
244+
try (BufferedWriter file = new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8))) {
243245
file.write(this.getJson());
244246
}
245247
}

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.net.URI;
3232
import java.net.URISyntaxException;
3333
import java.net.URL;
34+
import java.nio.charset.Charset;
3435
import java.nio.charset.StandardCharsets;
3536
import java.nio.file.FileSystem;
3637
import java.nio.file.*;
@@ -64,6 +65,11 @@ public class Package extends JSONBase{
6465
JSON_KEY_KEYWORDS, JSONBase.JSON_KEY_SCHEMA, JSONBase.JSON_KEY_NAME, JSONBase.JSON_KEY_DATA,
6566
JSONBase.JSON_KEY_DIALECT, JSONBase.JSON_KEY_LICENSES, JSONBase.JSON_KEY_SOURCES, JSONBase.JSON_KEY_PROFILE);
6667

68+
/**
69+
* The charset (encoding) for writing
70+
*/
71+
@JsonIgnore
72+
private final Charset charset = StandardCharsets.UTF_8;
6773

6874
// Filesystem path pointing to the Package; either ZIP file or directory
6975
private Object basePath = null;
@@ -544,6 +550,19 @@ public void write (File outputDir, boolean zipCompressed) throws Exception {
544550
write (outputDir, null, zipCompressed);
545551
}
546552

553+
private File findOrCreateOutputDir(File outputDir, boolean zipCompressed) {
554+
if (!zipCompressed) {
555+
String fName = outputDir.getName().toLowerCase();
556+
if ((fName.endsWith(".zip") || (fName.endsWith(".json")))) {
557+
outputDir = outputDir.getParentFile();
558+
}
559+
if (!outputDir.exists()) {
560+
outputDir.mkdirs();
561+
}
562+
}
563+
return outputDir;
564+
}
565+
547566
/**
548567
* Write this datapackage to an output directory or ZIP file. Creates at least a
549568
* datapackage.json file and if this Package object holds file-based
@@ -558,10 +577,11 @@ public void write (File outputDir, boolean zipCompressed) throws Exception {
558577
*/
559578
public void write (File outputDir, Consumer<Path> callback, boolean zipCompressed) throws Exception {
560579
this.isArchivePackage = zipCompressed;
561-
FileSystem outFs = getTargetFileSystem(outputDir, zipCompressed);
580+
File outDir = findOrCreateOutputDir(outputDir, zipCompressed);
581+
FileSystem outFs = getTargetFileSystem(outDir, zipCompressed);
562582
String parentDirName = "";
563583
if (!zipCompressed) {
564-
parentDirName = outputDir.getPath();
584+
parentDirName = outDir.getPath();
565585
}
566586

567587
// only file-based Resources need to be written to the DataPackage, URLs stay as
@@ -641,7 +661,7 @@ public void writeJson (File outputFile) throws IOException{
641661
* @throws IOException if writing fails
642662
*/
643663
public void writeJson (OutputStream output) throws IOException{
644-
try (BufferedWriter file = new BufferedWriter(new OutputStreamWriter(output))) {
664+
try (BufferedWriter file = new BufferedWriter(new OutputStreamWriter(output, charset))) {
645665
file.write(this.getJsonNode().toPrettyString());
646666
}
647667
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.io.IOException;
1313
import java.io.InputStream;
1414
import java.nio.charset.Charset;
15+
import java.nio.charset.StandardCharsets;
1516
import java.nio.file.Files;
1617
import java.nio.file.Path;
1718
import java.util.*;
@@ -25,6 +26,12 @@ public class FilebasedResource extends AbstractReferencebasedResource<File> {
2526
@JsonIgnore
2627
private boolean isInArchive;
2728

29+
@JsonIgnore
30+
/**
31+
* The charset (encoding) for writing
32+
*/
33+
private final Charset charset = StandardCharsets.UTF_8;
34+
2835
public FilebasedResource(String name, Collection<File> paths, File basePath, Charset encoding) {
2936
super(name, paths);
3037
this.encoding = encoding.name();
@@ -85,7 +92,7 @@ public File getBasePath() {
8592
byte[] getRawData(File input) throws IOException {
8693
if (this.isInArchive) {
8794
String fileName = input.getPath().replaceAll("\\\\", "/");
88-
return getZipFileContentAsString (basePath.toPath(), fileName).getBytes();
95+
return getZipFileContentAsString (basePath.toPath(), fileName).getBytes(charset);
8996
} else {
9097
File file = new File(this.basePath, input.getPath());
9198
try (InputStream inputStream = Files.newInputStream(file.toPath())) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public interface Resource<T> extends BaseInterface {
162162
*
163163
* @return A CSV representation of the data as a String.
164164
*/
165+
@JsonIgnore
165166
String getDataAsCsv();
166167

167168
/**

src/test/java/io/frictionlessdata/datapackage/DialectTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void testDialectFromJson() {
6060

6161
@Test
6262
@DisplayName("clone Dialect")
63-
void testCloneDialect() {
63+
void testCloneDialect() throws CloneNotSupportedException {
6464
String json = "{ "+
6565
" \"delimiter\":\"\t\", "+
6666
" \"header\":\"false\", "+

0 commit comments

Comments
 (0)