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