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