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 java.net.*;
016import java.text.*;
017
018import org.apache.juneau.*;
019
020/**
021 * Simple implementation of the {@link BeanDefMapper} interface.
022 *
023 * <p>
024 * IDs are created by calling {@link Class#getSimpleName()}.
025 * <p>
026 * URIs are constructed using the pattern <js>"#/definitions/{id}"</js>.
027 */
028public class BasicBeanDefMapper implements BeanDefMapper {
029
030   private final MessageFormat format;
031
032   /**
033    * Default constructor.
034    */
035   public BasicBeanDefMapper() {
036      this("#/definitions/{0}");
037   }
038
039   /**
040    * Constructor that allows you to override the URI pattern.
041    *
042    * @param uriPattern The URI pattern using {@link MessageFormat}-style arguments.
043    */
044   protected BasicBeanDefMapper(String uriPattern) {
045      format = new MessageFormat(uriPattern);
046   }
047
048   @Override /* BeanDefMapper */
049   public String getId(ClassMeta<?> cm) {
050      return cm.getSimpleName();
051   }
052
053   @Override /* BeanDefMapper */
054   public URI getURI(ClassMeta<?> cm) {
055      return getURI(getId(cm));
056   }
057
058   @Override /* BeanDefMapper */
059   public URI getURI(String id) {
060      return URI.create(format.format(new Object[]{id}));
061   }
062}