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