thymeleaf 0.1.0-beta.1

A framework-neutral Thymeleaf-compatible dynamic template engine for Rust
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use std::sync::Arc;

use crate::IEngineConfiguration;
use crate::TemplateMode;
use crate::exceptions::TemplateEngineException;
use crate::model::AttributeValueQuotes;
use crate::text::{ITextHandler, TextParseCause, TextParseException};
use crate::util::Utf16String;

use super::{
    Attribute, Attributes, CloseElementTag, ITemplateHandler, OpenElementTag, StandaloneElementTag,
    TemplateEnd, TemplateStart, Text,
};

const DEFAULT_OPERATOR: &[u16] = &[0x003D];

/// 将 TEXT/JAVASCRIPT/CSS parser 回调转换为标准 Engine 模板事件。
///
/// 元素开始回调只保存源位置并清空属性;元素结束回调统一解析 ElementDefinition、
/// 组装合成属性空白并发送不可变标签。正数偏移先减一,因而嵌入模板的第一行和
/// 第一列与 Java 版完全相同。
///
/// 对应 Java: `org.thymeleaf.engine.TemplateHandlerAdapterTextHandler`。
pub struct TemplateHandlerAdapterTextHandler {
    template_name: Option<Utf16String>,
    template_handler: Box<dyn ITemplateHandler>,
    configuration: Arc<dyn IEngineConfiguration>,
    template_mode: TemplateMode,
    line_offset: i32,
    col_offset: i32,
    current_element_line: i32,
    current_element_col: i32,
    current_element_attributes: Vec<Arc<Attribute>>,
}

impl TemplateHandlerAdapterTextHandler {
    /// 创建文本 parser 到 Engine Handler 的适配器。
    ///
    /// `configuration` 持有 Java 中 ElementDefinitions 与 AttributeDefinitions 两个
    /// repository 的共同所有者,避免 Rust 借用越过 parser handler 生命周期。
    #[must_use]
    /// 对应 Java 语义:`TemplateHandlerAdapterTextHandler` 的 `new` 行为(Rust 侧辅助/私有路径)。
    pub fn new(
        template_name: Option<Utf16String>,
        template_handler: Box<dyn ITemplateHandler>,
        configuration: Arc<dyn IEngineConfiguration>,
        template_mode: TemplateMode,
        line_offset: i32,
        col_offset: i32,
    ) -> Self {
        Self {
            template_name,
            template_handler,
            configuration,
            template_mode,
            line_offset: if line_offset > 0 {
                line_offset - 1
            } else {
                line_offset
            },
            col_offset: if col_offset > 0 {
                col_offset - 1
            } else {
                col_offset
            },
            current_element_line: -1,
            current_element_col: -1,
            current_element_attributes: Vec::with_capacity(10),
        }
    }

    fn location(&self, line: i32, col: i32) -> (i32, i32) {
        (
            self.line_offset.wrapping_add(line),
            (if line == 1 { self.col_offset } else { 0 }).wrapping_add(col),
        )
    }

    fn start_element(&mut self, line: i32, col: i32) {
        self.current_element_line = line;
        self.current_element_col = col;
        self.current_element_attributes.clear();
    }

    fn attributes(&self) -> Option<Arc<Attributes>> {
        if self.current_element_attributes.is_empty() {
            return None;
        }
        let spaces = vec![Utf16String::from_rust_str(" "); self.current_element_attributes.len()];
        Some(Attributes::new(
            Some(self.current_element_attributes.clone()),
            Some(spaces),
        ))
    }
}

impl ITextHandler for TemplateHandlerAdapterTextHandler {
    fn handle_document_start(
        &mut self,
        _start_time_nanos: i64,
        _line: i32,
        _col: i32,
    ) -> Result<(), Box<TextParseException>> {
        self.template_handler
            .handle_template_start(TemplateStart::instance())
            .map_err(handler_error)
    }

    fn handle_document_end(
        &mut self,
        _end_time_nanos: i64,
        _total_time_nanos: i64,
        _line: i32,
        _col: i32,
    ) -> Result<(), Box<TextParseException>> {
        self.template_handler
            .handle_template_end(TemplateEnd::instance())
            .map_err(handler_error)
    }

