Char2String.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 javax.xml.bind.annotation.adapters.XmlAdapter;

  22. /**
  23.  * Char2String
  24.  *
  25.  *
  26.  * @author Nardi Lorenzo (nardi@link.it)
  27.  * @author $Author$
  28.  * @version $Rev$, $Date$
  29.  */
  30. public class Char2String extends XmlAdapter<String, Character> {


  31.     @Override
  32.     public Character unmarshal(String valueParam) {
  33.         if(valueParam==null){
  34.             return null;
  35.         }
  36.         String value = valueParam.trim();
  37.         return Character.valueOf(value.charAt(0));
  38.     }

  39.     @Override
  40.     public String marshal(Character value) {
  41.         if (value == null) {
  42.             return null;
  43.         }
  44.         char charValue = value.charValue();
  45.         if(charValue==0){ // == ''
  46.             // devo ritornare cmq un carattere,
  47.             // se torno il carattere speciale '\u0000' ottengo un errore in fase di  Unmarshalling Error: Illegal character (NULL, unicode 0) encountered: not valid in any content
  48.             // se torno null, ottengo un errore di validazione xsd, essendo atteso un carattere (length=1)
  49.             return " ";
  50.         }
  51.         return charValue+"";
  52.     }

  53. }