2020
2121import org .entur .gbfs .validation .model .FileValidationError ;
2222import org .entur .gbfs .validation .model .FileValidationResult ;
23- import org .entur .gbfs .validation .versions .Version ;
24- import org .entur .gbfs .validation .versions .VersionFactory ;
25- import org .everit .json .schema .Schema ;
23+ import org .entur .gbfs .validation .validator .versions .Version ;
24+ import org .entur .gbfs .validation .validator .versions .VersionFactory ;
2625import org .everit .json .schema .ValidationException ;
27- import org .everit .json .schema .loader .SchemaLoader ;
2826import org .json .JSONObject ;
29- import org .json .JSONTokener ;
3027import org .slf4j .Logger ;
3128import org .slf4j .LoggerFactory ;
3229
33- import java .io .InputStream ;
3430import java .util .Collections ;
35- import java .util .HashMap ;
3631import java .util .List ;
3732import java .util .Map ;
3833import java .util .Optional ;
4237public class FileValidator {
4338 private static final Logger logger = LoggerFactory .getLogger (FileValidator .class );
4439 private final Version version ;
45- private final Map <String , Schema > schemas ;
46-
4740 private static final Map <String , FileValidator > FILE_VALIDATORS = new ConcurrentHashMap <>();
4841
42+
4943 public static FileValidator getFileValidator (
5044 String detectedVersion
5145 ) {
5246 if (FILE_VALIDATORS .containsKey (detectedVersion )) {
5347 return FILE_VALIDATORS .get (detectedVersion );
5448 } else {
5549 Version version = VersionFactory .createVersion (detectedVersion );
56- Map < String , Schema > schemas = FileValidator . getSchemas ( version );
57- FileValidator fileValidator = new FileValidator (version , schemas );
50+
51+ FileValidator fileValidator = new FileValidator (version );
5852 FILE_VALIDATORS .put (detectedVersion , fileValidator );
5953 return fileValidator ;
6054 }
6155 }
6256
63- private FileValidator (
64- Version version ,
65- Map <String , Schema > schemas
57+ protected FileValidator (
58+ Version version
6659 ) {
6760 this .version = version ;
68- this .schemas = schemas ;
6961 }
7062
71- public FileValidationResult validate (String feedName , JSONObject feed ) {
72- if (schemas .containsKey (feedName )) {
73- return validate (schemas .get (feedName ), feed , feedName );
74- }
75-
76- logger .warn ("Schema not found for gbfs feed={} version={}" , feedName , version .getVersion ());
77- return null ;
78- }
63+ public FileValidationResult validate (String feedName , Map <String , JSONObject > feedMap ) {
64+ if (version .getFileNames ().contains (feedName )) {
65+ JSONObject feed = feedMap .get (feedName );
66+ FileValidationResult fileValidationResult = new FileValidationResult ();
67+ fileValidationResult .setFile (feedName );
68+ fileValidationResult .setRequired (isRequired (feedName ));
69+ fileValidationResult .setExists (feed != null );
70+ fileValidationResult .setSchema (version .getSchema (feedName , feedMap ).toString ());
71+ fileValidationResult .setFileContents (Optional .ofNullable (feed ).map (JSONObject ::toString ).orElse (null ));
72+ fileValidationResult .setVersion (version .getVersionString ());
73+
74+ try {
75+ version .validate (feedName , feedMap );
76+ } catch (ValidationException validationException ) {
77+ fileValidationResult .setErrors (mapToValidationErrors (validationException ));
78+ fileValidationResult .setErrorsCount (validationException .getViolationCount ());
79+ }
7980
80- private FileValidationResult validate (Schema schema , JSONObject feed , String feedName ) {
81- FileValidationResult fileValidationResult = new FileValidationResult ();
82- fileValidationResult .setFile (feedName );
83- fileValidationResult .setRequired (isRequired (feedName ));
84- fileValidationResult .setExists (feed != null );
85- fileValidationResult .setSchema (schema .toString ());
86- fileValidationResult .setFileContents (Optional .ofNullable (feed ).map (JSONObject ::toString ).orElse (null ));
87- fileValidationResult .setVersion (version .getVersion ());
88-
89- try {
90- schema .validate (feed );
91- } catch (ValidationException validationException ) {
92- fileValidationResult .setErrors (mapToValidationErrors (validationException ));
93- fileValidationResult .setErrorsCount (validationException .getViolationCount ());
81+ return fileValidationResult ;
9482 }
9583
96- return fileValidationResult ;
84+ logger .warn ("Schema not found for gbfs feed={} version={}" , feedName , version .getVersionString ());
85+ return null ;
9786 }
9887
9988 List <FileValidationError > mapToValidationErrors (ValidationException validationException ) {
@@ -115,39 +104,11 @@ private boolean isRequired(String feedName) {
115104 return version .isFileRequired (feedName );
116105 }
117106
118- protected static Map <String , Schema > getSchemas (Version version ) {
119- Map <String , Schema > schemas = new HashMap <>();
120- version .getFeeds ().forEach (feed -> {
121- Schema schema = loadSchema (version .getVersion (), feed );
122- if (schema != null ) {
123- schemas .put (feed , schema );
124- }
125- });
126- return schemas ;
127- }
128-
129- protected static Schema loadSchema (String version , String feedName ) {
130- InputStream inputStream = FileValidator .class .getClassLoader ().getResourceAsStream ("schema/v" +version +"/" +feedName +".json" );
131-
132- if (inputStream == null ) {
133- logger .warn ("Unable to load schema version={} feedName={}" , version , feedName );
134- return null ;
135- }
136-
137- JSONObject rawSchema = new JSONObject (new JSONTokener (inputStream ));
138- SchemaLoader schemaLoader = SchemaLoader .builder ()
139- .enableOverrideOfBuiltInFormatValidators ()
140- .addFormatValidator (new URIFormatValidator ())
141- .schemaJson (rawSchema )
142- .build ();
143-
144- return schemaLoader .load ().build ();
145- }
146107
147108 public void validateMissingFile (FileValidationResult fvr ) {
148- if (version .getFeeds ().contains (fvr .getFile ())) {
149- fvr .setVersion (version .getVersion ());
150- fvr .setSchema (schemas . get (fvr .getFile ()).toString ());
109+ if (version .getFileNames ().contains (fvr .getFile ())) {
110+ fvr .setVersion (version .getVersionString ());
111+ fvr .setSchema (version . getSchema (fvr .getFile ()).toString ());
151112 fvr .setRequired (version .isFileRequired (fvr .getFile ()));
152113 }
153114 }
0 commit comments