001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util.xml.jaxb.adapter; 017 018import javax.xml.bind.annotation.adapters.XmlAdapter; 019 020import org.kuali.common.util.Assert; 021import org.kuali.common.util.Str; 022 023import com.google.common.base.Optional; 024 025public final class FlattenOptionalStringAdapter extends XmlAdapter<String, Optional<String>> { 026 027 public FlattenOptionalStringAdapter() { 028 this(FlattenConstants.DEFAULT_CR_REPLACEMENT, FlattenConstants.DEFAULT_LF_REPLACEMENT); 029 } 030 031 public FlattenOptionalStringAdapter(String carriageReturnReplacement, String linefeedReplacement) { 032 // No blanks because this needs to work in both directions (flatten + inflate) 033 Assert.noBlanks(carriageReturnReplacement, linefeedReplacement); 034 this.carriageReturnReplacement = carriageReturnReplacement; 035 this.linefeedReplacement = linefeedReplacement; 036 } 037 038 private final String carriageReturnReplacement; 039 private final String linefeedReplacement; 040 041 @Override 042 public String marshal(Optional<String> optional) { 043 if (optional.isPresent()) { 044 return Str.flatten(optional.get(), carriageReturnReplacement, linefeedReplacement); 045 } else { 046 return null; 047 } 048 } 049 050 @Override 051 public Optional<String> unmarshal(String value) { 052 if (value == null) { 053 return Optional.<String> absent(); 054 } else { 055 return Optional.<String> of(Str.inflate(value, carriageReturnReplacement, linefeedReplacement)); 056 } 057 } 058 059 public String getCarriageReturnReplacement() { 060 return carriageReturnReplacement; 061 } 062 063 public String getLinefeedReplacement() { 064 return linefeedReplacement; 065 } 066 067}