Class Converter
- java.lang.Object
-
- org.xml.sax.helpers.DefaultHandler
-
- com.xmlmind.fo.converter.Converter
-
- All Implemented Interfaces:
ErrorHandler,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
Converterclass implements the Formatting Objects conversion engine. A typical use is as follows:- Set conversion options using the
setPropertyorsetPropertiesmethod. - Create and initialize the source and destination specifications (an
org.xml.sax.InputSourceand anOutputDestinationobject respectively). - Perform the conversion using the
convertmethod.
Alternatively, a properly initialized instance of this class may be registered as a
ContentHandlerwith an external SAX parser.
-
-
Field Summary
Fields Modifier and Type Field Description protected com.xmlmind.fo.converter.Contextcontext
-
Constructor Summary
Constructors Constructor Description Converter()Creates a newConverterinstance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidabort()protected voidbadPropertyWarning(String key, String value, String reason)voidcharacters(char[] chars, int start, int length)Not for public use: implementation of ContentHandler.protected voidclean()voidconvert(InputSource input, OutputDestination output)Performs conversion.voidendDocument()Not for public use: implementation of ContentHandler.voidendElement(String namespace, String localName, String qualifiedName)Not for public use: implementation of ContentHandler.voidendPrefixMapping(String prefix)Not for public use: implementation of ContentHandler.voiderror(Exception exception)Not for public use: implementation of ErrorHandler.protected voiderror(String message)voiderror(SAXParseException exception)Not for public use: implementation of ErrorHandler.voidfatalError(SAXParseException exception)Not for public use: implementation of ErrorHandler.protected voidfinalize()UriResolvergetUriResolver()Returns the registered URI resolver.UserStylesgetUserStyles()Returns the set of user styles specified usingsetUserStyles(com.xmlmind.fo.converter.UserStyles).XMLReadergetXMLReader()Returns the registered external XML reader.voidignorableWhitespace(char[] chars, int start, int length)Not for public use: implementation of ContentHandler.protected voidinitFontSize(com.xmlmind.fo.properties.PropertyValues properties)static String[]listEncodings(String format)Returns the list of available encodings for the specified output format.protected UserStylesloadUserStyles(URL url)static voidmain(String[] args)Not for public use: a simple test.voidprocessingInstruction(String target, String data)Not for public use: implementation of ContentHandler.Stringresolve(String uri)Not for public use: implementation of Translator.UriResolver.URLresolve(String spec, URL base)Not for public use: implementation of UriResolver.protected voidsetDefaultValues(com.xmlmind.fo.properties.PropertyValues properties)Set contextual default values.voidsetDocumentLocator(Locator locator)Not for public use: implementation of ContentHandler.voidsetErrorHandler(ErrorHandler handler)Registers a conversion error handler.protected voidsetFont(com.xmlmind.fo.properties.PropertyValues properties, String font)protected voidsetFontSize(com.xmlmind.fo.properties.PropertyValues properties, String fontSize)protected voidsetOutput(OutputDestination output)protected voidsetProperties(com.xmlmind.fo.properties.PropertyValues properties, Attributes attributes, boolean computeLineHeight)voidsetProperties(Properties properties)Sets conversion options.voidsetProperty(String key, String value)Sets a conversion option.voidsetUriResolver(UriResolver resolver)Registers a URI resolver.voidsetUserStyles(UserStyles styles)Specifies the set of user styles during the conversion.voidsetXMLReader(XMLReader reader)Registers an external XML reader.voidskippedEntity(String name)Not for public use: implementation of ContentHandler.voidstartDocument()Not for public use: implementation of ContentHandler.voidstartElement(String namespace, String localName, String qualifiedName, Attributes attributes)Not for public use: implementation of ContentHandler.voidstartPrefixMapping(String prefix, String uri)Not for public use: implementation of ContentHandler.voidwarning(Exception exception)Not for public use: implementation of ErrorHandler.protected voidwarning(String message)voidwarning(SAXParseException exception)Not for public use: implementation of ErrorHandler.-
Methods inherited from class org.xml.sax.helpers.DefaultHandler
notationDecl, resolveEntity, unparsedEntityDecl
-
-
-
-
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,docxorodt)- Returns:
- the list of available encodings
- Throws:
IllegalArgumentException- if theformatargument 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.
Conversion parameters Name Value Description Default alwaysSaveAsPNGtrue,false,jpegorsvgIf jpegortrue, do not attempt to save JPEG images as JPEG files in the output file. Instead always convert JPEG images to PNG.If
svgortrue, do not attempt to save SVG images as SVG files in the output file. Instead always convert SVG images to PNG.falsefor the ODT output format (which supports SVG);svgfor all the other output formats (which do not support SVG).baseURL(1)URL base URL of relative paths in property values None (paths are taken relative to the input source URL) detectListstrueorfalseCreate proper lists by inferring the numbering style of the list from the label of its first item. truedocx.keepSVGtrueorfalseIf false, discard input SVG graphics after converting them to PNG. Only keep the converted PNG graphics in the DOCX output file. Iftrue, keep both input SVG graphics and the converted PNG graphics in the DOCX output file. Ignored ifdocx.useVML=true.truedocx.useVMLtrueorfalseUse VML -deprecated- elements rather than DrawingML elements to represent images falsedocx.variantMS-Word_version[strict]?Marks generated DOCX file as being compatible with MS-Word having specified major version. Any version other than 14(MS-Word 2010),15(MS-Word 2013),16(MS-Word 2016) is currently ignored.For example, specifying
15suppresses the "[Compatibility Mode]" text appearing in the title bar of MS-Word 2013 AND 2016.Moreover suffix
strict(only whenMS-Word_version>= 15; example15strict) may be used to generate a DOCX file marked as being "Strict Open XML".None. Generated DOCX file is not marked as being compatible with a specific version of MS-Word. eastAsiaFontFamiliesA string having this syntax: map = entry [',' entry]* entry = east_asian_family '=' western_family
May be used to map East Asian font families to Western font families. Such East Asian fonts are used to render mainly CJK text, possibly mixed with Western text. Example: MS Mincho=Cambria,Meiryo=CalibriArial Unicode MS=Arial(for compatibility with previous versions of XFC)genericFontFamiliesA 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 NewimageResolutionPositive 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 imageRendererResolutionPositive 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.
outputEncodingSee below output encoding See below outputFormatrtf,wml,docxorodtoutput format rtfprescaleImagestrueorfalseimage scaling policy (2) falseprotectionA string having this syntax: 'limited-formatting' | ('read-only'|'comments-only'|'fill-forms-only'| 'tracked-changes-only' [ '+limited-formatting' ]?)Specifies how the generated document is to be restricted in terms of editing and/or formatting. None. The document is not protected. meta.metadata_nameThe value of the metadata Specifies a metadata to be added to the document information section of the generated document. N/A rtf.targetMSWordtarget RTF viewer None (no specified target) screenResolutionPositive integer screen resolution in DPI. Used to convert pxlengths to other units.96 set.graphic_factory_name.parameter_nameDepends 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.YYYimmediately 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"); // DPIwithout actually using converterconvis a simple way to configure graphic factorySVG.Depends on the parameter singleSidedLayouttrueorfalsesingle-sided page layout (3) falsestylesAn 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. unprotectPasswordA clear text password This password lets the user remove the restrictions specified by property protection.None. The document protection is not enforced using a password. The
outputEncodingproperty possible values depend on the target output format:- For RTF output supported values are
ASCII,Cp1250(Windows Eastern European),Cp1251(Windows Cyrillic) andCp1252(Windows Latin-1). The default value isCp1252.The encoding specified in the
OutputDestinationpassed toconvert(org.xml.sax.InputSource, com.xmlmind.fo.converter.OutputDestination)has priority over the encoding specified by propertyoutputEncoding. - For WML output all encodings available in the current JVM are
supported. The property value may be either the encoding name (e.g.
ISO8859_1) or the charset name (e.g.ISO-8859-1). The complete list of supported encodings is returned by thelistEncodingsmethod. The default value isCp1252.The encoding specified in the
OutputDestinationpassed toconvert(org.xml.sax.InputSource, com.xmlmind.fo.converter.OutputDestination)has priority over the encoding specified by propertyoutputEncoding. - For Open XML output the
outputEncodingproperty specifiesthe encoding of XML content in the output document. Supported values areUTF-8andUTF-16. The default value isUTF-8.The encoding specified in the
OutputDestinationpassed toconvert(org.xml.sax.InputSource, com.xmlmind.fo.converter.OutputDestination)is ignored. Only the encoding possibly specified by propertyoutputEncodingis taken into account. - For OpenDocument output the
outputEncodingproperty specifies the encoding of XML content (filesstyles.xmlandcontent.xml) in the output document. All encodings available in the current JVM are supported. The property value may be either the encoding name (e.g.ISO8859_1) or the charset name (e.g.ISO-8859-1). The complete list of supported encodings is returned by thelistEncodingsmethod. The default value isUTF8.The encoding specified in the
OutputDestinationpassed toconvert(org.xml.sax.InputSource, com.xmlmind.fo.converter.OutputDestination)is ignored. Only the encoding possibly specified by propertyoutputEncodingis taken into account.
(1) Use of the
baseURLproperty 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
prescaleImagesproperty totrueif 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
singleSidedLayoutproperty must be set totrue.- Parameters:
key- the property namevalue- the property value. Specifynullto remove propertykey.- See Also:
setProperties
- For RTF output supported values are
-
setProperties
public void setProperties(Properties properties)
Sets conversion options.All conversion options are specified as standard Java properties. See the
setPropertymethod 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
setPropertyis used, the user styles are parsed each timeconvert(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 benull- See Also:
UserStyles.loadFrom(java.io.File),getUserStyles()
-
getUserStyles
public UserStyles getUserStyles()
Returns the set of user styles specified usingsetUserStyles(com.xmlmind.fo.converter.UserStyles). May returnnull.
-
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
errormethod of this default handler throws an exception, while thewarningmethod 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
option is set.baseURL- Parameters:
resolver- the URI resolver- See Also:
getUriResolver
-
getUriResolver
public UriResolver getUriResolver()
Returns the registered URI resolver.- Returns:
- the URI resolver, or
nullif 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.XMLReaderobject) 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
nullif 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()
-
finalize
protected void finalize() throws Throwable
-
startDocument
public void startDocument() throws SAXExceptionNot for public use: implementation of ContentHandler.- Specified by:
startDocumentin interfaceContentHandler- Overrides:
startDocumentin classDefaultHandler- Throws:
SAXException
-
loadUserStyles
protected UserStyles loadUserStyles(URL url) throws Exception
- Throws:
Exception
-
endDocument
public void endDocument() throws SAXExceptionNot for public use: implementation of ContentHandler.- Specified by:
endDocumentin interfaceContentHandler- Overrides:
endDocumentin classDefaultHandler- 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:
startElementin interfaceContentHandler- Overrides:
startElementin classDefaultHandler- Throws:
SAXException
-
endElement
public void endElement(String namespace, String localName, String qualifiedName) throws SAXException
Not for public use: implementation of ContentHandler.- Specified by:
endElementin interfaceContentHandler- Overrides:
endElementin classDefaultHandler- Throws:
SAXException
-
ignorableWhitespace
public void ignorableWhitespace(char[] chars, int start, int length) throws SAXExceptionNot for public use: implementation of ContentHandler.- Specified by:
ignorableWhitespacein interfaceContentHandler- Overrides:
ignorableWhitespacein classDefaultHandler- Throws:
SAXException
-
characters
public void characters(char[] chars, int start, int length) throws SAXExceptionNot for public use: implementation of ContentHandler.- Specified by:
charactersin interfaceContentHandler- Overrides:
charactersin classDefaultHandler- Throws:
SAXException
-
processingInstruction
public void processingInstruction(String target, String data) throws SAXException
Not for public use: implementation of ContentHandler.- Specified by:
processingInstructionin interfaceContentHandler- Overrides:
processingInstructionin classDefaultHandler- Throws:
SAXException
-
skippedEntity
public void skippedEntity(String name) throws SAXException
Not for public use: implementation of ContentHandler.- Specified by:
skippedEntityin interfaceContentHandler- Overrides:
skippedEntityin classDefaultHandler- Throws:
SAXException
-
startPrefixMapping
public void startPrefixMapping(String prefix, String uri) throws SAXException
Not for public use: implementation of ContentHandler.- Specified by:
startPrefixMappingin interfaceContentHandler- Overrides:
startPrefixMappingin classDefaultHandler- Throws:
SAXException
-
endPrefixMapping
public void endPrefixMapping(String prefix) throws SAXException
Not for public use: implementation of ContentHandler.- Specified by:
endPrefixMappingin interfaceContentHandler- Overrides:
endPrefixMappingin classDefaultHandler- Throws:
SAXException
-
setDocumentLocator
public void setDocumentLocator(Locator locator)
Not for public use: implementation of ContentHandler.- Specified by:
setDocumentLocatorin interfaceContentHandler- Overrides:
setDocumentLocatorin classDefaultHandler
-
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.- Specified by:
errorin interfaceErrorHandler- Parameters:
exception- the error information, encapsulated in an exception- Throws:
Exception- if an error occurs
-
warning
public void warning(Exception exception) throws Exception
Not for public use: implementation of ErrorHandler.- Specified by:
warningin interfaceErrorHandler- Parameters:
exception- the warning information, encapsulated in an exception- Throws:
Exception- if an error occurs
-
fatalError
public void fatalError(SAXParseException exception) throws SAXException
Not for public use: implementation of ErrorHandler.- Specified by:
fatalErrorin interfaceErrorHandler- Overrides:
fatalErrorin classDefaultHandler- Throws:
SAXException
-
error
public void error(SAXParseException exception) throws SAXException
Not for public use: implementation of ErrorHandler.- Specified by:
errorin interfaceErrorHandler- Overrides:
errorin classDefaultHandler- Throws:
SAXException
-
warning
public void warning(SAXParseException exception) throws SAXException
Not for public use: implementation of ErrorHandler.- Specified by:
warningin interfaceErrorHandler- Overrides:
warningin classDefaultHandler- Throws:
SAXException
-
resolve
public URL resolve(String spec, URL base) throws Exception
Not for public use: implementation of UriResolver.- Specified by:
resolvein interfaceUriResolver- Parameters:
spec- the URI specificationbase- the input source URI, ornullif unknown- Returns:
- the resolved URI, or
nullif 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:
resolvein interfacecom.xmlmind.fo.converter.Translator.UriResolver- Throws:
Exception
-
-