Skip to content

Commit

Permalink
feat(java): added toJSON method to Entry
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickersoft committed May 19, 2021
1 parent 474e10a commit d566821
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
57 changes: 28 additions & 29 deletions java/main/java/org/odict/Dictionary.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.odict;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.adamh.utils.NativeUtils;
import java.io.*;
Expand Down Expand Up @@ -56,11 +55,11 @@ public Dictionary(String path, Boolean index) throws IOException {
}

public Entry lookup(String term) {
System.out.println(Arrays.toString(term.getBytes(StandardCharsets.UTF_8)));
System.out.println(Arrays.toString(term.getBytes(StandardCharsets.UTF_8)));
schema.Entry entry = this.dict.entriesByKey(term.toLowerCase());

if (entry == null) {
return null;
return null;
}

return new Entry(entry);
Expand All @@ -83,45 +82,45 @@ public String search(String query) {
}

private schema.Dictionary read(String filePath) throws IOException {
try (FileInputStream fis = new FileInputStream(filePath)) {
try (BufferedInputStream stream = new BufferedInputStream(fis)) {
// Read in signature and validate it
byte[] signature = new byte[5];
try (FileInputStream fis = new FileInputStream(filePath)) {
try (BufferedInputStream stream = new BufferedInputStream(fis)) {
// Read in signature and validate it
byte[] signature = new byte[5];

stream.read(signature, 0, 5);
stream.read(signature, 0, 5);

// Validate file signature
if (!new String(signature).equals("ODICT")) {
throw new Error("Invalid ODict file signature");
}
// Validate file signature
if (!new String(signature).equals("ODICT")) {
throw new Error("Invalid ODict file signature");
}

// Read in version number
byte[] version_b = new byte[2];
// Read in version number
byte[] version_b = new byte[2];

stream.read(version_b);
stream.read(version_b);

short version = ByteBuffer.wrap(version_b).order(ByteOrder.LITTLE_ENDIAN).getShort();
short version = ByteBuffer.wrap(version_b).order(ByteOrder.LITTLE_ENDIAN).getShort();

// Read in length of compressed data
byte[] compressed_size_b = new byte[8];
// Read in length of compressed data
byte[] compressed_size_b = new byte[8];

stream.read(compressed_size_b);
stream.read(compressed_size_b);

long compressed_size = ByteBuffer.wrap(compressed_size_b).order(ByteOrder.LITTLE_ENDIAN).getLong();
long compressed_size = ByteBuffer.wrap(compressed_size_b).order(ByteOrder.LITTLE_ENDIAN).getLong();

// Read in compressed data
byte[] compressed = new byte[(int) compressed_size];
// Read in compressed data
byte[] compressed = new byte[(int) compressed_size];

stream.read(compressed);
stream.read(compressed);

// Decompress data
byte[] uncompressed = Snappy.uncompress(compressed);
// Decompress data
byte[] uncompressed = Snappy.uncompress(compressed);

this.version = version;
this.version = version;

// Convert to dictionary and return
return schema.Dictionary.getRootAsDictionary(ByteBuffer.wrap(uncompressed));
}
// Convert to dictionary and return
return schema.Dictionary.getRootAsDictionary(ByteBuffer.wrap(uncompressed));
}
}
}
}
6 changes: 6 additions & 0 deletions java/main/java/org/odict/models/Entry.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.odict.models;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -29,4 +31,8 @@ public String getTerm() {
public List<Etymology> getEtymologies() {
return etymologies;
}

public String toJSON() throws JsonProcessingException {
return new ObjectMapper().writeValueAsString(this);
}
}
1 change: 1 addition & 0 deletions java/test/java/org/odict/TestDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void testLookup() throws Exception {
Entry entry = dict.lookup("run");

assertNotNull(entry);
assertNotEquals(entry.toJSON(), "{}");
}

@Test
Expand Down

0 comments on commit d566821

Please sign in to comment.