001package org.hl7.fhir.r5.model; 002 003/*- 004 * #%L 005 * org.hl7.fhir.r5 006 * %% 007 * Copyright (C) 2014 - 2019 Health Level 7 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 023 024import java.io.IOException; 025import java.io.Serializable; 026import java.util.ArrayList; 027import java.util.Date; 028import java.util.HashMap; 029import java.util.List; 030import java.util.Map; 031 032import org.hl7.fhir.exceptions.FHIRException; 033import org.hl7.fhir.instance.model.api.IBase; 034import org.hl7.fhir.r5.elementmodel.Element; 035import org.hl7.fhir.r5.elementmodel.ObjectConverter; 036import org.hl7.fhir.utilities.Utilities; 037import org.hl7.fhir.utilities.xhtml.XhtmlComposer; 038import org.hl7.fhir.utilities.xhtml.XhtmlNode; 039import org.hl7.fhir.utilities.xhtml.XhtmlParser; 040 041import ca.uhn.fhir.model.api.IElement; 042 043public abstract class Base implements Serializable, IBase, IElement { 044 045 /** 046 * User appended data items - allow users to add extra information to the class 047 */ 048private Map<String, Object> userData; 049 050 /** 051 * Round tracking xml comments for testing convenience 052 */ 053 private List<String> formatCommentsPre; 054 055 /** 056 * Round tracking xml comments for testing convenience 057 */ 058 private List<String> formatCommentsPost; 059 060 061 public Object getUserData(String name) { 062 if (userData == null) 063 return null; 064 return userData.get(name); 065 } 066 067 public void setUserData(String name, Object value) { 068 if (userData == null) 069 userData = new HashMap<String, Object>(); 070 userData.put(name, value); 071 } 072 073 public void clearUserData(String name) { 074 if (userData != null) 075 userData.remove(name); 076 } 077 078 public void setUserDataINN(String name, Object value) { 079 if (value == null) 080 return; 081 082 if (userData == null) 083 userData = new HashMap<String, Object>(); 084 userData.put(name, value); 085 } 086 087 public boolean hasUserData(String name) { 088 if (userData == null) 089 return false; 090 else 091 return userData.containsKey(name); 092 } 093 094 public String getUserString(String name) { 095 Object ud = getUserData(name); 096 if (ud == null) 097 return null; 098 if (ud instanceof String) 099 return (String) ud; 100 return ud.toString(); 101 } 102 103 public int getUserInt(String name) { 104 if (!hasUserData(name)) 105 return 0; 106 return (Integer) getUserData(name); 107 } 108 109 public boolean hasFormatComment() { 110 return (formatCommentsPre != null && !formatCommentsPre.isEmpty()) || (formatCommentsPost != null && !formatCommentsPost.isEmpty()); 111 } 112 113 public List<String> getFormatCommentsPre() { 114 if (formatCommentsPre == null) 115 formatCommentsPre = new ArrayList<String>(); 116 return formatCommentsPre; 117 } 118 119 public List<String> getFormatCommentsPost() { 120 if (formatCommentsPost == null) 121 formatCommentsPost = new ArrayList<String>(); 122 return formatCommentsPost; 123 } 124 125 // these 3 allow evaluation engines to get access to primitive values 126 public boolean isPrimitive() { 127 return false; 128 } 129 130 public boolean isBooleanPrimitive() { 131 return false; 132 } 133 134 public boolean hasPrimitiveValue() { 135 return isPrimitive(); 136 } 137 138 public String primitiveValue() { 139 return null; 140 } 141 142 public boolean isDateTime() { 143 return false; 144 } 145 146 public BaseDateTimeType dateTimeValue() { 147 return null; 148 } 149 150 public abstract String fhirType() ; 151 152 public boolean hasType(String... name) { 153 String t = fhirType(); 154 for (String n : name) 155 if (n.equalsIgnoreCase(t)) 156 return true; 157 return false; 158 } 159 160 protected void listChildren(List<Property> result) { 161 // nothing 162 } 163 164 public Base setProperty(String name, Base value) throws FHIRException { 165 throw new FHIRException("Attempt to set unknown property "+name); 166 } 167 168 public Base addChild(String name) throws FHIRException { 169 throw new FHIRException("Attempt to add child with unknown name "+name); 170 } 171 172 /** 173 * Supports iterating the children elements in some generic processor or browser 174 * All defined children will be listed, even if they have no value on this instance 175 * 176 * Note that the actual content of primitive or xhtml elements is not iterated explicitly. 177 * To find these, the processing code must recognise the element as a primitive, typecast 178 * the value to a {@link DataType}, and examine the value 179 * 180 * @return a list of all the children defined for this element 181 */ 182 public List<Property> children() { 183 List<Property> result = new ArrayList<Property>(); 184 listChildren(result); 185 return result; 186 } 187 188 public Property getChildByName(String name) { 189 List<Property> children = new ArrayList<Property>(); 190 listChildren(children); 191 for (Property c : children) 192 if (c.getName().equals(name)) 193 return c; 194 return null; 195 } 196 197 public List<Base> listChildrenByName(String name) throws FHIRException { 198 List<Base> result = new ArrayList<Base>(); 199 for (Base b : listChildrenByName(name, true)) 200 if (b != null) 201 result.add(b); 202 return result; 203 } 204 205 public Base[] listChildrenByName(String name, boolean checkValid) throws FHIRException { 206 if (name.equals("*")) { 207 List<Property> children = new ArrayList<Property>(); 208 listChildren(children); 209 List<Base> result = new ArrayList<Base>(); 210 for (Property c : children) 211 result.addAll(c.getValues()); 212 return result.toArray(new Base[result.size()]); 213 } 214 else 215 return getProperty(name.hashCode(), name, checkValid); 216 } 217 218 public boolean isEmpty() { 219 return true; // userData does not count 220 } 221 222 public boolean equalsDeep(Base other) { 223 return other != null; 224 } 225 226 public boolean equalsShallow(Base other) { 227 return other != null; 228 } 229 230 public static boolean compareDeep(String s1, String s2, boolean allowNull) { 231 if (allowNull) { 232 boolean noLeft = s1 == null || Utilities.noString(s1); 233 boolean noRight = s2 == null || Utilities.noString(s2); 234 if (noLeft && noRight) { 235 return true; 236 } 237 } 238 if (s1 == null || s2 == null) 239 return false; 240 return s1.equals(s2); 241 } 242 243 public static boolean compareDeep(List<? extends Base> e1, List<? extends Base> e2, boolean allowNull) { 244 if (noList(e1) && noList(e2) && allowNull) 245 return true; 246 if (noList(e1) || noList(e2)) 247 return false; 248 if (e1.size() != e2.size()) 249 return false; 250 for (int i = 0; i < e1.size(); i++) { 251 if (!compareDeep(e1.get(i), e2.get(i), allowNull)) 252 return false; 253 } 254 return true; 255 } 256 257 private static boolean noList(List<? extends Base> list) { 258 return list == null || list.isEmpty(); 259 } 260 261 public static boolean compareDeep(Base e1, Base e2, boolean allowNull) { 262 if (allowNull) { 263 boolean noLeft = e1 == null || e1.isEmpty(); 264 boolean noRight = e2 == null || e2.isEmpty(); 265 if (noLeft && noRight) { 266 return true; 267 } 268 } 269 if (e1 == null || e2 == null) 270 return false; 271 if (e2.isMetadataBased() && !e1.isMetadataBased()) // respect existing order for debugging consistency; outcome must be the same either way 272 return e2.equalsDeep(e1); 273 else 274 return e1.equalsDeep(e2); 275 } 276 277 public static boolean compareDeep(XhtmlNode div1, XhtmlNode div2, boolean allowNull) { 278 if (div1 == null && div2 == null && allowNull) 279 return true; 280 if (div1 == null || div2 == null) 281 return false; 282 return div1.equalsDeep(div2); 283 } 284 285 286 public static boolean compareValues(List<? extends PrimitiveType> e1, List<? extends PrimitiveType> e2, boolean allowNull) { 287 if (e1 == null && e2 == null && allowNull) 288 return true; 289 if (e1 == null || e2 == null) 290 return false; 291 if (e1.size() != e2.size()) 292 return false; 293 for (int i = 0; i < e1.size(); i++) { 294 if (!compareValues(e1.get(i), e2.get(i), allowNull)) 295 return false; 296 } 297 return true; 298 } 299 300 public static boolean compareValues(PrimitiveType e1, PrimitiveType e2, boolean allowNull) { 301 boolean noLeft = e1 == null || e1.isEmpty(); 302 boolean noRight = e2 == null || e2.isEmpty(); 303 if (noLeft && noRight && allowNull) { 304 return true; 305 } 306 if (noLeft != noRight) 307 return false; 308 return e1.equalsShallow(e2); 309 } 310 311 protected boolean isMetadataBased() { 312 return false; 313 } 314 315 public Base[] getProperty(int hash, String name, boolean checkValid) throws FHIRException { 316 if (checkValid) 317 throw new FHIRException("Attempt to read invalid property '"+name+"' on type "+fhirType()); 318 return null; 319 } 320 321 public Base setProperty(int hash, String name, Base value) throws FHIRException { 322 throw new FHIRException("Attempt to write to invalid property '"+name+"' on type "+fhirType()); 323 } 324 325 public Base makeProperty(int hash, String name) throws FHIRException { 326 throw new FHIRException("Attempt to make an invalid property '"+name+"' on type "+fhirType()); 327 } 328 329 public String[] getTypesForProperty(int hash, String name) throws FHIRException { 330 throw new FHIRException("Attempt to get types for an invalid property '"+name+"' on type "+fhirType()); 331 } 332 333 public static boolean equals(String v1, String v2) { 334 if (v1 == null && v2 == null) 335 return true; 336 else if (v1 == null || v2 == null) 337 return false; 338 else 339 return v1.equals(v2); 340 } 341 342 public boolean isResource() { 343 return false; 344 } 345 346 347 public abstract String getIdBase(); 348 public abstract void setIdBase(String value); 349 350 public Property getNamedProperty(String _name) throws FHIRException { 351 return getNamedProperty(_name.hashCode(), _name, false); 352 } 353 public Property getNamedProperty(int _hash, String _name, boolean _checkValid) throws FHIRException { 354 if (_checkValid) 355 throw new FHIRException("Attempt to read invalid property '"+_name+"' on type "+fhirType()); 356 return null; 357 } 358 359 public void copyValues(Base dst) { 360 } 361 362}