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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
use std::sync::Arc;

use crate::TemplateMode;
use crate::cdatasection::{ICDATASectionProcessor, ICDATASectionStructureHandler};
use crate::comment::{ICommentProcessor, ICommentStructureHandler};
use crate::context::ITemplateContext;
use crate::doctype::{IDocTypeProcessor, IDocTypeStructureHandler};
use crate::element::{
    IElementModelProcessor, IElementModelStructureHandler, IElementProcessor, IElementTagProcessor,
    IElementTagStructureHandler, MatchingAttributeName, MatchingElementName,
};
use crate::exceptions::{ConfigurationException, TemplateEngineException};
use crate::model::{
    ICDATASection, IComment, IDocType, IModel, IProcessableElementTag, IProcessingInstruction,
    ITemplateEnd, ITemplateStart, IText, IXMLDeclaration,
};
use crate::postprocessor::IPostProcessor;
use crate::preprocessor::IPreProcessor;
use crate::processinginstruction::{
    IProcessingInstructionProcessor, IProcessingInstructionStructureHandler,
};
use crate::processor::IProcessor;
use crate::templateboundaries::{
    ITemplateBoundariesProcessor, ITemplateBoundariesStructureHandler,
};
use crate::text::{ITextProcessor, ITextStructureHandler};
use crate::xmldeclaration::{IXMLDeclarationProcessor, IXMLDeclarationStructureHandler};

/// 为配置中的 Processor 附加方言 precedence,并保留其具体处理能力。
///
/// 对应 Java: `org.thymeleaf.util.ProcessorConfigurationUtils`。Java 的内部 wrapper
/// 层次在本文件中保持一一可追踪;Rust 使用共享 `Arc` 保存被包装对象身份。
pub struct ProcessorConfigurationUtils;

impl ProcessorConfigurationUtils {
    /// 去除元素 Processor 的方言包装器。
    ///
    /// 对应 Java: `ProcessorConfigurationUtils#unwrap(IElementProcessor)`。
    #[must_use]
    pub fn unwrap_element(processor: &dyn IElementProcessor) -> &dyn IElementProcessor {
        processor
            .get_wrapped_processor()
            .and_then(IProcessor::as_element_processor)
            .unwrap_or(processor)
    }

    /// 去除 CDATA Processor 的方言包装器。
    ///
    /// 对应 Java: `ProcessorConfigurationUtils#unwrap(ICDATASectionProcessor)`。
    #[must_use]
    pub fn unwrap_cdata_section(
        processor: &dyn ICDATASectionProcessor,
    ) -> &dyn ICDATASectionProcessor {
        processor
            .get_wrapped_processor()
            .and_then(IProcessor::as_cdata_section_processor)
            .unwrap_or(processor)
    }

    /// 去除 Comment Processor 的方言包装器。
    ///
    /// 对应 Java: `ProcessorConfigurationUtils#unwrap(ICommentProcessor)`。
    #[must_use]
    pub fn unwrap_comment(processor: &dyn ICommentProcessor) -> &dyn ICommentProcessor {
        processor
            .get_wrapped_processor()
            .and_then(IProcessor::as_comment_processor)
            .unwrap_or(processor)
    }

    /// 去除 DOCTYPE Processor 的方言包装器。
    ///
    /// 对应 Java: `ProcessorConfigurationUtils#unwrap(IDocTypeProcessor)`。
    #[must_use]
    pub fn unwrap_doc_type(processor: &dyn IDocTypeProcessor) -> &dyn IDocTypeProcessor {
        processor
            .get_wrapped_processor()
            .and_then(IProcessor::as_doc_type_processor)
            .unwrap_or(processor)
    }

    /// 去除 ProcessingInstruction Processor 的方言包装器。
    ///
    /// 对应 Java: `ProcessorConfigurationUtils#unwrap(IProcessingInstructionProcessor)`。
    #[must_use]
    pub fn unwrap_processing_instruction(
        processor: &dyn IProcessingInstructionProcessor,
    ) -> &dyn IProcessingInstructionProcessor {
        processor
            .get_wrapped_processor()
            .and_then(IProcessor::as_processing_instruction_processor)
            .unwrap_or(processor)
    }

