private boolean mapProperty ( JSONObject root , JSONObject node , String key , String ... mapping ) throws JSONException { boolean deleteProperty = false ; for ( String value : mapping ) { Matcher matcher = MAPPED_PATTERN . matcher ( value ) ; if ( matcher . matches ( ) ) { // this is a mapped property, we will delete it if the mapped destination // property doesn't exist deleteProperty = true ; String path = matcher . group ( 2 ) ; // unwrap quoted property paths path = StringUtils . removeStart ( StringUtils . stripEnd ( path , "\'" ) , "\'" ) ; if ( root . has ( cleanup ( path ) ) ) { // replace property by mapped value in the original tree Object originalValue = root . get ( cleanup ( path ) ) ; node . put ( cleanup ( key ) , originalValue ) ; // negate boolean properties if negation character has been set String negate = matcher . group ( 1 ) ; if ( "!" . equals ( negate ) && ( originalValue instanceof Boolean ) ) { node . put ( cleanup ( key ) , ! ( ( Boolean ) originalValue ) ) ; } // the mapping was successful deleteProperty = false ; break ; } else { String defaultValue = matcher . group ( 4 ) ; if ( defaultValue != null ) { node . put ( cleanup ( key ) , defaultValue ) ; deleteProperty = false ; break ; } } } } if ( deleteProperty ) { // mapped destination does not exist, we don't include the property in replacement tree node . remove ( key ) ; return false ; } return true ; }
Replaces the value of a mapped property with a value from the original tree .