JsonSchemaParserValidatorImpl.java
5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemds.guide.service.IJsonSchemaValidatorParser
* com.adobe.aemds.guide.service.JsonSchemaVisitor
* com.fasterxml.jackson.databind.JsonNode
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.guide.addon.service.impl.jsonschema;
import com.adobe.aemds.guide.addon.service.impl.jsonschema.JsonSchemaParserValidator;
import com.adobe.aemds.guide.service.IJsonSchemaValidatorParser;
import com.adobe.aemds.guide.service.JsonSchemaVisitor;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.Map;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(label="Generic Json schema parser and validator", description="Generic Json schema parser and validator service", immediate=1, metatype=0)
@Service(value={IJsonSchemaValidatorParser.class})
public class JsonSchemaParserValidatorImpl
implements IJsonSchemaValidatorParser {
private static final Logger logger = LoggerFactory.getLogger(JsonSchemaParserValidatorImpl.class);
public Map<String, String> getDefinitions(Object rootJsonSchema, Object[] referredJsonSchema) throws Exception {
JsonSchemaParserValidator jsonParser = new JsonSchemaParserValidator(rootJsonSchema, referredJsonSchema);
return jsonParser.getDefinitions();
}
public JsonNode getNodeAtPath(Object rootJsonSchema, Object[] referredJsonSchema, String jsonPath) throws Exception {
JsonSchemaParserValidator jsonParser = new JsonSchemaParserValidator(rootJsonSchema, referredJsonSchema);
return jsonParser.getNodeAtPath(jsonPath);
}
public Object getParserInstance(Object rootJsonSchema, Object[] referredJsonSchema) throws Exception {
return new JsonSchemaParserValidator(rootJsonSchema, referredJsonSchema);
}
public JsonNode getNodeAtPath(Object parserInstance, String jsonPath) throws Exception {
return ((JsonSchemaParserValidator)parserInstance).getNodeAtPath(jsonPath);
}
public JsonNode getResolvedObject(Object rootJsonSchema, Object[] referredJsonSchema) throws Exception {
return this.getResolvedObject(rootJsonSchema, referredJsonSchema, false);
}
public JsonNode getResolvedObject(Object rootJsonSchema, Object[] referredJsonSchema, boolean bMergeTitle) throws Exception {
JsonSchemaParserValidator jsonParser = new JsonSchemaParserValidator(rootJsonSchema, referredJsonSchema, false, bMergeTitle);
return jsonParser.getResolvedNode();
}
public JsonNode getResolvedObject(Object rootJsonSchema, Object[] referredJsonSchema, boolean includeReferredDefinition, boolean bMergeTitle) throws Exception {
JsonSchemaParserValidator jsonParser = new JsonSchemaParserValidator(rootJsonSchema, referredJsonSchema, includeReferredDefinition, bMergeTitle);
return jsonParser.getResolvedNode();
}
public Object getSchemaNodeWithReferences(Object rootJsonSchema, Object[] referredJsonSchema, String rootName, String jsonPointer) throws Exception {
JsonSchemaParserValidator jsonParser = new JsonSchemaParserValidator(rootJsonSchema, referredJsonSchema, true);
String[] splitPath = jsonPointer.split("/");
String bindPath = "/properties/" + splitPath[0];
JsonNode contextNode = jsonParser.getNodeAtPath(bindPath);
if (contextNode.isMissingNode()) {
JsonNode resolvedDefinitions = jsonParser.getResolvedDefinitions();
contextNode = resolvedDefinitions.get(rootName);
}
JsonNode node = contextNode;
if (!node.isMissingNode()) {
for (int i = 1; i < splitPath.length; ++i) {
bindPath = "/properties/" + splitPath[i];
node = contextNode.at(bindPath);
if (node.isMissingNode() && (node = contextNode.at(bindPath = "/items/" + splitPath[i])).isMissingNode()) {
bindPath = "/items/properties/" + splitPath[i];
node = contextNode.at(bindPath);
}
contextNode = node;
}
}
return node;
}
public boolean isValid(Object rootJsonSchema, Object[] referredJsonSchema) throws Exception {
JsonSchemaParserValidator jsonParser = new JsonSchemaParserValidator(rootJsonSchema, referredJsonSchema);
return jsonParser.isValid();
}
public String getValue(Object rootJsonSchema, Object[] referredJsonSchema, String jsonPath) throws Exception {
JsonNode node = this.getNodeAtPath(rootJsonSchema, referredJsonSchema, jsonPath);
String retVal = null;
if (node != null) {
retVal = node.isNull() ? null : node.asText();
} else {
logger.debug("Unable to resolve JSON Node at path ", (Object)jsonPath);
retVal = null;
}
return retVal;
}
public void walk(Object rootJsonSchema, Object[] referredJsonSchema, JsonSchemaVisitor visitor) throws Exception {
JsonSchemaParserValidator jsonParser = new JsonSchemaParserValidator(rootJsonSchema, referredJsonSchema);
jsonParser.walk(visitor);
}
}