    /// 去除 TemplateBoundaries Processor 的方言包装器。
    ///
    /// 对应 Java: `ProcessorConfigurationUtils#unwrap(ITemplateBoundariesProcessor)`。
    #[must_use]
    pub fn unwrap_template_boundaries(
        processor: &dyn ITemplateBoundariesProcessor,
    ) -> &dyn ITemplateBoundariesProcessor {
        processor
            .get_wrapped_processor()
            .and_then(IProcessor::as_template_boundaries_processor)
            .unwrap_or(processor)
    }

    /// 去除 Text Processor 的方言包装器。
    ///
    /// 对应 Java: `ProcessorConfigurationUtils#unwrap(ITextProcessor)`。
    #[must_use]
    pub fn unwrap_text(processor: &dyn ITextProcessor) -> &dyn ITextProcessor {
        processor
            .get_wrapped_processor()
            .and_then(IProcessor::as_text_processor)
            .unwrap_or(processor)
    }

    /// 去除 XMLDeclaration Processor 的方言包装器。
    ///
    /// 对应 Java: `ProcessorConfigurationUtils#unwrap(IXMLDeclarationProcessor)`。
    #[must_use]
    pub fn unwrap_xml_declaration(
        processor: &dyn IXMLDeclarationProcessor,
    ) -> &dyn IXMLDeclarationProcessor {
        processor
            .get_wrapped_processor()
            .and_then(IProcessor::as_xml_declaration_processor)
            .unwrap_or(processor)
    }

    /// 去除 PreProcessor 的方言包装器。
    ///
    /// 对应 Java: `ProcessorConfigurationUtils#unwrap(IPreProcessor)`。
    #[must_use]
    pub fn unwrap_pre_processor(processor: &dyn IPreProcessor) -> &dyn IPreProcessor {
        processor.get_wrapped_pre_processor().unwrap_or(processor)
    }

    /// 去除 PostProcessor 的方言包装器。
    ///
    /// 对应 Java: `ProcessorConfigurationUtils#unwrap(IPostProcessor)`。
    #[must_use]
    pub fn unwrap_post_processor(processor: &dyn IPostProcessor) -> &dyn IPostProcessor {
        processor.get_wrapped_post_processor().unwrap_or(processor)
    }

    /// 包装元素 Processor,并按 Tag/Model 能力创建对应 Java wrapper。
    pub fn wrap_element(
        processor: Arc<dyn IProcessor>,
        dialect_precedence: i32,
    ) -> Result<Arc<dyn IElementProcessor>, ConfigurationException> {
        let element = processor.as_element_processor().ok_or_else(|| {
            configuration_error(format!(
                "Unknown element processor interface implemented by {}",
                processor.class_name()
            ))
        })?;
        if element.as_element_tag_processor().is_some() {
            return Ok(Arc::new(ElementTagProcessorWrapper::new(
                processor,
                dialect_precedence,
            )));
        }
        if element.as_element_model_processor().is_some() {
            return Ok(Arc::new(ElementModelProcessorWrapper::new(
                processor,
                dialect_precedence,
            )));
        }
        Err(configuration_error(format!(
            "Unknown element processor interface implemented by {}",
            processor.class_name()
        )))
    }

    /// 包装 CDATA Processor。
    /// 对应 Java 语义:`ProcessorConfigurationUtils` 的 `wrap_cdata_section` 行为(Rust 侧辅助/私有路径)。
    pub fn wrap_cdata_section(
        processor: Arc<dyn IProcessor>,
        dialect_precedence: i32,
    ) -> Result<Arc<dyn ICDATASectionProcessor>, ConfigurationException> {
        require_capability(
            processor.as_cdata_section_processor().is_some(),
            &processor,
            "CDATA section",
        )?;
        Ok(Arc::new(CDATASectionProcessorWrapper::new(
            processor,
            dialect_precedence,
        )))
    }

