1    /*
2     * Copyright 2009 :torweg free software group
3     * 
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    */
17   package org.torweg.pulse.util.xml.bind;
18   
19   import java.util.Locale;
20   
21   import javax.xml.bind.annotation.adapters.XmlAdapter;
22   
23   import org.torweg.pulse.util.LocaleUtils;
24   
25   /**
26    * an adapter for marshaling and unmarshaling a {@code Locale} using the
27    * locale's string representation (
28    * {@code @XmlJavaTypeAdapter(LocaleXmlAdapter.class)} ).
29    * 
30    * @author Thomas Weber
31    * @version $Revision: 1426 $
32    */
33   public class LocaleXmlAdapter extends XmlAdapter<String, Locale> {
34   
35       /**
36        * marshals the locale to its string representation.
37        * 
38        * @param locale
39        *            the {@code Locale}
40        * 
41        * @return the string
42        * 
43        * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
44        */
45       @Override
46       public String marshal(final Locale locale) {
47           return locale.toString();
48       }
49   
50       /**
51        * unmarshals the locale from its string representation.
52        * 
53        * @param string
54        *            the locale-string
55        * 
56        * @return the {@code Locale}
57        * 
58        * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
59        */
60       @Override
61       public Locale unmarshal(final String string) {
62           return LocaleUtils.localeFromString(string);
63       }
64   
65   }
66