org.nervousync.commons.adapter.xml.CDataAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils-jdk11 Show documentation
Show all versions of utils-jdk11 Show documentation
Java utility collections, development by Nervousync Studio (NSYC)
/*
* Licensed to the Nervousync Studio (NSYC) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.nervousync.commons.adapter.xml;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;
import org.nervousync.utils.StringUtils;
/**
* CData adapter
* CDATA数据转换器
*
* @author Steven Wee [email protected]
* @version $Revision: 1.1.0 $Date: Jun 15, 2020 14:09:27 $
*/
public final class CDataAdapter extends XmlAdapter {
/**
* Begin string of CDATA
* CDATA起始字符串
*/
public static final String CDATA_BEGIN = "End string of CDATA
* CDATA终止字符串
*/
public static final String CDATA_END = "]]>";
/**
* @see XmlAdapter#unmarshal(Object)
*/
@Override
public String unmarshal(String v) {
if (StringUtils.isEmpty(v)) {
return "";
}
String dataValue = v;
if (dataValue.startsWith(CDATA_BEGIN)) {
dataValue = dataValue.substring(CDATA_BEGIN.length());
}
if (dataValue.endsWith(CDATA_END)) {
dataValue = dataValue.substring(0, dataValue.length() - CDATA_END.length());
}
return dataValue;
}
/**
* @see XmlAdapter#marshal(Object)
*/
@Override
public String marshal(String v) {
if (StringUtils.isEmpty(v)) {
return CDATA_BEGIN + CDATA_END;
} else {
return CDATA_BEGIN + v + CDATA_END;
}
}
}