com.xmlmind.ditac.preprocess.PreProcessor
Advanced embedding method: first invoke a preprocessor which will generate intermediate .ditac files, then invoke the XSLT 2.0 engine in order to transform all these .ditac files.
ditac_lists.ditac_lists
file and
one or more .ditac files; then invoking the Saxon Embed2
sample is found
in Embed2.java
PreProcessor
to pre-process
the DITA source files into a
ditac_lists.ditac_lists
file and one or more
.ditac
files.PreProcessor
.Console console = new Console() { public void showMessage(String message, MessageType messageType) { System.err.println(message); } }; PreProcessor preProc = new PreProcessor(console); preProc.setChunking(Chunking.SINGLE); preProc.setMedia(Media.SCREEN); ResourceCopier resourceCopier = new ResourceCopier(); resourceCopier.parseParameters("img"); preProc.setResourceHandler(resourceCopier);
PreProcessor
.preProc.setChunking(Chunking.SINGLE)
PreProcessor
is not concerned about
the exact output format. However its behaves differently
depending on the target Media PreProcessor
handles to an ResourceHandler ResourceHandler
is
registered with a PreProcessor
using method
setResourceHandler Embed2
sample, we use the simplest
possible ResourceHandler
which is ResourceCopier URL inFileURL = null; try { inFileURL = inFile.toURI().toURL(); } catch (MalformedURLException cannotHappen) {} File[] preProcFiles = null; try { preProcFiles = preProc.process(new URL[] { inFileURL }, outFile); } catch (IOException e) { console.showMessage(e.toString(), Console.MessageType.ERROR); } if (preProcFiles == null) { return false; }
null
if an error other than an
IOException
has caused the pre-processing to
fail. When this is the case, errors messages are displayed on the
Console
.PreProcessor
is not thread-safe. Each thread must
own its PreProcessor
. However, the
process
method of a
PreProcessor
may be invoked several
times..ditac
files. Note that this is done using the
standard JAXP String ditacListsURI = ""; int count = preProcFiles.length; for (int i = 0; i < count; ++i) { File ditacFile = preProcFiles[i]; if (ditacFile.getPath().endsWith(".ditac_lists")) { ditacListsURI = ditacFile.toURI().toASCIIString(); break; } } String[] params = { "ditacListsURI", ditacListsURI, "xsl-resources-directory", "res", "use-note-icon", "yes", "default-table-width", "100%" };
TransformerFactory
, then configure this
TransformerFactory
.private static TransformerFactory createTransformerFactory(URIResolver uriResolver, ErrorListener errorListener) throws Exception { Class<?> cls = Class.forName("net.sf.saxon.TransformerFactoryImpl"); TransformerFactory transformerFactory = (TransformerFactory) cls.newInstance(); ExtensionFunctions.registerAll(transformerFactory); transformerFactory.setURIResolver(uriResolver); transformerFactory.setErrorListener(errorListener); return transformerFactory; }
Transformer
.private static Transformer createTransformer(String[] params, Console console) throws Exception { URIResolver uriResolver = Resolve.getURIResolver(); ErrorListener errorListener = new ConsoleErrorListener(console); TransformerFactory factory = createTransformerFactory(uriResolver, errorListener); File xslFile = AppUtil.getXSLResourceFile("xhtml/html.xsl"); Transformer transformer = factory.newTransformer(new StreamSource(xslFile)); transformer.setURIResolver(uriResolver); transformer.setErrorListener(errorListener); for (int i = 0; i < params.length; i += 2) { transformer.setParameter(params[i], params[i+1]); } return transformer; }
Resolve
automatically loads all the XML
catalogs specified using the xml.catalog.files Java™ system property. Excerpts of the
ant
build.xml <target name="embed2" depends="compile,clean_embed2"> <java classpathref="cp" fork="yes" classname="Embed2"> <sysproperty key="xml.catalog.files" value="${ditac.dir}/schema/catalog.xml" /> <arg value="${ditac.dir}/docsrc/manual/manual.ditamap" /> <arg value="manual.html" /> </java> </target>
ErrorListener
which
displays its messages on a Console
. ditac_install_dir/xsl/
)..ditac
file.for (int i = 0; i < count; ++i) { File ditacFile = preProcFiles[i]; String ditacFilePath = ditacFile.getPath(); if (ditacFilePath.endsWith(".ditac")) { File transformedFile = new File( ditacFilePath.substring(0, ditacFilePath.length()-5) + "html"); try { transformer.transform(new StreamSource(ditacFile), new StreamResult(transformedFile)); } catch (Exception e) { console.showMessage(e.toString(), Console.MessageType.ERROR); cleanUp(preProcFiles); return false; } } }
Embed2
, the above loop is not
strictly needed. We specified
preProc.setChunking(Chunking.SINGLE)
and therefore
the PreProcessor
generates a single
.ditac file.res/
. Note that
the images referenced in the DITA source, if any, have already been
copied to output subdirectory img/
by the
ImageCopier
.File dstDir = new File("res"); if (!dstDir.exists()) { File srcDir = AppUtil.getXSLResourceFile("xhtml/resources"); try { FileUtil.copyDir(srcDir, dstDir, false); } catch (IOException e) { console.showMessage(e.toString(), Console.MessageType.ERROR); cleanUp(preProcFiles); return false; } }
ditac_lists.ditac_lists
and
.ditac
files.cleanUp(preProcFiles);
ditac.jar
, xmlresolver.jar
,
saxon12.jar
, etc, which are all listed in
ditac_install_dir/doc/manual/embed/build.xml
(see below), this kind of embedding also needs to access:ditac_install_dir/schema/
.ditac_install_dir/xsl/
.ditac_install_dir/schema/catalog.xml
or to an equivalent of this XML catalog.ditac_install_dir/schema/catalog.xml
contains the following entry:
<rewriteURI uriStartString="ditac-xsl:" rewritePrefix="../xsl/" />This
<rewriteURI>
entry Embed2
sampleEmbed2
sample by running
ant
in
ditac_install_dir/doc/manual/embed/
.Embed2
sample by running
ant
embed2 in
ditac_install_dir/doc/manual/embed/
.
This will convert
ditac_install_dir/docsrc/manual/manual.ditamap
to single HTML 4.01 page
ditac_install_dir/doc/manual/embed/manual.html
.