Add verigraph code base
[parser.git] / verigraph / src / main / java / it / polito / escape / verify / validation / DpiValidator.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Apache License, Version 2.0
6  * which accompanies this distribution, and is available at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *******************************************************************************/
9
10 package it.polito.escape.verify.validation;
11
12 import com.fasterxml.jackson.databind.JsonNode;
13
14 import it.polito.escape.verify.model.Configuration;
15 import it.polito.escape.verify.model.Graph;
16 import it.polito.escape.verify.model.Node;
17 import it.polito.escape.verify.validation.exception.ValidationException;
18
19 public class DpiValidator implements ValidationInterface {
20
21         public DpiValidator(){
22
23         }
24
25         private void validateKey(String key) throws ValidationException {
26                 if (!key.matches("\\w+"))
27                         throw new ValidationException("'" + key + "' is not a valid configuration string for a 'dpi'");
28         }
29
30         @Override
31         public void validate(Graph graph, Node node, Configuration configuration) throws ValidationException {
32
33                 JsonNode conf = configuration.getConfiguration();
34
35                 if (!conf.isArray()) {
36                         throw new ValidationException("Configuration of a 'dpi' must be an array");
37                 }
38                 for (JsonNode key : conf) {
39                         if (!key.isTextual())
40                                 throw new ValidationException("Configuration of a 'dpi' must be an array of strings");
41                         validateKey(key.asText());
42                 }
43         }
44
45 }