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.transforms; 014 015import static org.apache.juneau.internal.StringUtils.*; 016 017import javax.xml.datatype.*; 018 019import org.apache.juneau.*; 020import org.apache.juneau.transform.*; 021 022/** 023 * Transforms {@link XMLGregorianCalendar XMLGregorianCalendars} to ISO8601 date-time {@link String Strings}. 024 * 025 * <p> 026 * Objects are converted to strings using {@link XMLGregorianCalendar#toXMLFormat()}. 027 * 028 * <p> 029 * Strings are converted to objects using {@link DatatypeFactory#newXMLGregorianCalendar(String)}. 030 */ 031public class XMLGregorianCalendarSwap extends StringSwap<XMLGregorianCalendar> { 032 033 private DatatypeFactory dtf; 034 035 /** 036 * Constructor. 037 */ 038 public XMLGregorianCalendarSwap() { 039 try { 040 this.dtf = DatatypeFactory.newInstance(); 041 } catch (DatatypeConfigurationException e) { 042 throw new RuntimeException(e); 043 } 044 } 045 046 /** 047 * Converts the specified <c>XMLGregorianCalendar</c> to a {@link String}. 048 */ 049 @Override /* PojoSwap */ 050 public String swap(BeanSession session, XMLGregorianCalendar b) throws Exception { 051 return b.toXMLFormat(); 052 } 053 054 /** 055 * Converts the specified {@link String} to an <c>XMLGregorianCalendar</c>. 056 */ 057 @Override /* PojoSwap */ 058 public XMLGregorianCalendar unswap(BeanSession session, String s, ClassMeta<?> hint) throws Exception { 059 if (isEmpty(s)) 060 return null; 061 return dtf.newXMLGregorianCalendar(s); 062 } 063}