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