    /// 包装 Comment Processor。
    /// 对应 Java 语义:`ProcessorConfigurationUtils` 的 `wrap_comment` 行为(Rust 侧辅助/私有路径)。
    pub fn wrap_comment(
        processor: Arc<dyn IProcessor>,
        dialect_precedence: i32,
    ) -> Result<Arc<dyn ICommentProcessor>, ConfigurationException> {
        require_capability(
            processor.as_comment_processor().is_some(),
            &processor,
            "comment",
        )?;
        Ok(Arc::new(CommentProcessorWrapper::new(
            processor,
            dialect_precedence,
        )))
    }

    /// 包装 DOCTYPE Processor。
    /// 对应 Java 语义:`ProcessorConfigurationUtils` 的 `wrap_doc_type` 行为(Rust 侧辅助/私有路径)。
    pub fn wrap_doc_type(
        processor: Arc<dyn IProcessor>,
        dialect_precedence: i32,
    ) -> Result<Arc<dyn IDocTypeProcessor>, ConfigurationException> {
        require_capability(
            processor.as_doc_type_processor().is_some(),
            &processor,
            "DOCTYPE",
        )?;
        Ok(Arc::new(DocTypeProcessorWrapper::new(
            processor,
            dialect_precedence,
        )))
    }

    /// 包装 ProcessingInstruction Processor。
    /// 对应 Java 语义:`ProcessorConfigurationUtils` 的 `wrap_processing_instruction` 行为(Rust 侧辅助/私有路径)。
    pub fn wrap_processing_instruction(
        processor: Arc<dyn IProcessor>,
        dialect_precedence: i32,
    ) -> Result<Arc<dyn IProcessingInstructionProcessor>, ConfigurationException> {
        require_capability(
            processor.as_processing_instruction_processor().is_some(),
            &processor,
            "processing instruction",
        )?;
        Ok(Arc::new(ProcessingInstructionProcessorWrapper::new(
            processor,
            dialect_precedence,
        )))
    }

    /// 包装 TemplateBoundaries Processor。
    /// 对应 Java 语义:`ProcessorConfigurationUtils` 的 `wrap_template_boundaries` 行为(Rust 侧辅助/私有路径)。
    pub fn wrap_template_boundaries(
        processor: Arc<dyn IProcessor>,
        dialect_precedence: i32,
    ) -> Result<Arc<dyn ITemplateBoundariesProcessor>, ConfigurationException> {
        require_capability(
            processor.as_template_boundaries_processor().is_some(),
            &processor,
            "template boundaries",
        )?;
        Ok(Arc::new(TemplateBoundariesProcessorWrapper::new(
            processor,
            dialect_precedence,
        )))
    }

    /// 包装 Text Processor。
    /// 对应 Java 语义:`ProcessorConfigurationUtils` 的 `wrap_text` 行为(Rust 侧辅助/私有路径)。
    pub fn wrap_text(
        processor: Arc<dyn IProcessor>,
        dialect_precedence: i32,
    ) -> Result<Arc<dyn ITextProcessor>, ConfigurationException> {
        require_capability(processor.as_text_processor().is_some(), &processor, "text")?;
        Ok(Arc::new(TextProcessorWrapper::new(
            processor,
            dialect_precedence,
        )))
    }

    /// 包装 XMLDeclaration Processor。
    /// 对应 Java 语义:`ProcessorConfigurationUtils` 的 `wrap_xml_declaration` 行为(Rust 侧辅助/私有路径)。
    pub fn wrap_xml_declaration(
        processor: Arc<dyn IProcessor>,
        dialect_precedence: i32,
    ) -> Result<Arc<dyn IXMLDeclarationProcessor>, ConfigurationException> {
        require_capability(
            processor.as_xml_declaration_processor().is_some(),
            &processor,
            "XML declaration",
        )?;
        Ok(Arc::new(XMLDeclarationProcessorWrapper::new(
            processor,
            dialect_precedence,
        )))
    }

    /// 为 PreProcessor 附加方言级 precedence 并保留原对象身份。
    ///
    /// 对应 Java: `ProcessorConfigurationUtils#wrap(IPreProcessor,IProcessorDialect)`。
    pub fn wrap_pre_processor(
        pre_processor: Arc<dyn IPreProcessor>,
        dialect_precedence: i32,
    ) -> Arc<dyn IPreProcessor> {
        Arc::new(PreProcessorWrapper {
            pre_processor,
            dialect_precedence,
        })
    }

