001/* 002 * #%L 003 * HAPI FHIR - Core Library 004 * %% 005 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.model.primitive; 021 022import java.net.URI; 023import java.net.URISyntaxException; 024 025import org.apache.commons.lang3.StringUtils; 026 027import ca.uhn.fhir.model.api.BasePrimitive; 028import ca.uhn.fhir.model.api.annotation.DatatypeDef; 029import ca.uhn.fhir.model.api.annotation.SimpleSetter; 030 031@DatatypeDef(name = "uri") 032public class UriDt extends BasePrimitive<String> { 033 034 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(UriDt.class); 035 036 /** 037 * Create a new String 038 */ 039 public UriDt() { 040 // nothing 041 } 042 043 /** 044 * Create a new String 045 */ 046 @SimpleSetter 047 public UriDt(@SimpleSetter.Parameter(name = "theUri") String theValue) { 048 setValueAsString(theValue); 049 } 050 051 @Override 052 protected String encode(String theValue) { 053 return theValue; 054 } 055 056 @Override 057 public boolean equals(Object obj) { 058 if (this == obj) 059 return true; 060 if (obj == null) 061 return false; 062 if (getClass() != obj.getClass()) 063 return false; 064 065 UriDt other = (UriDt) obj; 066 if (getValue() == null && other.getValue() == null) { 067 return true; 068 } 069 if (getValue() == null || other.getValue() == null) { 070 return false; 071 } 072 073 String normalize = normalize(getValue()); 074 String normalize2 = normalize(other.getValue()); 075 return normalize.equals(normalize2); 076 } 077 078 /** 079 * Compares the given string to the string representation of this URI. In many cases it is preferable to use this 080 * instead of the standard {@link #equals(Object)} method, since that method returns <code>false</code> unless it is 081 * passed an instance of {@link UriDt} 082 */ 083 public boolean equals(String theString) { 084 return StringUtils.equals(getValueAsString(), theString); 085 } 086 087 @Override 088 public int hashCode() { 089 final int prime = 31; 090 int result = 1; 091 092 String normalize = normalize(getValue()); 093 result = prime * result + ((normalize == null) ? 0 : normalize.hashCode()); 094 095 return result; 096 } 097 098 private String normalize(String theValue) { 099 if (theValue == null) { 100 return null; 101 } 102 URI retVal; 103 try { 104 retVal = new URI(theValue).normalize(); 105 String urlString = retVal.toString(); 106 if (urlString.endsWith("/") && urlString.length() > 1) { 107 retVal = new URI(urlString.substring(0, urlString.length() - 1)); 108 } 109 } catch (URISyntaxException e) { 110 ourLog.debug("Failed to normalize URL '{}', message was: {}", theValue, e.toString()); 111 return theValue; 112 } 113 114 return retVal.toASCIIString(); 115 } 116 117 @Override 118 protected String parse(String theValue) { 119 return theValue; 120 } 121 122 /** 123 * Creates a new UriDt instance which uses the given OID as the content (and prepends "urn:oid:" to the OID string 124 * in the value of the newly created UriDt, per the FHIR specification). 125 * 126 * @param theOid 127 * The OID to use (<code>null</code> is acceptable and will result in a UriDt instance with a 128 * <code>null</code> value) 129 * @return A new UriDt instance 130 */ 131 public static UriDt fromOid(String theOid) { 132 if (theOid == null) { 133 return new UriDt(); 134 } 135 return new UriDt("urn:oid:" + theOid); 136 } 137 138}