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.marshall.html;
018
019import java.io.*;
020
021import org.apache.juneau.marshall.*;
022import org.apache.juneau.marshall.jsonschema.*;
023import org.apache.juneau.marshall.serializer.*;
024
025/**
026 * Context object that lives for the duration of a single serialization of {@link HtmlSchemaSerializer} and its subclasses.
027 *
028 * <h5 class='section'>Notes:</h5><ul>
029 *    <li class='warn'>This class is not thread safe and is typically discarded after one use.
030 * </ul>
031 *
032 * <h5 class='section'>See Also:</h5><ul>
033 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HtmlSupport">HTML</a>
034 * </ul>
035 */
036@SuppressWarnings({
037   "java:S110" // Inheritance depth acceptable for HtmlSchemaSerializerSession hierarchy
038})
039public class HtmlSchemaSerializerSession extends HtmlSerializerSession {
040
041   /**
042    * Builder class.
043    */
044   public static class Builder extends HtmlSerializerSession.Builder<Builder> {
045
046      private final HtmlSchemaSerializer ctx;
047
048      /**
049       * Constructor
050       *
051       * @param ctx The context creating this session.
052       */
053      protected Builder(HtmlSchemaSerializer ctx) {
054         super(ctx);
055         this.ctx = ctx;
056      }
057
058      @Override
059      public HtmlSchemaSerializerSession build() {
060         return new HtmlSchemaSerializerSession(this);
061      }
062
063   }
064
065   /**
066    * Creates a new builder for this object.
067    *
068    * @param ctx The context creating this session.
069    * @return A new builder.
070    */
071   public static Builder create(HtmlSchemaSerializer ctx) {
072      return new Builder(ctx);
073   }
074
075   private final JsonSchemaGeneratorSession genSession;
076
077   /**
078    * Constructor.
079    *
080    * @param builder The builder for this object.
081    */
082   protected HtmlSchemaSerializerSession(Builder builder) {
083      super(builder);
084      this.genSession = builder.ctx.getGenerator().getSession();
085   }
086
087   @Override /* Overridden from SerializerSession */
088   protected void doWrite(SerializerPipe out, Object o) throws IOException, SerializeException {
089      try {
090         super.doWrite(out, genSession.getSchema(o));
091      } catch (MarshallingRecursionException e) {
092         throw new SerializeException(e);
093      }
094   }
095}