    /// 为 PostProcessor 附加方言级 precedence 并保留原对象身份。
    ///
    /// 对应 Java: `ProcessorConfigurationUtils#wrap(IPostProcessor,IProcessorDialect)`。
    pub fn wrap_post_processor(
        post_processor: Arc<dyn IPostProcessor>,
        dialect_precedence: i32,
    ) -> Arc<dyn IPostProcessor> {
        Arc::new(PostProcessorWrapper {
            post_processor,
            dialect_precedence,
        })
    }
}

struct AbstractProcessorWrapper {
    processor: Arc<dyn IProcessor>,
    dialect_precedence: i32,
    processor_precedence: i32,
}

impl AbstractProcessorWrapper {
    fn new(processor: Arc<dyn IProcessor>, dialect_precedence: i32) -> Self {
        let processor_precedence = processor.get_precedence();
        Self {
            processor,
            dialect_precedence,
            processor_precedence,
        }
    }

    fn template_mode(&self) -> Option<TemplateMode> {
        self.processor.get_template_mode()
    }

    /// 返回包装前的 Processor 动态实例。
    ///
    /// 对应 Java: `AbstractProcessorWrapper#unwrap()`。
    fn unwrap(&self) -> &dyn IProcessor {
        self.processor.as_ref()
    }
}

macro_rules! implement_base_processor {
    ($wrapper:ty) => {
        impl IProcessor for $wrapper {
            fn is_attribute_definitions_aware(&self) -> bool {
                self.base.processor.is_attribute_definitions_aware()
            }

            fn set_attribute_definitions(
                &self,
                definitions: Arc<crate::engine::AttributeDefinitions>,
            ) {
                self.base.processor.set_attribute_definitions(definitions);
            }

            fn is_element_definitions_aware(&self) -> bool {
                self.base.processor.is_element_definitions_aware()
            }

            fn set_element_definitions(&self, definitions: Arc<crate::engine::ElementDefinitions>) {
                self.base.processor.set_element_definitions(definitions);
            }

            fn class_name(&self) -> &'static str {
                self.base.processor.class_name()
            }

            fn get_dialect_precedence(&self) -> Option<i32> {
                Some(self.base.dialect_precedence)
            }

            fn get_wrapped_processor(&self) -> Option<&dyn IProcessor> {
                Some(self.base.unwrap())
            }

            fn get_template_mode(&self) -> Option<TemplateMode> {
                self.base.template_mode()
            }

            fn get_precedence(&self) -> i32 {
                self.base.processor_precedence
            }
        }
    };
}

struct AbstractElementProcessorWrapper {
    base: AbstractProcessorWrapper,
}

impl AbstractElementProcessorWrapper {
    fn new(processor: Arc<dyn IProcessor>, dialect_precedence: i32) -> Self {
        Self {
            base: AbstractProcessorWrapper::new(processor, dialect_precedence),
        }
    }

    fn processor(&self) -> &dyn IElementProcessor {
        self.base
            .processor
            .as_element_processor()
            .expect("element wrapper capability was checked")
    }
}

struct ElementTagProcessorWrapper {
    base: AbstractElementProcessorWrapper,
}

impl ElementTagProcessorWrapper {
    fn new(processor: Arc<dyn IProcessor>, dialect_precedence: i32) -> Self {
        Self {
            base: AbstractElementProcessorWrapper::new(processor, dialect_precedence),
        }
    }
}

