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