com.xmlmind.fo.converter
Class Converter

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by com.xmlmind.fo.converter.Converter
All Implemented Interfaces:
com.xmlmind.fo.converter.Translator.UriResolver, UriResolver, ContentHandler, DTDHandler, EntityResolver, ErrorHandler

public class Converter
extends DefaultHandler
implements ErrorHandler, UriResolver, com.xmlmind.fo.converter.Translator.UriResolver

A conversion engine.

The Converter class implements the Formatting Objects conversion engine. A typical use is as follows:

  1. Set conversion options using the setProperty or setProperties method.
  2. Create and initialize the source and destination specifications (an org.xml.sax.InputSource and an OutputDestination object respectively).
  3. Perform the conversion using the convert method.

Alternatively, a properly initialized instance of this class may be registered as a ContentHandler with an external SAX parser.


Field Summary
protected  com.xmlmind.fo.converter.Context context
           
 
Constructor Summary
Converter()
          Creates a new Converter instance.
 
Method Summary
protected  void abort()
           
protected  void badPropertyWarning(String key, String value, String reason)
           
 void characters(char[] chars, int start, int length)
          Not for public use: implementation of ContentHandler.
protected  void clean()
           
 void convert(InputSource input, OutputDestination output)
          Performs conversion.
 void endDocument()
          Not for public use: implementation of ContentHandler.
 void endElement(String namespace, String localName, String qualifiedName)
          Not for public use: implementation of ContentHandler.
 void endPrefixMapping(String prefix)
          Not for public use: implementation of ContentHandler.
 void error(Exception exception)
          Not for public use: implementation of ErrorHandler.
 void error(SAXParseException exception)
          Not for public use: implementation of ErrorHandler.
protected  void error(String message)
           
 void fatalError(SAXParseException exception)
          Not for public use: implementation of ErrorHandler.
protected  void finalize()
           
 UriResolver getUriResolver()
          Returns the registered URI resolver.
 UserStyles getUserStyles()
          Returns the set of user styles specified using setUserStyles(com.xmlmind.fo.converter.UserStyles).
 XMLReader getXMLReader()
          Returns the registered external XML reader.
 void ignorableWhitespace(char[] chars, int start, int length)
          Not for public use: implementation of ContentHandler.
protected  void initFontSize(com.xmlmind.fo.properties.PropertyValues properties)
           
static String[] listEncodings(String format)
          Returns the list of available encodings for the specified output format.
protected  UserStyles loadUserStyles(URL url)
           
static void main(String[] args)
          Not for public use: a simple test.
 void processingInstruction(String target, String data)
          Not for public use: implementation of ContentHandler.
 String resolve(String uri)
          Not for public use: implementation of Translator.UriResolver.
 URL resolve(String spec, URL base)
          Not for public use: implementation of UriResolver.
protected  void setDefaultValues(com.xmlmind.fo.properties.PropertyValues properties)
          Set contextual default values.
 void setDocumentLocator(Locator locator)
          Not for public use: implementation of ContentHandler.
 void setErrorHandler(ErrorHandler handler)
          Registers a conversion error handler.
protected  void setFont(com.xmlmind.fo.properties.PropertyValues properties, String font)
           
protected  void setFontSize(com.xmlmind.fo.properties.PropertyValues properties, String fontSize)
           
protected  void setOutput(OutputDestination output)
           
 void setProperties(Properties properties)
          Sets conversion options.
protected  void setProperties(com.xmlmind.fo.properties.PropertyValues properties, Attributes attributes, boolean computeLineHeight)
           
 void setProperty(String key, String value)
          Sets a conversion option.
 void setUriResolver(UriResolver resolver)
          Registers a URI resolver.
 void setUserStyles(UserStyles styles)
          Specifies the set of user styles during the conversion.
 void setXMLReader(XMLReader reader)
          Registers an external XML reader.
 void skippedEntity(String name)
          Not for public use: implementation of ContentHandler.
 void startDocument()
          Not for public use: implementation of ContentHandler.
 void startElement(String namespace, String localName, String qualifiedName, Attributes attributes)
          Not for public use: implementation of ContentHandler.
 void startPrefixMapping(String prefix, String uri)
          Not for public use: implementation of ContentHandler.
 void warning(Exception exception)
          Not for public use: implementation of ErrorHandler.
 void warning(SAXParseException exception)
          Not for public use: implementation of ErrorHandler.