impl IProcessor for ElementTagProcessorWrapper {
    fn is_attribute_definitions_aware(&self) -> bool {
        self.base.base.processor.is_attribute_definitions_aware()
    }
    fn set_attribute_definitions(&self, definitions: Arc<crate::engine::AttributeDefinitions>) {
        self.base
            .base
            .processor
            .set_attribute_definitions(definitions);
    }
    fn is_element_definitions_aware(&self) -> bool {
        self.base.base.processor.is_element_definitions_aware()
    }
    fn set_element_definitions(&self, definitions: Arc<crate::engine::ElementDefinitions>) {
        self.base
            .base
            .processor
            .set_element_definitions(definitions);
    }
    fn as_element_processor(&self) -> Option<&dyn IElementProcessor> {
        Some(self)
    }
    fn class_name(&self) -> &'static str {
        self.base.base.processor.class_name()
    }
    fn get_dialect_precedence(&self) -> Option<i32> {
        Some(self.base.base.dialect_precedence)
    }
    fn get_wrapped_processor(&self) -> Option<&dyn IProcessor> {
        Some(self.base.base.processor.as_ref())
    }
    fn get_template_mode(&self) -> Option<TemplateMode> {
        self.base.base.template_mode()
    }
    fn get_precedence(&self) -> i32 {
        self.base.base.processor_precedence
    }
}

impl IElementProcessor for ElementTagProcessorWrapper {
    fn as_element_tag_processor(&self) -> Option<&dyn IElementTagProcessor> {
        Some(self)
    }
    fn get_matching_element_name(&self) -> Option<&MatchingElementName> {
        self.base.processor().get_matching_element_name()
    }
    fn get_matching_attribute_name(&self) -> Option<&MatchingAttributeName> {
        self.base.processor().get_matching_attribute_name()
    }
}

impl IElementTagProcessor for ElementTagProcessorWrapper {
    fn process(
        &self,
        context: &dyn ITemplateContext,
        tag: &dyn IProcessableElementTag,
        structure_handler: &mut dyn IElementTagStructureHandler,
    ) -> Result<(), Box<dyn TemplateEngineException>> {
        self.base
            .processor()
            .as_element_tag_processor()
            .expect("tag wrapper capability was checked")
            .process(context, tag, structure_handler)
    }
}

struct ElementModelProcessorWrapper {
    base: AbstractElementProcessorWrapper,
}

impl ElementModelProcessorWrapper {
    fn new(processor: Arc<dyn IProcessor>, dialect_precedence: i32) -> Self {
        Self {
            base: AbstractElementProcessorWrapper::new(processor, dialect_precedence),
        }
    }
}

impl IProcessor for ElementModelProcessorWrapper {
    fn is_attribute_definitions_aware(&self) -> bool {
        self.base.base.processor.is_attribute_definitions_aware()
    }
    fn set_attribute_definitions(&self, definitions: Arc<crate::engine::AttributeDefinitions>) {
        self.base
            .base
            .processor
            .set_attribute_definitions(definitions);
    }
    fn is_element_definitions_aware(&self) -> bool {
        self.base.base.processor.is_element_definitions_aware()
    }
    fn set_element_definitions(&self, definitions: Arc<crate::engine::ElementDefinitions>) {
        self.base
            .base
            .processor
            .set_element_definitions(definitions);
    }
    fn as_element_processor(&self) -> Option<&dyn IElementProcessor> {
        Some(self)
    }
    fn class_name(&self) -> &'static str {
        self.base.base.processor.class_name()
    }
    fn get_dialect_precedence(&self) -> Option<i32> {
        Some(self.base.base.dialect_precedence)
    }
    fn get_wrapped_processor(&self) -> Option<&dyn IProcessor> {
        Some(self.base.base.processor.as_ref())
    }
    fn get_template_mode(&self) -> Option<TemplateMode> {
        self.base.base.template_mode()
    }
    fn get_precedence(&self) -> i32 {
        self.base.base.processor_precedence
    }
}

impl IElementProcessor for ElementModelProcessorWrapper {
    fn as_element_model_processor(&self) -> Option<&dyn IElementModelProcessor> {
        Some(self)
    }
    fn get_matching_element_name(&self) -> Option<&MatchingElementName> {
        self.base.processor().get_matching_element_name()
    }
    fn get_matching_attribute_name(&self) -> Option<&MatchingAttributeName> {
        self.base.processor().get_matching_attribute_name()
    }
}

