-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathidentifiers.xsl
More file actions
236 lines (216 loc) · 14.2 KB
/
identifiers.xsl
File metadata and controls
236 lines (216 loc) · 14.2 KB
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
<?xml version="1.0" encoding="UTF-8"?>
<?modxslt-stylesheet type="text/xsl" media="fuffa, screen and $GET[stylesheet]" href="./%24GET%5Bstylesheet%5D" alternate="no" title="Translation using provided stylesheet" charset="ISO-8859-1" ?>
<?modxslt-stylesheet type="text/xsl" media="screen" alternate="no" title="Show raw source of the XML file" charset="ISO-8859-1" ?>
<xsl:stylesheet
xmlns:yaslt="http://www.mod-xslt2.com/ns/1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
xmlns:exsl="http://exslt.org/common"
xmlns:str="http://exslt.org/strings"
xmlns:func="http://exslt.org/functions"
xmlns:my="http://localhost.localdomain/localns"
exclude-result-prefixes="my"
extension-element-prefixes="yaslt exsl func str">
<xsl:include href="./identifiers.common.xsl"/>
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- MAIN, FILE GENERATION -->
<xsl:template match="/constants">
<!-- JAVA FILE -->
<xsl:variable name="jname">
<xsl:value-of select="($name)"/>
</xsl:variable>
<exsl:document href="{$prefix}{$jname}.java" method="text">
<xsl:text>//package ... ;
</xsl:text>
<xsl:text>import imasjava.*;
</xsl:text>
<xsl:apply-templates select="header" mode="Java"/>
<xsl:apply-templates select="include[@name='Java']"/><xsl:text>

</xsl:text>
<xsl:text>

