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.internal.StringUtils.*;
016
017import org.apache.juneau.*;
018import org.apache.juneau.internal.*;
019import org.apache.juneau.json.*;
020import org.apache.juneau.jsonschema.annotation.*;
021import org.apache.juneau.parser.*;
022
023/**
024 * Metadata on classes specific to the JSON-Schema serializer and pulled from the {@link JsonSchema @JsonSchema} annotation on
025 * the class.
026 */
027public class JsonSchemaClassMeta extends ClassMetaExtended {
028
029   private final JsonSchema jsonSchema;
030   private final String type, format, description;
031   private Object example;
032
033   /**
034    * Constructor.
035    *
036    * @param cm The class that this annotation is defined on.
037    */
038   public JsonSchemaClassMeta(ClassMeta<?> cm) {
039      super(cm);
040      this.jsonSchema = ClassUtils.getAnnotation(JsonSchema.class, getInnerClass());
041      if (jsonSchema != null) {
042         type = nullIfEmpty(jsonSchema.type());
043         format = nullIfEmpty(jsonSchema.format());
044         description = nullIfEmpty(jsonSchema.description());
045         try {
046            example = jsonSchema.example().isEmpty() ? null : JsonParser.DEFAULT.parse(jsonSchema.example(), Object.class);
047         } catch (ParseException e) {
048            throw new BeanRuntimeException(e);
049         }
050      } else {
051         type = null;
052         format = null;
053         description = null;
054      }
055   }
056
057   /**
058    * Returns the {@link JsonSchema @JsonSchema} annotation defined on the class.
059    *
060    * @return The value of the annotation, or <jk>null</jk> if not specified.
061    */
062   protected JsonSchema getAnnotation() {
063      return jsonSchema;
064   }
065
066   /**
067    * Returns the {@link JsonSchema#type() @JsonSchema(type)} annotation defined on the class.
068    *
069    * @return The value of the annotation, or <jk>null</jk> if not specified.
070    */
071   protected String getType() {
072      return type;
073   }
074
075   /**
076    * Returns the {@link JsonSchema#format() @JsonSchema(format)} annotation defined on the class.
077    *
078    * @return The value of the annotation, or <jk>null</jk> if not specified.
079    */
080   protected String getFormat() {
081      return format;
082   }
083
084   /**
085    * Returns the {@link JsonSchema#description() @JsonSchema(description)} annotation defined on the class.
086    *
087    * @return The value of the annotation, or <jk>null</jk> if not specified.
088    */
089   protected String getDescription() {
090      return description;
091   }
092
093   /**
094    * Returns the {@link JsonSchema#example() @JsonSchema(example)} annotation defined on the class.
095    *
096    * @return The value of the annotation, or <jk>null</jk> if not specified.
097    */
098   protected Object getExample() {
099      return example;
100   }
101}