001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2015 ForgeRock AS. 015 */ 016 017package org.forgerock.audit.util; 018 019import java.util.LinkedHashSet; 020import java.util.Set; 021 022import org.forgerock.json.JsonValue; 023 024/** 025 * Contains Utility methods for dealing with JsonSchema data. 026 */ 027public final class JsonSchemaUtils { 028 029 private static final String PROPERTIES = "properties"; 030 private static final String ID = "id"; 031 private static final String TYPE = "type"; 032 private static final String OBJECT = "object"; 033 private static final String FORWARD_SLASH = "/"; 034 035 private JsonSchemaUtils() { 036 // prevent instantiation 037 } 038 039 /** 040 * Generates the Set of {@link org.forgerock.json.JsonPointer JsonPointer}s in a given JsonSchema. 041 * @param schema the JsonSchema to generate the {@link org.forgerock.json.JsonPointer JsonPointer}s for 042 * @return a set of JsonPointers as strings 043 */ 044 public static Set<String> generateJsonPointers(final JsonValue schema) { 045 return concatPrefix(getPointers(schema.get(PROPERTIES)), schema.get(ID).asString()); 046 } 047 048 private static Set<String> getPointers(final JsonValue properties) { 049 final Set<String> pointers = new LinkedHashSet<>(); 050 final Set<String> keys = properties.keys(); 051 for (String key : keys) { 052 final JsonValue property = properties.get(key); 053 if (OBJECT.equals(property.get(TYPE).asString())) { 054 //get pointers in sub object 055 final Set<String> subPointers = getPointers(property.get(PROPERTIES)); 056 pointers.addAll(concatPrefix(subPointers, key)); 057 } else { 058 //add the primitive type id 059 pointers.add(key); 060 } 061 } 062 return pointers; 063 } 064 065 private static Set<String> concatPrefix(final Set<String> pointers, final String id) { 066 final Set<String> newPointers = new LinkedHashSet<>(); 067 if (pointers.isEmpty()) { 068 newPointers.add(id); 069 return newPointers; 070 } 071 for (String pointer : pointers) { 072 final StringBuilder stringBuilder = new StringBuilder(); 073 if (FORWARD_SLASH.equals(id)) { 074 stringBuilder.append(id).append(pointer); 075 } else { 076 stringBuilder.append(id).append(FORWARD_SLASH).append(pointer); 077 } 078 newPointers.add(stringBuilder.toString()); 079 } 080 return newPointers; 081 } 082}