protected  void warning(String message)
           
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
notationDecl, resolveEntity, unparsedEntityDecl
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

context

protected com.xmlmind.fo.converter.Context context
Constructor Detail

Converter

public Converter()
Creates a new Converter instance.

Method Detail

listEncodings

public static String[] listEncodings(String format)
                              throws IllegalArgumentException
Returns the list of available encodings for the specified output format.

Parameters:
format - the output format (rtf, wml, docx or odt)
Returns:
the list of available encodings
Throws:
IllegalArgumentException - if the format argument is invalid
See Also:
setProperty

setProperty

public void setProperty(String key,
                        String value)
Sets a conversion option.

All conversion options are specified as standard Java properties. Property names and values are summarized in the table below.

Name Value Description Default
alwaysSaveAsPNG true or false If true, do not attempt to save JPEG images as JPEG files in the output file. Instead always transcode JPEG images to PNG. false
baseURL (1) URL base URL of relative paths in property values none (paths are taken relative to the input source URL)
docx.useVML true or false Use VML -deprecated- elements rather than DrawingML elements to represent images false
genericFontFamilies A string having this syntax:
map -> entry [',' entry]*
entry -> generic_family '=' actual_family
generic_family -> 'serif' | 'sans-serif' | 'monospace'
                  | 'cursive' | 'fantasy'
May be used to map generic font families to actual font families. Depends on the output format. Generally: serif=Times New Roman,sans-serif=Arial,monospace=Courier New
imageResolution Positive integer default image resolution in DPI.

Used to compute the intrinsic size of an image when an image file does not contain resolution or absolute size information.

96
imageRendererResolution Positive integer default image resolution in DPI.

Used to compute the intrinsic size of an image, according to the image renderer (that is, MS-Word or OpenOffice), when an image file does not contain resolution or absolute size information.

Depends on the output format. Generally 96.

It is strongly recommended to use this default value.

outputEncoding See below output encoding See below
outputFormat rtf, wml, docx or odt output format rtf
prescaleImages true or false image scaling policy (2) false
rtf.target MSWord target RTF viewer none (no specified target)
screenResolution Positive integer screen resolution in DPI. Used to convert px lengths to other units. 96
set.graphic_factory_name.parameter_name Depends on the parameter Sets parameter parameter_name on graphic factory called graphic_factory_name (case-insensitive). graphic factory examples: ImageIO, WMF, EMF, SVG, MathML.

Important: unlike all the other properties which only have an effect on this converter, property set.XXX.YYY immediately configures once for all specified graphic factory and have no direct effect on this converter. Example: invoking something like:

Converter conv = new Converter();
conv.setProperty("set.SVG.resolution", "300"); // DPI
without actually using converter conv is a simple way to configure graphic factory SVG.
Depends on the parameter
singleSidedLayout true or false single-sided page layout (3) false
styles An URL in its string form (e.g. "file:///C:/My%20Folder/styles.xfc") or a filename (e.g. "C:\My Folder\styles.xfc"). A relative filename is relative to the current working directory. Specifies the location of an XML file containing the set of user styles to be used during the conversion. Do not use user styles; only use direct formatting/automatic styles.

The outputEncoding property possible values depend on the target output format:

(1) Use of the baseURL property is deprecated. Applications should register their own URI resolver to handle URIs in property values.

(2) By default images are not prescaled. Instead, the original size of images is preserved and scaling directives are inserted in the output document. Set the prescaleImages property to true if you prefer to minimize the size of the output document.

(3) By default RTF, WML and Open XML output documents are given a double-sided page layout regardless of the input document properties. To force a single-sided page layout the singleSidedLayout property must be set to true.

Parameters:
key - the property name
value - the property value. Specify null to remove property key.
See Also:
setProperties

badPropertyWarning

protected void badPropertyWarning(String key,
                                  String value,
                                  String reason)

setProperties

public void setProperties(Properties properties)
Sets conversion options.