impl IElementModelProcessor for ElementModelProcessorWrapper {
    fn process(
        &self,
        context: &dyn ITemplateContext,
        model: &mut dyn IModel,
        structure_handler: &mut dyn IElementModelStructureHandler,
    ) -> Result<(), Box<dyn TemplateEngineException>> {
        self.base
            .processor()
            .as_element_model_processor()
            .expect("model wrapper capability was checked")
            .process(context, model, structure_handler)
    }
}

macro_rules! define_event_wrapper {
    (
        $name:ident,
        $capability:ident,
        $trait_name:ident,
        $event:ty,
        $handler:ty
    ) => {
        struct $name {
            base: AbstractProcessorWrapper,
        }

        impl $name {
            fn new(processor: Arc<dyn IProcessor>, dialect_precedence: i32) -> Self {
                Self {
                    base: AbstractProcessorWrapper::new(processor, dialect_precedence),
                }
            }
        }

        implement_base_processor!($name);

        impl $trait_name for $name {
            fn process(
                &self,
                context: &dyn ITemplateContext,
                event: &$event,
                structure_handler: &mut $handler,
            ) -> Result<(), Box<dyn TemplateEngineException>> {
                self.base
                    .processor
                    .$capability()
                    .expect("event wrapper capability was checked")
                    .process(context, event, structure_handler)
            }
        }
    };
}

define_event_wrapper!(
    CDATASectionProcessorWrapper,
    as_cdata_section_processor,
    ICDATASectionProcessor,
    dyn ICDATASection,
    dyn ICDATASectionStructureHandler
);
define_event_wrapper!(
    CommentProcessorWrapper,
    as_comment_processor,
    ICommentProcessor,
    dyn IComment,
    dyn ICommentStructureHandler
);
define_event_wrapper!(
    DocTypeProcessorWrapper,
    as_doc_type_processor,
    IDocTypeProcessor,
    dyn IDocType,
    dyn IDocTypeStructureHandler
);
define_event_wrapper!(
    ProcessingInstructionProcessorWrapper,
    as_processing_instruction_processor,
    IProcessingInstructionProcessor,
    dyn IProcessingInstruction,
    dyn IProcessingInstructionStructureHandler
);
define_event_wrapper!(
    TextProcessorWrapper,
    as_text_processor,
    ITextProcessor,
    dyn IText,
    dyn ITextStructureHandler
);
define_event_wrapper!(
    XMLDeclarationProcessorWrapper,
    as_xml_declaration_processor,
    IXMLDeclarationProcessor,
    dyn IXMLDeclaration,
    dyn IXMLDeclarationStructureHandler
);

struct TemplateBoundariesProcessorWrapper {
    base: AbstractProcessorWrapper,
}

impl TemplateBoundariesProcessorWrapper {
    fn new(processor: Arc<dyn IProcessor>, dialect_precedence: i32) -> Self {
        Self {
            base: AbstractProcessorWrapper::new(processor, dialect_precedence),
        }
    }
}

implement_base_processor!(TemplateBoundariesProcessorWrapper);

impl ITemplateBoundariesProcessor for TemplateBoundariesProcessorWrapper {
    fn process_template_start(
        &self,
        context: &dyn ITemplateContext,
        template_start: &dyn ITemplateStart,
        structure_handler: &mut dyn ITemplateBoundariesStructureHandler,
    ) -> Result<(), Box<dyn TemplateEngineException>> {
        self.base
            .processor
            .as_template_boundaries_processor()
            .expect("template boundaries capability was checked")
            .process_template_start(context, template_start, structure_handler)
    }

    fn process_template_end(
        &self,
        context: &dyn ITemplateContext,
        template_end: &dyn ITemplateEnd,
        structure_handler: &mut dyn ITemplateBoundariesStructureHandler,
    ) -> Result<(), Box<dyn TemplateEngineException>> {
        self.base
            .processor
            .as_template_boundaries_processor()
            .expect("template boundaries capability was checked")
            .process_template_end(context, template_end, structure_handler)
    }
}

/// 附加方言 precedence 的 PreProcessor 委托包装器。
///
/// 对应 Java: `ProcessorConfigurationUtils.PreProcessorWrapper`。
struct PreProcessorWrapper {
    pre_processor: Arc<dyn IPreProcessor>,
    dialect_precedence: i32,
}

