Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.fesod.sheet.converters.byteconverter.ByteBooleanConverter;
import org.apache.fesod.sheet.converters.byteconverter.ByteNumberConverter;
import org.apache.fesod.sheet.converters.byteconverter.ByteStringConverter;
import org.apache.fesod.sheet.converters.clob.ClobStringConverter;
import org.apache.fesod.sheet.converters.date.DateDateConverter;
import org.apache.fesod.sheet.converters.date.DateNumberConverter;
import org.apache.fesod.sheet.converters.date.DateStringConverter;
Expand Down Expand Up @@ -111,6 +112,8 @@ private static void initAllConverter() {
putAllConverter(new ByteNumberConverter());
putAllConverter(new ByteStringConverter());

putAllConverter(new ClobStringConverter());

putAllConverter(new DateNumberConverter());
putAllConverter(new DateStringConverter());

Expand Down Expand Up @@ -166,6 +169,7 @@ private static void initDefaultWriteConverter() {
putWriteConverter(new LongNumberConverter());
putWriteConverter(new ShortNumberConverter());
putWriteConverter(new StringStringConverter());
putWriteConverter(new ClobStringConverter());
putWriteConverter(new FileImageConverter());
putWriteConverter(new InputStreamImageConverter());
putWriteConverter(new ByteArrayImageConverter());
Expand All @@ -187,6 +191,7 @@ private static void initDefaultWriteConverter() {
putWriteStringConverter(new LongStringConverter());
putWriteStringConverter(new ShortStringConverter());
putWriteStringConverter(new StringStringConverter());
putWriteStringConverter(new ClobStringConverter());
defaultWriteConverter = Collections.unmodifiableMap(defaultWriteConverter);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Licensed to the Apache Software Foundation (ASF) 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.apache.fesod.sheet.converters.clob;

import java.io.Reader;
import java.sql.Clob;
import javax.sql.rowset.serial.SerialClob;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;

/**
* Clob and string converter. Clob data is represented as a string cell; reads return a
* {@link SerialClob} so callers receive a usable, detached JDBC Clob value.
*/
public class ClobStringConverter implements Converter<Clob> {
@Override
public Class<?> supportJavaTypeKey() {
return Clob.class;
}

@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}

@Override
public Clob convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)
throws Exception {
// Materialize cell text into a detached JDBC Clob, avoiding a database-specific Clob implementation.
return new SerialClob(cellData.getStringValue().toCharArray());
}

@Override
public WriteCellData<?> convertToExcelData(
Clob value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)
throws Exception {
StringBuilder text = new StringBuilder();
char[] buffer = new char[4096];
try (Reader reader = value.getCharacterStream()) {
int count;
while ((count = reader.read(buffer)) != -1) {
text.append(buffer, 0, count);
}
}
return new WriteCellData<>(text.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

package org.apache.fesod.sheet.converters;

import java.sql.Clob;
import java.time.LocalTime;
import java.util.Map;
import org.apache.fesod.sheet.converters.ConverterKeyBuild.ConverterKey;
import org.apache.fesod.sheet.converters.clob.ClobStringConverter;
import org.apache.fesod.sheet.converters.localtime.LocalTimeDateConverter;
import org.apache.fesod.sheet.converters.localtime.LocalTimeNumberConverter;
import org.apache.fesod.sheet.converters.localtime.LocalTimeStringConverter;
Expand Down Expand Up @@ -67,6 +69,21 @@ void loadConvertersRegistersLocalTimeFamily() {
writeConverter.get(ConverterKeyBuild.buildKey(LocalTime.class, CellDataTypeEnum.STRING)));
}

@Test
void loadConvertersRegistersClobStringConverter() {
Map<ConverterKey, Converter<?>> allConverter = DefaultConverterLoader.loadAllConverter();
Assertions.assertInstanceOf(
ClobStringConverter.class,
allConverter.get(ConverterKeyBuild.buildKey(Clob.class, CellDataTypeEnum.STRING)));

Map<ConverterKey, Converter<?>> writeConverter = DefaultConverterLoader.loadDefaultWriteConverter();
Assertions.assertInstanceOf(
ClobStringConverter.class, writeConverter.get(ConverterKeyBuild.buildKey(Clob.class)));
Assertions.assertInstanceOf(
ClobStringConverter.class,
writeConverter.get(ConverterKeyBuild.buildKey(Clob.class, CellDataTypeEnum.STRING)));
}

private static void assertLoadIsImmutableAndCopyIsMutable(
Map<ConverterKey, Converter<?>> loaded, Map<ConverterKey, Converter<?>> copy) {
Map.Entry<ConverterKey, Converter<?>> entry =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) 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.apache.fesod.sheet.converters.clob;

import java.sql.Clob;
import javax.sql.rowset.serial.SerialClob;
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.testkit.Tags;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag(Tags.UNIT)
class ClobStringConverterTest {
private static final GlobalConfiguration GLOBAL_CONFIGURATION = new GlobalConfiguration();
private final ClobStringConverter converter = new ClobStringConverter();

@Test
void supportKeys() {
Assertions.assertEquals(Clob.class, converter.supportJavaTypeKey());
Assertions.assertEquals(CellDataTypeEnum.STRING, converter.supportExcelTypeKey());
}

@Test
void convertsCellStringToDetachedClob() throws Exception {
Clob actual = converter.convertToJavaData(new ReadCellData<>("Large text 字符"), null, GLOBAL_CONFIGURATION);

Assertions.assertEquals("Large text 字符", actual.getSubString(1, (int) actual.length()));
}

@Test
void convertsClobCharacterStreamToCellString() throws Exception {

@nkuprins nkuprins Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test gap on the write path? convertsClobCharacterStreamToCellString uses a 13-char value, so the 4096-char read loop only runs once. Changing while to if (so only the first chunk is kept) still passes all three tests. Using the cell text limit as the length covers several chunks and delei's boundary at the same time:

@Test
void convertsClobAtCellTextLimitToCellString() throws Exception {
    StringBuilder expected = new StringBuilder();
    for (int i = 0; i < 32767; i++) {
        expected.append((char) ('a' + i % 26));
    }
    Clob value = new SerialClob(expected.toString().toCharArray());

    WriteCellData<?> written = converter.convertToExcelData(value, null, GLOBAL_CONFIGURATION);

    Assertions.assertEquals(expected.toString(), written.getStringValue());
}

Clob value = new SerialClob("Large text 字符".toCharArray());

WriteCellData<?> written = converter.convertToExcelData(value, null, GLOBAL_CONFIGURATION);

Assertions.assertEquals(CellDataTypeEnum.STRING, written.getType());
Assertions.assertEquals("Large text 字符", written.getStringValue());
}
}