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 static org.apache.juneau.common.internal.ThrowableUtils.*;
016
017import java.util.function.*;
018
019import org.apache.juneau.*;
020import org.apache.juneau.html.annotation.*;
021
022/**
023 * Metadata on classes specific to the HTML serializers and parsers pulled from the {@link Html @Html} annotation on
024 * the class.
025 *
026 * <h5 class='section'>See Also:</h5><ul>
027 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.HtmlDetails">HTML Details</a>
028
029 * </ul>
030 */
031public class HtmlClassMeta extends ExtendedClassMeta {
032
033   private final boolean noTables, noTableHeaders;
034   private final HtmlFormat format;
035   private final HtmlRender<?> render;
036
037   /**
038    * Constructor.
039    *
040    * @param cm The class that this annotation is defined on.
041    * @param mp HTML metadata provider (for finding information about other artifacts).
042    */
043   public HtmlClassMeta(ClassMeta<?> cm, HtmlMetaProvider mp) {
044      super(cm);
045
046      Value<Boolean> noTables = Value.empty(), noTableHeaders = Value.empty();
047      Value<HtmlFormat> format = Value.empty();
048      Value<HtmlRender<?>> render = Value.empty();
049
050      Consumer<Html> c = x -> {
051         if (x.noTables())
052            noTables.set(true);
053         if (x.noTableHeaders())
054            noTableHeaders.set(true);
055         if (x.format() != HtmlFormat.HTML)
056            format.set(x.format());
057         if (x.render() != HtmlRender.class) {
058            try {
059               render.set(x.render().getDeclaredConstructor().newInstance());
060            } catch (Exception e) {
061               throw asRuntimeException(e);
062            }
063         }
064      };
065      cm.forEachAnnotation(Html.class, x -> true, c);
066
067      this.noTables = noTables.orElse(false);
068      this.noTableHeaders = noTableHeaders.orElse(false);
069      this.render = render.orElse(null);
070      this.format = format.orElse(HtmlFormat.HTML);
071   }
072
073   /**
074    * Returns the {@link Html#format() @Html(format)} annotation defined on the class.
075    *
076    * @return The value of the annotation.
077    */
078   protected HtmlFormat getFormat() {
079      return format;
080   }
081
082   /**
083    * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#XML}.
084    *
085    * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#XML}.
086    */
087   protected boolean isXml() {
088      return format == HtmlFormat.XML;
089   }
090
091   /**
092    * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#PLAIN_TEXT}.
093    *
094    * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#PLAIN_TEXT}.
095    */
096   protected boolean isPlainText() {
097      return format == HtmlFormat.PLAIN_TEXT;
098   }
099
100   /**
101    * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML}.
102    *
103    * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML}.
104    */
105   protected boolean isHtml() {
106      return format == HtmlFormat.HTML;
107   }
108
109   /**
110    * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML_CDC}.
111    *
112    * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML_CDC}.
113    */
114   protected boolean isHtmlCdc() {
115      return format == HtmlFormat.HTML_CDC;
116   }
117
118   /**
119    * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML_SDC}.
120    *
121    * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML_SDC}.
122    */
123   protected boolean isHtmlSdc() {
124      return format == HtmlFormat.HTML_SDC;
125   }
126
127   /**
128    * Returns the {@link Html#noTables() @Html(noTables)} annotation defined on the class.
129    *
130    * @return The value of the annotation.
131    */
132   protected boolean isNoTables() {
133      return noTables;
134   }
135
136   /**
137    * Returns the {@link Html#noTableHeaders() @Html(noTableHeaders)} annotation defined on the class.
138    *
139    * @return The value of the annotation.
140    */
141   public boolean isNoTableHeaders() {
142      return noTableHeaders;
143   }
144
145   /**
146    * Returns the {@link Html#render() @Html(render)} annotation defined on the class.
147    *
148    * @return The value of the annotation.
149    */
150   public HtmlRender<?> getRender() {
151      return render;
152   }
153}