impl IPreProcessor for PreProcessorWrapper {
    fn get_dialect_precedence(&self) -> Option<i32> {
        Some(self.dialect_precedence)
    }

    fn get_wrapped_pre_processor(&self) -> Option<&dyn IPreProcessor> {
        Some(self.unwrap())
    }

    fn is_attribute_definitions_aware(&self) -> bool {
        self.pre_processor.is_attribute_definitions_aware()
    }

    fn set_attribute_definitions(
        &self,
        attribute_definitions: Arc<crate::engine::AttributeDefinitions>,
    ) {
        self.pre_processor
            .set_attribute_definitions(attribute_definitions);
    }

    fn is_element_definitions_aware(&self) -> bool {
        self.pre_processor.is_element_definitions_aware()
    }

    fn set_element_definitions(&self, element_definitions: Arc<crate::engine::ElementDefinitions>) {
        self.pre_processor
            .set_element_definitions(element_definitions);
    }

    fn get_template_mode(&self) -> Option<TemplateMode> {
        self.pre_processor.get_template_mode()
    }

    fn get_precedence(&self) -> i32 {
        self.pre_processor.get_precedence()
    }

    fn get_handler_class(&self) -> Option<&crate::engine::TemplateHandlerClass> {
        self.pre_processor.get_handler_class()
    }

    fn class_name(&self) -> &'static str {
        self.pre_processor.class_name()
    }
}

impl PreProcessorWrapper {
    /// 返回包装前的 PreProcessor。
    ///
    /// 对应 Java: `PreProcessorWrapper#unwrap()`。
    fn unwrap(&self) -> &dyn IPreProcessor {
        self.pre_processor.as_ref()
    }
}

/// 附加方言 precedence 的 PostProcessor 委托包装器。
///
/// 对应 Java: `ProcessorConfigurationUtils.PostProcessorWrapper`。
struct PostProcessorWrapper {
    post_processor: Arc<dyn IPostProcessor>,
    dialect_precedence: i32,
}

impl IPostProcessor for PostProcessorWrapper {
    fn get_dialect_precedence(&self) -> Option<i32> {
        Some(self.dialect_precedence)
    }

    fn get_wrapped_post_processor(&self) -> Option<&dyn IPostProcessor> {
        Some(self.unwrap())
    }

    fn is_attribute_definitions_aware(&self) -> bool {
        self.post_processor.is_attribute_definitions_aware()
    }

    fn set_attribute_definitions(
        &self,
        attribute_definitions: Arc<crate::engine::AttributeDefinitions>,
    ) {
        self.post_processor
            .set_attribute_definitions(attribute_definitions);
    }

    fn is_element_definitions_aware(&self) -> bool {
        self.post_processor.is_element_definitions_aware()
    }

    fn set_element_definitions(&self, element_definitions: Arc<crate::engine::ElementDefinitions>) {
        self.post_processor
            .set_element_definitions(element_definitions);
    }

    fn get_template_mode(&self) -> Option<TemplateMode> {
        self.post_processor.get_template_mode()
    }

    fn get_precedence(&self) -> i32 {
        self.post_processor.get_precedence()
    }

    fn get_handler_class(&self) -> Option<&crate::engine::TemplateHandlerClass> {
        self.post_processor.get_handler_class()
    }

    fn class_name(&self) -> &'static str {
        self.post_processor.class_name()
    }
}

impl PostProcessorWrapper {
    /// 返回包装前的 PostProcessor。
    ///
    /// 对应 Java: `PostProcessorWrapper#unwrap()`。
    fn unwrap(&self) -> &dyn IPostProcessor {
        self.post_processor.as_ref()
    }
}

fn require_capability(
    present: bool,
    processor: &Arc<dyn IProcessor>,
    kind: &str,
) -> Result<(), ConfigurationException> {
    if present {
        Ok(())
    } else {
        Err(configuration_error(format!(
            "Processor {} does not implement the required {kind} processor interface",
            processor.class_name()
        )))
    }
}

fn configuration_error(message: String) -> ConfigurationException {
    ConfigurationException::new(Some(message))
}