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.internal.StringUtils.*;
016
017import java.text.*;
018
019import org.apache.juneau.html.*;
020import org.apache.juneau.html.annotation.*;
021import org.apache.juneau.httppart.*;
022import org.apache.juneau.utils.*;
023
024/**
025 * Simple bean that implements a hyperlink for the HTML serializer.
026 * 
027 * <p>
028 * The name and url properties correspond to the following parts of a hyperlink in an HTML document...
029 * <p class='bcode'>
030 *    <xt>&lt;a</xt> <xa>href</xa>=<xs>'href'</xs><xt>&gt;</xt>name<xt>&lt;/a&gt;</xt>
031 * </p>
032 * 
033 * <p>
034 * When encountered by the {@link HtmlSerializer} class, this object gets converted to a hyperlink.
035 * All other serializers simply convert it to a simple bean.
036 */
037@HtmlLink(nameProperty = "name", hrefProperty = "href")
038public class LinkString implements Comparable<LinkString> {
039   private String name, href;
040
041   /** No-arg constructor. */
042   public LinkString() {}
043
044   /**
045    * Constructor.
046    * 
047    * @param name Corresponds to the text inside of the <xt>&lt;A&gt;</xt> element.
048    * @param href Corresponds to the value of the <xa>href</xa> attribute of the <xt>&lt;A&gt;</xt> element.
049    * @param hrefArgs Optional arguments for {@link MessageFormat} style arguments in the href.
050    */
051   public LinkString(String name, String href, Object...hrefArgs) {
052      setName(name);
053      setHref(href, hrefArgs);
054   }
055
056
057   //--------------------------------------------------------------------------------
058   // Bean properties
059   //--------------------------------------------------------------------------------
060
061   /**
062    * Bean property getter:  <property>name</property>.
063    * 
064    * <p>
065    * Corresponds to the text inside of the <xt>&lt;A&gt;</xt> element.
066    * 
067    * @return The value of the <property>name</property> property on this bean, or <jk>null</jk> if it is not set.
068    */
069   public String getName() {
070      return name;
071   }
072
073   /**
074    * Bean property setter:  <property>name</property>.
075    * 
076    * @param name The new value for the <property>name</property> property on this bean.
077    * @return This object (for method chaining).
078    */
079   public LinkString setName(String name) {
080      this.name = name;
081      return this;
082   }
083
084   /**
085    * Bean property getter:  <property>href</property>.
086    * 
087    * <p>
088    * Corresponds to the value of the <xa>href</xa> attribute of the <xt>&lt;A&gt;</xt> element.
089    * 
090    * @return The value of the <property>href</property> property on this bean, or <jk>null</jk> if it is not set.
091    */
092   public String getHref() {
093      return href;
094   }
095
096   /**
097    * Bean property setter:  <property>href</property>.
098    * 
099    * @param href The new value for the <property>href</property> property on this bean.
100    * @return This object (for method chaining).
101    */
102   public LinkString setHref(String href) {
103      setHref(href, new Object[0]);
104      return this;
105   }
106
107   /**
108    * Bean property setter:  <property>href</property>.
109    * 
110    * <p>
111    * Same as {@link #setHref(String)} except allows for {@link MessageFormat} style arguments.
112    * 
113    * @param href The new href.
114    * @param args Optional {@link MessageFormat}-style arguments.
115    * @return This object (for method chaining).
116    */
117   public LinkString setHref(String href, Object...args) {
118      for (int i = 0; i < args.length; i++)
119         args[i] = SimpleUonPartSerializer.DEFAULT.serialize(HttpPartType.PATH, args[i]);
120      this.href = format(href, args);
121      return this;
122   }
123
124   /**
125    * Returns the name so that the {@link PojoQuery} class can search against it.
126    */
127   @Override /* Object */
128   public String toString() {
129      return name;
130   }
131
132   @Override /* Comparable */
133   public int compareTo(LinkString o) {
134      return name.compareTo(o.name);
135   }
136
137   @Override /* Object */
138   public boolean equals(Object o) {
139      if (! (o instanceof LinkString))
140         return false;
141      return (compareTo((LinkString)o) == 0);
142   }
143
144   @Override /* Object */
145   public int hashCode() {
146      return super.hashCode();
147   }
148}