JExcelApiExporter.java

Index Score
net.sf.jasperreports.engine.export
JasperReports

View: Reasons, Metrics, Source Code

These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.

MetricDescription
LOCLines of code
CYCLOMATICCyclomatic complexity
OPERANDSNumber of operands
ELOCEffective lines of code
PROGRAM_LENGTHHalstead program length
OPERATORSNumber of operators
EXITSProcedure exits
COMPARISONSNumber of comparison operators
LOGICAL_LINESNumber of statements
LINESNumber of lines in the source file
INTERFACE_COMPLEXITYInterface complexity
UNIQUE_OPERANDSNumber of unique operands
BLOCKSNumber of blocks
PARAMSNumber of formal parameter declarations
PROGRAM_VOCABHalstead program vocabulary
SIZESize of the file in bytes
RETURNSNumber of return points from functions
JAVA0145JAVA0145 Tab character used in source file
FUNCTIONSNumber of function declarations
JAVA0076JAVA0076 Use of magic number
JAVA0034JAVA0034 Missing braces in if statement
JAVA0117JAVA0117 Missing javadoc: method 'method'
JAVA0031JAVA0031 Case statement not properly closed
WHITESPACENumber of whitespace lines
JAVA0130JAVA0130 Non-static method does not use instance fields
JAVA0177JAVA0177 Variable declaration missing initializer
JAVA0166JAVA0166 Generic exception caught
JAVA0144JAVA0144 Line exceeds maximum M characters
JAVA0128JAVA0128 Public constructor in non-public class
JAVA0029JAVA0029 Private method not used
JAVA0270JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
UNIQUE_OPERATORSNumber of unique operators
EXEC_COMMENTSComments in executable code
LINE_COMMENTNumber of line comments
PROGRAM_VOLUMEHalstead program volume
JAVA0136JAVA0136 N methods defined in class (maximum: M)
JAVA0138JAVA0138 N parameters defined for method (maximum: M)
JAVA0126JAVA0126 Method declares unchecked exception in throws
JAVA0100JAVA0100 Class contains N non-final fields (maximum: M)
DOC_COMMENTNumber of javadoc comment lines
DECL_COMMENTSComments in declarations
NEST_DEPTHMaximum nesting depth
BLOCK_COMMENTNumber of block comment lines
/* * ============================================================================ * GNU Lesser General Public License * ============================================================================ * * JasperReports - Free Java report-generating library. * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation; either version 2.1 of the License, or (at your * option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * JasperSoft Corporation * 303 Second Street, Suite 450 North * San Francisco, CA 94107 * http://www.jaspersoft.com */ /* * Contributor: Manuel Paul <mpaul@ratundtat.com>, * Rat & Tat Beratungsgesellschaft mbH, * Muehlenkamp 6c, * 22303 Hamburg, * Germany. */ package net.sf.jasperreports.engine.export; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.Shape; import java.awt.geom.Dimension2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.Map; import jxl.CellView; import jxl.JXLException; import jxl.SheetSettings; import jxl.Workbook; import jxl.biff.DisplayFormat; import jxl.format.Alignment; import jxl.format.BoldStyle; import jxl.format.Border; import jxl.format.BorderLineStyle; import jxl.format.Colour; import jxl.format.Orientation; import jxl.format.PageOrientation; import jxl.format.PaperSize; import jxl.format.Pattern; import jxl.format.RGB; import jxl.format.UnderlineStyle; import jxl.format.VerticalAlignment; import jxl.write.Blank; import jxl.write.DateTime; import jxl.write.Formula; import jxl.write.Label; import jxl.write.Number; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableHyperlink; import jxl.write.WritableImage; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.CellValue; import jxl.write.biff.RowsExceededException; import net.sf.jasperreports.engine.JRAbstractExporter; import net.sf.jasperreports.engine.JRAlignment; import net.sf.jasperreports.engine.JRCommonGraphicElement; import net.sf.jasperreports.engine.JRElement; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRFont; import net.sf.jasperreports.engine.JRHyperlink; import net.sf.jasperreports.engine.JRImage; import net.sf.jasperreports.engine.JRImageRenderer; import net.sf.jasperreports.engine.JRLine; import net.sf.jasperreports.engine.JRLineBox; import net.sf.jasperreports.engine.JRPen; import net.sf.jasperreports.engine.JRPrintElement; import net.sf.jasperreports.engine.JRPrintFrame; import net.sf.jasperreports.engine.JRPrintGraphicElement; import net.sf.jasperreports.engine.JRPrintImage; import net.sf.jasperreports.engine.JRPrintLine; import net.sf.jasperreports.engine.JRPrintText; import net.sf.jasperreports.engine.JRRenderable; import net.sf.jasperreports.engine.JRReport; import net.sf.jasperreports.engine.JRTextElement; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.data.BooleanTextValue; import net.sf.jasperreports.engine.export.data.DateTextValue; import net.sf.jasperreports.engine.export.data.NumberTextValue; import net.sf.jasperreports.engine.export.data.StringTextValue; import net.sf.jasperreports.engine.export.data.TextValue; import net.sf.jasperreports.engine.export.data.TextValueHandler; import net.sf.jasperreports.engine.util.JRImageLoader; import net.sf.jasperreports.engine.util.JRStyledText; import org.apache.commons.collections.ReferenceMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * @author Manuel Paul (mpaul@ratundtat.com) * @version $Id: JExcelApiExporter.java 2253 2008-08-07 14:29:38Z teodord $ */ public class JExcelApiExporter extends JRXlsAbstractExporter { private static final Log log = LogFactory.getLog(JExcelApiExporter.class); protected static final Colour WHITE = Colour.WHITE; protected static final Colour BLACK = Colour.BLACK; protected static final String EMPTY_SHEET_NAME = "Sheet1"; private static Map colorsCache = new ReferenceMap(); private static Colour[] FIXED_COLOURS = new Colour[] {WHITE, BLACK, Colour.PALETTE_BLACK, Colour.DEFAULT_BACKGROUND, Colour.DEFAULT_BACKGROUND1, Colour.AUTOMATIC, Colour.UNKNOWN}; private Map loadedCellStyles = new HashMap(); private WritableWorkbook workbook = null; private WritableSheet sheet = null; private Pattern backgroundMode = Pattern.SOLID; private Map numberFormats; private Map dateFormats; protected Map formatPatternsMap = null; protected boolean createCustomPalette; protected Map workbookColours = new HashMap(); protected Map usedColours = new HashMap(); protected String password; protected ExporterNature nature = null; public JExcelApiExporter() { numberFormats = new HashMap(); dateFormats = new HashMap(); } protected void setParameters() { super.setParameters(); formatPatternsMap = (Map)getParameter(JRXlsExporterParameter.FORMAT_PATTERNS_MAP); createCustomPalette = getBooleanParameter( JExcelApiExporterParameter.CREATE_CUSTOM_PALETTE, JExcelApiExporterParameter.PROPERTY_CREATE_CUSTOM_PALETTE, false ); if (createCustomPalette) { initCustomPalette(); } password = getStringParameter( JExcelApiExporterParameter.PASSWORD, JExcelApiExporterParameter.PROPERTY_PASSWORD ); nature = new JExcelApiExporterNature(filter, isIgnoreGraphics, isIgnorePageMargins); } protected void initCustomPalette() { //mark "fixed" colours as always used for (int i = 0; i < FIXED_COLOURS.length; i++) { Colour colour = FIXED_COLOURS[i]; setColourUsed(colour); } } protected void setColourUsed(Colour colour) { usedColours.put(colour, colour.getDefaultRGB()); } protected void setColourUsed(Colour colour, Color reportColour) { if (log.isDebugEnabled()) { log.debug("Modifying palette colour " + colour.getValue() + " to " + reportColour); } int red = reportColour.getRed(); int green = reportColour.getGreen(); int blue = reportColour.getBlue(); workbook.setColourRGB(colour, red, green, blue); RGB customRGB = new RGB(red, green, blue); usedColours.put(colour, customRGB); } protected void setBackground() { if (isWhitePageBackground) { this.backgroundMode = Pattern.SOLID; } else { this.backgroundMode = Pattern.NONE; } } protected void openWorkbook(OutputStream os) throws JRException { try { workbook = Workbook.createWorkbook(os); } catch (IOException e) { throw new JRException("Error generating XLS report : " + jasperPrint.getName(), e); } } protected void createSheet(String name) { sheet = workbook.createSheet(name, Integer.MAX_VALUE); setSheetSettings(sheet); } protected void closeWorkbook(OutputStream os) throws JRException { if (sheet == null)//empty document { //creating an empty sheet so that write() doesn't fail workbook.createSheet(EMPTY_SHEET_NAME, Integer.MAX_VALUE); } try { workbook.write(); workbook.close(); } catch (IOException e) { throw new JRException("Error generating XLS report : " + jasperPrint.getName(), e); } catch (WriteException e) { throw new JRException("Error generating XLS report : " + jasperPrint.getName(), e); } } protected void setColumnWidth(int col, int width) { CellView cv = new CellView(); cv.setSize(width); sheet.setColumnView(col, cv); } protected void setRowHeight(int y, int lastRowHeight) throws JRException { try { sheet.setRowView(y, (lastRowHeight * 20)); // twips } catch (RowsExceededException e) { throw new JRException("Error generating XLS report : " + jasperPrint.getName(), e); } } protected void setCell(int x, int y) { } protected void removeColumn(int col) { sheet.removeColumn(col); } protected void addBlankCell(JRExporterGridCell gridCell, int colIndex, int rowIndex) throws JRException { Colour forecolor = BLACK; if (gridCell.getForecolor() != null) { forecolor = getWorkbookColour(gridCell.getForecolor()); } Pattern mode = backgroundMode; Colour backcolor = WHITE; if (gridCell.getCellBackcolor() != null) { mode = Pattern.SOLID; backcolor = getWorkbookColour(gridCell.getCellBackcolor()); } WritableFont cellFont = getLoadedFont(getDefaultFont(), forecolor.getValue()); WritableCellFormat cellStyle = getLoadedCellStyle( mode, backcolor, cellFont, gridCell ); try { sheet.addCell(new Blank(colIndex, rowIndex, cellStyle)); } catch (RowsExceededException e) { throw new JRException("Error generating XLS report : " + jasperPrint.getName(), e);//FIXMENOW raise same exception everywhere } catch (WriteException e) { throw new JRException("Error generating XLS report : " + jasperPrint.getName(), e); } } protected void exportLine(JRPrintLine line, JRExporterGridCell gridCell, int col, int row) throws JRException { addMergeRegion(gridCell, col, row); Colour forecolor2 = getWorkbookColour(line.getLinePen().getLineColor()); WritableFont cellFont2 = getLoadedFont(getDefaultFont(), forecolor2.getValue()); Colour backcolor = WHITE; Pattern mode = this.backgroundMode; if (gridCell.getCellBackcolor() != null) { mode = Pattern.SOLID; backcolor = getWorkbookColour(gridCell.getCellBackcolor()); } int side = BoxStyle.TOP; float ratio = line.getWidth() / line.getHeight(); if (ratio > 1) { if (line.getDirection() == JRLine.DIRECTION_TOP_DOWN) { side = BoxStyle.TOP; } else { side = BoxStyle.BOTTOM; } } else { if (line.getDirection() == JRLine.DIRECTION_TOP_DOWN) { side = BoxStyle.LEFT; } else { side = BoxStyle.RIGHT; } } BoxStyle boxStyle = new BoxStyle(side, line.getLinePen()); WritableCellFormat cellStyle2 = getLoadedCellStyle( mode, backcolor, cellFont2, boxStyle ); Blank cell2 = new Blank(col, row, cellStyle2); try { sheet.addCell(cell2); } catch (Exception e) { throw new JRException("Can't add cell.", e); } } protected void exportRectangle(JRPrintGraphicElement element, JRExporterGridCell gridCell, int col, int row) throws JRException { addMergeRegion(gridCell, col, row); Colour backcolor = WHITE; Pattern mode = this.backgroundMode; if (gridCell.getCellBackcolor() != null) { mode = Pattern.SOLID; backcolor = getWorkbookColour(gridCell.getCellBackcolor()); } Colour forecolor = getWorkbookColour(element.getLinePen().getLineColor()); WritableFont cellFont2 = getLoadedFont(getDefaultFont(), forecolor.getValue()); WritableCellFormat cellStyle2 = getLoadedCellStyle( mode, backcolor, cellFont2, gridCell ); Blank cell2 = new Blank(col, row, cellStyle2); try { sheet.addCell(cell2); } catch (Exception e) { throw new JRException("Can't add cell.", e); } } protected void exportText(JRPrintText text, JRExporterGridCell gridCell, int col, int row) throws JRException { addMergeRegion(gridCell, col, row); JRStyledText styledText = getStyledText(text); if (styledText != null) { Colour forecolor = getWorkbookColour(text.getForecolor()); WritableFont cellFont = this.getLoadedFont(text, forecolor.getValue()); TextAlignHolder alignment = getTextAlignHolder(text); int horizontalAlignment = getHorizontalAlignment(alignment); int verticalAlignment = getVerticalAlignment(alignment); int rotation = getRotation(alignment); Pattern mode = this.backgroundMode; Colour backcolor = WHITE; if (gridCell.getCellBackcolor() != null) { mode = Pattern.SOLID; backcolor = getWorkbookColour(gridCell.getCellBackcolor()); } StyleInfo baseStyle = new StyleInfo( mode, backcolor, horizontalAlignment, verticalAlignment, rotation, cellFont, gridCell ); String textStr = styledText.getText(); String href = null; JRHyperlinkProducer customHandler = getCustomHandler(text); if (customHandler == null) { switch (text.getHyperlinkType()) { case JRHyperlink.HYPERLINK_TYPE_REFERENCE: { href = text.getHyperlinkReference(); break; } case JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR : case JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE : case JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR : case JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE : case JRHyperlink.HYPERLINK_TYPE_NONE: default: { } } } else { href = customHandler.getHyperlink(text); } try { if (href != null) { try { URL url = new URL(href); WritableHyperlink hyperlink = new WritableHyperlink(col, row, col, row, url); sheet.addHyperlink(hyperlink); } catch (MalformedURLException e) { log.warn("Reference \"" + href + "\" could not be parsed as URL: " + e); } } addCell(col, row, text, textStr, baseStyle); } catch (Exception e) { throw new JRException("Can't add cell.", e); } } } protected void addCell(int x, int y, JRPrintText text, String textStr, StyleInfo baseStyle) throws WriteException, RowsExceededException, JRException { String textFormula = text.getPropertiesMap().getProperty(JRAbstractExporter.PROPERTY_CELL_FORMULA); if( textFormula != null) { Formula formula = null; try { formula = new Formula(x, y, textFormula, getLoadedCellStyle(baseStyle)); } catch(Exception e) { if(log.isWarnEnabled()) { log.warn(e.getMessage()); } } if(formula != null) { sheet.addCell(formula); return; } } CellValue cellValue; if (isDetectCellType) { cellValue = getDetectedCellValue(x, y, text, textStr, baseStyle); } else if (isAutoDetectCellType) { cellValue = getAutoDetectedCellValue(x, y, textStr, baseStyle); } else { cellValue = getLabelCell(x, y, textStr, baseStyle); } sheet.addCell(cellValue); } protected CellValue getDetectedCellValue(int x, int y, JRPrintText text, String textStr, StyleInfo baseStyle) throws JRException { TextValue textValue = getTextValue(text, textStr); CellTextValueHandler handler = new CellTextValueHandler(x, y, baseStyle); textValue.handle(handler); return handler.getResult(); } protected class CellTextValueHandler implements TextValueHandler { private final int x; private final int y; private final StyleInfo baseStyle; private CellValue result; public CellTextValueHandler(int x, int y, StyleInfo baseStyle) { this.x = x; this.y = y; this.baseStyle = baseStyle; } public void handle(StringTextValue textValue) throws JRException { WritableCellFormat cellStyle = getLoadedCellStyle(baseStyle); result = new Label(x, y, textValue.getText(), cellStyle); } public void handle(NumberTextValue textValue) throws JRException { if (textValue.getPattern() != null) { baseStyle.setDisplayFormat(getNumberFormat(textValue.getPattern())); } WritableCellFormat cellStyle = getLoadedCellStyle(baseStyle); if (textValue.getValue() == null) { result = blank(cellStyle); } else { result = new Number(x, y, textValue.getValue().doubleValue(), cellStyle); } } public void handle(DateTextValue textValue) throws JRException { baseStyle.setDisplayFormat(getDateFormat(textValue.getPattern())); WritableCellFormat cellStyle = getLoadedCellStyle(baseStyle); if (textValue.getValue() == null) { result = blank(cellStyle); } else { result = new DateTime(x, y, textValue.getValue(), cellStyle); } } public void handle(BooleanTextValue textValue) throws JRException { WritableCellFormat cellStyle = getLoadedCellStyle(baseStyle); if (textValue.getValue() == null) { result = blank(cellStyle); } else { result = new jxl.write.Boolean(x, y, textValue.getValue().booleanValue(), cellStyle); } } protected Blank blank(WritableCellFormat cellStyle) { return new Blank(x, y, cellStyle); } public CellValue getResult() { return result; } } protected jxl.write.NumberFormat getNumberFormat(String pattern) { String convertedPattern = getConvertedPattern(pattern); jxl.write.NumberFormat cellFormat = (jxl.write.NumberFormat) numberFormats.get(convertedPattern); if (cellFormat == null) { cellFormat = new jxl.write.NumberFormat(convertedPattern); numberFormats.put(convertedPattern, cellFormat); } return cellFormat; } protected jxl.write.DateFormat getDateFormat(String pattern) { String convertedPattern = getConvertedPattern(pattern); jxl.write.DateFormat cellFormat = (jxl.write.DateFormat) dateFormats.get(convertedPattern); if (cellFormat == null) { cellFormat = new jxl.write.DateFormat(convertedPattern); dateFormats.put(convertedPattern, cellFormat); } return cellFormat; } protected CellValue getAutoDetectedCellValue(int x, int y, String textStr, StyleInfo baseStyle) throws JRException { CellValue cellValue; try { double d = Double.parseDouble(textStr); WritableCellFormat cellStyle = getLoadedCellStyle(baseStyle); cellValue = new Number(x, y, d, cellStyle); } catch (NumberFormatException nfe) { cellValue = getLabelCell(x, y, textStr, baseStyle); } return cellValue; } protected CellValue getLabelCell(int x, int y, String textStr, StyleInfo baseStyle) throws JRException { WritableCellFormat cellStyle = getLoadedCellStyle(baseStyle); CellValue cellValue = new Label(x, y, textStr, cellStyle); return cellValue; } protected void addMergeRegion(JRExporterGridCell gridCell, int x, int y) throws JRException { if (gridCell.getColSpan() > 1 || gridCell.getRowSpan() > 1) { try { if (isCollapseRowSpan) { sheet.mergeCells(x, y, (x + gridCell.getColSpan() - 1), y); } else { sheet.mergeCells(x, y, (x + gridCell.getColSpan() - 1), (y + gridCell.getRowSpan() - 1)); } } catch (JXLException e) { throw new JRException("Can't merge cells.", e); } } } private int getHorizontalAlignment(TextAlignHolder alignment) { switch (alignment.horizontalAlignment) { case JRAlignment.HORIZONTAL_ALIGN_RIGHT: return Alignment.RIGHT.getValue(); case JRAlignment.HORIZONTAL_ALIGN_CENTER: return Alignment.CENTRE.getValue(); case JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED: return Alignment.JUSTIFY.getValue(); case JRAlignment.HORIZONTAL_ALIGN_LEFT: default: return Alignment.LEFT.getValue(); } } private int getVerticalAlignment(TextAlignHolder alignment) { switch (alignment.verticalAlignment) { case JRAlignment.VERTICAL_ALIGN_BOTTOM: return VerticalAlignment.BOTTOM.getValue(); case JRAlignment.VERTICAL_ALIGN_MIDDLE: return VerticalAlignment.CENTRE.getValue(); case JRAlignment.VERTICAL_ALIGN_JUSTIFIED: return VerticalAlignment.JUSTIFY.getValue(); case JRAlignment.VERTICAL_ALIGN_TOP: default: return VerticalAlignment.TOP.getValue(); } } private int getRotation(TextAlignHolder alignment) { switch (alignment.rotation) { case JRTextElement.ROTATION_LEFT: return Orientation.PLUS_90.getValue(); case JRTextElement.ROTATION_RIGHT: return Orientation.MINUS_90.getValue(); case JRTextElement.ROTATION_UPSIDE_DOWN: case JRTextElement.ROTATION_NONE: default: return Orientation.HORIZONTAL.getValue(); } } protected void exportImage(JRPrintImage element, JRExporterGridCell gridCell, int col, int row, int emptyCols) throws JRException { addMergeRegion(gridCell, col, row); int topPadding = Math.max(element.getLineBox().getTopPadding().intValue(), getImageBorderCorrection(element.getLineBox().getTopPen())); int leftPadding = Math.max(element.getLineBox().getLeftPadding().intValue(), getImageBorderCorrection(element.getLineBox().getLeftPen())); int bottomPadding = Math.max(element.getLineBox().getBottomPadding().intValue(), getImageBorderCorrection(element.getLineBox().getBottomPen())); int rightPadding = Math.max(element.getLineBox().getRightPadding().intValue(), getImageBorderCorrection(element.getLineBox().getRightPen())); int availableImageWidth = element.getWidth() - leftPadding - rightPadding; availableImageWidth = availableImageWidth < 0 ? 0 : availableImageWidth; int availableImageHeight = element.getHeight() - topPadding - bottomPadding; availableImageHeight = availableImageHeight < 0 ? 0 : availableImageHeight; JRRenderable renderer = element.getRenderer(); if ( renderer != null && availableImageWidth > 0 && availableImageHeight > 0 ) { if (renderer.getType() == JRRenderable.TYPE_IMAGE) { // Image renderers are all asked for their image data and dimension at some point. // Better to test and replace the renderer now, in case of lazy load error. renderer = JRImageRenderer.getOnErrorRendererForImageData(renderer, element.getOnErrorType()); if (renderer != null) { renderer = JRImageRenderer.getOnErrorRendererForDimension(renderer, element.getOnErrorType()); } } } else { renderer = null; } if (renderer != null) { int normalWidth = availableImageWidth; int normalHeight = availableImageHeight; Dimension2D dimension = renderer.getDimension(); if (dimension != null) { normalWidth = (int) dimension.getWidth(); normalHeight = (int) dimension.getHeight(); } float xalignFactor = 0f; switch (element.getHorizontalAlignment()) { case JRAlignment.HORIZONTAL_ALIGN_RIGHT: { xalignFactor = 1f; break; } case JRAlignment.HORIZONTAL_ALIGN_CENTER: { xalignFactor = 0.5f; break; } case JRAlignment.HORIZONTAL_ALIGN_LEFT: default: { xalignFactor = 0f; break; } } float yalignFactor = 0f; switch (element.getVerticalAlignment()) { case JRAlignment.VERTICAL_ALIGN_BOTTOM: { yalignFactor = 1f; break; } case JRAlignment.VERTICAL_ALIGN_MIDDLE: { yalignFactor = 0.5f; break; } case JRAlignment.VERTICAL_ALIGN_TOP: default: { yalignFactor = 0f; break; } } BufferedImage bi = new BufferedImage(element.getWidth(), element.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D grx = bi.createGraphics(); switch (element.getScaleImage()) { case JRImage.SCALE_IMAGE_CLIP: { int xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth)); int yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight)); Shape oldClipShape = grx.getClip(); grx.clip( new Rectangle( leftPadding, topPadding, availableImageWidth, availableImageHeight ) ); try { renderer.render( grx, new Rectangle( xoffset + leftPadding, yoffset + topPadding, normalWidth, normalHeight ) ); } finally { grx.setClip(oldClipShape); } break; } case JRImage.SCALE_IMAGE_FILL_FRAME: { renderer.render( grx, new Rectangle( leftPadding, topPadding, availableImageWidth, availableImageHeight ) ); break; } case JRImage.SCALE_IMAGE_RETAIN_SHAPE: default: { if (element.getHeight() > 0) { double ratio = (double) normalWidth / (double) normalHeight; if (ratio > (double) availableImageWidth / (double) availableImageHeight) { normalWidth = availableImageWidth; normalHeight = (int) (availableImageWidth / ratio); } else { normalWidth = (int) (availableImageHeight * ratio); normalHeight = availableImageHeight; } int xoffset = leftPadding + (int) (xalignFactor * (availableImageWidth - normalWidth)); int yoffset = topPadding + (int) (yalignFactor * (availableImageHeight - normalHeight)); renderer.render( grx, new Rectangle( xoffset, yoffset, normalWidth, normalHeight ) ); } break; } } Pattern mode = this.backgroundMode; Colour background = WHITE; if (gridCell.getCellBackcolor() != null) { mode = Pattern.SOLID; background = getWorkbookColour(gridCell.getCellBackcolor()); } if(element.getMode() == JRElement.MODE_OPAQUE ) { background = getWorkbookColour(element.getBackcolor()); } Colour forecolor = getWorkbookColour(element.getLineBox().getPen().getLineColor()); WritableFont cellFont2 = this.getLoadedFont(getDefaultFont(), forecolor.getValue()); WritableCellFormat cellStyle2 = getLoadedCellStyle( mode, background, cellFont2, gridCell ); try { sheet.addCell(new Blank(col, row, cellStyle2)); WritableImage image = new WritableImage( col - emptyCols, row, gridCell.getColSpan(), gridCell.getRowSpan(), JRImageLoader.loadImageDataFromAWTImage(bi, JRRenderable.IMAGE_TYPE_PNG) ); sheet.addImage(image); } catch (Exception ex) { throw new JRException("The cell cannot be added", ex); } catch (Error err) { throw new JRException("The cell cannot be added", err); } } } protected Colour getWorkbookColour(Color awtColor) { Colour colour; if (createCustomPalette) { colour = (Colour) workbookColours.get(awtColor); if (colour == null) { colour = determineWorkbookColour(awtColor); workbookColours.put(awtColor, colour); } } else { colour = getNearestColour(awtColor); } return colour; } protected Colour determineWorkbookColour(Color awtColor) { //nearest match int minDist = 999; Colour minColour = null; //nearest match among the available (not used) colours int minDistAvailable = 999; Colour minColourAvailable = null; Colour[] colors = Colour.getAllColours(); for (int i = 0; i < colors.length; i++) { Colour colour = colors[i]; RGB customRGB = (RGB) usedColours.get(colour); RGB rgb = customRGB == null ? colour.getDefaultRGB() : customRGB; int dist = rgbDistance(awtColor, rgb); if (dist < minDist) { minDist = dist; minColour = colour; } if (dist == 0)//exact match { break; } if (customRGB == null)//the colour is not used { if (dist < minDistAvailable) { minDistAvailable = dist; minColourAvailable = colour; } } } Colour workbookColour; if (minDist == 0)//exact match found { if (!usedColours.containsKey(minColour)) { //if the colour is not marked as used, mark it setColourUsed(minColour); } workbookColour = minColour; } else if (minColourAvailable == null)//all the colours are used { if (log.isWarnEnabled()) { log.warn("No more available colours in the palette. Using the nearest match for " + awtColor); } workbookColour = minColour; } else { //modifying the nearest available colour to the report colour setColourUsed(minColourAvailable, awtColor); workbookColour = minColourAvailable; } return workbookColour; } protected static Colour getNearestColour(Color awtColor) { Colour color = (Colour) colorsCache.get(awtColor); if (color == null) { Colour[] colors = Colour.getAllColours(); if ((colors != null) && (colors.length > 0)) { int minDiff = 999; for (int i = 0; i < colors.length; i++) { Colour crtColor = colors[i]; int diff = rgbDistance(awtColor, crtColor.getDefaultRGB()); if (diff < minDiff) { minDiff = diff; color = crtColor; } } } colorsCache.put(awtColor, color); } return color; } protected static int rgbDistance(Color awtColor, RGB rgb) { int diff = Math.abs(rgb.getRed() - awtColor.getRed()) + Math.abs(rgb.getGreen() - awtColor.getGreen()) + Math.abs(rgb.getBlue() - awtColor.getBlue()); return diff; } /*private static Colour getNearestColour(Color awtColor) { Colour retVal = null; Colour[] colors = Colour.getAllColours(); int diff = 50; if (colors != null && colors.length > 0 ){ Colour crtColor = null; for (int i = 0; i < colors.length; i++) { crtColor = colors[i]; int red = crtColor.getDefaultRGB().getRed(); if (Math.abs(awtColor.getRed() - red) < diff) { int green = crtColor.getDefaultRGB().getGreen(); if (Math.abs(awtColor.getGreen() - green) < diff) { int blue = crtColor.getDefaultRGB().getBlue(); if (Math.abs(awtColor.getBlue() - blue) < diff) { retVal = crtColor; } } } } } return retVal; }*/ private WritableFont getLoadedFont(JRFont font, int forecolor) throws JRException { WritableFont cellFont = null; if (this.loadedFonts != null && this.loadedFonts.size() > 0) { for (int i = 0; i < this.loadedFonts.size(); i++) { WritableFont cf = (WritableFont) this.loadedFonts.get(i); int fontSize = font.getFontSize(); if (isFontSizeFixEnabled) fontSize -= 1; String fontName = font.getFontName(); if (fontMap != null && fontMap.containsKey(fontName)) { fontName = (String) fontMap.get(fontName); } if ((cf.getName().equals(fontName)) && (cf.getColour().getValue() == forecolor) && (cf.getPointSize() == fontSize) && (cf.getUnderlineStyle() == UnderlineStyle.SINGLE ? (font.isUnderline()) : (!font.isUnderline())) && (cf.isStruckout() == font.isStrikeThrough()) && (cf.getBoldWeight() == BoldStyle.BOLD.getValue() ? (font.isBold()) : (!font.isBold())) && (cf.isItalic() == font.isItalic())) { cellFont = cf; break; } } } try { if (cellFont == null) { int fontSize = font.getFontSize(); if (isFontSizeFixEnabled) fontSize -= 1; String fontName = font.getFontName(); if (fontMap != null && fontMap.containsKey(fontName)) { fontName = (String) fontMap.get(fontName); } cellFont = new WritableFont( WritableFont.createFont(fontName), fontSize, font.isBold() ? WritableFont.BOLD : WritableFont.NO_BOLD, font.isItalic(), font.isUnderline() ? UnderlineStyle.SINGLE : UnderlineStyle.NO_UNDERLINE, Colour.getInternalColour(forecolor) ); cellFont.setStruckout(font.isStrikeThrough()); this.loadedFonts.add(cellFont); } } catch (Exception e) { throw new JRException("Can't get loaded fonts.", e); } return cellFont; } protected class BoxStyle { protected static final int TOP = 0; protected static final int LEFT = 1; protected static final int BOTTOM = 2; protected static final int RIGHT = 3; protected BorderLineStyle[] borderStyle = new BorderLineStyle[]{BorderLineStyle.NONE, BorderLineStyle.NONE, BorderLineStyle.NONE, BorderLineStyle.NONE}; protected Colour[] borderColour = new Colour[]{BLACK, BLACK, BLACK, BLACK}; private int hash; public BoxStyle(int side, JRPen pen) { borderStyle[side] = getBorderLineStyle(pen); borderColour[side] = getWorkbookColour(pen.getLineColor()); hash = computeHash(); } public BoxStyle(JRExporterGridCell gridCell) { JRLineBox lineBox = gridCell.getBox(); if (lineBox != null) setBox(lineBox); JRPrintElement element = gridCell.getElement(); if (element instanceof JRCommonGraphicElement) setPen(((JRCommonGraphicElement)element).getLinePen()); hash = computeHash(); } public void setBox(JRLineBox box) { borderStyle[TOP] = getBorderLineStyle(box.getTopPen()); borderColour[TOP] = getWorkbookColour(box.getTopPen().getLineColor()); borderStyle[BOTTOM] = getBorderLineStyle(box.getBottomPen()); borderColour[BOTTOM] = getWorkbookColour(box.getBottomPen().getLineColor()); borderStyle[LEFT] = getBorderLineStyle(box.getLeftPen()); borderColour[LEFT] = getWorkbookColour(box.getLeftPen().getLineColor()); borderStyle[RIGHT] = getBorderLineStyle(box.getRightPen()); borderColour[RIGHT] = getWorkbookColour(box.getRightPen().getLineColor()); hash = computeHash(); } public void setPen(JRPen pen) { if ( borderStyle[TOP] == BorderLineStyle.NONE && borderStyle[LEFT] == BorderLineStyle.NONE && borderStyle[BOTTOM] == BorderLineStyle.NONE && borderStyle[RIGHT] == BorderLineStyle.NONE ) { BorderLineStyle style = getBorderLineStyle(pen); Colour colour = getWorkbookColour(pen.getLineColor()); borderStyle[TOP] = style; borderStyle[LEFT] = style; borderStyle[BOTTOM] = style; borderStyle[RIGHT] = style; borderColour[TOP] = colour; borderColour[LEFT] = colour; borderColour[BOTTOM] = colour; borderColour[RIGHT] = colour; } hash = computeHash(); } private int computeHash() { int hashCode = borderStyle[TOP].hashCode(); hashCode = 31*hashCode + borderColour[TOP].hashCode(); hashCode = 31*hashCode + borderStyle[BOTTOM].hashCode(); hashCode = 31*hashCode + borderColour[BOTTOM].hashCode(); hashCode = 31*hashCode + borderStyle[LEFT].hashCode(); hashCode = 31*hashCode + borderColour[LEFT].hashCode(); hashCode = 31*hashCode + borderStyle[RIGHT].hashCode(); hashCode = 31*hashCode + borderColour[RIGHT].hashCode(); return hashCode; } public int hashCode() { return hash; } public boolean equals(Object o) { BoxStyle b = (BoxStyle) o; return b.borderStyle[TOP].equals(borderStyle[TOP]) && b.borderColour[TOP].equals(borderColour[TOP]) && b.borderStyle[LEFT].equals(borderStyle[LEFT]) && b.borderColour[LEFT].equals(borderColour[LEFT]) && b.borderStyle[BOTTOM].equals(borderStyle[BOTTOM]) && b.borderColour[BOTTOM].equals(borderColour[BOTTOM]) && b.borderStyle[RIGHT].equals(borderStyle[RIGHT]) && b.borderColour[RIGHT].equals(borderColour[RIGHT]); } public String toString() { return "(" + borderStyle[TOP].getValue() + "/" + borderColour[TOP].getValue() + "," + borderStyle[BOTTOM].getValue() + "/" + borderColour[BOTTOM].getValue() + "," + borderStyle[LEFT].getValue() + "/" + borderColour[LEFT].getValue() + "," + borderStyle[RIGHT].getValue() + "/" + borderColour[RIGHT].getValue() + ")"; } } protected class StyleInfo { protected final Pattern mode; protected final Colour backcolor; protected final int horizontalAlignment; protected final int verticalAlignment; protected final int rotation; protected final WritableFont font; protected final BoxStyle box; private DisplayFormat displayFormat; private int hashCode; protected StyleInfo( Pattern mode, Colour backcolor, int horizontalAlignment, int verticalAlignment, int rotation, WritableFont font, JRExporterGridCell gridCell ) { this( mode, backcolor, horizontalAlignment, verticalAlignment, rotation, font, new BoxStyle(gridCell) ); } protected StyleInfo( Pattern mode, Colour backcolor, int horizontalAlignment, int verticalAlignment, int rotation, WritableFont font, BoxStyle box ) { this.mode = mode; this.backcolor = backcolor; this.horizontalAlignment = horizontalAlignment; this.verticalAlignment = verticalAlignment; this.rotation = rotation; this.font = font; this.box = box; computeHash(); } protected void computeHash() { int hash = this.mode.hashCode(); hash = 31*hash + this.backcolor.hashCode(); hash = 31*hash + this.horizontalAlignment; hash = 31*hash + this.verticalAlignment; hash = 31*hash + this.rotation; hash = 31*hash + this.font.hashCode(); hash = 31*hash + (this.box == null ? 0 : this.box.hashCode()); hash = 31*hash + (this.displayFormat == null ? 0 : this.displayFormat.hashCode()); hashCode = hash; } public int hashCode() { return hashCode; } public boolean equals(Object o) { StyleInfo k = (StyleInfo) o; return k.mode.equals(mode) && k.backcolor.equals(backcolor) && k.horizontalAlignment == horizontalAlignment && k.verticalAlignment == verticalAlignment && k.rotation == rotation && k.font.equals(font) && (k.box == null ? box == null : (box != null && k.box.equals(box))) && (k.displayFormat == null ? displayFormat == null : (displayFormat!= null && k.displayFormat.equals(displayFormat))); } public DisplayFormat getDisplayFormat() { return displayFormat; } public void setDisplayFormat(DisplayFormat displayFormat) { this.displayFormat = displayFormat; computeHash(); } public String toString() { return "(" + mode + "," + backcolor + "," + horizontalAlignment + "," + verticalAlignment + "," + rotation + "," + font + "," + box + "," + displayFormat + ")"; } } private WritableCellFormat getLoadedCellStyle( Pattern mode, Colour backcolor, WritableFont font, JRExporterGridCell gridCell ) throws JRException { StyleInfo styleKey = new StyleInfo( mode, backcolor, Alignment.LEFT.getValue(), VerticalAlignment.TOP.getValue(), Orientation.HORIZONTAL.getValue(), font, gridCell ); return getLoadedCellStyle(styleKey); } private WritableCellFormat getLoadedCellStyle( Pattern mode, Colour backcolor, WritableFont font, BoxStyle box ) throws JRException { StyleInfo styleKey = new StyleInfo( mode, backcolor, Alignment.LEFT.getValue(), VerticalAlignment.TOP.getValue(), Orientation.HORIZONTAL.getValue(), font, box ); return getLoadedCellStyle(styleKey); } protected WritableCellFormat getLoadedCellStyle(StyleInfo styleKey) throws JRException { WritableCellFormat cellStyle = (WritableCellFormat) loadedCellStyles.get(styleKey); if (cellStyle == null) { try { if (styleKey.getDisplayFormat() == null) { cellStyle = new WritableCellFormat(styleKey.font); } else { cellStyle = new WritableCellFormat(styleKey.font, styleKey.getDisplayFormat()); } cellStyle.setBackground(styleKey.backcolor, styleKey.mode); cellStyle.setAlignment(Alignment.getAlignment(styleKey.horizontalAlignment)); cellStyle.setVerticalAlignment(VerticalAlignment.getAlignment(styleKey.verticalAlignment)); cellStyle.setOrientation(Orientation.getOrientation(styleKey.rotation)); cellStyle.setWrap(true); if (!isIgnoreCellBorder) { BoxStyle box = styleKey.box; cellStyle.setBorder(Border.TOP, box.borderStyle[BoxStyle.TOP], box.borderColour[BoxStyle.TOP]); cellStyle.setBorder(Border.BOTTOM, box.borderStyle[BoxStyle.BOTTOM], box.borderColour[BoxStyle.BOTTOM]); cellStyle.setBorder(Border.LEFT, box.borderStyle[BoxStyle.LEFT], box.borderColour[BoxStyle.LEFT]); cellStyle.setBorder(Border.RIGHT, box.borderStyle[BoxStyle.RIGHT], box.borderColour[BoxStyle.RIGHT]); } } catch (Exception e) { throw new JRException("Error setting cellFormat-template.", e); } loadedCellStyles.put(styleKey, cellStyle); } return cellStyle; } /** * */ protected static BorderLineStyle getBorderLineStyle(JRPen pen) { float lineWidth = pen.getLineWidth().floatValue(); if (lineWidth > 0f) { switch (pen.getLineStyle().byteValue()) { case JRPen.LINE_STYLE_DOUBLE : { return BorderLineStyle.DOUBLE; } case JRPen.LINE_STYLE_DOTTED : { return BorderLineStyle.DOTTED; } case JRPen.LINE_STYLE_DASHED : { return BorderLineStyle.DASHED; } case JRPen.LINE_STYLE_SOLID : default : { if (lineWidth >= 2f) { return BorderLineStyle.THICK; } else if (lineWidth >= 1f) { return BorderLineStyle.MEDIUM;//FIXMEBORDER there is also BorderLineStyle.MEDIUM_DASHED available } else if (lineWidth >= 0.5f) { return BorderLineStyle.THIN; } return BorderLineStyle.HAIR; } } } return BorderLineStyle.NONE; } private final void setSheetSettings(WritableSheet sheet) { PageOrientation po; PaperSize ps; if (jasperPrint.getOrientation() == JRReport.ORIENTATION_PORTRAIT) po = PageOrientation.PORTRAIT; else po = PageOrientation.LANDSCAPE; if ((ps = getSuitablePaperSize(jasperPrint)) != null) sheet.setPageSetup(po, ps, 0, 0); else sheet.setPageSetup(po); SheetSettings sheets = sheet.getSettings(); sheets.setTopMargin(0.0); sheets.setLeftMargin(0.0); sheets.setRightMargin(0.0); sheets.setBottomMargin(0.0); sheets.setHeaderMargin(0.0); sheets.setFooterMargin(0.0); if(password != null) { sheets.setPassword(password); sheets.setProtected(true); } } private final PaperSize getSuitablePaperSize(JasperPrint jasP) { if (jasP == null) return null; long width = 0; long height = 0; PaperSize ps = null; if ((jasP.getPageWidth() != 0) && (jasP.getPageHeight() != 0)) { double dWidth = (jasP.getPageWidth() / 72.0); double dHeight = (jasP.getPageHeight() / 72.0); height = Math.round(dHeight * 25.4); width = Math.round(dWidth * 25.4); // Compare to ISO 216 A-Series (A3-A5). All other ISO 216 formats // not supported by JExcelApi yet. for (int i = 3; i < 6; i++) { int w = calculateWidthForDinAN(i); int h = calculateHeightForDinAN(i); if (((w == width) && (h == height)) || ((h == width) && (w == height))) { if (i == 3) ps = PaperSize.A3; else if (i == 4) ps = PaperSize.A4; else if (i == 5) ps = PaperSize.A5; break; } } // Compare to common North American Paper Sizes (ANSI X3.151-1987). if (ps == null) { // ANSI X3.151-1987 - "Letter" (216 ÔøΩ 279 mm) if (((width == 216) && (height == 279)) || ((width == 279) && (height == 216))) { ps = PaperSize.LETTER; } // ANSI X3.151-1987 - "Legal" (216 ÔøΩ 356 mm) if (((width == 216) && (height == 356)) || ((width == 356) && (height == 216))) { ps = PaperSize.LEGAL; } // ANSI X3.151-1987 - "Executive" (190 ÔøΩ 254 mm) // Not supperted by JExcelApi yet. // ANSI X3.151-1987 - "Ledger/Tabloid" (279 ÔøΩ 432 mm) // Not supperted by JExcelApi yet. } } return ps; } public static TextAlignHolder getTextAlignHolder(JRPrintText textElement) { short horizontalAlignment; short verticalAlignment; short rotation = textElement.getRotation(); switch (textElement.getRotation()) { case JRTextElement.ROTATION_LEFT : { switch (textElement.getHorizontalAlignment()) { case JRAlignment.HORIZONTAL_ALIGN_LEFT : { verticalAlignment = JRAlignment.VERTICAL_ALIGN_BOTTOM; break; } case JRAlignment.HORIZONTAL_ALIGN_CENTER : { verticalAlignment = JRAlignment.VERTICAL_ALIGN_MIDDLE; break; } case JRAlignment.HORIZONTAL_ALIGN_RIGHT : { verticalAlignment = JRAlignment.VERTICAL_ALIGN_TOP; break; } case JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED : { verticalAlignment = JRAlignment.VERTICAL_ALIGN_JUSTIFIED; break; } default : { verticalAlignment = JRAlignment.VERTICAL_ALIGN_BOTTOM; } } switch (textElement.getVerticalAlignment()) { case JRAlignment.VERTICAL_ALIGN_TOP : { horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_LEFT; break; } case JRAlignment.VERTICAL_ALIGN_MIDDLE : { horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_CENTER; break; } case JRAlignment.VERTICAL_ALIGN_BOTTOM : { horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_RIGHT; break; } default : { horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_LEFT; } } break; } case JRTextElement.ROTATION_RIGHT : { switch (textElement.getHorizontalAlignment()) { case JRAlignment.HORIZONTAL_ALIGN_LEFT : { verticalAlignment = JRAlignment.VERTICAL_ALIGN_TOP; break; } case JRAlignment.HORIZONTAL_ALIGN_CENTER : { verticalAlignment = JRAlignment.VERTICAL_ALIGN_MIDDLE; break; } case JRAlignment.HORIZONTAL_ALIGN_RIGHT : { verticalAlignment = JRAlignment.VERTICAL_ALIGN_BOTTOM; break; } case JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED : { verticalAlignment = JRAlignment.VERTICAL_ALIGN_JUSTIFIED; break; } default : { verticalAlignment = JRAlignment.VERTICAL_ALIGN_TOP; } } switch (textElement.getVerticalAlignment()) { case JRAlignment.VERTICAL_ALIGN_TOP : { horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_RIGHT; break; } case JRAlignment.VERTICAL_ALIGN_MIDDLE : { horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_CENTER; break; } case JRAlignment.VERTICAL_ALIGN_BOTTOM : { horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_LEFT; break; } default : { horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_RIGHT; } } break; } case JRTextElement.ROTATION_UPSIDE_DOWN: case JRTextElement.ROTATION_NONE : default : { horizontalAlignment = textElement.getHorizontalAlignment(); verticalAlignment = textElement.getVerticalAlignment(); } } return new TextAlignHolder(horizontalAlignment, verticalAlignment, rotation); } // Berechnungsvorschriften fÔøΩr die DIN Formate A, B, und C. // Die Angabe der Breite/HÔøΩhe erfolgt in [mm]. private final int calculateWidthForDinAN(int n) { return (int) (Math.pow(2.0, (-0.25 - (n / 2.0))) * 1000.0); } private final int calculateHeightForDinAN(int n) { return (int) (Math.pow(2.0, (0.25 - (n / 2.0))) * 1000.0); } private final int calculateWidthForDinBN(int n) { return (int) (Math.pow(2.0, -(n / 2.0)) * 1000.0); } private final int calculateHeightForDinBN(int n) { return (int) (Math.pow(2.0, (0.5 - (n / 2.0))) * 1000.0); } private final int calculateWidthForDinCN(int n) { return (int) (Math.pow(2.0, (-0.125 - (n / 2.0))) * 1000.0); } private final int calculateHeightForDinCN(int n) { return (int) (Math.pow(2.0, (0.375 - (n / 2.0))) * 1000.0); } protected void exportFrame(JRPrintFrame frame, JRExporterGridCell gridCell, int col, int row) throws JRException { addMergeRegion(gridCell, col, row); Colour forecolor = getWorkbookColour(frame.getForecolor()); Colour backcolor = WHITE; Pattern mode = backgroundMode; if (frame.getMode() == JRElement.MODE_OPAQUE) { mode = Pattern.SOLID; backcolor = getWorkbookColour(frame.getBackcolor()); } WritableFont cellFont = getLoadedFont(getDefaultFont(), forecolor.getValue()); WritableCellFormat cellStyle = getLoadedCellStyle( mode, backcolor, cellFont, gridCell ); Blank cell = new Blank(col, row, cellStyle); try { sheet.addCell(cell); } catch (JXLException e) { throw new JRException("Can't add cell.", e); } } /** * This method is intended to modify a given format pattern so to include * only the accepted proprietary format characters. The resulted pattern * will possibly truncate the original pattern * @param pattern * @return pattern converted to accepted proprietary formats */ private String getConvertedPattern(String pattern) { if (formatPatternsMap != null && formatPatternsMap.containsKey(pattern)) { return (String) formatPatternsMap.get(pattern); } return pattern; } protected ExporterNature getNature() { return nature; } }

The table below shows all metrics for JExcelApiExporter.java.

MetricValueDescription
BLOCKS234.00Number of blocks
BLOCK_COMMENT60.00Number of block comment lines
COMMENTS93.00Comment lines
COMMENT_DENSITY 0.09Comment density
COMPARISONS187.00Number of comparison operators
CYCLOMATIC280.00Cyclomatic complexity
DECL_COMMENTS 7.00Comments in declarations
DOC_COMMENT14.00Number of javadoc comment lines
ELOC1060.00Effective lines of code
EXEC_COMMENTS13.00Comments in executable code
EXITS197.00Procedure exits
FUNCTIONS72.00Number of function declarations
HALSTEAD_DIFFICULTY119.53Halstead difficulty
HALSTEAD_EFFORT 0.00Halstead effort
INTERFACE_COMPLEXITY258.00Interface complexity
JAVA0001 0.00JAVA0001 Package name does not contain only lower case letters
JAVA0002 0.00JAVA0002 Package name does not begin with a top level domain name or country code
JAVA0003 0.00JAVA0003 Minimize use of on-demand (.*) imports
JAVA0004 0.00JAVA0004 Unnecessary import from java.lang
JAVA0005 1.00JAVA0005 Imports not in specified order
JAVA0006 0.00JAVA0006 Empty finally block
JAVA0007 0.00JAVA0007 Should not declare public field
JAVA0008 0.00JAVA0008 Empty catch block
JAVA0009 0.00JAVA0009 Protected member in final class
JAVA0010 0.00JAVA0010 Non-instantiable class does not contain a non-private static member
JAVA0011 0.00JAVA0011 Abstract class does not contain an abstract method
JAVA0012 0.00JAVA0012 Non-constructor method with same name as declaring class
JAVA0013 0.00JAVA0013 Non-blank final field is not static
JAVA0014 0.00JAVA0014 Class with only static members has non-private constructor
JAVA0015 0.00JAVA0015 Package class contains public nested type
JAVA0016 0.00JAVA0016 Abstract class contains public constructor
JAVA0017 0.00JAVA0017 Class name does not have required form
JAVA0018 0.00JAVA0018 Method name does not have required form
JAVA0019 0.00JAVA0019 Interface name does not have required form
JAVA0020 0.00JAVA0020 Field name does not have required form
JAVA0021 0.00JAVA0021 Interface method name does not have required form
JAVA0022 0.00JAVA0022 Static final field name does not have required form
JAVA0023 0.00JAVA0023 Empty finalize method
JAVA0024 0.00JAVA0024 Empty class
JAVA0025 0.00JAVA0025 Method override is empty
JAVA0026 0.00JAVA0026 Finalize method with parameters
JAVA0029 4.00JAVA0029 Private method not used
JAVA0030 0.00JAVA0030 Private field not used
JAVA0031 6.00JAVA0031 Case statement not properly closed
JAVA0032 0.00JAVA0032 Switch statement missing default
JAVA0033 0.00JAVA0033 default: not last case in switch statement
JAVA003412.00JAVA0034 Missing braces in if statement
JAVA0035 0.00JAVA0035 Missing braces in for statement
JAVA0036 0.00JAVA0036 Missing braces in while statement
JAVA0038 0.00JAVA0038 Non-case label in switch statement
JAVA0039 0.00JAVA0039 Break statement with label
JAVA0040 0.00JAVA0040 Switch statement contains N cases (maximum: M)
JAVA0041 0.00JAVA0041 Nested synchronized block
JAVA0042 0.00JAVA0042 Empty synchronized statement
JAVA0043 0.00JAVA0043 Inner class does not use outer class
JAVA0044 0.00JAVA0044 Serializable class with no instance variables
JAVA0045 0.00JAVA0045 Serializable class with only transient fields
JAVA0046 0.00JAVA0046 Name of class not derived from Exception ends with 'Exception'
JAVA0047 0.00JAVA0047 Serializable class derives from invalid base class
JAVA0048 0.00JAVA0048 Name of class derived from Exception does not end with 'Exception'
JAVA0049 0.00JAVA0049 Nested block at depth N (maximum: M)
JAVA0050 0.00JAVA0050 Class derives from java.lang.Error
JAVA0051 0.00JAVA0051 Class derives from java.lang.RuntimeException
JAVA0052 0.00JAVA0052 Class derives from java.lang.Throwable
JAVA0053 0.00JAVA0053 Unused label
JAVA0054 0.00JAVA0054 Inheritance depth N exceeds maximum M
JAVA0055 0.00JAVA0055 Class should be interface
JAVA0056 0.00JAVA0056 Unnecessary abstract modifier for interface or annotation
JAVA0057 0.00JAVA0057 Unnecessary default constructor
JAVA0058 0.00JAVA0058 Constructor calls super()
JAVA0059 0.00JAVA0059 Method override only calls super()
JAVA0061 0.00JAVA0061 Inaccessible member in anonymous class
JAVA0062 0.00JAVA0062 Public class missing public member or protected constructor
JAVA0063 0.00JAVA0063 Identifier name should not contain '$'
JAVA0064 0.00JAVA0064 N variations of identifier name (maximum: M)
JAVA0065 0.00JAVA0065 Unnecessary final modifier for method in final class
JAVA0066 0.00JAVA0066 Unnecessary modifier for interface nested type
JAVA0067 0.00JAVA0067 Array descriptor on identifier name
JAVA0068 0.00JAVA0068 Modifiers not declared in recommended order
JAVA0071 0.00JAVA0071 Strings compared with ==
JAVA0073 1.00JAVA0073 Integer division in floating-point context
JAVA0074 0.00JAVA0074 Use of Object.notify()
JAVA0075 0.00JAVA0075 Method parameter hides field
JAVA007640.00JAVA0076 Use of magic number
JAVA0077 0.00JAVA0077 Private field not used in declaring class
JAVA0078 0.00JAVA0078 Floating point values compared with ==
JAVA0079 0.00JAVA0079 Use of instance to reference static member
JAVA0080 0.00JAVA0080 Import declaration not used
JAVA0081 0.00JAVA0081 Boolean literal in comparison
JAVA0082 0.00JAVA0082 Unnecessary widening cast
JAVA0083 0.00JAVA0083 Unnecessary instanceof test
JAVA0084 0.00JAVA0084 Should use compound assignment operator
JAVA0085 0.00JAVA0085 Use of sun.* class
JAVA0087 0.00JAVA0087 Use of Thread.sleep()
JAVA0089 0.00JAVA0089 Use of restricted package
JAVA0092 0.00JAVA0092 Use of restricted type
JAVA0093 0.00JAVA0093 Redundant assignment
JAVA0094 0.00JAVA0094 Field hides a superclass field
JAVA0095 0.00JAVA0095 Uninitialized private field
JAVA0096 0.00JAVA0096 Field in nested class hides outer field
JAVA0098 0.00JAVA0098 Minimize use of implicit field initializers
JAVA0100 1.00JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0101 0.00JAVA0101 Unnecessary modifier for field in interface
JAVA0102 0.00JAVA0102 Last statement in finalize() not super.finalize()
JAVA0103 0.00JAVA0103 Explicit call to finalize()
JAVA0104 0.00JAVA0104 finalize() only calls super.finalize()
JAVA0105 0.00JAVA0105 Duplicate import declaration
JAVA0106 0.00JAVA0106 Unnecessary import from current package
JAVA0108 1.00JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0109 0.00JAVA0109 Incorrect javadoc: no parameter 'parameter'
JAVA0110 1.00JAVA0110 Incorrect javadoc: no @return tag
JAVA0111 0.00JAVA0111 Incorrect javadoc: @return tag for void method
JAVA0112 0.00JAVA0112 Incorrect javadoc: no exception 'exception' in throws
JAVA0113 0.00JAVA0113 Incorrect javadoc: no @author tag
JAVA0114 0.00JAVA0114 Incorrect javadoc: no @version tag
JAVA0115 0.00JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA0116 0.00JAVA0116 Missing javadoc: field 'field'
JAVA011729.00JAVA0117 Missing javadoc: method 'method'
JAVA0118 0.00JAVA0118 Missing javadoc: type 'type'
JAVA0119 0.00JAVA0119 Control variable changed within body of for loop
JAVA0123 0.00JAVA0123 Use all three components of for loop
JAVA0125 0.00JAVA0125 Continue statement with label
JAVA0126 0.00JAVA0126 Method declares unchecked exception in throws
JAVA0128 3.00JAVA0128 Public constructor in non-public class
JAVA0130 9.00JAVA0130 Non-static method does not use instance fields
JAVA0131 0.00JAVA0131 Compatible method does not override base
JAVA0132 0.00JAVA0132 Method overload with compatible signature
JAVA0133 0.00JAVA0133 Non-synchronized method overrides synchronized method
JAVA0135 0.00JAVA0135 Only one of Object.equals and Object.hashCode defined: missing 'method'
JAVA0136 1.00JAVA0136 N methods defined in class (maximum: M)
JAVA0137 0.00JAVA0137 Non-abstract class missing constructor
JAVA0138 2.00JAVA0138 N parameters defined for method (maximum: M)
JAVA0139 0.00JAVA0139 Definition of main other than public static void main(java.lang.String[])
JAVA0141 0.00JAVA0141 Unnecessary modifier for method in interface
JAVA0143 0.00JAVA0143 Synchronized method
JAVA0144 5.00JAVA0144 Line exceeds maximum M characters
JAVA01454871.00JAVA0145 Tab character used in source file
JAVA0150 0.00JAVA0150 java.lang.Error (or subclass) thrown
JAVA0153 0.00JAVA0153 Inefficient conversion of integer to string
JAVA0159 0.00JAVA0159 Inefficient conversion of string to integer
JAVA0160 0.00JAVA0160 Method does not throw specified exception
JAVA0161 0.00JAVA0161 Conditional wait() not in loop
JAVA0163 0.00JAVA0163 Empty statement
JAVA0165 0.00JAVA0165 Conflicting return statement in finally block
JAVA0166 8.00JAVA0166 Generic exception caught
JAVA0167 0.00JAVA0167 ThreadDeath not rethrown
JAVA0169 0.00JAVA0169 Unnecessary catch block: exception 'exception'
JAVA0170 1.00JAVA0170 Caught exception not derived from java.lang.Exception
JAVA0171 0.00JAVA0171 Unused local variable
JAVA0173 0.00JAVA0173 Unused method parameter
JAVA0174 0.00JAVA0174 Assigned local variable never used
JAVA0175 0.00JAVA0175 Successive assignment to variable
JAVA0176 0.00JAVA0176 Local variable name does not have required form
JAVA0177 8.00JAVA0177 Variable declaration missing initializer
JAVA0179 0.00JAVA0179 Local variable hides visible field
JAVA0233 0.00JAVA0233 Definition of serialVersionUID other than 'private static final long serialVersionUID'
JAVA0234 0.00JAVA0234 Class is Serializable but does not define serialVersionUID
JAVA0235 0.00JAVA0235 Class defines serialVersionUID but does not implement Serializable
JAVA0236 0.00JAVA0236 Attempt to clone an object which does not implement Cloneable
JAVA0237 0.00JAVA0237 Class implements Cloneable but does not have public clone method
JAVA0238 0.00JAVA0238 Clone method does not call super.clone()
JAVA0239 0.00JAVA0239 Class declares 'readObject' or 'writeObject' but does not implement Serializable
JAVA0240 0.00JAVA0240 Serializable class which declares readObject or writeObject but not both
JAVA0241 0.00JAVA0241 'readObject' or 'writeObject' should be declared private in Serializable class
JAVA0242 0.00JAVA0242 Transient field in non-Serializable class
JAVA0243 0.00JAVA0243 'readResolve' or 'writeReplace' should be declared private or protected
JAVA0244 0.00JAVA0244 Field or method name in subclass differs only by case from inherited field or method
JAVA0245 0.00JAVA0245 JUnit TestCase with non-trivial constructor
JAVA0246 0.00JAVA0246 JUnit assertXXX statement missing message parameter
JAVA0247 0.00JAVA0247 JUnit 'setUp()' and 'tearDown()' should call super method
JAVA0248 0.00JAVA0248 JUnit method 'setUp' or 'tearDown' with incorrect signature
JAVA0249 0.00JAVA0249 JUnit TestCase 'suite()' should be declared static
JAVA0250 0.00JAVA0250 JUnit TestCase declares testXXX method with incorrect signature
JAVA0251 0.00JAVA0251 Use '%n' for line breaks in printf/format for platform independence
JAVA0252 0.00JAVA0252 'enum' is a Java 1.5 reserved word
JAVA0253 0.00JAVA0253 Not all enum constants consumed in switch statement
JAVA0254 0.00JAVA0254 Use enhanced for loop construct instead of Iterator
JAVA0255 0.00JAVA0255 Result of method invocation not used
JAVA0256 0.00JAVA0256 Assignment of external collection/array to field
JAVA0257 0.00JAVA0257 Use of 'Constant Interface' anti-pattern
JAVA0258 0.00JAVA0258 Implement Iterable for foreach compatibility
JAVA0259 0.00JAVA0259 Return of collection/array field
JAVA0260 0.00JAVA0260 Use 'enum' instead of Enumerated Type pattern
JAVA0261 0.00JAVA0261 Use specialized Enum collection types
JAVA0262 0.00JAVA0262 Use of char in integer context
JAVA0263 0.00JAVA0263 Long literal ends with 'l' instead of 'L'
JAVA0264 0.00JAVA0264 Integer math in long context - check for overflow
JAVA0265 0.00JAVA0265 Use of Throwable.printStackTrace()
JAVA0266 0.00JAVA0266 Use of System.out
JAVA0267 0.00JAVA0267 Use of System.err
JAVA0269 0.00JAVA0269 Contents of StringBuffer never used
JAVA0270 3.00JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
JAVA0271 0.00JAVA0271 Minimize use of on-demand (.*) static imports
JAVA0272 0.00JAVA0272 Thread.run() called
JAVA0273 0.00JAVA0273 Non-final derivative of Thread calls start() in constructor
JAVA0274 0.00JAVA0274 Serializable class has a synchronized readObject()
JAVA0275 0.00JAVA0275 Serializable class has a synchronized writeObject() and no other synchronized methods
JAVA0276 0.00JAVA0276 Unnecessary use of String constructor
JAVA0277 0.00JAVA0277 Iterator.next() implementation does not throw NoSuchElementException
JAVA0278 0.00JAVA0278 Unnecessary use of Boolean constructor
JAVA0279 0.00JAVA0279 Serialization method readObject or readObjectNoData calls an overridable method
JAVA0280 0.00JAVA0280 IllegalMonitorStateException caught
JAVA0281 0.00JAVA0281 Iterator.next() not called in loop
JAVA0282 0.00JAVA0282 Call to Iterator.next() in loop which does not test Iterator.hasNext()
JAVA0283 0.00JAVA0283 Control variable not updated in loop body
JAVA0284 0.00JAVA0284 Explicit garbage collection
JAVA0285 0.00JAVA0285 Dereference of potentially null variable
JAVA0286 0.00JAVA0286 Dereference of null variable
JAVA0287 0.00JAVA0287 Unnecessary null check
JAVA0288 0.00JAVA0288 Inconsistent null check
LINES1900.00Number of lines in the source file