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