001package org.apache.turbine.services.urlmapper.model;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.LinkedHashMap;
023import java.util.Map;
024import java.util.stream.Collectors;
025
026import javax.xml.bind.annotation.adapters.XmlAdapter;
027
028import org.apache.turbine.services.urlmapper.model.XmlParameterList.XmlParameter;
029
030/**
031 * Creates Map objects from XmlParameterList objects and vice-versa.
032 *
033 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
034 */
035public class XmlParameterAdapter extends XmlAdapter<XmlParameterList, Map<String, String>>
036{
037    /**
038     * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
039     */
040    @Override
041    public Map<String, String> unmarshal(XmlParameterList xmlList) throws Exception
042    {
043        // Make sure that order is kept
044        return xmlList.getXmlParameters().stream()
045                .collect(Collectors.toMap(xml -> xml.key, xml -> xml.value,
046                        (e1, e2) -> e1, LinkedHashMap::new));
047    }
048
049    /**
050     * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
051     */
052    @Override
053    public XmlParameterList marshal(Map<String, String> map) throws Exception
054    {
055        XmlParameterList xmlList = new XmlParameterList();
056        xmlList.setXmlParameters(map.entrySet().stream()
057                .map(entry -> new XmlParameter(entry.getKey(), entry.getValue()))
058                .collect(Collectors.toList()));
059
060        return xmlList;
061    }
062}