Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2014 TIBCO Software Inc. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports 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 3 of the License, or
* (at your option) any later version.
*
* JasperReports 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 JasperReports. If not, see .
*/
package net.sf.jasperreports.engine.export.ooxml;
import java.awt.Color;
import java.awt.font.TextAttribute;
import java.io.Writer;
import java.text.AttributedCharacterIterator.Attribute;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;
import net.sf.jasperreports.engine.JRDefaultStyleProvider;
import net.sf.jasperreports.engine.JRPrintText;
import net.sf.jasperreports.engine.JRStyle;
import net.sf.jasperreports.engine.JasperReportsContext;
import net.sf.jasperreports.engine.base.JRBasePrintText;
import net.sf.jasperreports.engine.type.ColorEnum;
import net.sf.jasperreports.engine.type.ModeEnum;
import net.sf.jasperreports.engine.util.JRColorUtil;
import net.sf.jasperreports.engine.util.JRStringUtil;
/**
* @author sanda zaharia ([email protected])
*/
public class DocxRunHelper extends BaseHelper
{
/**
*
*/
private String exporterKey;
/**
*
*/
public DocxRunHelper(JasperReportsContext jasperReportsContext, Writer writer, String exporterKey)
{
super(jasperReportsContext, writer);
this.exporterKey = exporterKey;
}
/**
*
*/
public void export(
JRStyle style,
Map attributes,
String text,
Locale locale,
boolean hiddenText,
String invalidCharReplacement,
Color backcolor,
boolean isNewLineAsParagraph)
{
if (text != null)
{
write(" \n");
boolean highlightText = backcolor == null || !backcolor.equals(attributes.get(TextAttribute.BACKGROUND));
exportProps(
getAttributes(style),
attributes,
locale,
hiddenText,
highlightText
);
StringTokenizer tkzer = new StringTokenizer(text, "\n", true);
while(tkzer.hasMoreTokens())
{
String token = tkzer.nextToken();
if ("\n".equals(token))
{
if(isNewLineAsParagraph)
{
write("\n");
}
else
{
write("");
}
}
else
{
write("");
write(JRStringUtil.xmlEncode(token, invalidCharReplacement));//FIXMEODT try something nicer for replace
write("\n");
}
}
write(" \n");
}
}
/**
*
*/
public void exportProps(JRDefaultStyleProvider defaultStyleProvider, JRStyle style, Locale locale)
{
JRStyle baseStyle = defaultStyleProvider.getStyleResolver().getBaseStyle(style);
exportProps(
getAttributes(baseStyle),
getAttributes(style),
locale,
false,
false
);
}
/**
*
*/
public void exportProps(Map parentAttrs, Map attrs, Locale locale, boolean hiddenText, boolean highlightText)
{
write(" \n");
Object value = attrs.get(TextAttribute.FAMILY);
Object oldValue = parentAttrs.get(TextAttribute.FAMILY);
if (value != null && !value.equals(oldValue))//FIXMEDOCX the text locale might be different from the report locale, resulting in different export font
{
String fontFamilyAttr = (String)value;
String fontFamily = fontUtil.getExportFontFamily(fontFamilyAttr, locale, exporterKey);
write(" \n");
}
value = attrs.get(TextAttribute.FOREGROUND);
oldValue = parentAttrs.get(TextAttribute.FOREGROUND);
if (value != null && !value.equals(oldValue))
{
write(" \n");
}
if(highlightText)
{
value = attrs.get(TextAttribute.BACKGROUND);
if (value != null && ColorEnum.getByColor((Color)value) != null)
{
//the highlight does not accept the color hexadecimal expression, but only few color names
write(" \n");
}
}
value = attrs.get(TextAttribute.SIZE);
oldValue = parentAttrs.get(TextAttribute.SIZE);
if (value != null && !value.equals(oldValue))
{
float fontSize = ((Float)value).floatValue();
fontSize = fontSize == 0 ? 0.5f : fontSize;// only the special EMPTY_CELL_STYLE would have font size zero
write(" \n");
}
value = attrs.get(TextAttribute.WEIGHT);
oldValue = parentAttrs.get(TextAttribute.WEIGHT);
if (value != null && !value.equals(oldValue))
{
write(" \n");
}
value = attrs.get(TextAttribute.POSTURE);
oldValue = parentAttrs.get(TextAttribute.POSTURE);
if (value != null && !value.equals(oldValue))
{
write(" \n");
}
value = attrs.get(TextAttribute.UNDERLINE);
oldValue = parentAttrs.get(TextAttribute.UNDERLINE);
if (
(value == null && oldValue != null)
|| (value != null && !value.equals(oldValue))
)
{
write(" \n");
}
value = attrs.get(TextAttribute.STRIKETHROUGH);
oldValue = parentAttrs.get(TextAttribute.STRIKETHROUGH);
if (
(value == null && oldValue != null)
|| (value != null && !value.equals(oldValue))
)
{
write(" \n");
}
value = attrs.get(TextAttribute.SUPERSCRIPT);
if (TextAttribute.SUPERSCRIPT_SUPER.equals(value))
{
write(" \n");
}
else if (TextAttribute.SUPERSCRIPT_SUB.equals(value))
{
write(" \n");
}
if (hiddenText)
{
write(" \n");
}
write(" \n");
}
/**
*
*/
private Map getAttributes(JRStyle style)//FIXMEDOCX put this in util?
{
Map styledTextAttributes = new HashMap();
if (style != null)
{
JRPrintText text = new JRBasePrintText(null);
text.setStyle(style);
//JRFontUtil.getAttributes(styledTextAttributes, text, (Locale)null);//FIXMEDOCX getLocale());
fontUtil.getAttributesWithoutAwtFont(styledTextAttributes, text);
styledTextAttributes.put(TextAttribute.FOREGROUND, text.getForecolor());
if (text.getModeValue() == ModeEnum.OPAQUE)
{
styledTextAttributes.put(TextAttribute.BACKGROUND, text.getBackcolor());
}
}
return styledTextAttributes;
}
}