Browse Source

Bugzilla #35937:

FormattingResults mechanism ported from maintenance branch to trunk.
Submitted by: Manuel Mall <mm.at.arcus.com.au>

Patch tweaked slightly to avoid the reference of Fop.java into the area package.

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@226511 13f79535-47bb-0310-9956-ffa450edef68
tags/fop-0_90-alpha1
Jeremias Maerki 19 years ago
parent
commit
db1b760fee

+ 16
- 1
examples/embedding/java/embedding/ExampleFO2PDF.java View File

@@ -1,5 +1,5 @@
/*
* Copyright 1999-2004 The Apache Software Foundation.
* Copyright 1999-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,6 +37,8 @@ import javax.xml.transform.sax.SAXResult;
// FOP
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FormattingResults;
import org.apache.fop.apps.PageSequenceResults;

/**
* This class demonstrates the conversion of an FO file to PDF using FOP.
@@ -76,6 +78,18 @@ public class ExampleFO2PDF {
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
// Result processing
FormattingResults foResults = fop.getResults();
java.util.List pageSequences = foResults.getPageSequences();
for (java.util.Iterator it = pageSequences.iterator(); it.hasNext();) {
PageSequenceResults pageSequenceResults = (PageSequenceResults)it.next();
System.out.println("PageSequence "
+ (String.valueOf(pageSequenceResults.getID()).length() > 0
? pageSequenceResults.getID() : "<no id>")
+ " generated " + pageSequenceResults.getPageCount() + " pages.");
}
System.out.println("Generated " + foResults.getPageCount() + " pages in total.");

} catch (Exception e) {
e.printStackTrace(System.err);
@@ -102,6 +116,7 @@ public class ExampleFO2PDF {

//Setup input and output files
File fofile = new File(baseDir, "xml/fo/helloworld.fo");
//File fofile = new File(baseDir, "../fo/pagination/franklin_2pageseqs.fo");
File pdffile = new File(outDir, "ResultFO2PDF.pdf");

System.out.println("Input: XSL-FO (" + fofile + ")");

+ 25
- 1
src/java/org/apache/fop/apps/Fop.java View File

@@ -61,6 +61,9 @@ public class Fop implements Constants {
// FOUserAgent object to set processing options
private FOUserAgent foUserAgent = null;

// FOTreeBuilder object to maintain reference for access to results
private FOTreeBuilder foTreeBuilder = null;

/**
* Constructor for use with already-created FOUserAgents
* @param renderType the type of renderer to use. Must be one of
@@ -133,7 +136,28 @@ public class Fop implements Constants {
* @throws FOPException if setting up the DefaultHandler fails
*/
public DefaultHandler getDefaultHandler() throws FOPException {
return new FOTreeBuilder(renderType, foUserAgent, stream);
if (foTreeBuilder == null) {
this.foTreeBuilder = new FOTreeBuilder(renderType, foUserAgent, stream);
}
return this.foTreeBuilder;
}

/**
* Returns the results of the rendering process. Information includes
* the total number of pages generated and the number of pages per
* page-sequence. Call this method only after the rendering process is
* finished. Note that the results are only available for output formats
* which make use of FOP's layout engine (PDF, PS, etc.).
* @return the results of the rendering process, or null for flow-oriented
* output formats like RTF and MIF.
*/
public FormattingResults getResults() {
if (foTreeBuilder == null) {
throw new IllegalStateException(
"Results are only available after calling getDefaultHandler().");
} else {
return foTreeBuilder.getResults();
}
}

/**

+ 84
- 0
src/java/org/apache/fop/apps/FormattingResults.java View File

@@ -0,0 +1,84 @@
/*
* Copyright 1999-2003,2005 The Apache Software Foundation.
*
* Licensed 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.
*/

/* $Id$ */

package org.apache.fop.apps;

import java.util.List;

import org.apache.fop.fo.pagination.PageSequence;

/**
* Class for reporting back formatting results to the calling application.
*/
public class FormattingResults {

private int pageCount = 0;
private List pageSequences = null;

/**
* Constructor for the FormattingResults object
*/
public FormattingResults() {
}

/**
* Gets the number of pages rendered
*
* @return The number of pages overall
*/
public int getPageCount() {
return this.pageCount;
}

/**
* Gets the results for the individual page-sequences.
*
* @return A List with PageSequenceResults objects
*/
public List getPageSequences() {
return this.pageSequences;
}

/**
* Resets this object
*/
public void reset() {
this.pageCount = 0;
if (this.pageSequences != null) {
this.pageSequences.clear();
}
}

/**
* Reports the result of one page sequence rendering
* back into this object.
*
* @param pageSequence the PageSequence which just completed rendering
* @param pageCount the number of pages rendered for that PageSequence
*/
public void haveFormattedPageSequence(PageSequence pageSequence, int pageCount) {
this.pageCount += pageCount;
if (this.pageSequences == null) {
this.pageSequences = new java.util.ArrayList();
}
this.pageSequences.add(
new PageSequenceResults(pageSequence.getId(),
pageCount));
}
}