All conversion options are specified as standard Java properties. See the setProperty method for an option summary.

Parameters:
properties - a property set
See Also:
setProperty

setUserStyles

public void setUserStyles(UserStyles styles)
Specifies the set of user styles during the conversion.

It's also possible to specify a set of user styles by its location --an URL or absolute filename-- by the means of setProperty("styles", location).

However when setProperty is used, the user styles are parsed each time convert(org.xml.sax.InputSource, com.xmlmind.fo.converter.OutputDestination) is invoked. This means that for an application embedding XMLmind XSL-FO Converter, a set of user styles may be parsed many times.

User styles using this method, of course, overrides those specified using setProperty.

Parameters:
styles - a set of user styles; may be null
See Also:
UserStyles.loadFrom(java.io.File), getUserStyles()

getUserStyles

public UserStyles getUserStyles()
Returns the set of user styles specified using setUserStyles(com.xmlmind.fo.converter.UserStyles). May return null.


setErrorHandler

public void setErrorHandler(ErrorHandler handler)
Registers a conversion error handler.

If no error handler is registered by an application, a default implementation will be used. The error method of this default handler throws an exception, while the warning method prints a warning message to the standard error output stream.

Parameters:
handler - the error handler

setUriResolver

public void setUriResolver(UriResolver resolver)
Registers a URI resolver.

An application may use this method to specify a custom URI resolver to be used for handling URIs in property values. By default URI specifications that do not denote an absolute location are considered relative to the input source URI, unless the baseURL option is set.

Parameters:
resolver - the URI resolver
See Also:
getUriResolver

getUriResolver

public UriResolver getUriResolver()
Returns the registered URI resolver.

Returns:
the URI resolver, or null if none has been specified
See Also:
setUriResolver

setXMLReader

public void setXMLReader(XMLReader reader)
Registers an external XML reader.

An application may use this method to specify an external XML reader (org.xml.sax.XMLReader object) to be used for the conversion. If no XML reader is specified one is created internally.

Parameters:
reader - the XML reader
See Also:
getXMLReader

getXMLReader

public XMLReader getXMLReader()
Returns the registered external XML reader.

Returns:
the external XML reader, or null if none has been specified
See Also:
setXMLReader

setOutput

protected void setOutput(OutputDestination output)

convert

public void convert(InputSource input,
                    OutputDestination output)
             throws Exception
Performs conversion.

Parameters:
input - the input source.

Unless the input source has been specified simply as a systemId, the client code has the responsiblity to close the input stream/reader.

output - the output destination.

Unless the output destination has been specified simply as a fileName, the client code has the responsiblity to close the output stream/writer.

Throws:
Exception - if an error occurs

abort

protected void abort()

clean

protected void clean()
              throws Exception
Throws:
Exception

finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable

startDocument

public void startDocument()
                   throws SAXException
Not for public use: implementation of ContentHandler.

Specified by:
startDocument in interface ContentHandler
Overrides:
startDocument in class DefaultHandler
Throws:
SAXException

loadUserStyles

protected UserStyles loadUserStyles(URL url)
                             throws Exception
Throws:
Exception

endDocument

public void endDocument()
                 throws SAXException
Not for public use: implementation of ContentHandler.

Specified by:
endDocument in interface ContentHandler
Overrides:
endDocument in class DefaultHandler
Throws:
SAXException

startElement

public void startElement(String namespace,
                         String localName,
                         String qualifiedName,
                         Attributes attributes)
                  throws SAXException
Not for public use: implementation of ContentHandler.

Specified by:
startElement in interface ContentHandler
Overrides:
startElement in class DefaultHandler
Throws:
SAXException

endElement

public void endElement(String namespace,
                       String localName,
                       String qualifiedName)
                throws SAXException
Not for public use: implementation of ContentHandler.

Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class DefaultHandler
Throws:
SAXException

ignorableWhitespace

public void ignorableWhitespace(char[] chars,
                                int start,
                                int length)
                         throws SAXException
Not for public use: implementation of ContentHandler.

Specified by:
ignorableWhitespace in interface ContentHandler
Overrides:
ignorableWhitespace in class DefaultHandler
Throws:
SAXException

