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 static org.apache.juneau.commons.utils.ClassUtils.*;
020
021import java.io.*;
022import java.lang.reflect.*;
023import java.util.*;
024
025import org.apache.juneau.marshall.serializer.*;
026
027/**
028 * Session object that lives for the duration of a single use of {@link HtmlStrippedDocSerializer}.
029 *
030 * <h5 class='section'>Notes:</h5><ul>
031 *    <li class='warn'>This class is not thread safe and is typically discarded after one use.
032 * </ul>
033 *
034 * <h5 class='section'>See Also:</h5><ul>
035 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HtmlSupport">HTML</a>
036 * </ul>
037 */
038@SuppressWarnings({
039   "java:S110", // Deep inheritance inherent to the serializer/parser session hierarchy
040   "java:S3740" // Raw ObjectMap/ClassMeta types propagated from parent serializer session hierarchy where type parameters are erased
041})
042public class HtmlStrippedDocSerializerSession extends HtmlSerializerSession {
043
044   /**
045    * Builder class.
046    */
047   @SuppressWarnings({
048      "java:S119" // 'SELF' (CRTP self-type) is intentional and clearer than a single-letter name.
049   })
050   public abstract static class Builder<SELF extends Builder<SELF>> extends HtmlSerializerSession.Builder<SELF> {
051
052      /**
053       * Constructor
054       *
055       * @param ctx The context creating this session.
056       */
057      protected Builder(HtmlStrippedDocSerializer ctx) {
058         super(ctx);
059      }
060
061      @Override
062      public HtmlStrippedDocSerializerSession build() {
063         return new HtmlStrippedDocSerializerSession(this);
064      }
065
066   }
067
068   /**
069    * Concrete default builder leaf for the non-subclassed {@code create()} path (CRTP terminal).
070    */
071   public static final class DefaultBuilder extends Builder<DefaultBuilder> {
072
073      DefaultBuilder(HtmlStrippedDocSerializer ctx) {
074         super(ctx);
075      }
076   }
077
078   /**
079    * Creates a new builder for this object.
080    *
081    * @param ctx The context creating this session.
082    * @return A new builder.
083    */
084   @SuppressWarnings({
085      "java:S1452" // Builder<?> wildcard return intentional; callers use it to construct session instances polymorphically
086   })
087   public static Builder<?> create(HtmlStrippedDocSerializer ctx) {
088      return new DefaultBuilder(ctx);
089   }
090
091   /**
092    * Constructor.
093    *
094    * @param builder The builder for this object.
095    */
096   protected HtmlStrippedDocSerializerSession(Builder<?> builder) {
097      super(builder);
098   }
099
100   @SuppressWarnings({
101      "resource" // w is closed by try-with-resources; analyzer false positive on super.doWrite path
102   })
103   @Override /* Overridden from SerializerSession */
104   protected void doWrite(SerializerPipe out, Object o) throws IOException, SerializeException {
105      try (var w = getHtmlWriter(out)) {
106         if (o == null || (o instanceof Collection o2 && o2.isEmpty()) || (isArray(o) && Array.getLength(o) == 0))
107            w.sTag(1, "p").append("No Results").eTag("p").nl(1);
108         else
109            super.doWrite(out, o);
110      }
111   }
112}