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.common.utils.StringUtils.*; 020import static org.apache.juneau.common.utils.Utils.*; 021import static org.apache.juneau.internal.ClassUtils.*; 022 023import java.util.*; 024 025import org.apache.juneau.collections.*; 026import org.apache.juneau.common.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 * Concatenates and parses a string array as a JSON object. 040 * 041 * @param ss The array to concatenate and parse. 042 * @return The parsed contents. 043 * @throws ParseException Invalid JSON encountered. 044 */ 045 public static JsonMap parseMap(String[] ss) throws ParseException { 046 if (ss.length == 0) 047 return null; 048 String s = joinnl(ss); 049 if (s.isEmpty()) 050 return null; 051 if (! isJsonObject(s, true)) 052 s = "{" + s + "}"; 053 return JsonMap.ofJson(s); 054 } 055 056 /** 057 * Parses a generic object as JSON and converts it to an {@link JsonMap}. 058 * 059 * @param o The object to convert. 060 * @return The parsed contents. 061 * @throws ParseException Invalid JSON encountered. 062 */ 063 public static JsonMap parseMap(Object o) throws ParseException { 064 if (o == null) 065 return null; 066 if (o instanceof String[]) 067 o = joinnl((String[])o); 068 if (o instanceof String) { 069 String s = o.toString(); 070 if (s.isEmpty()) 071 return null; 072 if ("IGNORE".equalsIgnoreCase(s)) 073 return JsonMap.of("ignore", true); 074 if (! isJsonObject(s, true)) 075 s = "{" + s + "}"; 076 return JsonMap.ofJson(s); 077 } 078 if (o instanceof JsonMap) 079 return (JsonMap)o; 080 throw new ParseException("Unexpected data type ''{0}''. Expected JsonMap or String.", className(o)); 081 } 082 083 /** 084 * Concatenates and parses a string array as JSON array or comma-delimited list. 085 * 086 * @param ss The array to concatenate and parse. 087 * @return The parsed contents. 088 * @throws ParseException Invalid JSON encountered. 089 */ 090 public static Set<String> parseSet(String[] ss) throws ParseException { 091 if (ss.length == 0) 092 return null; 093 String s = joinnl(ss); 094 if (s.isEmpty()) 095 return null; 096 Set<String> set = set(); 097 JsonList.ofJsonOrCdl(s).forEach(x -> set.add(x.toString())); 098 return set; 099 } 100 101 /** 102 * Joins an array of strings with newlines. 103 * 104 * @param s The array to join. 105 * @return A new joined string. 106 */ 107 public static String joinnl(String[]...s) { 108 for (String[] ss : s) { 109 if (ss.length != 0) 110 return Utils.joinnl(ss).trim(); 111 } 112 return ""; 113 } 114}