Java源码示例:org.pentaho.reporting.engine.classic.core.modules.output.table.html.HtmlReportUtil
示例1
public static void main(final String[] args)
throws ReportProcessingException, IOException, ReportDefinitionException, ReportWriterException
{
ClassicEngineBoot.getInstance().start();
final ExtSubReportDemo demoHandler = new ExtSubReportDemo();
final boolean preview = true;
if (preview)
{
final SimpleDemoFrame frame = new SimpleDemoFrame(demoHandler);
frame.init();
frame.pack();
LibSwingUtil.centerFrameOnScreen(frame);
frame.setVisible(true);
}
else
{
HtmlReportUtil.createStreamHTML(demoHandler.createReport(), "/tmp/test.html");
}
}
示例2
public static void processStreamHtml( MasterReport report, OutputStream out ) throws ReportProcessingException,
IOException {
ReportStructureValidator validator = new ReportStructureValidator();
if ( validator.isValidForFastProcessing( report ) == false ) {
HtmlReportUtil.createStreamHTML( report, out );
return;
}
final StreamRepository targetRepository = new StreamRepository( out );
final ContentLocation targetRoot = targetRepository.getRoot();
final FastHtmlContentItems contentItems = new FastHtmlContentItems();
contentItems.setContentWriter( targetRoot, new DefaultNameGenerator( targetRoot, "index", "html" ) );
contentItems.setDataWriter( null, null );
contentItems.setUrlRewriter( new FileSystemURLRewriter() );
final FastHtmlExportProcessor reportProcessor = new FastHtmlExportProcessor( report, contentItems );
reportProcessor.processReport();
reportProcessor.close();
out.flush();
}
示例3
public void testRunSample() throws Exception {
final URL url = getClass().getResource( "Prd-2849.prpt" );
assertNotNull( url );
final ResourceManager resourceManager = new ResourceManager();
resourceManager.registerDefaults();
final Resource directly = resourceManager.createDirectly( url, MasterReport.class );
final MasterReport report = (MasterReport) directly.getResource();
report.getReportConfiguration().setConfigProperty(
"org.pentaho.reporting.engine.classic.core.modules.output.pageable.xml.Encoding", "UTF-8" );
final ByteArrayOutputStream out = new ByteArrayOutputStream();
HtmlReportUtil.createStreamHTML( report, out );
final String outText = out.toString( "UTF-8" );
assertTrue( outText.indexOf( "<p>Here is my HTML.</p>" ) > 0 );
/*
* int count = 0; int occurence = -1; do { occurence = outText.indexOf(">Label</text>", occurence + 1); if
* (occurence > -1) { count += 1; } } while (occurence != -1); assertEquals(2, count);
*/
// System.out.println(outText);
}
示例4
public void testRunSample() throws Exception {
final URL url = getClass().getResource( "Prd-2849.prpt" );
assertNotNull( url );
final ResourceManager resourceManager = new ResourceManager();
resourceManager.registerDefaults();
final Resource directly = resourceManager.createDirectly( url, MasterReport.class );
final MasterReport report = (MasterReport) directly.getResource();
report.getReportConfiguration().setConfigProperty(
"org.pentaho.reporting.engine.classic.core.modules.output.pageable.xml.Encoding", "UTF-8" );
final ByteArrayOutputStream out = new ByteArrayOutputStream();
HtmlReportUtil.createZIPHTML( report, out, "report.html" );
// final String outText = out.toString("UTF-8");
// assertTrue(outText.indexOf("<p>Here is my HTML.</p>") > 0);
/*
* int count = 0; int occurence = -1; do { occurence = outText.indexOf(">Label</text>", occurence + 1); if
* (occurence > -1) { count += 1; } } while (occurence != -1); assertEquals(2, count);
*/
// System.out.println(outText);
}
示例5
public void testHtmlExport() throws ReportProcessingException, IOException, ResourceException {
final Band tableHeader = createBodyBox( "header" );
tableHeader.getStyle().setStyleProperty( BandStyleKeys.LAYOUT, BandStyleKeys.LAYOUT_TABLE_HEADER );
final Band table = new Band();
table.getStyle().setStyleProperty( BandStyleKeys.LAYOUT, BandStyleKeys.LAYOUT_TABLE );
table.getStyle().setStyleProperty( ElementStyleKeys.MIN_WIDTH, -100f );
table.getStyle().setStyleProperty( ElementStyleKeys.MIN_HEIGHT, 200f );
table.addElement( tableHeader );
table.addElement( createBodyBox( "body" ) );
final MasterReport report = new MasterReport();
final ReportHeader band = report.getReportHeader();
band.addElement( table );
final MemoryByteArrayOutputStream outputStream = new MemoryByteArrayOutputStream();
HtmlReportUtil.createStreamHTML( report, outputStream );
final String htmlText = new String( outputStream.toByteArray(), "UTF-8" );
assertTrue( htmlText.contains( "<td valign=\"top\" class=\"style-1\">header</td>" ) );
assertTrue( htmlText.contains( "<td valign=\"top\" class=\"style-1\">body</td>" ) );
}
示例6
@Test
public void testHTML() throws ResourceException, IOException {
URL url = getClass().getResource( "BACKLOG-6818.prpt" );
MasterReport report = (MasterReport) new ResourceManager().createDirectly( url, MasterReport.class ).getResource();
try ( ByteArrayOutputStream stream = new ByteArrayOutputStream() ) {
HtmlReportUtil.createStreamHTML( report, stream );
final String html = new String( stream.toByteArray(), "UTF-8" );
int k = 1;
for ( int i = 1; i < 5; i++, k = -k ) {
final Pattern pattern = Pattern.compile( "(.*id=\"test" + i + "\".*style=\")(.*)(\".*)" );
final Matcher matcher = pattern.matcher( html );
if ( matcher.find() ) {
final String group = matcher.group( 2 );
assertTrue( group.contains( k > 0 ? TextRotation.D_90.getCss() : TextRotation.D_270.getCss() ) );
}
}
} catch ( final IOException | ReportProcessingException e ) {
fail();
}
}
示例7
/**
* Generates a simple HTML report and returns the HTML output back to the browser
*/
private void generateReport( final HttpServletRequest req, final HttpServletResponse resp ) throws ServletException, IOException {
// Generate the report definition
final MasterReport report = createReportDefinition();
// Run the report and save the HTML output to a byte stream
resp.setContentType( "text/html" ); // Change to "application/pdf" for PDF output
OutputStream out = resp.getOutputStream();
try {
// Use the HtmlReportUtil to generate the report to a Stream HTML
HtmlReportUtil.createStreamHTML( report, out );
//NOTE: Changing this to use PDF is simple:
// 1. Change the above setContent call to use "application/pdf"
// 2. Instead of HtmlReportUtil, use the following line:
// PdfReportUtil.createPDF(report, out)
} catch ( ReportProcessingException rpe ) {
rpe.printStackTrace();
} finally {
out.close();
}
}
示例8
@Override
public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
try {
// Getting the report.
ResourceManager manager = new ResourceManager();
manager.registerDefaults();
Resource res = manager.createDirectly(
new URL("file:resources/interactive_report_2.prpt"),
MasterReport.class);
MasterReport report = (MasterReport) res.getResource();
// Mandatory parameter.
report.getParameterValues().put("LINE", request.getParameter("line"));
// Conversion to HTML and rendering.
response.setContentType("text/html");
HtmlReportUtil.createStreamHTML(report, response.getOutputStream());
}
catch (Exception e)
{
e.printStackTrace();
}
}
示例9
@Override
public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
try {
// Getting the report.
ResourceManager manager = new ResourceManager();
manager.registerDefaults();
Resource res = manager.createDirectly(
new URL("file:resources/interactive_report_1.prpt"),
MasterReport.class);
MasterReport report = (MasterReport) res.getResource();
// Conversion to HTML and rendering.
response.setContentType("text/html");
HtmlReportUtil.createStreamHTML(report, response.getOutputStream());
}
catch (Exception e)
{
e.printStackTrace();
}
}
示例10
public static void main(String[] args)
throws ResourceException, IOException,
ReportProcessingException, ReportDefinitionException
{
ClassicEngineBoot.getInstance().start();
final InteractiveHtmlDemo handler = new InteractiveHtmlDemo();
HtmlReportUtil.createStreamHTML(handler.createReport(), "/tmp/report.html");
}
示例11
/**
* Entry point for running the demo application...
*
* @param args ignored.
*/
public static void main(final String[] args) throws ReportDefinitionException, ReportProcessingException, IOException
{
// initialize JFreeReport
ClassicEngineBoot.getInstance().start();
final VeryLargeReportDemo handler = new VeryLargeReportDemo();
// final SimpleDemoFrame frame = new SimpleDemoFrame(handler);
// frame.init();
// frame.pack();
// LibSwingUtil.centerFrameOnScreen(frame);
// frame.setVisible(true);
PdfReportUtil.createPDF(handler.createReport(), "/tmp/report.pdf");
HtmlReportUtil.createStreamHTML(handler.createReport(), "/tmp/report.html");
long startPDF = System.currentTimeMillis();
PdfReportUtil.createPDF(handler.createReport(), "/tmp/report.pdf");
long endPDF = System.currentTimeMillis();
long startHTML = System.currentTimeMillis();
HtmlReportUtil.createStreamHTML(handler.createReport(), "/tmp/report.html");
long endHTML = System.currentTimeMillis();
System.out.println("PDF: " + (endPDF - startPDF));
System.out.println("HTML: " + (endHTML - startHTML));
}
示例12
/**
* Entry point for running the demo application...
*
* @param args ignored.
*/
public static void main(final String[] args) throws ReportProcessingException, IOException, ReportDefinitionException
{
// initialize JFreeReport
ClassicEngineBoot.getInstance().start();
final LGPLTextDemo handler = new LGPLTextDemo();
HtmlReportUtil.createDirectoryHTML(handler.createReport(), "/tmp/report.html");
// final SimpleDemoFrame frame = new SimpleDemoFrame(handler);
// frame.init();
// frame.pack();
// RefineryUtilities.centerFrameOnScreen(frame);
// frame.setVisible(true);
}
示例13
public static void main(final String[] args)
throws ReportProcessingException, IOException, ReportDefinitionException, ReportWriterException
{
ClassicEngineBoot.getInstance().start();
final SubReportDemo demoHandler = new SubReportDemo();
final boolean preview = false;
if (preview)
{
final SimpleDemoFrame frame = new SimpleDemoFrame(demoHandler);
frame.init();
frame.pack();
LibSwingUtil.centerFrameOnScreen(frame);
frame.setVisible(true);
}
else
{
HtmlReportUtil.createStreamHTML(demoHandler.createReport(), "/tmp/test.html");
}
// final JFreeReport report = demoHandler.createReport();
// final ReportWriter writer = new ReportWriter(report, "ISO-8859-1",
// ReportWriter.createDefaultConfiguration(report));
// writer.addClassFactoryFactory(new URLClassFactory());
// writer.addClassFactoryFactory(new DefaultClassFactory());
// writer.addClassFactoryFactory(new BandLayoutClassFactory());
// writer.addClassFactoryFactory(new ArrayClassFactory());
// writer.addClassFactoryFactory(new ExtraShapesClassFactory());
// writer.addStyleKeyFactory(new DefaultStyleKeyFactory());
// writer.addStyleKeyFactory(new PageableLayoutStyleKeyFactory());
// writer.addTemplateCollection(new DefaultTemplateCollection());
// writer.addElementFactory(new DefaultElementFactory());
// writer.addDataSourceFactory(new DefaultDataSourceFactory());
// final OutputStreamWriter w = new OutputStreamWriter(System.out);
// writer.write(w);
// w.close();
}
示例14
public static void main(final String[] args)
throws ReportDefinitionException, ReportProcessingException, IOException
{
ClassicEngineBoot.getInstance().start();
final OpenSourceXMLDemoHandler handler = new OpenSourceXMLDemoHandler();
// PdfReportUtil.createPDF(handler.createReport(), "/tmp/report.pdf");
HtmlReportUtil.createDirectoryHTML(handler.createReport(), "/tmp/report.html");
}
示例15
@Test
public void testComplexCrosstab() throws ResourceException, ReportProcessingException {
final MasterReport report = DebugReportRunner.parseGoldenSampleReport( "Prd-4523.prpt" );
report.getReportConfiguration().setConfigProperty(
"org.pentaho.reporting.engine.classic.core.modules.output.table.base.FailOnCellConflicts", "true" );
HtmlReportUtil.createStreamHTML( report, new NullOutputStream() );
}
示例16
@Test
public void testHtml() throws Exception {
URL resource = getClass().getResource( "Prd-5295.prpt" );
ResourceManager mgr = new ResourceManager();
MasterReport report = (MasterReport) mgr.createDirectly( resource, MasterReport.class ).getResource();
ByteArrayOutputStream boutFast = new ByteArrayOutputStream();
ByteArrayOutputStream boutSlow = new ByteArrayOutputStream();
FastHtmlReportUtil.processStreamHtml( report, boutFast );
HtmlReportUtil.createStreamHTML( report, boutSlow );
String htmlFast = boutFast.toString( "UTF-8" );
String htmlSlow = boutSlow.toString( "UTF-8" );
Assert.assertEquals( htmlSlow, htmlFast );
}
示例17
@Test
public void testFastHtmlExportWork() throws Exception {
URL url = getClass().getResource( "Prd-5240.prpt" );
MasterReport report = (MasterReport) new ResourceManager().createDirectly( url, MasterReport.class ).getResource();
final ByteArrayOutputStream boutFast = new ByteArrayOutputStream();
final ByteArrayOutputStream boutSlow = new ByteArrayOutputStream();
FastHtmlReportUtil.processStreamHtml( report, boutFast );
HtmlReportUtil.createStreamHTML( report, boutSlow );
String htmlFast = boutFast.toString( "UTF-8" );
String htmlSlow = boutSlow.toString( "UTF-8" );
Assert.assertEquals( htmlSlow, htmlFast );
}
示例18
@Test
public void testFastHtmlExportWork() throws Exception {
MasterReport report = DebugReportRunner.parseGoldenSampleReport( "Prd-5143.prpt" );
final ByteArrayOutputStream boutFast = new ByteArrayOutputStream();
final ByteArrayOutputStream boutSlow = new ByteArrayOutputStream();
FastHtmlReportUtil.processStreamHtml( report, boutFast );
HtmlReportUtil.createStreamHTML( report, boutSlow );
String htmlFast = boutFast.toString( "UTF-8" );
String htmlSlow = boutSlow.toString( "UTF-8" );
Assert.assertEquals( htmlSlow, htmlFast );
}
示例19
@Test
public void testHtmlExport() throws ReportProcessingException, IOException {
MasterReport report = createReport();
ByteArrayOutputStream boutFast = new ByteArrayOutputStream();
ByteArrayOutputStream boutSlow = new ByteArrayOutputStream();
FastHtmlReportUtil.processStreamHtml( report, boutFast );
HtmlReportUtil.createStreamHTML( report, boutSlow );
String htmlFast = boutFast.toString( "UTF-8" );
String htmlSlow = boutSlow.toString( "UTF-8" );
Assert.assertEquals( htmlSlow, htmlFast );
}
示例20
@Test
public void testFrom52() throws Exception {
URL url = getClass().getResource( "Prd-5245-from52.prpt" );
MasterReport report = (MasterReport) new ResourceManager().createDirectly( url, MasterReport.class ).getResource();
final ByteArrayOutputStream boutFast = new ByteArrayOutputStream();
final ByteArrayOutputStream boutSlow = new ByteArrayOutputStream();
FastHtmlReportUtil.processStreamHtml( report, boutFast );
HtmlReportUtil.createStreamHTML( report, boutSlow );
String htmlFast = boutFast.toString( "UTF-8" );
String htmlSlow = boutSlow.toString( "UTF-8" );
Assert.assertEquals( htmlSlow, htmlFast );
}
示例21
@Test
public void testFrom39() throws Exception {
URL url = getClass().getResource( "Prd-5245-from39.prpt" );
MasterReport report = (MasterReport) new ResourceManager().createDirectly( url, MasterReport.class ).getResource();
final ByteArrayOutputStream boutFast = new ByteArrayOutputStream();
final ByteArrayOutputStream boutSlow = new ByteArrayOutputStream();
FastHtmlReportUtil.processStreamHtml( report, boutFast );
HtmlReportUtil.createStreamHTML( report, boutSlow );
String htmlFast = boutFast.toString( "UTF-8" );
String htmlSlow = boutSlow.toString( "UTF-8" );
Assert.assertEquals( htmlSlow, htmlFast );
}
示例22
public void testHtmlExportFull() throws Exception {
final URL url = getClass().getResource( "Prd-3931.prpt" );
assertNotNull( url );
final ResourceManager resourceManager = new ResourceManager();
resourceManager.registerDefaults();
final Resource directly = resourceManager.createDirectly( url, MasterReport.class );
final MasterReport report = (MasterReport) directly.getResource();
report.setCompatibilityLevel( null );
report.getReportConfiguration().setConfigProperty( ClassicEngineCoreModule.COMPLEX_TEXT_CONFIG_OVERRIDE_KEY,
"false" );
final Group rootGroup = report.getRootGroup();
assertTrue( rootGroup instanceof CrosstabGroup );
final CrosstabGroup ct = (CrosstabGroup) rootGroup;
ct.setPrintColumnTitleHeader( true );
ct.setPrintDetailsHeader( false );
final MemoryByteArrayOutputStream outputStream = new MemoryByteArrayOutputStream();
HtmlReportUtil.createStreamHTML( report, outputStream );
final String htmlText = new String( outputStream.toByteArray(), "UTF-8" );
DebugLog.log( htmlText );
assertTrue( htmlText.contains( "<td colspan=\"2\" valign=\"top\" class=\"style-1\">2003</td>" ) );
assertTrue( htmlText.contains( "<td colspan=\"2\" valign=\"top\" class=\"style-1\">2004</td>" ) );
assertTrue( htmlText.contains( "<td colspan=\"2\" valign=\"top\" class=\"style-1\">2005</td>" ) );
assertTrue( htmlText.contains( "<td valign=\"top\" class=\"style-3\">Product Line</td>" ) );
assertTrue( htmlText.contains( "<td valign=\"top\" class=\"style-3\">Market</td>" ) );
}
示例23
public void testHtmlExportFullComplexText() throws Exception {
final URL url = getClass().getResource( "Prd-3931.prpt" );
assertNotNull( url );
final ResourceManager resourceManager = new ResourceManager();
resourceManager.registerDefaults();
final Resource directly = resourceManager.createDirectly( url, MasterReport.class );
final MasterReport report = (MasterReport) directly.getResource();
report.setCompatibilityLevel( null );
report.getReportConfiguration()
.setConfigProperty( ClassicEngineCoreModule.COMPLEX_TEXT_CONFIG_OVERRIDE_KEY, "true" );
final Group rootGroup = report.getRootGroup();
assertTrue( rootGroup instanceof CrosstabGroup );
final CrosstabGroup ct = (CrosstabGroup) rootGroup;
ct.setPrintColumnTitleHeader( true );
ct.setPrintDetailsHeader( false );
final MemoryByteArrayOutputStream outputStream = new MemoryByteArrayOutputStream();
HtmlReportUtil.createStreamHTML( report, outputStream );
final String htmlText = new String( outputStream.toByteArray(), "UTF-8" );
DebugLog.log( htmlText );
assertTrue( htmlText.contains( "<td colspan=\"2\" valign=\"top\" class=\"style-1\">2003</td>" ) );
assertTrue( htmlText.contains( "<td colspan=\"2\" valign=\"top\" class=\"style-1\">2004</td>" ) );
assertTrue( htmlText.contains( "<td colspan=\"2\" valign=\"top\" class=\"style-1\">2005</td>" ) );
assertTrue( htmlText.contains( "<td valign=\"top\" class=\"style-3\">Product Line</td>" ) );
assertTrue( htmlText.contains( "<td valign=\"top\" class=\"style-3\">Market</td>" ) );
}
示例24
@Test
public void testCreateParentFolder() {
String filename = getTestDirectory() + "/" + "testfile.html";
MasterReport report = Mockito.mock( MasterReport.class );
ModifiableConfiguration conf = Mockito.spy( new DefaultConfiguration() );
conf.setConfigProperty( "org.pentaho.reporting.engine.classic.core.modules.gui.html.paged.CreateParentFolder", "true" );
Mockito.doReturn(conf).when(report).getConfiguration();
File file = null;
File directory;
try {
file = new File( filename ).getCanonicalFile();
directory = file.getParentFile();
if ( directory != null ) {
if ( directory.exists() == true ) {
directory.delete();
}
}
Assert.assertFalse( directory.exists() );
HtmlReportUtil.createDirectoryHTML( report, filename );
} catch (Exception e1) {
e1.printStackTrace();
}
directory = file.getParentFile();
Assert.assertTrue( directory != null );
Assert.assertTrue( directory.exists() );
file.delete();
directory.delete();
}
示例25
@Test
public void testHandleRotatedTextHTML() throws Exception {
URL url = getClass().getResource( "BACKLOG-10064.prpt" );
MasterReport report = (MasterReport) new ResourceManager().createDirectly( url, MasterReport.class ).getResource();
report.getReportConfiguration().setConfigProperty( HtmlTableModule.INLINE_STYLE, "true" );
report.getReportConfiguration().setConfigProperty( ClassicEngineCoreModule.STRICT_ERROR_HANDLING_KEY, "false" );
List<String> elementsIdList = Arrays.asList("topLeft90","topCenter90","topRight90","topJustify90",
"topLeft-90","topCenter-90","topRight-90","topJustify-90",
"middleLeft90","middleCenter90","middleRight90","middleJustify90",
"middleLeft-90","middleCenter-90","middleRight-90","middleJustify-90",
"bottomLeft90","bottomCenter90","bottomRight90","bottomJustify90",
"bottomLeft-90","bottomCenter-90","bottomRight-90","bottomJustify-90");
XPathFactory xpathFactory = XPathFactory.newInstance();
try ( ByteArrayOutputStream stream = new ByteArrayOutputStream() ) {
HtmlReportUtil.createStreamHTML( report, stream );
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware( true );
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse( new ByteArrayInputStream( stream.toByteArray() ) );
for ( String elementId : elementsIdList ) {
org.w3c.dom.Element element = document.getElementById( elementId );
Node rotatedTextStyle = element.getFirstChild().getAttributes().getNamedItem( "style" );
Node trSryle = element.getParentNode().getParentNode().getAttributes().getNamedItem( "style" );
assertTrue( isStyleValid( rotatedTextStyle.getNodeValue(), trSryle.getNodeValue(), elementId.contains( "middle" ) ) );
}
} catch ( final IOException | ReportProcessingException e ) {
fail();
}
}
示例26
public static void createStreamHTML( final MasterReport report ) throws Exception {
try {
HtmlReportUtil.createStreamHTML( report, new NullOutputStream() );
} catch ( ReportParameterValidationException e ) {
Assert.fail();
}
}
示例27
public static void createZIPHTML( final MasterReport report ) throws Exception {
try {
HtmlReportUtil.createZIPHTML( report, new NullOutputStream(), "report.html" );
} catch ( ReportParameterValidationException e ) {
Assert.fail();
}
}
示例28
public static void createStreamHTML( final MasterReport report )
throws Exception {
try {
HtmlReportUtil.createStreamHTML( report, new NullOutputStream() );
} catch ( ReportParameterValidationException e ) {
}
}
示例29
public static void createZIPHTML( final MasterReport report )
throws Exception {
try {
HtmlReportUtil.createZIPHTML( report, new NullOutputStream(), "report.html" );
} catch ( ReportParameterValidationException e ) {
}
}
示例30
public void render() throws Exception {
createReport();
ClassLoader originalClassloader = Thread.currentThread().getContextClassLoader();
try {
// we need to change the current classloader for the reporting to find our plugin classes
Thread.currentThread().setContextClassLoader( KettleReportBuilder.class.getClassLoader() );
switch ( options.getOutputType() ) {
case PDF:
PdfReportUtil.createPDF( report, targetFilename );
break;
case DOC:
RTFReportUtil.createRTF( report, targetFilename );
break;
case XLS:
ExcelReportUtil.createXLS( report, targetFilename );
break;
case HTML:
HtmlReportUtil.createDirectoryHTML( report, targetFilename );
break;
case CSV:
CSVReportUtil.createCSV( report, targetFilename );
break;
default:
break;
}
} finally {
Thread.currentThread().setContextClassLoader( originalClassloader );
}
}