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