• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.networknt.schema.annotation;
17 
18 import java.util.ArrayList;
19 import java.util.LinkedHashMap;
20 import java.util.List;
21 import java.util.Map;
22 
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.networknt.schema.JsonNodePath;
25 import com.networknt.schema.serialization.JsonMapperFactory;
26 
27 /**
28  * The JSON Schema annotations.
29  *
30  * @see <a href=
31  *      "https://github.com/json-schema-org/json-schema-spec/issues/530">Details
32  *      of annotation collection</a>
33  */
34 public class JsonNodeAnnotations {
35 
36     /**
37      * Stores the annotations.
38      * <p>
39      * instancePath to annotation
40      */
41     private final Map<JsonNodePath, List<JsonNodeAnnotation>> values = new LinkedHashMap<>();
42 
43     /**
44      * Gets the annotations.
45      * <p>
46      * instancePath to annotation
47      *
48      * @return the annotations
49      */
asMap()50     public Map<JsonNodePath, List<JsonNodeAnnotation>> asMap() {
51         return this.values;
52     }
53 
54     /**
55      * Puts the annotation.
56      *
57      * @param annotation the annotation
58      */
put(JsonNodeAnnotation annotation)59     public void put(JsonNodeAnnotation annotation) {
60         this.values.computeIfAbsent(annotation.getInstanceLocation(), (k) -> new ArrayList<>()).add(annotation);
61 
62     }
63 
64     @Override
toString()65     public String toString() {
66         return Formatter.format(this.values);
67     }
68 
69     /**
70      * Formatter for pretty printing the annotations.
71      */
72     public static class Formatter {
73         /**
74          * Formats the annotations.
75          *
76          * @param annotations the annotations
77          * @return the formatted JSON
78          */
format(Map<JsonNodePath, List<JsonNodeAnnotation>> annotations)79         public static String format(Map<JsonNodePath, List<JsonNodeAnnotation>> annotations) {
80             Map<String, Map<String, Map<String, Object>>> results = new LinkedHashMap<>();
81             for (List<JsonNodeAnnotation> list : annotations.values()) {
82                 for (JsonNodeAnnotation annotation : list) {
83                     String keyword = annotation.getKeyword();
84                     String instancePath = annotation.getInstanceLocation().toString();
85                     String evaluationPath = annotation.getEvaluationPath().toString();
86                     Map<String, Object> values = results
87                             .computeIfAbsent(instancePath, (key) -> new LinkedHashMap<>())
88                             .computeIfAbsent(keyword, (key) -> new LinkedHashMap<>());
89                     values.put(evaluationPath, annotation.getValue());
90                 }
91             }
92 
93             try {
94                 return JsonMapperFactory.getInstance().writerWithDefaultPrettyPrinter().writeValueAsString(results);
95             } catch (JsonProcessingException e) {
96                 return "";
97             }
98         }
99 
100     }
101 
102 }
103