public final class </xsl:text>
<xsl:value-of select="$jname"/><xsl:text> {

</xsl:text>
<xsl:apply-templates select="comment" mode="Java"/>
<xsl:if test="//constants[@create_mapping_function]">
<xsl:text>
 // Mapping functionality
</xsl:text>
<xsl:text> public static int getIndex(String name) {
</xsl:text>
<xsl:for-each select="//constants/int[@name]">
<xsl:text> if ("</xsl:text><xsl:value-of select="@name"/><xsl:text>".equals(name)) {
</xsl:text>
<xsl:text> return </xsl:text><xsl:value-of select="."/><xsl:text>;
</xsl:text>
<xsl:text> }
</xsl:text>
<xsl:if test="@alias">
<xsl:text> if ("</xsl:text><xsl:value-of select="@alias"/><xsl:text>".equals(name)) {
</xsl:text>
<xsl:text> return </xsl:text><xsl:value-of select="."/><xsl:text>;
</xsl:text>
<xsl:text> }
</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text> return -999999999; // Unknown identifier
</xsl:text>
<xsl:text> }

</xsl:text>
<xsl:text> public static String getDescription(int idx) {
</xsl:text>
<xsl:text> switch(idx) {
</xsl:text>
<xsl:for-each select="//constants/int[@name]">
<xsl:text> case </xsl:text>
<xsl:value-of select="."/>
<xsl:text>:
</xsl:text>
<xsl:text> return "</xsl:text>
<xsl:value-of select="@description"/>
<xsl:text>";
</xsl:text>
</xsl:for-each>
<xsl:text> }
</xsl:text>
<xsl:text> return "unknown";
</xsl:text>
<xsl:text> }

</xsl:text>
<xsl:text> public static String getName(int idx) {
</xsl:text>
<xsl:text> switch(idx) {
</xsl:text>
<xsl:for-each select="//constants/int[@name]">
<xsl:text> case </xsl:text>
<xsl:value-of select="."/>
<xsl:text>:
</xsl:text>
<xsl:text> return "</xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>";
</xsl:text>
</xsl:for-each>
<xsl:text> }
</xsl:text>
<xsl:text> return "unknown";
</xsl:text>
<xsl:text> }

</xsl:text>
<xsl:text> public static class IdentifierData {
</xsl:text>
<xsl:text> public int index;
</xsl:text>
<xsl:text> public String originalname;
</xsl:text>
<xsl:text> public String description;
</xsl:text>
<xsl:text> 
</xsl:text>
<xsl:text> public IdentifierData(int index, String originalname, String description) {
</xsl:text>
<xsl:text> this.index = index;
</xsl:text>
<xsl:text> this.originalname = originalname;
</xsl:text>
<xsl:text> this.description = description;
</xsl:text>
<xsl:text> }
</xsl:text>
<xsl:text> }

</xsl:text>
<xsl:text> public static IdentifierData getIdentifierDataByName(String name) throws IllegalArgumentException {
</xsl:text>
<xsl:for-each select="//constants/int[@name]">
<xsl:text> if ("</xsl:text><xsl:value-of select="@name"/><xsl:text>".equals(name)) {
</xsl:text>
<xsl:text> return new IdentifierData(</xsl:text><xsl:value-of select="."/><xsl:text>, "</xsl:text><xsl:value-of select="@name"/><xsl:text>","</xsl:text><xsl:value-of select="@description"/><xsl:text>");
</xsl:text>
<xsl:text> }
</xsl:text>
<xsl:if test="@alias">
<xsl:text> if ("</xsl:text><xsl:value-of select="@alias"/><xsl:text>".equals(name)) {
</xsl:text>
<xsl:text> return new IdentifierData(</xsl:text><xsl:value-of select="."/><xsl:text>, "</xsl:text><xsl:value-of select="@name"/><xsl:text>","</xsl:text><xsl:value-of select="@description"/><xsl:text>");
</xsl:text>
<xsl:text> }
</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text> // Unknown identifier - throw exception
</xsl:text>
<xsl:text> throw new IllegalArgumentException("Unknown identifier: '" + name + "'");
</xsl:text>
<xsl:text> }

</xsl:text>
<xsl:text> // Setter for any object using reflection
</xsl:text>
<xsl:text> public static void setIdentifier(Object obj, String name) throws IllegalArgumentException {
</xsl:text>
<xsl:text> try {
</xsl:text>
<xsl:text> IdentifierData IdentifierData = getIdentifierDataByName(name);
</xsl:text>
<xsl:text> String objName = IdentifierData.originalname;
</xsl:text>
<xsl:text> 
</xsl:text>
<xsl:text> // Use reflection to set the properties
</xsl:text>
<xsl:text> Class<?> clazz = obj.getClass();
</xsl:text>
<xsl:text> 
</xsl:text>
<xsl:text> // Set index field
</xsl:text>
<xsl:text> try {
</xsl:text>
<xsl:text> java.lang.reflect.Field indexField = clazz.getField("index");
</xsl:text>
<xsl:text> indexField.setAccessible(true);
</xsl:text>
<xsl:text> indexField.set(obj, IdentifierData.index);
</xsl:text>
<xsl:text> } catch (NoSuchFieldException | IllegalAccessException e) {
</xsl:text>
<xsl:text> throw new IllegalArgumentException("Cannot set index on object of type " + clazz.getSimpleName() + ": no index field found", e);
</xsl:text>
<xsl:text> }
</xsl:text>
<xsl:text> 
</xsl:text>
<xsl:text> // Set name field
</xsl:text>
<xsl:text> try {
</xsl:text>
<xsl:text> java.lang.reflect.Field nameField = clazz.getField("name");
</xsl:text>
<xsl:text> nameField.setAccessible(true);
</xsl:text>
<xsl:text> nameField.set(obj, objName);
</xsl:text>
<xsl:text> } catch (NoSuchFieldException | IllegalAccessException e) {
</xsl:text>
<xsl:text> throw new IllegalArgumentException("Cannot set name on object of type " + clazz.getSimpleName() + ": no name field found", e);
</xsl:text>
<xsl:text> }
</xsl:text>
<xsl:text> 
</xsl:text>
<xsl:text> // Set description field
</xsl:text>
<xsl:text> try {
</xsl:text>
<xsl:text> java.lang.reflect.Field descriptionField = clazz.getField("description");
</xsl:text>
<xsl:text> descriptionField.setAccessible(true);
</xsl:text>
<xsl:text> descriptionField.set(obj, IdentifierData.description);
</xsl:text>
<xsl:text> } catch (NoSuchFieldException | IllegalAccessException e) {
</xsl:text>
<xsl:text> throw new IllegalArgumentException("Cannot set description on object of type " + clazz.getSimpleName() + ": no description field found", e);
</xsl:text>
<xsl:text> }
</xsl:text>
<xsl:text> 
</xsl:text>
<xsl:text> } catch (IllegalArgumentException e) {
</xsl:text>
<xsl:text> // Re-throw with more context
</xsl:text>
<xsl:text> throw new IllegalArgumentException("Failed to set identifier: " + e.getMessage(), e);
</xsl:text>
<xsl:text> }
</xsl:text>
<xsl:text> }

</xsl:text>
<xsl:text> // Setter for any object with array fields using reflection
</xsl:text>
<xsl:text> public static void setIdentifier(Object obj, String[] names) throws IllegalArgumentException {
</xsl:text>
<xsl:text> int[] indices = new int[names.length];
</xsl:text>
<xsl:text> String[] nameResults = new String[names.length];
</xsl:text>
<xsl:text> String[] descriptions = new String[names.length];
</xsl:text>
<xsl:text> 
</xsl:text>
<xsl:text> for (int i = 0; i < names.length; i++) {
</xsl:text>
<xsl:text> try {
</xsl:text>
<xsl:text> IdentifierData IdentifierData = getIdentifierDataByName(names[i]);
</xsl:text>
<xsl:text> indices[i] = IdentifierData.index;
</xsl:text>
<xsl:text> nameResults[i] = IdentifierData.originalname;
</xsl:text>
<xsl:text> descriptions[i] = IdentifierData.description;
</xsl:text>
<xsl:text> } catch (IllegalArgumentException e) {
</xsl:text>
<xsl:text> // Re-throw with array index context
</xsl:text>
<xsl:text> throw new IllegalArgumentException("Failed to set identifier at index " + i + ": " + e.getMessage(), e);
</xsl:text>
<xsl:text> }
</xsl:text>
<xsl:text> }
</xsl:text>
<xsl:text> 
</xsl:text>
<xsl:text> // Create Vect1D objects for indices, names and descriptions
</xsl:text>
<xsl:text> imasjava.Vect1DInt indicesVect = new imasjava.Vect1DInt(indices);
</xsl:text>
<xsl:text> imasjava.Vect1DString namesVect = new imasjava.Vect1DString(nameResults);
</xsl:text>
<xsl:text> imasjava.Vect1DString descriptionsVect = new imasjava.Vect1DString(descriptions);
</xsl:text>
<xsl:text> 
</xsl:text>
<xsl:text> // Use reflection to set the vector properties
</xsl:text>
<xsl:text> Class<?> clazz = obj.getClass();
</xsl:text>
<xsl:text> 
</xsl:text>
<xsl:text> // Set indices field
</xsl:text>
<xsl:text> try {
</xsl:text>
<xsl:text> java.lang.reflect.Field indicesField = clazz.getField("indices");
</xsl:text>
<xsl:text> indicesField.setAccessible(true);
</xsl:text>
<xsl:text> indicesField.set(obj, indicesVect);
</xsl:text>
<xsl:text> } catch (NoSuchFieldException | IllegalAccessException e) {
</xsl:text>
<xsl:text> throw new IllegalArgumentException("Cannot set indices on object: " + e.getMessage(), e);
</xsl:text>
<xsl:text> }
</xsl:text>
<xsl:text> 
</xsl:text>
<xsl:text> // Set names field
</xsl:text>
<xsl:text> try {
</xsl:text>
<xsl:text> java.lang.reflect.Field namesField = clazz.getField("names");
</xsl:text>
<xsl:text> namesField.setAccessible(true);
</xsl:text>
<xsl:text> namesField.set(obj, namesVect);
</xsl:text>
<xsl:text> } catch (NoSuchFieldException | IllegalAccessException e) {
</xsl:text>
<xsl:text> throw new IllegalArgumentException("Cannot set names on object: " + e.getMessage(), e);
</xsl:text>
<xsl:text> }
</xsl:text>
<xsl:text> 
</xsl:text>
<xsl:text> // Set descriptions field
</xsl:text>
<xsl:text> try {
</xsl:text>
<xsl:text> java.lang.reflect.Field descriptionsField = clazz.getField("descriptions");
</xsl:text>
<xsl:text> descriptionsField.setAccessible(true);
</xsl:text>
<xsl:text> descriptionsField.set(obj, descriptionsVect);
</xsl:text>
<xsl:text> } catch (NoSuchFieldException | IllegalAccessException e) {
</xsl:text>
<xsl:text> throw new IllegalArgumentException("Cannot set descriptions on object: " + e.getMessage(), e);
</xsl:text>
<xsl:text> }
</xsl:text>
<xsl:text> }
</xsl:text>
</xsl:if>
<xsl:text>
}
</xsl:text>
</exsl:document>
<!-- DOCBOOK FILE -->
<exsl:document href="{$prefix}{$name}.xml" method="text">
<xsl:call-template name="docbook"/>
</exsl:document>
</xsl:template>
<!-- JAVA TEMPLATES -->
<xsl:template match="header" mode="Java">
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="concat('
',text())"/>
<xsl:with-param name="replace" select="'
'"/>
<xsl:with-param name="with" select="'
// '"/>
</xsl:call-template>
</xsl:template>
<!-- Removed static final variable generation templates -->
<xsl:template match="comment" mode="Java">
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="concat('
',text())"/>
<xsl:with-param name="replace" select="'
'"/>
<xsl:with-param name="with" select="'
// '"/>
</xsl:call-template>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>