001package org.hl7.fhir.dstu2.model; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030 */ 031 032 033 034import org.hl7.fhir.exceptions.FHIRException; 035 036/** 037 * in a language with helper classes, this would be a helper class (at least, the base exgtension helpers would be) 038 * @author Grahame 039 * 040 */ 041public class ExtensionHelper { 042 043 044 /** 045 * @param name the identity of the extension of interest 046 * @return true if the named extension is on this element. Will check modifier extensions too if appropriate 047 */ 048 public static boolean hasExtension(Element element, String name) { 049 if (element != null && element instanceof BackboneElement) 050 return hasExtension((BackboneElement) element, name); 051 052 if (name == null || element == null || !element.hasExtension()) 053 return false; 054 for (Extension e : element.getExtension()) { 055 if (name.equals(e.getUrl())) 056 return true; 057 } 058 return false; 059 } 060 061 /** 062 * @param name the identity of the extension of interest 063 * @return true if the named extension is on this element. Will check modifier extensions 064 */ 065 public static boolean hasExtension(BackboneElement element, String name) { 066 if (name == null || element == null || !(element.hasExtension() || element.hasModifierExtension())) 067 return false; 068 for (Extension e : element.getModifierExtension()) { 069 if (name.equals(e.getUrl())) 070 return true; 071 } 072 for (Extension e : element.getExtension()) { 073 if (name.equals(e.getUrl())) 074 return true; 075 } 076 return false; 077 } 078 079 080 /** 081 * @param name the identity of the extension of interest 082 * @return The extension, if on this element, else null. will check modifier extensions too, if appropriate 083 */ 084 public static Extension getExtension(Element element, String name) { 085 if (element != null && element instanceof BackboneElement) 086 return getExtension((BackboneElement) element, name); 087 088 if (name == null || element == null || !element.hasExtension()) 089 return null; 090 for (Extension e : element.getExtension()) { 091 if (name.equals(e.getUrl())) 092 return e; 093 } 094 return null; 095 } 096 097 /** 098 * @param name the identity of the extension of interest 099 * @return The extension, if on this element, else null. will check modifier extensions too 100 */ 101 public static Extension getExtension(BackboneElement element, String name) { 102 if (name == null || element == null || !element.hasExtension()) 103 return null; 104 for (Extension e : element.getModifierExtension()) { 105 if (name.equals(e.getUrl())) 106 return e; 107 } 108 for (Extension e : element.getExtension()) { 109 if (name.equals(e.getUrl())) 110 return e; 111 } 112 return null; 113 } 114 115 /** 116 * set the value of an extension on the element. if value == null, make sure it doesn't exist 117 * 118 * @param element - the element to act on. Can also be a backbone element 119 * @param modifier - whether this is a modifier. Note that this is a definitional property of the extension; don't alternate 120 * @param uri - the identifier for the extension 121 * @param value - the value of the extension. Delete if this is null 122 * @- if the modifier logic is incorrect 123 */ 124 public static void setExtension(Element element, boolean modifier, String uri, Type value) throws FHIRException { 125 if (value == null) { 126 // deleting the extension 127 if (element instanceof BackboneElement) 128 for (Extension e : ((BackboneElement) element).getModifierExtension()) { 129 if (uri.equals(e.getUrl())) 130 ((BackboneElement) element).getModifierExtension().remove(e); 131 } 132 for (Extension e : element.getExtension()) { 133 if (uri.equals(e.getUrl())) 134 element.getExtension().remove(e); 135 } 136 } else { 137 // it would probably be easier to delete and then create, but this would re-order the extensions 138 // not that order matters, but we'll preserve it anyway 139 boolean found = false; 140 if (element instanceof BackboneElement) 141 for (Extension e : ((BackboneElement) element).getModifierExtension()) { 142 if (uri.equals(e.getUrl())) { 143 if (!modifier) 144 throw new FHIRException("Error adding extension \""+uri+"\": found an existing modifier extension, and the extension is not marked as a modifier"); 145 e.setValue(value); 146 found = true; 147 } 148 } 149 for (Extension e : element.getExtension()) { 150 if (uri.equals(e.getUrl())) { 151 if (modifier) 152 throw new FHIRException("Error adding extension \""+uri+"\": found an existing extension, and the extension is marked as a modifier"); 153 e.setValue(value); 154 found = true; 155 } 156 } 157 if (!found) { 158 Extension ex = new Extension().setUrl(uri).setValue(value); 159 if (modifier) { 160 if (!(element instanceof BackboneElement)) 161 throw new FHIRException("Error adding extension \""+uri+"\": extension is marked as a modifier, but element is not a backbone element"); 162 ((BackboneElement) element).getModifierExtension().add(ex); 163 164 } else { 165 element.getExtension().add(ex); 166 } 167 } 168 } 169 } 170 171 public static boolean hasExtensions(Element element) { 172 if (element instanceof BackboneElement) 173 return element.hasExtension() || ((BackboneElement) element).hasModifierExtension(); 174 else 175 return element.hasExtension(); 176 } 177 178 179}