use std::fmt::Write as _;
use super::mapping::{DC, PDF, PDFA_ID, RDF, XMP, XMP_MM};
use super::rdf::RdfValue;
use super::tree::{Binding, Element, Scope};
use super::write::{escape, prefix_in, rdf_attribute_prefix, write_declarations};
pub const PDFA_EXTENSION: &str = "http://www.aiim.org/pdfa/ns/extension/";
pub const PDFA_SCHEMA: &str = "http://www.aiim.org/pdfa/ns/schema#";
pub const PDFA_PROPERTY: &str = "http://www.aiim.org/pdfa/ns/property#";
pub(super) const PDFA_TYPE: &str = "http://www.aiim.org/pdfa/ns/type#";
pub(super) const PDFA_FIELD: &str = "http://www.aiim.org/pdfa/ns/field#";
const PREDEFINED: [&str; 19] = [
DC,
XMP,
"http://ns.adobe.com/xap/1.0/rights/",
XMP_MM,
"http://ns.adobe.com/xap/1.0/bj/",
"http://ns.adobe.com/xap/1.0/t/pg/",
"http://ns.adobe.com/xmp/1.0/DynamicMedia/",
PDF,
"http://ns.adobe.com/photoshop/1.0/",
"http://ns.adobe.com/camera-raw-settings/1.0/",
"http://ns.adobe.com/tiff/1.0/",
"http://ns.adobe.com/exif/1.0/",
"http://ns.adobe.com/exif/1.0/aux/",
PDFA_ID,
PDFA_EXTENSION,
PDFA_SCHEMA,
PDFA_PROPERTY,
PDFA_TYPE,
PDFA_FIELD,
];
#[must_use]
pub fn is_predefined_in_pdfa(namespace: &str) -> bool {
PREDEFINED.contains(&namespace)
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ExtensionProperty {
pub namespace: String,
pub prefix: String,
pub name: String,
pub value_type: String,
}
#[derive(Debug, Clone)]
pub(super) struct Container {
insert_at: usize,
indent: String,
scope: Scope,
}
#[derive(Debug, Clone)]
pub(super) struct Schema {
namespace: String,
properties: Vec<String>,
seq: Option<Container>,
}
#[derive(Debug, Clone)]
pub(super) struct Extensions {
bag: Option<Container>,
schemas: Vec<Schema>,
}
impl Extensions {
pub(super) fn declares(&self, namespace: &str, name: &str) -> bool {
self.schemas
.iter()
.any(|s| s.namespace == namespace && s.properties.iter().any(|p| p == name))
}
}
pub(super) fn read(text: &str, e: &Element, scope: &Scope) -> Extensions {
let scope = scope.with(&e.declarations);
let bag = e.child_elements().find(|c| c.name.is(RDF, "Bag"));
let mut schemas = Vec::new();
if let Some(bag) = bag {
let inside = scope.with(&bag.declarations);
for li in bag.child_elements().filter(|c| c.name.is(RDF, "li")) {
if let Some(schema) = schema(text, li, &inside) {
schemas.push(schema);
}
}
}
Extensions {
bag: bag.and_then(|b| container(text, b, &scope)),
schemas,
}
}
fn fields<'e>(li: &'e Element, scope: &Scope) -> Option<(&'e Element, Scope)> {
let scope = scope.with(&li.declarations);
if li.attr(RDF, "parseType").is_some_and(|a| a.value == "Resource") {
return Some((li, scope));
}
let d = li.child_elements().find(|c| c.name.is(RDF, "Description"))?;
Some((d, scope.with(&d.declarations)))
}
fn field(holder: &Element, namespace: &str, local: &str) -> Option<String> {
holder
.child_elements()
.find(|c| c.name.is(namespace, local))
.map(Element::text)
.or_else(|| holder.attr(namespace, local).map(|a| a.value.clone()))
.map(|s| s.trim().to_owned())
}
fn schema(text: &str, li: &Element, scope: &Scope) -> Option<Schema> {
let (holder, scope) = fields(li, scope)?;
let namespace = field(holder, PDFA_SCHEMA, "namespaceURI")?;
let mut properties = Vec::new();
let mut seq = None;
if let Some(property) = holder.child_elements().find(|c| c.name.is(PDFA_SCHEMA, "property")) {
let scope = scope.with(&property.declarations);
if let Some(s) = property.child_elements().find(|c| c.name.is(RDF, "Seq")) {
let inside = scope.with(&s.declarations);
for item in s.child_elements().filter(|c| c.name.is(RDF, "li")) {
if let Some((h, _)) = fields(item, &inside)
&& let Some(name) = field(h, PDFA_PROPERTY, "name")
{
properties.push(name);
}
}
seq = container(text, s, &scope);
}
}
Some(Schema {
namespace,
properties,
seq,
})
}
fn container(text: &str, e: &Element, scope: &Scope) -> Option<Container> {
if e.empty {
return None;
}
let end_tag = e.span.end.checked_sub(e.name.qualified().len() + 3)?;
text.get(end_tag..e.span.end)?.starts_with("</").then_some(())?;
let before = text.get(..end_tag)?;
let insert_at = before.trim_end_matches([' ', '\t', '\n', '\r']).len();
let indent = match e.child_elements().next() {
Some(child) => line_indent(text, child.span.start),
None => format!("{} ", line_indent(text, e.span.start)),
};
Some(Container {
insert_at,
indent,
scope: scope.with(&e.declarations),
})
}
fn line_indent(text: &str, at: usize) -> String {
let before = text.get(..at).unwrap_or_default();
let line_start = before.rfind('\n').map_or(0, |i| i + 1);
let indent: String = before
.get(line_start..)
.unwrap_or_default()
.chars()
.take_while(|c| *c == ' ' || *c == '\t')
.collect();
format!("\n{indent}")
}
#[derive(Debug)]
pub(super) struct Insert {
pub(super) at: usize,
pub(super) text: String,
}
pub(super) fn plan<'p>(
extensions: Option<&Extensions>,
props: &'p [ExtensionProperty],
) -> (Vec<Insert>, Vec<&'p ExtensionProperty>) {
let mut inserts = Vec::new();
let mut leftover = Vec::new();
for group in by_namespace(props) {
let Some(first) = group.first() else { continue };
let existing = extensions.and_then(|x| x.schemas.iter().find(|s| s.namespace == first.namespace));
if let Some(seq) = existing.and_then(|s| s.seq.as_ref()) {
let mut text = String::new();
for p in &group {
text.push_str(&seq.indent);
text.push_str(&property_entry(p, &seq.scope));
}
inserts.push(Insert {
at: seq.insert_at,
text,
});
} else if let Some(bag) = extensions.and_then(|x| x.bag.as_ref()) {
let mut text = bag.indent.clone();
text.push_str(&schema_entry(&group, &bag.scope));
inserts.push(Insert {
at: bag.insert_at,
text,
});
} else {
leftover.extend(group);
}
}
(inserts, leftover)
}
pub(super) fn schemas_element(props: &[&ExtensionProperty], scope: &Scope) -> String {
let mut declarations = Vec::new();
let extension = prefix_in(scope, &mut declarations, PDFA_EXTENSION, "pdfaExtension");
let rdf = rdf_attribute_prefix(&scope.with(&declarations), &mut declarations);
let inside = scope.with(&declarations);
let mut schemas = String::new();
for group in by_namespace(props.iter().copied()) {
schemas.push_str(&schema_entry(&group, &inside));
}
let mut open = format!("{extension}:schemas");
write_declarations(&mut open, declarations.iter());
format!("<{open}><{rdf}:Bag>{schemas}</{rdf}:Bag></{extension}:schemas>")
}
fn by_namespace<'p>(props: impl IntoIterator<Item = &'p ExtensionProperty>) -> Vec<Vec<&'p ExtensionProperty>> {
let mut groups: Vec<Vec<&ExtensionProperty>> = Vec::new();
for p in props {
match groups
.iter_mut()
.find(|g| g.first().is_some_and(|f| f.namespace == p.namespace))
{
Some(g) => g.push(p),
None => groups.push(vec![p]),
}
}
groups
}
fn schema_entry(group: &[&ExtensionProperty], scope: &Scope) -> String {
let Some(first) = group.first() else {
return String::new();
};
let mut decl: Vec<Binding> = Vec::new();
let rdf = rdf_attribute_prefix(scope, &mut decl);
let s = prefix_in(scope, &mut decl, PDFA_SCHEMA, "pdfaSchema");
let inside = scope.with(&decl);
let mut properties = String::new();
for p in group {
properties.push_str(&property_entry(p, &inside));
}
let mut open = format!("{rdf}:li");
write_declarations(&mut open, decl.iter());
format!(
"<{open} {rdf}:parseType=\"Resource\"><{s}:schema>{}</{s}:schema><{s}:namespaceURI>{}</{s}:namespaceURI>\
<{s}:prefix>{}</{s}:prefix><{s}:property><{rdf}:Seq>{properties}</{rdf}:Seq></{s}:property></{rdf}:li>",
escape(&format!("Properties in the {} namespace", first.prefix)),
escape(&first.namespace),
escape(&first.prefix),
)
}
fn property_entry(p: &ExtensionProperty, scope: &Scope) -> String {
let mut own = Vec::new();
let rdf = rdf_attribute_prefix(scope, &mut own);
let n = prefix_in(scope, &mut own, PDFA_PROPERTY, "pdfaProperty");
let mut open = format!("{rdf}:li");
write_declarations(&mut open, own.iter());
let mut out = String::new();
let _ = write!(
out,
"<{open} {rdf}:parseType=\"Resource\"><{n}:name>{}</{n}:name><{n}:valueType>{}</{n}:valueType>\
<{n}:category>external</{n}:category><{n}:description>{}</{n}:description></{rdf}:li>",
escape(&p.name),
escape(&p.value_type),
escape(&format!("{}:{}", p.prefix, p.name)),
);
out
}
pub(super) fn parsed_type(value: &RdfValue) -> Option<String> {
use super::rdf::ArrayKind;
match value {
RdfValue::Text(_) => Some("Text".to_owned()),
RdfValue::Array { kind, items } => {
if !items.iter().all(|i| matches!(i.value, RdfValue::Text(_))) {
return None;
}
Some(
match kind {
ArrayKind::Alt if items.iter().all(|i| i.lang.is_some()) => "Lang Alt",
ArrayKind::Alt => "Alt Text",
ArrayKind::Seq => "Seq Text",
ArrayKind::Bag => "Bag Text",
}
.to_owned(),
)
}
RdfValue::Other(_) => None,
}
}