Skip to content

Commit 6d9d79b

Browse files
committed
Allow macros in entity statements.
As yet macros were only allowed in the `rules` statement. This limited their usefulness rather strongly. By allowing macros in `entity` statements many of the limitations are removed. It is currently not possible to also allow calls to macros in other collector statements, because the may only contain literal rules and no entities. However, as a macro can contain either a literal rules or entities they cannot be allowed in collectors. This would either require a check if the content of the macro is allowed at the current position or two types of macros have to be introduced one for entity rules and one for literal rules. In order to prevent the inadvertent introduction of inconsistencies by later changes of the schema, the `entity-rule` group was removed and replaced by direct references to the `entity` element.
1 parent 4ce0b4c commit 6d9d79b

File tree

2 files changed

+141
-8
lines changed

2 files changed

+141
-8
lines changed

src/main/resources/schemata/metamorph.xsd

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
<complexType>
8282
<choice minOccurs="0" maxOccurs="1">
8383
<group ref="tns:literal-rule" />
84-
<group ref="tns:entity-rule" />
84+
<element ref="tns:entity" />
8585
</choice>
8686
<attribute ref="xml:base" />
8787
<attribute name="name" use="required" type="string">
@@ -115,7 +115,7 @@
115115
<complexType>
116116
<choice minOccurs="0" maxOccurs="unbounded">
117117
<group ref="tns:literal-rule" />
118-
<group ref="tns:entity-rule" />
118+
<element ref="tns:entity" />
119119
<element ref="tns:call-macro" />
120120
<element ref="xi:include" />
121121
</choice>
@@ -407,6 +407,7 @@
407407
<element ref="tns:entity-name" minOccurs="0" maxOccurs="1" />
408408
<group ref="tns:literal-rule" minOccurs="0" maxOccurs="unbounded" />
409409
<element ref="tns:entity" />
410+
<element ref="tns:call-macro" />
410411
</choice>
411412
<attribute name="name" type="string" use="optional">
412413
<annotation>
@@ -628,12 +629,6 @@
628629
</restriction>
629630
</simpleType>
630631

631-
<group name="entity-rule">
632-
<choice>
633-
<element ref="tns:entity" />
634-
</choice>
635-
</group>
636-
637632
<group name="literal-rule">
638633
<choice>
639634

src/test/java/org/culturegraph/mf/morph/MacroTest.xml

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,144 @@
4747
</result>
4848
</test-case>
4949

50+
<test-case name="macro in entity">
51+
<input type="text/x-cg+xml">
52+
<cgxml:cgxml version="1.0">
53+
<cgxml:records>
54+
<cgxml:record id="1">
55+
<cgxml:literal name="data" value="Aloha" />
56+
<cgxml:literal name="data2" value="Hawaii" />
57+
</cgxml:record>
58+
</cgxml:records>
59+
</cgxml:cgxml>
60+
</input>
5061