characters

public void characters(char[] chars,
                       int start,
                       int length)
                throws SAXException
Not for public use: implementation of ContentHandler.

Specified by:
characters in interface ContentHandler
Overrides:
characters in class DefaultHandler
Throws:
SAXException

processingInstruction

public void processingInstruction(String target,
                                  String data)
                           throws SAXException
Not for public use: implementation of ContentHandler.

Specified by:
processingInstruction in interface ContentHandler
Overrides:
processingInstruction in class DefaultHandler
Throws:
SAXException

skippedEntity

public void skippedEntity(String name)
                   throws SAXException
Not for public use: implementation of ContentHandler.

Specified by:
skippedEntity in interface ContentHandler
Overrides:
skippedEntity in class DefaultHandler
Throws:
SAXException

startPrefixMapping

public void startPrefixMapping(String prefix,
                               String uri)
                        throws SAXException
Not for public use: implementation of ContentHandler.

Specified by:
startPrefixMapping in interface ContentHandler
Overrides:
startPrefixMapping in class DefaultHandler
Throws:
SAXException

endPrefixMapping

public void endPrefixMapping(String prefix)
                      throws SAXException
Not for public use: implementation of ContentHandler.

Specified by:
endPrefixMapping in interface ContentHandler
Overrides:
endPrefixMapping in class DefaultHandler
Throws:
SAXException

setDocumentLocator

public void setDocumentLocator(Locator locator)
Not for public use: implementation of ContentHandler.

Specified by:
setDocumentLocator in interface ContentHandler
Overrides:
setDocumentLocator in class DefaultHandler

initFontSize

protected void initFontSize(com.xmlmind.fo.properties.PropertyValues properties)

setFont

protected void setFont(com.xmlmind.fo.properties.PropertyValues properties,
                       String font)
                throws SAXException
Throws:
SAXException

setFontSize

protected void setFontSize(com.xmlmind.fo.properties.PropertyValues properties,
                           String fontSize)
                    throws SAXException
Throws:
SAXException

setProperties

protected void setProperties(com.xmlmind.fo.properties.PropertyValues properties,
                             Attributes attributes,
                             boolean computeLineHeight)
                      throws SAXException
Throws:
SAXException

setDefaultValues

protected void setDefaultValues(com.xmlmind.fo.properties.PropertyValues properties)
Set contextual default values.


warning

protected void warning(String message)
                throws SAXException
Throws:
SAXException

error

protected void error(String message)
              throws SAXException
Throws:
SAXException

error

public void error(Exception exception)
           throws Exception
Not for public use: implementation of ErrorHandler.

Throws:
Exception

warning

public void warning(Exception exception)
             throws Exception
Not for public use: implementation of ErrorHandler.

Throws:
Exception

fatalError

public void fatalError(SAXParseException exception)
                throws SAXException
Not for public use: implementation of ErrorHandler.

Specified by:
fatalError in interface ErrorHandler
Overrides:
fatalError in class DefaultHandler
Throws:
SAXException

error

public void error(SAXParseException exception)
           throws SAXException
Not for public use: implementation of ErrorHandler.

Specified by:
error in interface ErrorHandler
Overrides:
error in class DefaultHandler
Throws:
SAXException

warning

public void warning(SAXParseException exception)
             throws SAXException
Not for public use: implementation of ErrorHandler.

Specified by:
warning in interface ErrorHandler
Overrides:
warning in class DefaultHandler
Throws:
SAXException

resolve

public URL resolve(String spec,
                   URL base)
            throws Exception
Not for public use: implementation of UriResolver.

Specified by:
resolve in interface UriResolver
Parameters:
spec - the URI specification
base - the input source URI, or null if unknown
Returns:
the resolved URI, or null if undefined
Throws:
Exception - if an error occurs

resolve

public String resolve(String uri)
               throws Exception
Not for public use: implementation of Translator.UriResolver.

Specified by:
resolve in interface com.xmlmind.fo.converter.Translator.UriResolver
Throws:
Exception

main

public static void main(String[] args)
                 throws Exception
Not for public use: a simple test.

Throws:
Exception