/*
- * 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.
// 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.
// 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);
//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 + ")");
// 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
* @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();
+ }
}
/**
--- /dev/null
+/*
+ * 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));
+ }
+}
+
--- /dev/null
+/*
+ * 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;
+ }
+}
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;
// 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);
/**
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
// 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.");
+ }
}
}
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;
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();
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);
}
}
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.
*/