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 <code>Accept</code> types:  <code><b>text/html+schema</b></code>
025 * <p>
026 * Produces <code>Content-Type</code> types:  <code><b>text/html</b></code>
027 *
028 * <h5 class='topic'>Description</h5>
029 *
030 * Essentially the same as {@link HtmlSerializer}, 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 class HtmlSchemaSerializer extends HtmlSerializer {
040
041   /** Default serializer, all default settings.*/
042   public static final HtmlSchemaSerializer DEFAULT = new HtmlSchemaSerializer(PropertyStore.DEFAULT);
043
044   /** Default serializer, all default settings.*/
045   public static final HtmlSchemaSerializer DEFAULT_READABLE = new Readable(PropertyStore.DEFAULT);
046
047   /** Default serializer, single quotes, simple mode. */
048   public static final HtmlSchemaSerializer DEFAULT_SIMPLE = new Simple(PropertyStore.DEFAULT);
049
050   /** Default serializer, single quotes, simple mode, with whitespace. */
051   public static final HtmlSchemaSerializer DEFAULT_SIMPLE_READABLE = new SimpleReadable(PropertyStore.DEFAULT);
052
053   //-------------------------------------------------------------------------------------------------------------------
054   // Predefined subclasses
055   //-------------------------------------------------------------------------------------------------------------------
056
057   /** Default serializer, with whitespace. */
058   public static class Readable extends HtmlSchemaSerializer {
059
060      /**
061       * Constructor.
062       *
063       * @param ps The property store containing all the settings for this object.
064       */
065      public Readable(PropertyStore ps) {
066         super(
067            ps.builder().set(SERIALIZER_useWhitespace, true).build()
068         );
069      }
070   }
071
072   /** Default serializer, single quotes, simple mode. */
073   public static class Simple extends HtmlSchemaSerializer {
074
075      /**
076       * Constructor.
077       *
078       * @param ps The property store containing all the settings for this object.
079       */
080      public Simple(PropertyStore ps) {
081         super(
082            ps.builder()
083               .set(WSERIALIZER_quoteChar, '\'')
084               .build()
085            );
086      }
087   }
088
089   /** Default serializer, single quotes, simple mode, with whitespace. */
090   public static class SimpleReadable extends HtmlSchemaSerializer {
091
092      /**
093       * Constructor.
094       *
095       * @param ps The property store containing all the settings for this object.
096       */
097      public SimpleReadable(PropertyStore ps) {
098         super(
099            ps.builder()
100               .set(WSERIALIZER_quoteChar, '\'')
101               .set(SERIALIZER_useWhitespace, true)
102               .build()
103         );
104      }
105   }
106
107   //-------------------------------------------------------------------------------------------------------------------
108   // Instance
109   //-------------------------------------------------------------------------------------------------------------------
110
111   private final JsonSchemaGenerator generator;
112
113   /**
114    * Constructor.
115    *
116    * @param ps
117    *    The property store to use for creating the context for this serializer.
118    */
119   public HtmlSchemaSerializer(PropertyStore ps) {
120      super(
121         ps.builder()
122            .set(BEANTRAVERSE_detectRecursions, true)
123            .set(BEANTRAVERSE_ignoreRecursions, true)
124            .build(),
125         "text/html", "text/html+schema"
126      );
127
128      generator = JsonSchemaGenerator.create().apply(getPropertyStore()).build();
129   }
130
131   @Override /* Context */
132   public HtmlSchemaSerializerBuilder builder() {
133      return new HtmlSchemaSerializerBuilder(getPropertyStore());
134   }
135
136   @Override /* Serializer */
137   public HtmlSchemaSerializerSession createSession(SerializerSessionArgs args) {
138      return new HtmlSchemaSerializerSession(this, args);
139   }
140
141   @Override /* Context */
142   public HtmlSchemaSerializerSession createSession() {
143      return createSession(createDefaultSessionArgs());
144   }
145
146   JsonSchemaGenerator getGenerator() {
147      return generator;
148   }
149}