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