62+
<transformation type="text/x-metamorph+xml">
63+
<mm:metamorph version="1" entityMarker=".">
64+
<mm:macros>
65+
<mm:macro name="toUpper">
66+
<mm:data source="$[literal]" >
67+
<mm:case to="upper"/>
68+
</mm:data>
69+
</mm:macro>
70+
</mm:macros>
71+
<mm:rules>
72+
<mm:entity name="entity">
73+
<mm:call-macro name="toUpper" literal="data" />
74+
<mm:call-macro name="toUpper" literal="data2" />
75+
</mm:entity>
76+
</mm:rules>
77+
</mm:metamorph>
78+
</transformation>
79+
80+
<result type="text/x-cg+xml">
81+
<cgxml:cgxml version="1.0">
82+
<cgxml:records>
83+
<cgxml:record id="1">
84+
<cgxml:entity name="entity">
85+
<cgxml:literal name="data" value="ALOHA" />
86+
<cgxml:literal name="data2" value="HAWAII"/>
87+
</cgxml:entity>
88+
</cgxml:record>
89+
</cgxml:records>
90+
</cgxml:cgxml>
91+
</result>
92+
</test-case>
93+
94+
<test-case name="nested macros">
95+
<input type="text/x-cg+xml">
96+
<cgxml:cgxml version="1.0">
97+
<cgxml:records>
98+
<cgxml:record id="1">
99+
<cgxml:literal name="data" value="Aloha" />
100+
<cgxml:literal name="data2" value="Hawaii" />
101+
</cgxml:record>
102+
</cgxml:records>
103+
</cgxml:cgxml>
104+
</input>
105+
106+
<transformation type="text/x-metamorph+xml">
107+
<mm:metamorph version="1" entityMarker=".">
108+
<mm:macros>
109+
<mm:macro name="toUpper">
110+
<mm:data source="$[literal]" >
111+
<mm:case to="upper"/>
112+
</mm:data>
113+
</mm:macro>
114+
115+
<mm:macro name="macroEntity">
116+
<mm:entity name="$[entity]">
117+
<mm:call-macro name="toUpper" literal="data" />
118+
<mm:call-macro name="toUpper" literal="data2" />
119+
</mm:entity>
120+
</mm:macro>
121+
</mm:macros>
122+
<mm:rules>
123+
<mm:call-macro name="macroEntity" entity="macroEntity" />
124+
</mm:rules>
125+
</mm:metamorph>
126+
</transformation>
127+
128+
<result type="text/x-cg+xml">
129+
<cgxml:cgxml version="1.0">
130+
<cgxml:records>
131+
<cgxml:record id="1">
132+
<cgxml:entity name="macroEntity">
133+
<cgxml:literal name="data" value="ALOHA" />
134+
<cgxml:literal name="data2" value="HAWAII"/>
135+
</cgxml:entity>
136+
</cgxml:record>
137+
</cgxml:records>
138+
</cgxml:cgxml>
139+
</result>
140+
</test-case>
141+
142+
<test-case name="nested macros with forward reference">
143+
<input type="text/x-cg+xml">
144+
<cgxml:cgxml version="1.0">
145+
<cgxml:records>
146+
<cgxml:record id="1">
147+
<cgxml:literal name="data" value="Aloha" />
148+
<cgxml:literal name="data2" value="Hawaii" />
149+
</cgxml:record>
150+
</cgxml:records>
151+
</cgxml:cgxml>
152+
</input>
153+
154+
<transformation type="text/x-metamorph+xml">
155+
<mm:metamorph version="1" entityMarker=".">
156+
<mm:macros>
157+
<mm:macro name="macroEntity">
158+
<mm:entity name="$[entity]">
159+
<mm:call-macro name="toUpper" literal="data" />
160+
<mm:call-macro name="toUpper" literal="data2" />
161+
</mm:entity>
162+
</mm:macro>
163+
164+
<mm:macro name="toUpper">
165+
<mm:data source="$[literal]" >
166+
<mm:case to="upper"/>
167+
</mm:data>
168+
</mm:macro>
169+
</mm:macros>
170+
<mm:rules>
171+
<mm:call-macro name="macroEntity" entity="macroEntity" />
172+
</mm:rules>
173+
</mm:metamorph>
174+
</transformation>
175+
176+
<result type="text/x-cg+xml">
177+
<cgxml:cgxml version="1.0">
178+
<cgxml:records>
179+
<cgxml:record id="1">
180+
<cgxml:entity name="macroEntity">
181+
<cgxml:literal name="data" value="ALOHA" />
182+
<cgxml:literal name="data2" value="HAWAII"/>
183+
</cgxml:entity>
184+
</cgxml:record>
185+
</cgxml:records>
186+
</cgxml:cgxml>
187+
</result>
188+
</test-case>
51189

52190
</metamorph-test>

0 commit comments

Comments
 (0)