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.html;
014
015import org.apache.juneau.*;
016import org.apache.juneau.jsonschema.*;
017import org.apache.juneau.serializer.*;
018
019/**
020 * Serializes POJO metamodels to HTML.
021 *
022 * <h5 class='topic'>Media types</h5>
023 *
024 * Handles <c>Accept</c> types:  <bc>text/html+schema</bc>
025 * <p>
026 * Produces <c>Content-Type</c> types:  <bc>text/html</bc>
027 *
028 * <h5 class='topic'>Description</h5>
029 *
030 * Essentially the same as {@link HtmlDocSerializer}, except serializes the POJO metamodel instead of the model itself.
031 *
032 * <p>
033 * Produces output that describes the POJO metamodel similar to an XML schema document.
034 *
035 * <p>
036 * The easiest way to create instances of this class is through the {@link HtmlSerializer#getSchemaSerializer()},
037 * which will create a schema serializer with the same settings as the originating serializer.
038 */
039public final class HtmlSchemaDocSerializer extends HtmlDocSerializer {
040
041   private final JsonSchemaGenerator generator;
042
043   /**
044    * Constructor.
045    *
046    * @param ps
047    *    The property store to use for creating the context for this serializer.
048    */
049   public HtmlSchemaDocSerializer(PropertyStore ps) {
050      this(ps, "text/html", "text/html+schema");
051   }
052
053   /**
054    * Constructor.
055    *
056    * @param ps
057    *    The property store containing all the settings for this object.
058    * @param produces
059    *    The media type that this serializer produces.
060    * @param accept
061    *    The accept media types that the serializer can handle.
062    *    <p>
063    *    Can contain meta-characters per the <c>media-type</c> specification of
064    *    {@doc RFC2616.section14.1}
065    *    <p>
066    *    If empty, then assumes the only media type supported is <c>produces</c>.
067    *    <p>
068    *    For example, if this serializer produces <js>"application/json"</js> but should handle media types of
069    *    <js>"application/json"</js> and <js>"text/json"</js>, then the arguments should be:
070    *    <p class='bcode w800'>
071    *    <jk>super</jk>(ps, <js>"application/json"</js>, <js>"application/json,text/json"</js>);
072    *    </p>
073    *    <br>...or...
074    *    <p class='bcode w800'>
075    *    <jk>super</jk>(ps, <js>"application/json"</js>, <js>"*&#8203;/json"</js>);
076    *    </p>
077    * <p>
078    * The accept value can also contain q-values.
079    */
080   public HtmlSchemaDocSerializer(PropertyStore ps, String produces, String accept) {
081      super(
082         ps.builder()
083            .set(BEANTRAVERSE_detectRecursions, true)
084            .set(BEANTRAVERSE_ignoreRecursions, true)
085            .build(),
086         produces,
087         accept
088      );
089
090      generator = JsonSchemaGenerator.create().apply(getPropertyStore()).build();
091   }
092
093   @Override /* Serializer */
094   public HtmlSchemaDocSerializerSession createSession() {
095      return createSession(createDefaultSessionArgs());
096   }
097
098   @Override /* Serializer */
099   public HtmlSchemaDocSerializerSession createSession(SerializerSessionArgs args) {
100      return new HtmlSchemaDocSerializerSession(this, args);
101   }
102
103   JsonSchemaGenerator getGenerator() {
104      return generator;
105   }
106
107   //-----------------------------------------------------------------------------------------------------------------
108   // Other methods
109   //-----------------------------------------------------------------------------------------------------------------
110
111   @Override /* Context */
112   public ObjectMap toMap() {
113      return super.toMap()
114         .append("HtmlSchemaDocSerializer", new DefaultFilteringObjectMap()
115         );
116   }
117}