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