1818import org .w3c .dom .Node ;
1919
2020import java .util .List ;
21+ import java .util .Map ;
2122
2223import static uk .co .real_logic .sbe .xml .XmlSchemaParser .*;
2324
@@ -84,7 +85,7 @@ public Field(
8485 this .timeUnit = timeUnit ;
8586 }
8687
87- public void validate (final Node node )
88+ public void validate (final Node node , final Map < String , Type > typeByNameMap )
8889 {
8990 if (type != null &&
9091 semanticType != null &&
@@ -96,34 +97,43 @@ public void validate(final Node node)
9697
9798 checkForValidName (node , name );
9899
100+ if (null != valueRef )
101+ {
102+ validateValueRef (node , typeByNameMap );
103+ }
104+
99105 if (type instanceof EnumType && presence == Presence .CONSTANT )
100106 {
101107 if (null == valueRef )
102108 {
103109 handleError (node , "valueRef not set for constant enum" );
104110 }
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 ))
106118 {
107- final int periodIndex = valueRef .indexOf ('.' );
108- if (periodIndex < 1 || periodIndex == (valueRef .length () - 1 ))
119+ if (type instanceof EncodedDataType )
109120 {
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 );
113122
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+ }
118127 }
119-
120- final String validValueName = valueRef .substring (periodIndex + 1 );
121- final EnumType enumType = (EnumType )type ;
122- if (null == enumType .getValidValue (validValueName ))
128+ else
123129 {
124- handleError (node , "valueRef for validValue name not found : " + validValueName );
130+ handleError (node , "valueRef does not match field type : " + valueRef );
125131 }
126132 }
133+ else if (!valueRefType .equals (type .name ()))
134+ {
135+ handleError (node , "valueRef for enum name not found: " + valueRefType );
136+ }
127137 }
128138 }
129139
@@ -255,6 +265,38 @@ public String toString()
255265 '}' ;
256266 }
257267
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+
258300 public static class Builder
259301 {
260302 private String name ;
0 commit comments