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.bean;
018
019import static org.apache.juneau.common.utils.StringUtils.*;
020import static org.apache.juneau.common.utils.ThrowableUtils.*;
021
022import java.text.*;
023
024import org.apache.juneau.annotation.*;
025import org.apache.juneau.common.utils.*;
026import org.apache.juneau.html.*;
027import org.apache.juneau.html.annotation.*;
028import org.apache.juneau.httppart.*;
029import org.apache.juneau.oapi.*;
030import org.apache.juneau.objecttools.*;
031import org.apache.juneau.serializer.*;
032
033/**
034 * Simple bean that implements a hyperlink for the HTML serializer.
035 *
036 * <p>
037 * The name and url properties correspond to the following parts of a hyperlink in an HTML document...
038 * <p class='bxml'>
039 *    <xt>&lt;a</xt> <xa>href</xa>=<xs>'href'</xs><xt>&gt;</xt>name<xt>&lt;/a&gt;</xt>
040 * </p>
041 *
042 * <p>
043 * When encountered by the {@link HtmlSerializer} class, this object gets converted to a hyperlink.
044 * All other serializers simply convert it to a simple bean.
045 *
046 * <h5 class='section'>See Also:</h5><ul>
047
048 * </ul>
049 */
050@HtmlLink
051@Bean(findFluentSetters=true)
052public class LinkString implements Comparable<LinkString> {
053   private String name;
054   private java.net.URI uri;
055
056   /** No-arg constructor. */
057   public LinkString() {}
058
059   /**
060    * Constructor.
061    *
062    * @param name Corresponds to the text inside of the <xt>&lt;A&gt;</xt> element.
063    * @param uri Corresponds to the value of the <xa>href</xa> attribute of the <xt>&lt;A&gt;</xt> element.
064    * @param uriArgs Optional arguments for {@link MessageFormat} style arguments in the href.
065    */
066   public LinkString(String name, String uri, Object...uriArgs) {
067      setName(name);
068      setUri(uri, uriArgs);
069   }
070
071
072   //-----------------------------------------------------------------------------------------------------------------
073   // Bean properties
074   //-----------------------------------------------------------------------------------------------------------------
075
076   /**
077    * Bean property getter:  <property>name</property>.
078    *
079    * <p>
080    * Corresponds to the text inside of the <xt>&lt;A&gt;</xt> element.
081    *
082    * @return The property value, or <jk>null</jk> if it is not set.
083    */
084   public String getName() {
085      return name;
086   }
087
088   /**
089    * Bean property setter:  <property>name</property>.
090    *
091    * <p>
092    * Corresponds to the text inside of the <xt>&lt;A&gt;</xt> element.
093    *
094    * @param value
095    *    The new value for this property.
096    *    <br>Can be <jk>null</jk> to unset the property.
097    * @return This object
098    */
099   public LinkString setName(String value) {
100      this.name = value;
101      return this;
102   }
103
104   //-----------------------------------------------------------------------------------------------------------------
105   // uri
106   //-----------------------------------------------------------------------------------------------------------------
107
108   /**
109    * Bean property getter:  <property>uri</property>.
110    *
111    * <p>
112    * Corresponds to the value of the <xa>href</xa> attribute of the <xt>&lt;A&gt;</xt> element.
113    *
114    * @return The property value, or <jk>null</jk> if it is not set.
115    */
116   public java.net.URI getUri() {
117      return uri;
118   }
119
120   /**
121    * Bean property setter:  <property>uri</property>.
122    *
123    * <p>
124    * Corresponds to the value of the <xa>href</xa> attribute of the <xt>&lt;A&gt;</xt> element.
125    *
126    * @param value
127    *    The new value for this property.
128    *    <br>Can be <jk>null</jk> to unset the property.
129    * @return This object
130    */
131   public LinkString setUri(java.net.URI value) {
132      this.uri = value;
133      return this;
134   }
135   /**
136    * Bean property fluent setter:  <property>uri</property>.
137    *
138    * <p>
139    * Corresponds to the value of the <xa>href</xa> attribute of the <xt>&lt;A&gt;</xt> element.
140    *
141    * @param value
142    *    The new value for this property.
143    *    <br>Can be <jk>null</jk> to unset the property.
144    * @return This object.
145    */
146   public LinkString setUri(String value) {
147      setUri(value, new Object[0]);  // NOSONAR - Not fixable.
148      return this;
149   }
150
151   /**
152    * Bean property fluent setter:  <property>uri</property>.
153    *
154    * <p>
155    * Corresponds to the value of the <xa>href</xa> attribute of the <xt>&lt;A&gt;</xt> element.
156    *
157    * @param value
158    *    The new value for this property.
159    *    <br>Can be <jk>null</jk> to unset the property.
160    * @param args {@link MessageFormat}-style arguments in the URL.
161    * @return This object.
162    */
163   public LinkString setUri(String value, Object...args) {
164      for (var i = 0; i < args.length; i++)
165         try {
166            args[i] = OpenApiSerializer.DEFAULT.getSession().serialize(HttpPartType.PATH, null, args[i]);
167         } catch (SchemaValidationException | SerializeException e) {
168            throw asRuntimeException(e);
169         }
170      this.uri = java.net.URI.create(format(value, args));
171      return this;
172   }
173
174
175   /**
176    * Returns the name so that the {@link ObjectSearcher} class can search against it.
177    */
178   @Override /* Object */
179   public String toString() {
180      return name;
181   }
182
183   @Override /* Comparable */
184   public int compareTo(LinkString o) {
185      return name.compareTo(o.name);
186   }
187
188   @Override /* Object */
189   public boolean equals(Object o) {
190      return (o instanceof LinkString o2) && Utils.eq(this, o2, (x,y)->x.name.equals(y.name));
191   }
192
193   @Override /* Object */
194   public int hashCode() {
195      return super.hashCode();
196   }
197}