1 /**
2 License:
3     Boost Software License - Version 1.0 - August 17th, 2003
4 
5     Permission is hereby granted, free of charge, to any person or organization
6     obtaining a copy of the software and accompanying documentation covered by
7     this license (the "Software") to use, reproduce, display, distribute,
8     execute, and transmit the Software, and to prepare derivative works of the
9     Software, and to permit third-parties to whom the Software is furnished to
10     do so, all subject to the following:
11 
12     The copyright notices in the Software and this entire statement, including
13     the above license grant, this restriction and the following disclaimer,
14     must be included in all copies of the Software, in whole or in part, and
15     all derivative works of the Software, unless such copies or derivative
16     works are solely in the form of machine-executable object code generated by
17     a source language processor.
18 
19     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21     FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22     SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23     FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24     ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25     DEALINGS IN THE SOFTWARE.
26 
27 Authors:
28     Alexandru Ermicioi
29 **/
30 module aermicioi.aedi_property_reader.xml.inspector;
31 
32 import aermicioi.aedi.configurer.annotation.annotation : component;
33 import aermicioi.aedi_property_reader.convertor.inspector;
34 import std.xml;
35 import std.algorithm;
36 import std.range;
37 
38 @component
39 auto runtimeXmlInspector(Inspector!Element inspector) {
40     return new RuntimeInspector!Element(inspector);
41 }
42 
43 /**
44 Inspector for xml elements.
45 **/
46 @component
47 class XmlInspector : Inspector!Element {
48 
49     /**
50     Identify the type of child field of component.
51 
52     Params:
53         component = a composite component (class, struct, assoc array etc.) containing some fields
54 
55     Returns:
56         Type of field, or typeid(void) if field is not present in component
57     **/
58     TypeInfo typeOf(Element component, in string property) const nothrow {
59         auto elements = component.elements.filter!(c => c.tag.name == property);
60 
61         if (!elements.empty) {
62             return typeid(Element);
63         }
64 
65         auto attributes = component.tag.attr.byKeyValue.filter!(a => a.key == property);
66         if (!attributes.empty) {
67 
68             return typeid(string);
69         }
70 
71         return typeid(void);
72     }
73 
74     /**
75     Identify the type of component itself.
76 
77     Identify the type of component itself. It will inspect the component and will return accurate
78     type info that the component represents.
79 
80     Params:
81         component = component which should be identified.
82 
83     Returns:
84         Type info of component, or typeid(void) if component cannot be identified by inspector
85     **/
86     TypeInfo typeOf(Element component) const nothrow {
87         return typeid(component);
88     }
89 
90     /**
91     Check if component has a field or a property.
92 
93     Params:
94         component = component with fields
95         property = component property that is tested for existence
96 
97     Returns:
98         true if field is present either in readonly, or writeonly form (has getters and setters).
99     **/
100     bool has(Element component, in string property) const nothrow {
101         return
102             !component.elements.filter!(e => e.tag.name == property).empty ||
103             !component.tag.attr.byKeyValue.filter!(a => a.key == property).empty;
104     }
105 
106     /**
107     Return a list of properties that component holds.
108 
109     Params:
110         component = the component with fields
111 
112     Returns:
113         an arary of property identities.
114     **/
115     string[] properties(Element component) const nothrow {
116         return chain(
117             component.elements.map!(e => e.tag.name),
118             component.tag.attr.byKeyValue.map!(a => a.key)
119         ).array;
120     }
121 }