Question

I have my own class extending org.apache.cocoon.serialization.AbstractSerializer (quite basic)

public class ExcelSerializer extends AbstractSerializer {

private static final XLogger LOG = XLoggerFactory.getXLogger(ExcelSerializer.class);

private ExcelSheetCreator excelSheetCreator;

public ExcelSerializer() {
    try {
        excelSheetCreator = new ExcelSheetCreator();
    } catch (IOException e) {
        LOG.error("ERROR", e.getMessage());
    }
}



@Override
public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException {
    excelSheetCreator.startElement(uri, loc, raw, a);
}

@Override
public void characters(char[] c, int start, int len) throws SAXException {
    excelSheetCreator.characters(c, start, len);
}

@Override
public void endElement(String uri, String loc, String raw) throws SAXException {
    excelSheetCreator.endElement(uri, loc, raw);
}


@Override
public void endDocument() throws SAXException {
    excelSheetCreator.setOutputStream(this.output);
    excelSheetCreator.endDocument();

}

}

For the first attempt everything is OK, I got the expected output. From the second one onwards, the class that I call from ExcelSerializer throws an IOException, because the stream has already been closed.

@Override
public void endDocument() throws SAXException {
    try {
        workbook.write(outputStream);
    } catch (IOException e) {
        System.out.println("workbook");
        System.out.println("" + e.getMessage());
    }
    workbook.dispose();
}

I certainly did not close the Outputstream or at least not knowingly. What can I do to keep it open?

Here is my sitemap:

 <map:serializer  name="excelSerializer" logger="sitemap.serializer.excelSerializer" src="com.acrys.excel.ExcelSerializer" 
           mime-type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
         </map:serializer>
Was it helpful?

Solution

It turned out that some internal outputstream of workbook was closed and not the one from class ExcelSerializer. Once workbook.dispose() is called the workbook is no longer available to do any work. And the solution is to create a new workbook on each call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top