Skip to main content

document_svg/document/
xproc.rs

1//! Bounded XProc 3.0 XML-pipeline previews.
2//!
3//! XProc describes operations on XML documents. This adapter reports pipeline
4//! structure only; steps, file/network operations, XPath and external imports
5//! are never executed.
6
7use std::path::Path;
8
9use crate::convert::{ConvertOptions, PageConsumer, read_limited_file};
10use crate::document::html::{HtmlBlock, render_blocks_to_pages};
11use crate::error::{Error, Result};
12use crate::geospatial::xml_tree::{XmlElement, XmlLimits, parse_xml_tree};
13use crate::table::{TableAlign, TableData};
14
15const MAX_XPROC_BYTES: u64 = 128 * 1024 * 1024;
16const MAX_XPROC_EVENTS: usize = 1_000_000;
17const MAX_XPROC_NODES: usize = 500_000;
18const MAX_XPROC_DEPTH: usize = 128;
19const MAX_XPROC_TEXT_BYTES: usize = 32 * 1024 * 1024;
20const MAX_XPROC_ROWS: usize = 200_000;
21const MAX_XPROC_DISPLAY_BYTES: usize = 512;
22const XPROC_NAMESPACE: &str = "http://www.w3.org/ns/xproc";
23
24#[derive(Default)]
25struct Summary {
26    declarations: usize,
27    steps: usize,
28    inputs: usize,
29    outputs: usize,
30    options: usize,
31    variables: usize,
32    pipes: usize,
33    conditionals: usize,
34    loops: usize,
35    groups: usize,
36    errors: usize,
37    imports: usize,
38    includes: usize,
39    file_ops: usize,
40    http_ops: usize,
41    rows: Vec<Vec<String>>,
42}
43
44struct XprocPageSink<'a> {
45    inner: &'a mut dyn PageConsumer,
46    warnings: &'a [String],
47}
48impl PageConsumer for XprocPageSink<'_> {
49    fn consume(&mut self, mut page: crate::ir::Page) -> Result<()> {
50        page.source_format = "xproc".into();
51        if page.title.is_empty() {
52            page.title = "XProc pipeline".into();
53        }
54        page.description = "XProc pipeline structure is rendered as bounded inert metadata; pipeline steps and external resources are not executed".into();
55        for warning in self.warnings {
56            page.warn(warning.clone());
57        }
58        self.inner.consume(page)
59    }
60}
61
62pub(crate) fn looks_like_prefix(prefix: &[u8]) -> bool {
63    let text = String::from_utf8_lossy(prefix).to_ascii_lowercase();
64    (text.contains("<p:declare-step") || text.contains("<p:pipeline"))
65        && text.contains("www.w3.org/ns/xproc")
66}
67
68pub(crate) fn convert(
69    path: &Path,
70    options: &ConvertOptions,
71    sink: &mut dyn PageConsumer,
72) -> Result<Vec<String>> {
73    let bytes = read_limited_file(
74        path,
75        options.max_input_bytes.min(MAX_XPROC_BYTES),
76        "XProc input",
77    )?;
78    let root = parse_xml_tree(
79        &bytes,
80        &XmlLimits {
81            max_events: options.max_xml_events.min(MAX_XPROC_EVENTS),
82            max_nodes: MAX_XPROC_NODES,
83            max_depth: MAX_XPROC_DEPTH,
84            max_text_bytes: MAX_XPROC_TEXT_BYTES,
85        },
86        "XProc",
87    )?;
88    if !root.name.eq_ignore_ascii_case("declare-step")
89        && !root.name.eq_ignore_ascii_case("pipeline")
90    {
91        return Err(Error::InvalidInput(
92            "XProc root must be p:declare-step or p:pipeline".into(),
93        ));
94    }
95    if root.namespace.as_deref() != Some(XPROC_NAMESPACE) {
96        return Err(Error::InvalidInput(
97            "XProc root uses an unsupported namespace".into(),
98        ));
99    }
100    let mut summary = Summary {
101        declarations: usize::from(root.name.eq_ignore_ascii_case("declare-step")),
102        steps: count_named(&root, "declare-step")
103            + count_named(&root, "pipeline")
104            + count_named(&root, "step")
105            + count_named(&root, "identity"),
106        inputs: count_named(&root, "input") + count_named(&root, "with-input"),
107        outputs: count_named(&root, "output") + count_named(&root, "with-output"),
108        options: count_named(&root, "option"),
109        variables: count_named(&root, "variable"),
110        pipes: count_named(&root, "pipe"),
111        conditionals: count_named(&root, "choose")
112            + count_named(&root, "when")
113            + count_named(&root, "if"),
114        loops: count_named(&root, "for-each") + count_named(&root, "viewport"),
115        groups: count_named(&root, "group") + count_named(&root, "try"),
116        errors: count_named(&root, "catch") + count_named(&root, "when-error"),
117        imports: count_named(&root, "import"),
118        includes: count_named(&root, "include"),
119        file_ops: count_named(&root, "load") + count_named(&root, "store"),
120        http_ops: count_named(&root, "http-request"),
121        ..Summary::default()
122    };
123    push_row(
124        &mut summary.rows,
125        "Pipeline",
126        &summary.steps.to_string(),
127        &format!(
128            "declarations={} inputs={} outputs={}",
129            summary.declarations, summary.inputs, summary.outputs
130        ),
131    )?;
132    push_row(
133        &mut summary.rows,
134        "Bindings",
135        &summary.options.to_string(),
136        &format!("variables={} pipes={}", summary.variables, summary.pipes),
137    )?;
138    push_row(
139        &mut summary.rows,
140        "Control",
141        &summary.conditionals.to_string(),
142        &format!(
143            "loops={} groups={} errors={}",
144            summary.loops, summary.groups, summary.errors
145        ),
146    )?;
147    push_row(
148        &mut summary.rows,
149        "Resources",
150        &summary.file_ops.to_string(),
151        &format!(
152            "httpOps={} imports={} includes={}",
153            summary.http_ops, summary.imports, summary.includes
154        ),
155    )?;
156    let blocks = vec![HtmlBlock::Heading { level: 1, text: "XProc pipeline".into() }, HtmlBlock::Paragraph { text: "XProc XML pipeline declarations are summarized without running steps, file operations, HTTP requests or XPath.".into() }, HtmlBlock::Table(TableData { headers: vec!["Kind".into(), "Value".into(), "Detail".into()], rows: summary.rows, alignments: vec![TableAlign::Left; 3], raw_source: String::new() })];
157    let warnings = vec!["XProc step names, options, XPath expressions, file paths, HTTP URLs, variables and XML payloads are omitted or redacted".into(), "XProc load/store/http-request, pipeline imports/includes, extension steps, scripts and external resources never run".into()];
158    let mut page_sink = XprocPageSink {
159        inner: sink,
160        warnings: &warnings,
161    };
162    render_blocks_to_pages(&blocks, &mut page_sink, options)?;
163    Ok(warnings)
164}
165
166fn count_named(element: &XmlElement, name: &str) -> usize {
167    element
168        .children
169        .iter()
170        .map(|child| usize::from(child.name.eq_ignore_ascii_case(name)) + count_named(child, name))
171        .sum()
172}
173fn push_row(rows: &mut Vec<Vec<String>>, kind: &str, value: &str, detail: &str) -> Result<()> {
174    if rows.len() >= MAX_XPROC_ROWS {
175        return Err(Error::LimitExceeded(format!(
176            "XProc rows exceed {MAX_XPROC_ROWS}"
177        )));
178    }
179    rows.push(vec![truncate(kind), truncate(value), truncate(detail)]);
180    Ok(())
181}
182fn truncate(value: &str) -> String {
183    if value.len() <= MAX_XPROC_DISPLAY_BYTES {
184        return value.to_owned();
185    }
186    let mut end = MAX_XPROC_DISPLAY_BYTES;
187    while end > 0 && !value.is_char_boundary(end) {
188        end -= 1;
189    }
190    format!("{}…", &value[..end])
191}