18
18
import org .w3c .dom .Node ;
19
19
20
20
import java .util .List ;
21
+ import java .util .Map ;
21
22
22
23
import static uk .co .real_logic .sbe .xml .XmlSchemaParser .*;
23
24
@@ -84,7 +85,7 @@ public Field(
84
85
this .timeUnit = timeUnit ;
85
86
}
86
87
87
- public void validate (final Node node )
88
+ public void validate (final Node node , final Map < String , Type > typeByNameMap )
88
89
{
89
90
if (type != null &&
90
91
semanticType != null &&
@@ -96,34 +97,43 @@ public void validate(final Node node)
96
97
97
98
checkForValidName (node , name );
98
99
100
+ if (null != valueRef )
101
+ {
102
+ validateValueRef (node , typeByNameMap );
103
+ }
104
+
99
105
if (type instanceof EnumType && presence == Presence .CONSTANT )
100
106
{
101
107
if (null == valueRef )
102
108
{
103
109
handleError (node , "valueRef not set for constant enum" );
104
110
}
105
- else
111
+ }
112
+
113
+ if (null != valueRef && presence == Presence .CONSTANT )
114
+ {
115
+ final String valueRefType = valueRef .substring (0 , valueRef .indexOf ('.' ));
116
+
117
+ if (!(type instanceof EnumType ))
106
118
{
107
- final int periodIndex = valueRef .indexOf ('.' );
108
- if (periodIndex < 1 || periodIndex == (valueRef .length () - 1 ))
119
+ if (type instanceof EncodedDataType )
109
120
{
110
- handleError (
111
- node , "valueRef format not valid for constant (enum-name.valid-value-name): " + valueRef );
112
- }
121
+ final EnumType enumType = (EnumType )typeByNameMap .get (valueRefType );
113
122
114
- final String valueRefType = valueRef . substring ( 0 , periodIndex );
115
- if (! valueRefType . equals ( type . name ()))
116
- {
117
- handleError ( node , "valueRef for enum name not found: " + valueRefType );
123
+ if ((( EncodedDataType ) type ). primitiveType () != enumType . encodingType ())
124
+ {
125
+ handleError ( node , "valueRef does not match field type: " + valueRef );
126
+ }
118
127
}
119
-
120
- final String validValueName = valueRef .substring (periodIndex + 1 );
121
- final EnumType enumType = (EnumType )type ;
122
- if (null == enumType .getValidValue (validValueName ))
128
+ else
123
129
{
124
- handleError (node , "valueRef for validValue name not found : " + validValueName );
130
+ handleError (node , "valueRef does not match field type : " + valueRef );
125
131
}
126
132
}
133
+ else if (!valueRefType .equals (type .name ()))
134
+ {
135
+ handleError (node , "valueRef for enum name not found: " + valueRefType );
136
+ }
127
137
}
128
138
}
129
139
@@ -255,6 +265,38 @@ public String toString()
255
265
'}' ;
256
266
}
257
267
268
+ private void validateValueRef (final Node node , final Map <String , Type > typeByNameMap )
269
+ {
270
+ final int periodIndex = valueRef .indexOf ('.' );
271
+ if (periodIndex < 1 || periodIndex == (valueRef .length () - 1 ))
272
+ {
273
+ handleError (
274
+ node , "valueRef format not valid for constant (enum-name.valid-value-name): " + valueRef );
275
+ }
276
+
277
+ final String valueRefType = valueRef .substring (0 , periodIndex );
278
+ final Type valueType = typeByNameMap .get (valueRefType );
279
+ if (null == valueType )
280
+ {
281
+ handleError (node , "valueRef for enum name not found: " + valueRefType );
282
+ }
283
+
284
+ if (valueType instanceof EnumType )
285
+ {
286
+ final EnumType enumType = (EnumType )valueType ;
287
+ final String validValueName = valueRef .substring (periodIndex + 1 );
288
+
289
+ if (null == enumType .getValidValue (validValueName ))
290
+ {
291
+ handleError (node , "valueRef for validValue name not found: " + validValueName );
292
+ }
293
+ }
294
+ else
295
+ {
296
+ handleError (node , "valueRef for is not of type enum: " + valueRefType );
297
+ }
298
+ }
299
+
258
300
public static class Builder
259
301
{
260
302
private String name ;
0 commit comments