Skip to content

Commit 180f32e

Browse files
Internal refactorings
1 parent e5f2044 commit 180f32e

File tree

14 files changed

+270
-179
lines changed

14 files changed

+270
-179
lines changed

pom.xml

Lines changed: 4 additions & 17 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.7.3-SNAPSHOT</version>
7+
<version>0.7.5-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.7.3</tableschema-java-version>
26+
<tableschema-java-version>0.7.5</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>
@@ -213,14 +213,15 @@
213213
</build>
214214
<dependencies>
215215

216-
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
216+
<!-- logging -->
217217
<dependency>
218218
<groupId>org.slf4j</groupId>
219219
<artifactId>slf4j-simple</artifactId>
220220
<version>${slf4j-simple.version}</version>
221221
<scope>test</scope>
222222
</dependency>
223223

224+
<!-- collection helpers -->
224225
<dependency>
225226
<groupId>org.apache.commons</groupId>
226227
<artifactId>commons-collections4</artifactId>
@@ -233,14 +234,6 @@
233234
<artifactId>tableschema-java</artifactId>
234235
<version>${tableschema-java-version}</version>
235236
</dependency>
236-
<!--
237-
<dependency>
238-
<groupId>io.frictionlessdata</groupId>
239-
<artifactId>tableschema-java</artifactId>
240-
<version>0.6.2-SNAPSHOT</version>
241-
<scope>compile</scope>
242-
</dependency>
243-
-->
244237

245238
<!-- Unit Testing -->
246239
<dependency>
@@ -250,11 +243,5 @@
250243
<scope>test</scope>
251244
</dependency>
252245

253-
<dependency>
254-
<groupId>org.junit.vintage</groupId>
255-
<artifactId>junit-vintage-engine</artifactId>
256-
<version>${junit.version}</version>
257-
<scope>test</scope>
258-
</dependency>
259246
</dependencies>
260247
</project>
Lines changed: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package io.frictionlessdata.datapackage;
22

33
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
4+
import com.fasterxml.jackson.core.JsonParseException;
5+
import com.fasterxml.jackson.core.type.TypeReference;
6+
import com.fasterxml.jackson.databind.JsonNode;
47
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
8+
import com.fasterxml.jackson.databind.node.ArrayNode;
59
import io.frictionlessdata.datapackage.exceptions.DataPackageException;
10+
import io.frictionlessdata.tableschema.exception.JsonParsingException;
611
import io.frictionlessdata.tableschema.util.JsonUtil;
12+
import org.apache.commons.lang3.StringUtils;
713

