Properties2Map.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.utils.jaxb;

  21. import java.util.HashMap;
  22. import java.util.Iterator;
  23. import java.util.Map;
  24. import java.util.Map.Entry;

  25. import javax.xml.bind.annotation.adapters.XmlAdapter;


  26. /**
  27.  * Properties2Map
  28.  *
  29.  *
  30.  * @author Nardi Lorenzo (nardi@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class Properties2Map extends XmlAdapter<Properties, Map<String, String>>
  35. {
  36.     @Override
  37.     public Properties marshal(Map<String, String> hash) throws Exception {
  38.         if(hash==null){
  39.             return null;
  40.         }
  41.         Properties prop = new Properties();
  42.         Iterator<Entry<String,String>> i = hash.entrySet().iterator();
  43.         while(i.hasNext()){
  44.             Entry<String,String> e = i.next();
  45.             prop.getEntry().add(new Properties.Entry(e.getKey(),e.getValue()));
  46.         }
  47.         return prop;
  48.     }
  49.     @Override
  50.     public Map<String, String> unmarshal(Properties prop) throws Exception {
  51.         if(prop==null){
  52.             return null;
  53.         }
  54.         Map<String, String> hash = new HashMap<>();
  55.         Iterator<Properties.Entry> i = prop.getEntry().iterator();
  56.         while(i.hasNext()){
  57.             Properties.Entry e = i.next();
  58.             hash.put(e.getKey(), e.getValue());
  59.         }
  60.         return hash;
  61.     }
  62. }