11/*
22 Copyright (c) 2011+, HL7, Inc.
33 All rights reserved.
4-
5- Redistribution and use in source and binary forms, with or without modification,
4+
5+ Redistribution and use in source and binary forms, with or without modification,
66 are permitted provided that the following conditions are met:
7-
8- * Redistributions of source code must retain the above copyright notice, this
7+
8+ * Redistributions of source code must retain the above copyright notice, this
99 list of conditions and the following disclaimer.
10- * Redistributions in binary form must reproduce the above copyright notice,
11- this list of conditions and the following disclaimer in the documentation
10+ * Redistributions in binary form must reproduce the above copyright notice,
11+ this list of conditions and the following disclaimer in the documentation
1212 and/or other materials provided with the distribution.
13- * Neither the name of HL7 nor the names of its contributors may be used to
14- endorse or promote products derived from this software without specific
13+ * Neither the name of HL7 nor the names of its contributors may be used to
14+ endorse or promote products derived from this software without specific
1515 prior written permission.
16-
17- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20- IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
16+
17+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20+ IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2626 POSSIBILITY OF SUCH DAMAGE.
27-
27+
2828*/
2929
30- using P = Hl7 . Fhir . ElementModel . Types ;
30+ using P = Hl7 . Fhir . ElementModel . Types ;
3131using Hl7 . Fhir . Utility ;
32+ using Hl7 . Fhir . Rest ;
3233using System ;
3334
3435#nullable enable
@@ -37,6 +38,11 @@ namespace Hl7.Fhir.Model;
3738
3839public partial class Canonical
3940{
41+ /// <summary>
42+ /// The base uri for FHIR core profiles, which is "http://hl7.org/fhir/StructureDefinition/".
43+ /// </summary>
44+ public static readonly Uri FHIR_CORE_PROFILE_BASE_URI = new ( ResourceIdentity . CORE_BASE_URL ) ;
45+
4046 /// <summary>
4147 /// Constructs a Canonical based on a given <see cref="Uri"/>.
4248 /// </summary>
@@ -46,20 +52,19 @@ public Canonical(Uri uri) : this(uri.OriginalString)
4652 // nothing
4753 }
4854
49- /// <summary>
50- /// Constructs a canonical from its components.
51- /// </summary>
52- public Canonical ( string ? uri , string ? version , string ? fragment = null )
53- {
54- if ( ( uri is not null ) && uri . IndexOfAny ( [ '|' , '#' ] ) != - 1 )
55- throw Error . Argument ( nameof ( uri ) , "cannot contain version/fragment data" ) ;
56-
57- if ( ( version is not null ) && version . IndexOfAny ( [ '|' , '#' ] ) != - 1 )
58- throw Error . Argument ( nameof ( version ) , "cannot contain version/fragment data" ) ;
55+ /// <summary>
56+ /// Constructs a canonical from its components.
57+ /// </summary>
58+ public Canonical ( string ? uri , string ? version , string ? fragment = null )
59+ {
60+ if ( ( uri is not null ) && uri . IndexOfAny ( [ '|' , '#' ] ) != - 1 )
61+ throw Error . Argument ( nameof ( uri ) , "cannot contain version/fragment data" ) ;
5962
60- if ( ( fragment is not null ) && fragment . IndexOfAny ( [ '|' , '#' ] ) != - 1 )
61- throw Error . Argument ( nameof ( fragment ) , "already contains version/fragment data" ) ;
63+ if ( ( version is not null ) && version . IndexOfAny ( [ '|' , '#' ] ) != - 1 )
64+ throw Error . Argument ( nameof ( version ) , "cannot contain version/fragment data" ) ;
6265
66+ if ( ( fragment is not null ) && fragment . IndexOfAny ( [ '|' , '#' ] ) != - 1 )
67+ throw Error . Argument ( nameof ( fragment ) , "already contains version/fragment data" ) ;
6368
6469 Value = uri +
6570 ( version is not null ? "|" + version : null ) +
@@ -80,7 +85,7 @@ public void Deconstruct(out string? uri, out string? version, out string? fragme
8085 /// Converts a string to a canonical.
8186 /// </summary>
8287 /// <param name="value"></param>
83- public static implicit operator Canonical ( string value ) => new ( value ) ;
88+ public static implicit operator Canonical ( string ? value ) => new ( value ) ;
8489
8590 /// <summary>
8691 /// Converts a canonical to a string.
@@ -93,9 +98,17 @@ public void Deconstruct(out string? uri, out string? version, out string? fragme
9398 /// </summary>
9499 public static bool IsValidValue ( string value ) => FhirUri . IsValidValue ( value ) ;
95100
96- public static readonly Uri FHIR_CORE_PROFILE_BASE_URI = new ( @"http://hl7.org/fhir/StructureDefinition/" ) ;
97- public static Canonical CanonicalUriForFhirCoreType ( string typename ) => new ( FHIR_CORE_PROFILE_BASE_URI + typename ) ;
98-
101+ /// <summary>
102+ /// Constructs a Canonical to represent a FHIR core type given by name.
103+ /// </summary>
104+ /// <remarks>If the typename is an absolute url, this function assumes the
105+ /// typename is already fully qualified and will return a canonical with that value.
106+ /// </remarks>
107+ public static Canonical ForCoreType ( string typename )
108+ {
109+ var typeNameUri = new Canonical ( typename ) ;
110+ return typeNameUri . IsAbsolute ? typeNameUri : ResourceIdentity . Core ( typename ) . OriginalString ;
111+ }
99112
100113 /// <summary>
101114 /// The version string of the canonical (if present).
@@ -145,8 +158,8 @@ private static (string? url, string? version, string? fragment) splitCanonical(s
145158 if ( url . EndsWith ( separator . ToString ( ) ) ) url = url [ ..^ 1 ] ;
146159 var position = url . LastIndexOf ( separator ) ;
147160
148- return position == - 1 ?
149- ( url , null )
161+ return position == - 1
162+ ? ( url , null )
150163 : ( url [ ..position ] , url [ ( position + 1 ) ..] ) ;
151164 }
152165 }
@@ -157,8 +170,8 @@ private static (string? url, string? version, string? fragment) splitCanonical(s
157170 /// <exception cref="InvalidOperationException">The value of this canonical is null,
158171 /// which is not valid for System strings.</exception>
159172 public P . String ToSystemString ( ) => ( P . String ? ) TryConvertToSystemTypeInternal ( ) ??
160- throw new InvalidOperationException ( "Value is null." ) ;
173+ throw new InvalidOperationException ( "Value is null." ) ;
161174
162175 protected internal override P . Any ? TryConvertToSystemTypeInternal ( ) =>
163- Value is not null ? new P . String ( Value ) : null ;
176+ Value is not null ? new P . String ( Value ) : null ;
164177}
0 commit comments