+ 58
- 0
src/java/org/apache/fop/apps/PageSequenceResults.java View File

@@ -0,0 +1,58 @@
/*
* Copyright 1999-2003,2005 The Apache Software Foundation.
*
* Licensed 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.
*/

/* $Id$ */

package org.apache.fop.apps;

/**
* Class for reporting back formatting results to the calling application. This
* particular class is used to report the results of a single page-sequence.
*/
public class PageSequenceResults {

private String id;
private int pageCount;

/**
* Constructor for the PageSequenceResults object
*
* @param id ID of the page-sequence, if available
* @param pageCount The number of resulting pages
*/
public PageSequenceResults(String id, int pageCount) {
this.id = id;
this.pageCount = pageCount;
}

/**
* Gets the ID of the page-sequence if one was specified.
*
* @return The ID
*/
public String getID() {
return this.id;
}

/**
* Gets the number of pages that resulted by processing the page-sequence.
*
* @return The number of pages generated
*/
public int getPageCount() {
return this.pageCount;
}
}

+ 23
- 3
src/java/org/apache/fop/area/AreaTreeHandler.java View File

@@ -35,6 +35,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.FormattingResults;
import org.apache.fop.fo.FOEventHandler;
import org.apache.fop.fo.pagination.PageSequence;
import org.apache.fop.fo.pagination.Root;
@@ -92,6 +93,9 @@ public class AreaTreeHandler extends FOEventHandler {
// Each idref has a HashSet of Resolvable objects containing that idref
private Map unresolvedIDRefs = new HashMap();

// The formatting results to be handed back to the caller.
private FormattingResults results = new FormattingResults();

private static Log log = LogFactory.getLog(AreaTreeHandler.class);

/**
@@ -208,6 +212,15 @@ public class AreaTreeHandler extends FOEventHandler {
return (List) idLocations.get(id);
}

/**
* Get information about the rendered output, like
* number of pages created.
* @return the results structure
*/
public FormattingResults getResults() {
return this.results;
}

/**
* Add an Resolvable object with an unresolved idref
* @param idref the idref whose target id has not yet been located
@@ -256,10 +269,17 @@ public class AreaTreeHandler extends FOEventHandler {
// If no main flow, nothing to layout!
if (pageSequence.getMainFlow() != null) {
PageSequenceLayoutManager pageSLM;
pageSLM =
getLayoutManagerMaker().makePageSequenceLayoutManager(this,
pageSequence);
pageSLM = getLayoutManagerMaker().makePageSequenceLayoutManager(
this, pageSequence);
pageSLM.activateLayout();
this.results.haveFormattedPageSequence(pageSequence,
getAreaTreeModel().getPageCount(getAreaTreeModel().getPageSequenceCount()));
if (log.isDebugEnabled()) {
log.debug("Last page-sequence produced "
+ getAreaTreeModel().getPageCount(
getAreaTreeModel().getPageSequenceCount())
+ " pages.");
}
}
}


+ 29
- 3
src/java/org/apache/fop/fo/FOTreeBuilder.java View File

@@ -28,6 +28,8 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.FormattingResults;
import org.apache.fop.area.AreaTreeHandler;
import org.apache.fop.render.RendererFactory;
import org.apache.fop.util.Service;
import org.apache.fop.fo.ElementMapping.Maker;
@@ -129,8 +131,7 @@ public class FOTreeBuilder extends DefaultHandler {
addElementMapping("org.apache.fop.fo.extensions.ExtensionElementMapping");

// add mappings from available services
Iterator providers =
Service.providers(ElementMapping.class);
Iterator providers = Service.providers(ElementMapping.class);
if (providers != null) {
while (providers.hasNext()) {
String str = (String)providers.next();
@@ -191,7 +192,8 @@ public class FOTreeBuilder extends DefaultHandler {
public void characters(char[] data, int start, int length)
throws FOPException {
if (currentFObj != null) {
currentFObj.addCharacters(data, start, start + length, currentPropertyList, locator);
currentFObj.addCharacters(data, start, start + length,
currentPropertyList, locator);
}
}

@@ -342,6 +344,30 @@ public class FOTreeBuilder extends DefaultHandler {
throw e;
}

/**
* Provides access to the underlying FOEventHandler object.
* @return the FOEventHandler object
*/
public FOEventHandler getEventHandler() {
return foEventHandler;
}

/**
* Returns the results of the rendering process. Information includes
* the total number of pages generated and the number of pages per
* page-sequence.
* @return the results of the rendering process.
*/
public FormattingResults getResults() {
if (getEventHandler() instanceof AreaTreeHandler) {
return ((AreaTreeHandler)getEventHandler()).getResults();
} else {
//No formatting results available for output formats no
//involving the layout engine.
return null;
}
}
/**
* Resets this object for another run.
*/

Loading…
Cancel
Save