JsonSchemaParserValidatorImpl.java 5.24 KB
/*
 * 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);
    }
}