001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.jsonschema; 018 019import static org.apache.juneau.commons.utils.CollectionUtils.*; 020import static org.apache.juneau.commons.utils.StringUtils.*; 021import static org.apache.juneau.commons.utils.Utils.*; 022 023import java.util.*; 024 025import org.apache.juneau.collections.*; 026import org.apache.juneau.commons.utils.*; 027import org.apache.juneau.parser.*; 028 029/** 030 * Utilities for working with the schema annotations. 031 * 032 * <h5 class='section'>See Also:</h5><ul> 033 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JsonSchemaDetails">JSON-Schema Support</a> 034 * </ul> 035 */ 036public class SchemaUtils { 037 038 /** 039 * Joins an array of strings with newlines. 040 * 041 * @param s The array to join. 042 * @return A new joined string. 043 */ 044 public static String joinnl(String[]...s) { 045 for (var ss : s) { 046 if (ss.length != 0) 047 return StringUtils.joinnl(ss).trim(); 048 } 049 return ""; 050 } 051 052 /** 053 * Parses a generic object as JSON and converts it to an {@link JsonMap}. 054 * 055 * @param o The object to convert. 056 * @return The parsed contents. 057 * @throws ParseException Invalid JSON encountered. 058 */ 059 public static JsonMap parseMap(Object o) throws ParseException { 060 if (o == null) 061 return null; 062 if (o instanceof String[]) 063 o = joinnl((String[])o); 064 if (o instanceof String o2) { 065 var s = o2.toString(); 066 if (s.isEmpty()) 067 return null; 068 if ("IGNORE".equalsIgnoreCase(s)) 069 return JsonMap.of("ignore", true); 070 if (! isProbablyJsonObject(s, true)) 071 s = "{" + s + "}"; 072 return JsonMap.ofJson(s); 073 } 074 if (o instanceof JsonMap o2) 075 return o2; 076 throw new ParseException("Unexpected data type ''{0}''. Expected JsonMap or String.", cn(o)); 077 } 078 079 /** 080 * Concatenates and parses a string array as a JSON object. 081 * 082 * @param ss The array to concatenate and parse. 083 * @return The parsed contents. 084 * @throws ParseException Invalid JSON encountered. 085 */ 086 public static JsonMap parseMap(String[] ss) throws ParseException { 087 if (ss.length == 0) 088 return null; 089 String s = joinnl(ss); 090 if (s.isEmpty()) 091 return null; 092 if (! isProbablyJsonObject(s, true)) 093 s = "{" + s + "}"; 094 return JsonMap.ofJson(s); 095 } 096 097 /** 098 * Concatenates and parses a string array as JSON array or comma-delimited list. 099 * 100 * @param ss The array to concatenate and parse. 101 * @return The parsed contents. 102 * @throws ParseException Invalid JSON encountered. 103 */ 104 public static Set<String> parseSet(String[] ss) throws ParseException { 105 if (ss.length == 0) 106 return null; 107 String s = joinnl(ss); 108 if (s.isEmpty()) 109 return null; 110 Set<String> set = set(); 111 JsonList.ofJsonOrCdl(s).forEach(x -> set.add(x.toString())); 112 return set; 113 } 114}