001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.swaps;
014
015import static org.apache.juneau.common.internal.StringUtils.*;
016import static org.apache.juneau.common.internal.ThrowableUtils.*;
017
018import javax.xml.datatype.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.swap.*;
022
023/**
024 * Transforms {@link XMLGregorianCalendar XMLGregorianCalendars} to ISO8601 date-time {@link String Strings}.
025 *
026 * <p>
027 * Objects are converted to strings using {@link XMLGregorianCalendar#toXMLFormat()}.
028 *
029 * <p>
030 * Strings are converted to objects using {@link DatatypeFactory#newXMLGregorianCalendar(String)}.
031 *
032 * <h5 class='section'>See Also:</h5><ul>
033 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.Swaps">Swaps</a>
034
035 * </ul>
036 */
037public class XMLGregorianCalendarSwap extends StringSwap<XMLGregorianCalendar> {
038
039   private DatatypeFactory dtf;
040
041   /**
042    * Constructor.
043    */
044   public XMLGregorianCalendarSwap() {
045      try {
046         this.dtf = DatatypeFactory.newInstance();
047      } catch (DatatypeConfigurationException e) {
048         throw asRuntimeException(e);
049      }
050   }
051
052   /**
053    * Converts the specified <c>XMLGregorianCalendar</c> to a {@link String}.
054    */
055   @Override /* ObjectSwap */
056   public String swap(BeanSession session, XMLGregorianCalendar b) throws Exception {
057      return b.toXMLFormat();
058   }
059
060   /**
061    * Converts the specified {@link String} to an <c>XMLGregorianCalendar</c>.
062    */
063   @Override /* ObjectSwap */
064   public XMLGregorianCalendar unswap(BeanSession session, String s, ClassMeta<?> hint) throws Exception {
065      if (isEmpty(s))
066         return null;
067      return dtf.newXMLGregorianCalendar(s);
068   }
069}