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 org.apache.juneau.*;
016import org.apache.juneau.html.annotation.*;
017
018/**
019 * Metadata on bean properties specific to the HTML serializers and parsers pulled from the {@link Html @Html}
020 * annotation on the bean property.
021 */
022@SuppressWarnings("rawtypes")
023public final class HtmlBeanPropertyMeta extends BeanPropertyMetaExtended {
024
025   private final boolean asXml, noTables, noTableHeaders, asPlainText;
026   private final HtmlRender render;
027   private final String link, anchorText;
028
029   /**
030    * Constructor.
031    * 
032    * @param bpm The metadata of the bean property of this additional metadata.
033    * @throws Exception If render class could not be instantiated.
034    */
035   public HtmlBeanPropertyMeta(BeanPropertyMeta bpm) throws Exception {
036      super(bpm);
037      Builder b = new Builder();
038      if (bpm.getField() != null)
039         b.findHtmlInfo(bpm.getField().getAnnotation(Html.class));
040      if (bpm.getGetter() != null)
041         b.findHtmlInfo(bpm.getGetter().getAnnotation(Html.class));
042      if (bpm.getSetter() != null)
043         b.findHtmlInfo(bpm.getSetter().getAnnotation(Html.class));
044
045      this.asXml = b.asXml;
046      this.noTables = b.noTables;
047      this.noTableHeaders = b.noTableHeaders;
048      this.asPlainText = b.asPlainText;
049      this.render = bpm.getBeanMeta().getClassMeta().getBeanContext().newInstance(HtmlRender.class, b.render);
050      this.link = b.link;
051      this.anchorText = b.anchorText;
052   }
053
054   static final class Builder {
055      boolean asXml, noTables, noTableHeaders, asPlainText;
056      Class<? extends HtmlRender> render = HtmlRender.class;
057      String link, anchorText;
058
059      void findHtmlInfo(Html html) {
060         if (html == null)
061            return;
062         if (html.asXml())
063            asXml = html.asXml();
064         if (html.noTables())
065            noTables = html.noTables();
066         if (html.noTableHeaders())
067            noTableHeaders = html.noTableHeaders();
068         if (html.asPlainText())
069            asPlainText = html.asPlainText();
070         if (html.render() != HtmlRender.class)
071            render = html.render();
072         if (! html.link().isEmpty())
073            link = html.link();
074         if (! html.anchorText().isEmpty())
075            anchorText = html.anchorText();
076      }
077   }
078
079   /**
080    * Returns whether this bean property should be serialized as XML instead of HTML.
081    * 
082    * @return <jk>true</jk> if the the {@link Html @Html} annotation is specified, and {@link Html#asXml() @Html.asXml()} is <jk>true</jk>.
083    */
084   protected boolean isAsXml() {
085      return asXml;
086   }
087
088   /**
089    * Returns whether this bean property should be serialized as plain text instead of HTML.
090    * 
091    * @return
092    *    <jk>true</jk> if the the {@link Html @Html} annotation is specified, and {@link Html#asPlainText() @Html.asPlainText()} is
093    *    <jk>true</jk>.
094    */
095   protected boolean isAsPlainText() {
096      return asPlainText;
097   }
098
099   /**
100    * Returns whether this bean property should not be serialized as an HTML table.
101    * 
102    * @return
103    *    <jk>true</jk> if the the {@link Html @Html} annotation is specified, and {@link Html#noTables() @Html.noTables()} is
104    *    <jk>true</jk>.
105    */
106   protected boolean isNoTables() {
107      return noTables;
108   }
109
110   /**
111    * Returns whether this bean property should not include table headers when serialized as an HTML table.
112    * 
113    * @return
114    *    <jk>true</jk> if the the {@link Html @Html} annotation is specified, and {@link Html#noTableHeaders() @Html.noTableHeaders()} is
115    *    <jk>true</jk>.
116    */
117   public boolean isNoTableHeaders() {
118      return noTableHeaders;
119   }
120
121   /**
122    * Returns the render class for rendering the style and contents of this property value in HTML.
123    * 
124    * <p>
125    * This value is specified via the {@link Html#render() @Html.render()} annotation.
126    * 
127    * @return The render class, never <jk>null</jk>.
128    */
129   public HtmlRender getRender() {
130      return render;
131   }
132
133   /**
134    * Adds a hyperlink to this value in HTML.
135    * 
136    * <p>
137    * This value is specified via the {@link Html#link() @Html.link()} annotation.
138    * 
139    * @return The link string, or <jk>null</jk> if not specified.
140    */
141   public String getLink() {
142      return link;
143   }
144
145   /**
146    * Specifies the anchor text for this property.
147    * 
148    * <p>
149    * This value is specified via the {@link Html#anchorText() @Html.anchorText()} annotation.
150    * 
151    * @return The link string, or <jk>null</jk> if not specified.
152    */
153   public String getAnchorText() {
154      return anchorText;
155   }
156}