    fn handle_text(
        &mut self,
        buffer: Option<&mut [u16]>,
        offset: i32,
        len: i32,
        line: i32,
        col: i32,
    ) -> Result<(), Box<TextParseException>> {
        let text = slice(buffer.as_deref(), offset, len, line, col)?;
        let (line, col) = self.location(line, col);
        self.template_handler
            .handle_text(Arc::new(Text::with_location(
                Some(Arc::new(text)),
                self.template_name.clone(),
                line,
                col,
            )))
            .map_err(handler_error)
    }

    fn handle_comment(
        &mut self,
        _buffer: Option<&mut [u16]>,
        _content_offset: i32,
        _content_len: i32,
        _outer_offset: i32,
        _outer_len: i32,
        _line: i32,
        _col: i32,
    ) -> Result<(), Box<TextParseException>> {
        Ok(())
    }

    fn handle_standalone_element_start(
        &mut self,
        _buffer: Option<&mut [u16]>,
        _name_offset: i32,
        _name_len: i32,
        _minimized: bool,
        line: i32,
        col: i32,
    ) -> Result<(), Box<TextParseException>> {
        self.start_element(line, col);
        Ok(())
    }

    fn handle_standalone_element_end(
        &mut self,
        buffer: Option<&mut [u16]>,
        name_offset: i32,
        name_len: i32,
        minimized: bool,
        _line: i32,
        _col: i32,
    ) -> Result<(), Box<TextParseException>> {
        let name = slice(
            buffer.as_deref(),
            name_offset,
            name_len,
            self.current_element_line,
            self.current_element_col,
        )?;
        let definition = self
            .configuration
            .get_element_definitions()
            .for_name(Some(self.template_mode), Some(&name))
            .map_err(definition_error)?;
        let (line, col) = self.location(self.current_element_line, self.current_element_col);
        let tag = StandaloneElementTag::with_location(
            self.template_mode,
            definition,
            name,
            self.attributes(),
            false,
            minimized,
            self.template_name.clone(),
            line,
            col,
        )
        .map_err(|error| parse_cause(error, line, col))?;
        self.template_handler
            .handle_standalone_element(Arc::new(tag))
            .map_err(handler_error)
    }

    fn handle_open_element_start(
        &mut self,
        _buffer: Option<&mut [u16]>,
        _name_offset: i32,
        _name_len: i32,
        line: i32,
        col: i32,
    ) -> Result<(), Box<TextParseException>> {
        self.start_element(line, col);
        Ok(())
    }

    fn handle_open_element_end(
        &mut self,
        buffer: Option<&mut [u16]>,
        name_offset: i32,
        name_len: i32,
        _line: i32,
        _col: i32,
    ) -> Result<(), Box<TextParseException>> {
        let name = slice(
            buffer.as_deref(),
            name_offset,
            name_len,
            self.current_element_line,
            self.current_element_col,
        )?;
        let definition = self
            .configuration
            .get_element_definitions()
            .for_name(Some(self.template_mode), Some(&name))
            .map_err(definition_error)?;
        let (line, col) = self.location(self.current_element_line, self.current_element_col);
        self.template_handler
            .handle_open_element(Arc::new(OpenElementTag::with_location(
                self.template_mode,
                definition,
                name,
                self.attributes(),
                false,
                self.template_name.clone(),
                line,
                col,
            )))
            .map_err(handler_error)
    }

    fn handle_close_element_start(
        &mut self,
        _buffer: Option<&mut [u16]>,
        _name_offset: i32,
        _name_len: i32,
        line: i32,
        col: i32,
    ) -> Result<(), Box<TextParseException>> {
        self.start_element(line, col);
        Ok(())
    }

    fn handle_close_element_end(
        &mut self,
        buffer: Option<&mut [u16]>,
        name_offset: i32,
        name_len: i32,
        _line: i32,
        _col: i32,
    ) -> Result<(), Box<TextParseException>> {
        let name = slice(
            buffer.as_deref(),
            name_offset,
            name_len,
            self.current_element_line,
            self.current_element_col,
        )?;
        let definition = self
            .configuration
            .get_element_definitions()
            .for_name(Some(self.template_mode), Some(&name))
            .map_err(definition_error)?;
        let (line, col) = self.location(self.current_element_line, self.current_element_col);
        self.template_handler
            .handle_close_element(Arc::new(CloseElementTag::with_location(
                self.template_mode,
                definition,
                name,
                None,
                false,
                false,
                self.template_name.clone(),
                line,
                col,
            )))
            .map_err(handler_error)
    }

