1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
mod mem_file;
mod objects;
use objects::{IntoJObject, Pair};
pub use objects::{RELATIVE_AREA_CALCULATION_MODE, ABSOLUTE_AREA_CALCULATION_MODE, Rectangle, OutputFormat, ExtractionMethod};
use anyhow::Result;
use jni::{AttachGuard, InitArgsBuilder, JNIEnv, JNIVersion, JavaVM, objects::{JObject, JValue}, errors::Error as JError};
pub use jni;
use std::result::Result as StdResult;
use std::ops::Deref;
use std::path::Path;
pub type JResult<T> = StdResult<T, JError>;
pub struct TabulaVM(JavaVM);
impl <'env> TabulaVM {
pub fn new(libpath: &str, debug: bool) -> Result<Self> {
let mut jvm_args = InitArgsBuilder::new()
.version(JNIVersion::V8)
.option(&format!("-Djava.class.path={}", libpath));
if debug {
jvm_args = jvm_args.option("-Xcheck:jni");
}
let jvm_args = jvm_args.build()?;
Ok(Self(JavaVM::new(jvm_args)?))
}
pub fn attach(&'env self) -> Result<TabulaEnv<'env>> {
Ok(TabulaEnv(self.0.attach_current_thread()?))
}
}
pub struct TabulaEnv<'env>(AttachGuard<'env>);
impl <'env> TabulaEnv<'env> {
fn get_pages_jarray(&self, pages: &[i32]) -> JResult<*mut jni::sys::_jobject> {
let null = JObject::null();
let array = self.new_object_array(pages.len() as i32, "java/lang/Integer", null)?;
for (i, pg) in pages.iter().enumerate() {
self.set_object_array_element(array, i as i32, pg.get_jobject(self)?)?;
}
Ok(array)
}
fn get_page_areas_jarray(&self, page_areas: &[(i32, Rectangle)]) -> JResult<*mut jni::sys::_jobject> {
let null = JObject::null();
let array = self.new_object_array(page_areas.len() as i32, "technology/tabula/Pair", null)?;
for (i, (mode, rect)) in page_areas.iter().enumerate() {
let pga = Pair::new(*mode, *rect);
self.set_object_array_element(array, i as i32, pga.get_jobject(self)?)?;
}
Ok(array)
}
pub fn configure_tabula(&self,
page_areas: Option<&[(i32, Rectangle)]>,
pages: Option<&[i32]>,
output_format: OutputFormat,
guess: bool,
method: ExtractionMethod,
use_returns: bool,
password: Option<&str>
) -> JResult<Tabula> {
let areas = if let Some(page_areas) = page_areas {
JValue::from(self.get_page_areas_jarray(page_areas)?)
} else {
JValue::from(JObject::null())
};
let pages = if let Some(pages) = pages {
JValue::from(self.get_pages_jarray(pages)?)
} else {
JValue::from(JObject::null())
};
let password = password
.map(|pw| self.new_string(pw).ok()).flatten()
.map(|jstr| JValue::from(jstr))
.unwrap_or(JValue::from(JObject::null()));
let tabula = self.new_object("technology/tabula/CommandLineApp", "([Ltechnology/tabula/Pair;[Ljava/lang/Integer;Ltechnology/tabula/CommandLineApp$OutputFormat;ZLtechnology/tabula/CommandLineApp$ExtractionMethod;ZLjava/lang/String;)V", &[
areas,
pages,
JValue::from(output_format.get_jobject(self)?),
JValue::from(guess),
JValue::from(method.get_jobject(self)?),
JValue::from(use_returns),
password
])?;
Ok(Tabula {
env: self,
inner: tabula
})
}
}
impl <'env> Deref for TabulaEnv<'env> {
type Target = JNIEnv<'env>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub struct Tabula<'env> {
env: &'env TabulaEnv<'env>,
inner: JObject<'env>
}
impl Tabula<'_> {
pub fn parse_document(&self, path: &Path, descriptor_name: &str) -> Result<std::fs::File> {
let output = unsafe { mem_file::TmpMemFile::new(descriptor_name) }?;
let file = path.get_jobject(self.env)?;
let outfile = unsafe { output.get_path() }.get_jobject(self.env)?;
self.env.call_method(self.deref().clone(), "extractFileInto", "(Ljava/io/File;Ljava/io/File;)V", &[
JValue::Object(file),
JValue::Object(outfile)
])?;
let file = unsafe { output.get_file() };
Ok(file)
}
}
impl <'env> Deref for Tabula<'env> {
type Target = JObject<'env>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
#[cfg(test)]
mod tests;