public static void setExtension ( Element element , boolean modifier , String uri , Type value ) throws FHIRException { if ( value == null ) { // deleting the extension if ( element instanceof BackboneElement ) for ( Extension e : ( ( BackboneElement ) element ) . getModifierExtension ( ) ) { if ( uri . equals ( e . getUrl ( ) ) ) ( ( BackboneElement ) element ) . getModifierExtension ( ) . remove ( e ) ; } for ( Extension e : element . getExtension ( ) ) { if ( uri . equals ( e . getUrl ( ) ) ) element . getExtension ( ) . remove ( e ) ; } } else { // it would probably be easier to delete and then create, but this would re-order the extensions // not that order matters, but we'll preserve it anyway boolean found = false ; if ( element instanceof BackboneElement ) for ( Extension e : ( ( BackboneElement ) element ) . getModifierExtension ( ) ) { if ( uri . equals ( e . getUrl ( ) ) ) { if ( ! modifier ) throw new FHIRException ( "Error adding extension \"" + uri + "\": found an existing modifier extension, and the extension is not marked as a modifier" ) ; e . setValue ( value ) ; found = true ; } } for ( Extension e : element . getExtension ( ) ) { if ( uri . equals ( e . getUrl ( ) ) ) { if ( modifier ) throw new FHIRException ( "Error adding extension \"" + uri + "\": found an existing extension, and the extension is marked as a modifier" ) ; e . setValue ( value ) ; found = true ; } } if ( ! found ) { Extension ex = new Extension ( ) . setUrl ( uri ) . setValue ( value ) ; if ( modifier ) { if ( ! ( element instanceof BackboneElement ) ) throw new FHIRException ( "Error adding extension \"" + uri + "\": extension is marked as a modifier, but element is not a backbone element" ) ; ( ( BackboneElement ) element ) . getModifierExtension ( ) . add ( ex ) ; } else { element . getExtension ( ) . add ( ex ) ; } } } }
set the value of an extension on the element . if value == null make sure it doesn t exist