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