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 org.apache.juneau.*;
018import org.apache.juneau.html.annotation.*;
019
020/**
021 * Metadata on classes specific to the HTML serializers and parsers pulled from the {@link Html @Html} annotation on
022 * the class.
023 */
024public class HtmlClassMeta extends ExtendedClassMeta {
025
026   private final Html html;
027   private final boolean noTables, noTableHeaders;
028   private final HtmlFormat format;
029   private final HtmlRender<?> render;
030
031   /**
032    * Constructor.
033    *
034    * @param cm The class that this annotation is defined on.
035    * @param mp HTML metadata provider (for finding information about other artifacts).
036    */
037   public HtmlClassMeta(ClassMeta<?> cm, HtmlMetaProvider mp) {
038      super(cm);
039      this.html = cm.getAnnotation(Html.class);
040      if (html != null) {
041         format = html.format();
042         noTables = html.noTables();
043         noTableHeaders = html.noTableHeaders();
044         render = castOrCreate(HtmlRender.class, html.render());
045      } else {
046         format = HtmlFormat.HTML;
047         noTables = false;
048         noTableHeaders = false;
049         render = null;
050      }
051   }
052
053   /**
054    * Returns the {@link Html @Html} annotation defined on the class.
055    *
056    * @return The value of the annotation, or <jk>null</jk> if not specified.
057    */
058   protected Html getAnnotation() {
059      return html;
060   }
061
062   /**
063    * Returns the {@link Html#format() @Html(format)} annotation defined on the class.
064    *
065    * @return The value of the annotation.
066    */
067   protected HtmlFormat getFormat() {
068      return format;
069   }
070
071   /**
072    * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#XML}.
073    *
074    * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#XML}.
075    */
076   protected boolean isXml() {
077      return format == HtmlFormat.XML;
078   }
079
080   /**
081    * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#PLAIN_TEXT}.
082    *
083    * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#PLAIN_TEXT}.
084    */
085   protected boolean isPlainText() {
086      return format == HtmlFormat.PLAIN_TEXT;
087   }
088
089   /**
090    * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML}.
091    *
092    * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML}.
093    */
094   protected boolean isHtml() {
095      return format == HtmlFormat.HTML;
096   }
097
098   /**
099    * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML_CDC}.
100    *
101    * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML_CDC}.
102    */
103   protected boolean isHtmlCdc() {
104      return format == HtmlFormat.HTML_CDC;
105   }
106
107   /**
108    * Returns <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML_SDC}.
109    *
110    * @return <jk>true</jk> if {@link #getFormat()} returns {@link HtmlFormat#HTML_SDC}.
111    */
112   protected boolean isHtmlSdc() {
113      return format == HtmlFormat.HTML_SDC;
114   }
115
116   /**
117    * Returns the {@link Html#noTables() @Html(noTables)} annotation defined on the class.
118    *
119    * @return The value of the annotation.
120    */
121   protected boolean isNoTables() {
122      return noTables;
123   }
124
125   /**
126    * Returns the {@link Html#noTableHeaders() @Html(noTableHeaders)} annotation defined on the class.
127    *
128    * @return The value of the annotation.
129    */
130   public boolean isNoTableHeaders() {
131      return noTableHeaders;
132   }
133
134   /**
135    * Returns the {@link Html#render() @Html(render)} annotation defined on the class.
136    *
137    * @return The value of the annotation.
138    */
139   public HtmlRender<?> getRender() {
140      return render;
141   }
142}