org.kie.internal.jaxb.StringKeyStringValueMapXmlAdapter Maven / Gradle / Ivy
Show all versions of kie-internal Show documentation
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.internal.jaxb;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.bind.annotation.adapters.XmlAdapter;
/**
* This is a {@link XmlAdapter} for mapping Map<String, String> instances
* to classes/instances that both JAXB/XML and JSON can deal with.
*
* The most important reason for the existence of this class is that it works well
* with jackson JSON! JaxbMapAdapter, on the other hand, does not!
*/
public class StringKeyStringValueMapXmlAdapter extends XmlAdapter> {
@Override
public StringKeyStringValueMap marshal(Map map) throws Exception {
if( map == null ) {
return null;
}
StringKeyStringValueMap xmlMap = new StringKeyStringValueMap();
for(Entry entry : map.entrySet()) {
String value = entry.getValue();
String key = entry.getKey();
StringKeyStringValueEntry xmlEntry = new StringKeyStringValueEntry(key, value);
xmlMap.addEntry(xmlEntry);
}
return xmlMap;
}
@Override
public Map unmarshal(StringKeyStringValueMap xmlMap) {
if( xmlMap == null ) {
return null;
}
Map map = new HashMap<>();
for( StringKeyStringValueEntry xmlEntry : xmlMap.entries ) {
String key = xmlEntry.getKey();
String value = xmlEntry.getValue();
map.put(key, value);
}
return map;
}
}