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.annotation.*;
017import org.apache.juneau.collections.*;
018import org.apache.juneau.jsonschema.*;
019import org.apache.juneau.serializer.*;
020
021/**
022 * Serializes POJO metamodels to HTML.
023 *
024 * <h5 class='topic'>Media types</h5>
025 *
026 * Handles <c>Accept</c> types:  <bc>text/html+schema</bc>
027 * <p>
028 * Produces <c>Content-Type</c> types:  <bc>text/html</bc>
029 *
030 * <h5 class='topic'>Description</h5>
031 *
032 * Essentially the same as {@link HtmlSerializer}, except serializes the POJO metamodel instead of the model itself.
033 *
034 * <p>
035 * Produces output that describes the POJO metamodel similar to an XML schema document.
036 *
037 * <p>
038 * The easiest way to create instances of this class is through the {@link HtmlSerializer#getSchemaSerializer()},
039 * which will create a schema serializer with the same settings as the originating serializer.
040 */
041@ConfigurableContext
042public class HtmlSchemaSerializer extends HtmlSerializer {
043
044   //-------------------------------------------------------------------------------------------------------------------
045   // Configurable properties
046   //-------------------------------------------------------------------------------------------------------------------
047
048   static final String PREFIX = "HtmlSchemaSerializer";
049
050   //-------------------------------------------------------------------------------------------------------------------
051   // Predefined instances
052   //-------------------------------------------------------------------------------------------------------------------
053
054   /** Default serializer, all default settings.*/
055   public static final HtmlSchemaSerializer DEFAULT = new HtmlSchemaSerializer(PropertyStore.DEFAULT);
056
057   /** Default serializer, all default settings.*/
058   public static final HtmlSchemaSerializer DEFAULT_READABLE = new Readable(PropertyStore.DEFAULT);
059
060   /** Default serializer, single quotes, simple mode. */
061   public static final HtmlSchemaSerializer DEFAULT_SIMPLE = new Simple(PropertyStore.DEFAULT);
062
063   /** Default serializer, single quotes, simple mode, with whitespace. */
064   public static final HtmlSchemaSerializer DEFAULT_SIMPLE_READABLE = new SimpleReadable(PropertyStore.DEFAULT);
065
066   //-------------------------------------------------------------------------------------------------------------------
067   // Predefined subclasses
068   //-------------------------------------------------------------------------------------------------------------------
069
070   /** Default serializer, with whitespace. */
071   public static class Readable extends HtmlSchemaSerializer {
072
073      /**
074       * Constructor.
075       *
076       * @param ps The property store containing all the settings for this object.
077       */
078      public Readable(PropertyStore ps) {
079         super(
080            ps.builder().setDefault(WSERIALIZER_useWhitespace, true).build()
081         );
082      }
083   }
084
085   /** Default serializer, single quotes, simple mode. */
086   public static class Simple extends HtmlSchemaSerializer {
087
088      /**
089       * Constructor.
090       *
091       * @param ps The property store containing all the settings for this object.
092       */
093      public Simple(PropertyStore ps) {
094         super(
095            ps.builder()
096               .setDefault(WSERIALIZER_quoteChar, '\'')
097               .build()
098            );
099      }
100   }
101
102   /** Default serializer, single quotes, simple mode, with whitespace. */
103   public static class SimpleReadable extends HtmlSchemaSerializer {
104
105      /**
106       * Constructor.
107       *
108       * @param ps The property store containing all the settings for this object.
109       */
110      public SimpleReadable(PropertyStore ps) {
111         super(
112            ps.builder()
113               .setDefault(WSERIALIZER_quoteChar, '\'')
114               .setDefault(WSERIALIZER_useWhitespace, true)
115               .build()
116         );
117      }
118   }
119
120   //-------------------------------------------------------------------------------------------------------------------
121   // Instance
122   //-------------------------------------------------------------------------------------------------------------------
123
124   private final JsonSchemaGenerator generator;
125
126   /**
127    * Constructor.
128    *
129    * @param ps
130    *    The property store to use for creating the context for this serializer.
131    */
132   public HtmlSchemaSerializer(PropertyStore ps) {
133      super(
134         ps.builder()
135            .setDefault(BEANTRAVERSE_detectRecursions, true)
136            .setDefault(BEANTRAVERSE_ignoreRecursions, true)
137            .build(),
138         "text/html", "text/html+schema"
139      );
140
141      generator = JsonSchemaGenerator.create().apply(getPropertyStore()).build();
142   }
143
144   @Override /* Context */
145   public HtmlSchemaSerializerBuilder builder() {
146      return new HtmlSchemaSerializerBuilder(getPropertyStore());
147   }
148
149   @Override /* Context */
150   public HtmlSchemaSerializerSession createSession() {
151      return createSession(createDefaultSessionArgs());
152   }
153
154   @Override /* Serializer */
155   public HtmlSchemaSerializerSession createSession(SerializerSessionArgs args) {
156      return new HtmlSchemaSerializerSession(this, args);
157   }
158
159   JsonSchemaGenerator getGenerator() {
160      return generator;
161   }
162
163   //-----------------------------------------------------------------------------------------------------------------
164   // Other methods
165   //-----------------------------------------------------------------------------------------------------------------
166
167   @Override /* Context */
168   public OMap toMap() {
169      return super.toMap()
170         .a("HtmlSchemaSerializer", new DefaultFilteringOMap()
171            .a("generator", generator)
172         );
173   }
174}