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.internal.ClassUtils.*;
016
017import java.util.*;
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 */
026public class HtmlClassMeta extends ExtendedClassMeta {
027
028   private final List<Html> htmls;
029   private final boolean noTables, noTableHeaders;
030   private final HtmlFormat format;
031   private final HtmlRender<?> render;
032
033   /**
034    * Constructor.
035    *
036    * @param cm The class that this annotation is defined on.
037    * @param mp HTML metadata provider (for finding information about other artifacts).
038    */
039   public HtmlClassMeta(ClassMeta<?> cm, HtmlMetaProvider mp) {
040      super(cm);
041      this.htmls = cm.getAnnotations(Html.class);
042
043      boolean _noTables = false, _noTableHeaders = false;
044      HtmlRender<?> _render = null;
045      HtmlFormat _format = HtmlFormat.HTML;
046
047      for (Html a : this.htmls) {
048         _format = a.format();
049         _noTables = a.noTables();
050         _noTableHeaders = a.noTableHeaders();
051         _render = castOrCreate(HtmlRender.class, a.render());
052      }
053
054      this.noTables = _noTables;
055      this.noTableHeaders = _noTableHeaders;
056      this.render = _render;
057      this.format = _format;
058   }
059
060   /**
061    * Returns the {@link Html @Html} annotations defined on the class.
062    *
063    * @return An unmodifiable list of annotations ordered parent-to-child, or an empty list if not found.
064    */
065   protected List<Html> getAnnotations() {
066      return htmls;
067   }
068
069   /**
070    * Returns the {@link Html#format() @Html(format)} annotation defined on the class.
071    *
072    * @return The value of the annotation.
073    */
074   protected HtmlFormat getFormat() {
075      return format;
076   }
077
078   /**
079    * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#XML}.
080    *
081    * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#XML}.
082    */
083   protected boolean isXml() {
084      return format == HtmlFormat.XML;
085   }
086
087   /**
088    * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#PLAIN_TEXT}.
089    *
090    * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#PLAIN_TEXT}.
091    */
092   protected boolean isPlainText() {
093      return format == HtmlFormat.PLAIN_TEXT;
094   }
095
096   /**
097    * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML}.
098    *
099    * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML}.
100    */
101   protected boolean isHtml() {
102      return format == HtmlFormat.HTML;
103   }
104
105   /**
106    * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML_CDC}.
107    *
108    * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML_CDC}.
109    */
110   protected boolean isHtmlCdc() {
111      return format == HtmlFormat.HTML_CDC;
112   }
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() {
120      return format == HtmlFormat.HTML_SDC;
121   }
122
123   /**
124    * Returns the {@link Html#noTables() @Html(noTables)} annotation defined on the class.
125    *
126    * @return The value of the annotation.
127    */
128   protected boolean isNoTables() {
129      return noTables;
130   }
131
132   /**
133    * Returns the {@link Html#noTableHeaders() @Html(noTableHeaders)} annotation defined on the class.
134    *
135    * @return The value of the annotation.
136    */
137   public boolean isNoTableHeaders() {
138      return noTableHeaders;
139   }
140
141   /**
142    * Returns the {@link Html#render() @Html(render)} annotation defined on the class.
143    *
144    * @return The value of the annotation.
145    */
146   public HtmlRender<?> getRender() {
147      return render;
148   }
149}