pub mod backends;
pub mod xhtml_parser;
use crate::preprocessors::traits::Preprocessor;
use crate::types::*;
use anyhow::Result;
use std::path::Path;
pub use backends::PdfBackend;
#[cfg(feature = "jni-backend")]
pub use backends::TikaJniBackend;
pub enum PdfBackendImpl {
#[cfg(feature = "jni-backend")]
Jni(TikaJniBackend),
}
impl PdfBackend for PdfBackendImpl {
fn extract_to_xhtml(&self, pdf_bytes: &[u8]) -> Result<String> {
match self {
#[cfg(feature = "jni-backend")]
PdfBackendImpl::Jni(backend) => backend.extract_to_xhtml(pdf_bytes),
}
}
fn name(&self) -> &str {
match self {
#[cfg(feature = "jni-backend")]
PdfBackendImpl::Jni(backend) => backend.name(),
}
}
fn is_healthy(&self) -> bool {
match self {
#[cfg(feature = "jni-backend")]
PdfBackendImpl::Jni(backend) => backend.is_healthy(),
}
}
}
pub struct PdfPreprocessor {
backend: PdfBackendImpl,
}
impl PdfPreprocessor {
#[cfg(feature = "jni-backend")]
pub fn new_with_jni(jre_path: &Path, jar_path: &Path) -> Result<Self> {
Ok(Self {
backend: PdfBackendImpl::Jni(TikaJniBackend::new(jre_path, jar_path)?),
})
}
#[cfg(feature = "jni-backend")]
pub fn new_with_jni_args(jre_path: &Path, jar_path: &Path, jvm_args: &[String]) -> Result<Self> {
Ok(Self {
backend: PdfBackendImpl::Jni(TikaJniBackend::new_with_args(
jre_path, jar_path, jvm_args,
)?),
})
}
pub fn backend_name(&self) -> &str {
self.backend.name()
}
pub fn is_healthy(&self) -> bool {
self.backend.is_healthy()
}
}
impl Preprocessor for PdfPreprocessor {
fn parse_pdf_to_markup_language(&self, pdf_bytes: &[u8]) -> Result<String> {
self.backend.extract_to_xhtml(pdf_bytes)
}
fn parse_markup_to_preprocessor_output(&self, markup: &str) -> Result<PreprocessorOutput> {
xhtml_parser::parse_xhtml(markup)
}
fn name(&self) -> &str {
"PdfPreprocessor"
}
fn supports_file_type(&self, path: &Path) -> bool {
if let Some(extension) = path.extension() {
matches!(
extension.to_str().unwrap_or("").to_lowercase().as_str(),
"pdf"
)
} else {
false
}
}
}
pub type TikaPreprocessor = PdfPreprocessor;