814
import java.net.MalformedURLException;
915
import java.net.URL;
@@ -23,7 +29,7 @@ public class Contributor {
2329
private String title;
2430
private String email;
2531
private URL path;
26-
private Role role;
32+
private String role;
2733
private String organization;
2834

2935
public String getTitle() {
@@ -38,68 +44,37 @@ public URL getPath() {
3844
return path;
3945
}
4046

41-
public Role getRole() {
47+
public String getRole() {
4248
return role;
4349
}
4450

4551
public String getOrganization() {
4652
return organization;
4753
}
4854

49-
/**
50-
* Create a new Contributor object from a JSON representation
51-
*
52-
* @param jsonObj JSON representation, eg. from Package definition
53-
* @return new Dialect object with values from JSONObject
54-
*/
55-
public static Contributor fromJson(Map jsonObj) {
56-
if (null == jsonObj)
57-
return null;
58-
try {
59-
Contributor c = JsonUtil.getInstance().convertValue(jsonObj, Contributor.class);
60-
if (c.path != null && !isValidUrl(c.path)) {
61-
throw new DataPackageException(invalidUrlMsg);
62-
}
63-
return c;
64-
} catch (Exception ex) {
65-
Throwable cause = ex.getCause();
66-
if (Objects.nonNull(cause) && cause.getClass().isAssignableFrom(InvalidFormatException.class)) {
67-
if (Objects.nonNull(cause.getCause()) && cause.getCause().getClass().isAssignableFrom(MalformedURLException.class)) {
68-
throw new DataPackageException(invalidUrlMsg);
69-
}
70-
}
71-
throw new DataPackageException(ex);
72-
}
73-
}
74-
75-
/**
76-
* Create a new Contributor object from a JSON representation
77-
* @param jsonArr JSON representation, eg. from Package definition
78-
* @return new Dialect object with values from JSONObject
79-
*/
80-
public static Collection<Contributor> fromJson(Collection<Map<String,?>> jsonArr) {
81-
final Collection<Contributor> contributors = new ArrayList<>();
82-
Iterator<Map<String, ?>> iter = jsonArr.iterator();
83-
while (iter.hasNext()) {
84-
Map obj = (Map) iter.next();
85-
contributors.add(fromJson(obj));
55+
public static Collection<Contributor> fromJson(JsonNode json) {
56+
if ((null == json) || json.isEmpty() || (!(json instanceof ArrayNode))) {
57+
return null;
8658
}
87-
return contributors;
88-
}
8959

90-
public static Collection<Contributor> fromJson(String json) {
91-
Collection<Map<String, ?>> objArray = new ArrayList<>();
92-
JsonUtil.getInstance().createArrayNode(json).elements().forEachRemaining(o -> {
93-
objArray.add(JsonUtil.getInstance().convertValue(o, Map.class));
94-
});
95-
return fromJson(objArray);
60+
try {
61+
return JsonUtil.getInstance().deserialize(json, new TypeReference<>() {});
62+
} catch (Exception ex) {
63+
Throwable cause = ex.getCause();
64+
if (Objects.nonNull(cause) && cause.getClass().isAssignableFrom(InvalidFormatException.class)) {
65+
if (Objects.nonNull(cause.getCause()) && cause.getCause().getClass().isAssignableFrom(MalformedURLException.class)) {
66+
throw new DataPackageException(invalidUrlMsg);
67+
}
68+
}
69+
throw new DataPackageException(ex);
70+
}
9671
}
9772

98-
public static enum Role {
99-
AUTHOR,
100-
PUBLISHER,
101-
MAINTAINER,
102-
WRANGLER,
103-
CONTRIBUTOR
104-
}
73+
public static Collection<Contributor> fromJson(String json) {
74+
if (StringUtils.isEmpty(json)) {
75+
return null;
76+
}
77+
JsonNode jsonNode = JsonUtil.getInstance().readValue(json);
78+
return fromJson(jsonNode);
79+
}
10580
}

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
@JsonIgnoreProperties(ignoreUnknown = true)
4444
public class Dialect {
4545

46-
private FileReference reference;
46+
private FileReference<?> reference;
4747

4848
// we construct one instance that will always keep the default values
4949
public static Dialect DEFAULT = new Dialect(){
@@ -133,11 +133,11 @@ private void lazyCreate() {
133133
private Map<String, Object> additionalProperties = new HashMap<>();
134134

135135
@JsonIgnore
136-
public FileReference getReference() {
136+
public FileReference<?> getReference() {
137137
return reference;
138138
}
139139

140-
public void setReference (FileReference ref){
140+
public void setReference (FileReference<?> ref){
141141
reference = ref;
142142
}
143143

@@ -159,23 +159,24 @@ public Dialect clone() {
159159

160160
// will fail for multi-character delimiters. Oh my...
161161
public CSVFormat toCsvFormat() {
162-
CSVFormat format = CSVFormat.DEFAULT
163-
.withDelimiter(delimiter.charAt(0))
164-
.withEscape(escapeChar)
165-
.withIgnoreSurroundingSpaces(skipInitialSpace)
166-
.withNullString(nullSequence)
167-
.withCommentMarker(commentChar)
168-
.withSkipHeaderRecord(!hasHeaderRow)
169-
.withQuote(quoteChar)
170-
.withQuoteMode(doubleQuote ? QuoteMode.MINIMAL : QuoteMode.NONE);
162+
CSVFormat format = CSVFormat.DEFAULT.builder()
163+
.setDelimiter(delimiter.charAt(0))
164+
.setEscape(escapeChar)
165+
.setIgnoreSurroundingSpaces(skipInitialSpace)
166+
.setNullString(nullSequence)
167+
.setCommentMarker(commentChar)
168+
.setSkipHeaderRecord(!hasHeaderRow)
169+
.setQuote(quoteChar)
170+
.setQuoteMode(doubleQuote ? QuoteMode.MINIMAL : QuoteMode.NONE)
171+
.get();
171172
if (hasHeaderRow)
172-
format = format.withHeader();
173+
format = format.builder().setHeader().get();
173174
return format;
174175
}
175176

176177
public static Dialect fromCsvFormat(CSVFormat format) {
177178
Dialect dialect = new Dialect();
178-
dialect.setDelimiter(format.getDelimiter()+"");
179+
dialect.setDelimiter(format.getDelimiterString());
179180
dialect.setEscapeChar(format.getEscapeCharacter());
180181
dialect.setSkipInitialSpace(format.getIgnoreSurroundingSpaces());
181182
dialect.setNullSequence(format.getNullString());
@@ -194,7 +195,7 @@ public static Dialect fromCsvFormat(CSVFormat format) {
194195
* @param reference the File or URL to read dialect JSON data from
195196
* @throws Exception thrown if reading from the stream or parsing throws an exception
196197
*/
197-
public static Dialect fromJson (FileReference reference) throws Exception {
198+
public static Dialect fromJson (FileReference<?> reference) throws Exception {
198199
String dialectString = null;
199200
try (InputStreamReader ir = new InputStreamReader(reference.getInputStream(), StandardCharsets.UTF_8);
200201
BufferedReader br = new BufferedReader(ir)){

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
44
import com.fasterxml.jackson.annotation.JsonInclude.Include;
5+
import com.fasterxml.jackson.core.type.TypeReference;
56
import com.fasterxml.jackson.databind.JsonNode;
67
import com.fasterxml.jackson.databind.node.ArrayNode;
78
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -74,8 +75,8 @@ public abstract class JSONBase {
7475
protected String hash = null;
7576

7677
Dialect dialect;
77-
private ArrayNode sources = null;
78-
private ArrayNode licenses = null;
78+
private List<Source> sources = null;
79+
private List<License> licenses = null;
7980

8081
// Schema
8182
private Schema schema = null;
@@ -175,22 +176,22 @@ public void setProfile(String profile){
175176

176177
public void setSchema(Schema schema){this.schema = schema;}
177178

178-
public ArrayNode getSources(){
179+
public List<Source> getSources(){
179180
return sources;
180181
}
181182

182-
public void setSources(ArrayNode sources){
183+
public void setSources(List<Source> sources){
183184
this.sources = sources;
184185
}
185186
/**
186187
* @return the licenses
187188
*/
188-
public ArrayNode getLicenses(){return licenses;}
189+
public List<License> getLicenses(){return licenses;}
189190

190191
/**
191192
* @param licenses the licenses to set
192193
*/
193-
public void setLicenses(ArrayNode licenses){this.licenses = licenses;}
194+
public void setLicenses(List<License> licenses){this.licenses = licenses;}
194195

195196

196197
public Map<String, String> getOriginalReferences() {
@@ -258,13 +259,13 @@ public static void setFromJson(JsonNode resourceJson, JSONBase retVal, Schema sc
258259
Integer bytes = resourceJson.has(JSONBase.JSON_KEY_BYTES) ? resourceJson.get(JSONBase.JSON_KEY_BYTES).asInt() : null;
259260
String hash = textValueOrNull(resourceJson, JSONBase.JSON_KEY_HASH);
260261

261-
ArrayNode sources = null;
262+
List<Source> sources = null;
262263
if(resourceJson.has(JSONBase.JSON_KEY_SOURCES) && resourceJson.get(JSON_KEY_SOURCES).isArray()) {
263-
sources = (ArrayNode) resourceJson.get(JSON_KEY_SOURCES);
264+
sources = JsonUtil.getInstance().deserialize(resourceJson.get(JSONBase.JSON_KEY_SOURCES), new TypeReference<>() {});
264265
}
265-
ArrayNode licenses = null;
266+
List<License> licenses = null;
266267
if(resourceJson.has(JSONBase.JSON_KEY_LICENSES) && resourceJson.get(JSONBase.JSON_KEY_LICENSES).isArray()){
267-
licenses = (ArrayNode) resourceJson.get(JSONBase.JSON_KEY_LICENSES);
268+
licenses = JsonUtil.getInstance().deserialize(resourceJson.get(JSONBase.JSON_KEY_LICENSES), new TypeReference<>() {});
268269
}
269270

270271
retVal.setName(name);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.frictionlessdata.datapackage;
2+
3+
public class License {
4+
private String name;
5+
6+
private String path;
7+
8+
private String title;
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public void setName(String name) {
15+
this.name = name;
16+
}
17+
18+
public String getPath() {
19+
return path;
20+
}
21+
22+
public void setPath(String path) {
23+
this.path = path;
24+
}
25+
26+
public String getTitle() {
27+
return title;
28+
}
29+
30+
public void setTitle(String title) {
31+
this.title = title;
32+
}
33+
}

0 commit comments

Comments
 (0)