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.html;
018
019import static org.apache.juneau.commons.utils.Utils.*;
020
021import java.io.*;
022import java.lang.reflect.*;
023import java.nio.charset.*;
024import java.util.*;
025import java.util.function.*;
026
027import org.apache.juneau.*;
028import org.apache.juneau.httppart.*;
029import org.apache.juneau.serializer.*;
030import org.apache.juneau.svl.*;
031
032/**
033 * Session object that lives for the duration of a single use of {@link HtmlStrippedDocSerializer}.
034 *
035 * <h5 class='section'>Notes:</h5><ul>
036 *    <li class='warn'>This class is not thread safe and is typically discarded after one use.
037 * </ul>
038 *
039 * <h5 class='section'>See Also:</h5><ul>
040 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HtmlBasics">HTML Basics</a>
041 * </ul>
042 */
043@SuppressWarnings("resource")
044public class HtmlStrippedDocSerializerSession extends HtmlSerializerSession {
045   /**
046    * Builder class.
047    */
048   public static class Builder extends HtmlSerializerSession.Builder {
049
050      /**
051       * Constructor
052       *
053       * @param ctx The context creating this session.
054       */
055      protected Builder(HtmlStrippedDocSerializer ctx) {
056         super(ctx);
057      }
058
059      @Override /* Overridden from Builder */
060      public <T> Builder apply(Class<T> type, Consumer<T> apply) {
061         super.apply(type, apply);
062         return this;
063      }
064
065      @Override
066      public HtmlStrippedDocSerializerSession build() {
067         return new HtmlStrippedDocSerializerSession(this);
068      }
069
070      @Override /* Overridden from Builder */
071      public Builder debug(Boolean value) {
072         super.debug(value);
073         return this;
074      }
075
076      @Override /* Overridden from Builder */
077      public Builder fileCharset(Charset value) {
078         super.fileCharset(value);
079         return this;
080      }
081
082      @Override /* Overridden from Builder */
083      public Builder javaMethod(Method value) {
084         super.javaMethod(value);
085         return this;
086      }
087
088      @Override /* Overridden from Builder */
089      public Builder locale(Locale value) {
090         super.locale(value);
091         return this;
092      }
093
094      @Override /* Overridden from Builder */
095      public Builder mediaType(MediaType value) {
096         super.mediaType(value);
097         return this;
098      }
099
100      @Override /* Overridden from Builder */
101      public Builder mediaTypeDefault(MediaType value) {
102         super.mediaTypeDefault(value);
103         return this;
104      }
105
106      @Override /* Overridden from Builder */
107      public Builder properties(Map<String,Object> value) {
108         super.properties(value);
109         return this;
110      }
111
112      @Override /* Overridden from Builder */
113      public Builder property(String key, Object value) {
114         super.property(key, value);
115         return this;
116      }
117
118      @Override /* Overridden from Builder */
119      public Builder resolver(VarResolverSession value) {
120         super.resolver(value);
121         return this;
122      }
123
124      @Override /* Overridden from Builder */
125      public Builder schema(HttpPartSchema value) {
126         super.schema(value);
127         return this;
128      }
129
130      @Override /* Overridden from Builder */
131      public Builder schemaDefault(HttpPartSchema value) {
132         super.schemaDefault(value);
133         return this;
134      }
135
136      @Override /* Overridden from Builder */
137      public Builder streamCharset(Charset value) {
138         super.streamCharset(value);
139         return this;
140      }
141
142      @Override /* Overridden from Builder */
143      public Builder timeZone(TimeZone value) {
144         super.timeZone(value);
145         return this;
146      }
147
148      @Override /* Overridden from Builder */
149      public Builder timeZoneDefault(TimeZone value) {
150         super.timeZoneDefault(value);
151         return this;
152      }
153
154      @Override /* Overridden from Builder */
155      public Builder unmodifiable() {
156         super.unmodifiable();
157         return this;
158      }
159
160      @Override /* Overridden from Builder */
161      public Builder uriContext(UriContext value) {
162         super.uriContext(value);
163         return this;
164      }
165
166      @Override /* Overridden from Builder */
167      public Builder useWhitespace(Boolean value) {
168         super.useWhitespace(value);
169         return this;
170      }
171   }
172
173   /**
174    * Creates a new builder for this object.
175    *
176    * @param ctx The context creating this session.
177    * @return A new builder.
178    */
179   public static Builder create(HtmlStrippedDocSerializer ctx) {
180      return new Builder(ctx);
181   }
182
183   /**
184    * Constructor.
185    *
186    * @param builder The builder for this object.
187    */
188   protected HtmlStrippedDocSerializerSession(Builder builder) {
189      super(builder);
190   }
191
192   @Override /* Overridden from SerializerSession */
193   protected void doSerialize(SerializerPipe out, Object o) throws IOException, SerializeException {
194      try (var w = getHtmlWriter(out)) {
195         if (o == null || (o instanceof Collection o2 && o2.isEmpty()) || (isArray(o) && Array.getLength(o) == 0))
196            w.sTag(1, "p").append("No Results").eTag("p").nl(1);
197         else
198            super.doSerialize(out, o);
199      }
200   }
201}