    #[allow(clippy::too_many_arguments)]
    fn handle_attribute(
        &mut self,
        buffer: Option<&mut [u16]>,
        name_offset: i32,
        name_len: i32,
        name_line: i32,
        name_col: i32,
        operator_offset: i32,
        operator_len: i32,
        _operator_line: i32,
        _operator_col: i32,
        value_content_offset: i32,
        value_content_len: i32,
        value_outer_offset: i32,
        _value_outer_len: i32,
        _value_line: i32,
        _value_col: i32,
    ) -> Result<(), Box<TextParseException>> {
        let buffer = buffer.as_deref();
        let name = slice(buffer, name_offset, name_len, name_line, name_col)?;
        let definition = self
            .configuration
            .get_attribute_definitions()
            .for_name(Some(self.template_mode), Some(&name))
            .map_err(definition_error)?;
        let operator = if operator_len > 0 {
            let value = slice(buffer, operator_offset, operator_len, name_line, name_col)?;
            Some(if value.as_utf16() == DEFAULT_OPERATOR {
                Utf16String::from_rust_str("=")
            } else {
                value
            })
        } else {
            None
        };
        let value = operator
            .as_ref()
            .map(|_| {
                slice(
                    buffer,
                    value_content_offset,
                    value_content_len,
                    name_line,
                    name_col,
                )
            })
            .transpose()?;
        let quotes = value.as_ref().map(|_| {
            if value_outer_offset == value_content_offset {
                AttributeValueQuotes::NONE
            } else {
                match buffer.and_then(|buffer| {
                    usize::try_from(value_outer_offset)
                        .ok()
                        .and_then(|index| buffer.get(index).copied())
                }) {
                    Some(value) if value == u16::from(b'"') => AttributeValueQuotes::DOUBLE,
                    Some(value) if value == u16::from(b'\'') => AttributeValueQuotes::SINGLE,
                    _ => AttributeValueQuotes::NONE,
                }
            }
        });
        let (line, col) = self.location(name_line, name_col);
        self.current_element_attributes
            .push(Arc::new(Attribute::new(
                definition,
                name,
                operator,
                value,
                quotes,
                self.template_name.clone(),
                line,
                col,
            )));
        Ok(())
    }
}

fn slice(
    buffer: Option<&[u16]>,
    offset: i32,
    len: i32,
    line: i32,
    col: i32,
) -> Result<Utf16String, Box<TextParseException>> {
    let buffer = buffer.ok_or_else(|| invalid_range(line, col))?;
    let start = usize::try_from(offset).map_err(|_| invalid_range(line, col))?;
    let len = usize::try_from(len).map_err(|_| invalid_range(line, col))?;
    let end = start
        .checked_add(len)
        .filter(|end| *end <= buffer.len())
        .ok_or_else(|| invalid_range(line, col))?;
    Ok(Utf16String::from_utf16(buffer[start..end].to_vec()))
}

fn invalid_range(line: i32, col: i32) -> Box<TextParseException> {
    Box::new(TextParseException::with_message_at(
        Some(&Utf16String::from_rust_str("Invalid text buffer range")),
        line,
        col,
    ))
}

fn handler_error(error: Box<dyn TemplateEngineException>) -> Box<TextParseException> {
    Box::new(TextParseException::with_cause(Some(
        TextParseCause::with_java_metadata(
            error,
            "org.thymeleaf.exceptions.TemplateEngineException",
            None,
        ),
    )))
}

fn definition_error<E>(error: E) -> Box<TextParseException>
where
    E: std::error::Error + Send + Sync + 'static,
{
    Box::new(TextParseException::with_cause(Some(
        TextParseCause::with_java_metadata(
            Box::new(error),
            "java.lang.IllegalArgumentException",
            None,
        ),
    )))
}

fn parse_cause<E>(error: E, line: i32, col: i32) -> Box<TextParseException>
where
    E: std::error::Error + Send + Sync + 'static,
{
    Box::new(TextParseException::with_cause_at(
        Some(TextParseCause::with_java_metadata(
            Box::new(error),
            "java.lang.IllegalArgumentException",
            None,
        )),
        line,
        col,
    ))
}