1 package org.apache.turbine.services.urlmapper.model; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.util.LinkedHashMap; 23 import java.util.Map; 24 import java.util.stream.Collectors; 25 26 import javax.xml.bind.annotation.adapters.XmlAdapter; 27 28 import org.apache.turbine.services.urlmapper.model.XmlParameterList.XmlParameter; 29 30 /** 31 * Creates Map objects from XmlParameterList objects and vice-versa. 32 * 33 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a> 34 */ 35 public class XmlParameterAdapter extends XmlAdapter<XmlParameterList, Map<String, String>> 36 { 37 /** 38 * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object) 39 */ 40 @Override 41 public Map<String, String> unmarshal(XmlParameterList xmlList) throws Exception 42 { 43 // Make sure that order is kept 44 return xmlList.getXmlParameters().stream() 45 .collect(Collectors.toMap(xml -> xml.key, xml -> xml.value, 46 (e1, e2) -> e1, LinkedHashMap::new)); 47 } 48 49 /** 50 * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object) 51 */ 52 @Override 53 public XmlParameterList marshal(Map<String, String> map) throws Exception 54 { 55 XmlParameterList xmlList = new XmlParameterList(); 56 xmlList.setXmlParameters(map.entrySet().stream() 57 .map(entry -> new XmlParameter(entry.getKey(), entry.getValue())) 58 .collect(Collectors.toList())); 59 60 return xmlList; 61 } 62 }