org.apache.xalan.lib
Class Redirect

java.lang.Object
  extended by org.apache.xalan.lib.Redirect

public class Redirect
extends java.lang.Object

Implements three extension elements to allow an XSLT transformation to redirect its output to multiple output files. It is accessed by specifying a namespace URI as follows:

    xmlns:redirect="http://xml.apache.org/xalan/redirect"
 

You can either just use redirect:write, in which case the file will be opened and immediately closed after the write, or you can bracket the write calls by redirect:open and redirect:close, in which case the file will be kept open for multiple writes until the close call is encountered. Calls can be nested.

Calls can take a 'file' attribute and/or a 'select' attribute in order to get the filename. If a select attribute is encountered, it will evaluate that expression for a string that indicates the filename. If the string evaluates to empty, it will attempt to use the 'file' attribute as a default. Filenames can be relative or absolute. If they are relative, the base directory will be the same as the base directory for the output document. This is obtained by calling getOutputTarget() on the TransformerImpl. You can set this base directory by calling TransformerImpl.setOutputTarget() or it is automatically set when using the two argument form of transform() or transformNode().

Calls to redirect:write and redirect:open also take an optional attribute append="true|yes", which will attempt to simply append to an existing file instead of always opening a new file. The default behavior of always overwriting the file still happens if you do not specify append.

Note: this may give unexpected results when using xml or html output methods, since this is not coordinated with the serializers - hence, you may get extra xml decls in the middle of your file after appending to it.

Example:

 <?xml version="1.0"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                 version="1.0"
                 xmlns:redirect="http://xml.apache.org/xalan/redirect"
                 extension-element-prefixes="redirect">

   <xsl:template match="/">
     <out>
       default output.
     </out>
     <redirect:open file="doc3.out"/>
     <redirect:write file="doc3.out">
       <out>
         <redirect:write file="doc1.out">
           <out>
             doc1 output.
             <redirect:write file="doc3.out">
               Some text to doc3
             </redirect:write>
           </out>
         </redirect:write>
         <redirect:write file="doc2.out">
           <out>
             doc2 output.
             <redirect:write file="doc3.out">
               Some more text to doc3
               <redirect:write select="doc/foo">
                 text for doc4
               </redirect:write>
             </redirect:write>
           </out>
         </redirect:write>
       </out>
     </redirect:write>
     <redirect:close file="doc3.out"/>
   </xsl:template>

 </xsl:stylesheet>
 

See Also:
Example with Redirect extension

Field Summary
static boolean DEFAULT_APPEND_OPEN
          Default append mode for bare open calls.
static boolean DEFAULT_APPEND_WRITE
          Default append mode for bare write calls.
protected  java.util.Hashtable m_formatterListeners
          List of formatter listeners indexed by filename.
protected  java.util.Hashtable m_outputStreams
          List of output streams indexed by filename.
 
Constructor Summary
Redirect()
           
 
Method Summary
 void close(XSLProcessorContext context, ElemExtensionCall elem)
          Close the given file and remove it from the formatter listener's table.
 SerializationHandler createSerializationHandler(TransformerImpl transformer, java.io.FileOutputStream ostream, java.io.File file, OutputProperties format)
          A class that extends this one could over-ride this public method and receive a callback for the creation of the serializer used in the redirection.
 void endRedirection(TransformerImpl transf)
          A class that extends this class can over-ride this public method and receive a callback that redirection to the ContentHandler specified in the startRedirection() call has ended
private  java.lang.String getFilename(XSLProcessorContext context, ElemExtensionCall elem)
          Get the filename from the 'select' or the 'file' attribute.
private  org.xml.sax.ContentHandler makeFormatterListener(XSLProcessorContext context, ElemExtensionCall elem, java.lang.String fileName, boolean shouldPutInTable, boolean mkdirs, boolean append)
          Create a new ContentHandler, based on attributes of the current ContentHandler.
 void open(XSLProcessorContext context, ElemExtensionCall elem)
          Open the given file and put it in the XML, HTML, or Text formatter listener's table.
 void startRedirection(TransformerImpl transf, org.xml.sax.ContentHandler formatter)
          A class that extends this class can over-ride this public method and recieve a callback that redirection is about to start
private  java.lang.String urlToFileName(java.lang.String base)
           
 void write(XSLProcessorContext context, ElemExtensionCall elem)
          Write the evalutation of the element children to the given file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_formatterListeners

protected java.util.Hashtable m_formatterListeners
List of formatter listeners indexed by filename.


m_outputStreams

protected java.util.Hashtable m_outputStreams
List of output streams indexed by filename.


DEFAULT_APPEND_OPEN

public static final boolean DEFAULT_APPEND_OPEN
Default append mode for bare open calls. False for backwards compatibility (I think).

See Also:
Constant Field Values

DEFAULT_APPEND_WRITE

public static final boolean DEFAULT_APPEND_WRITE
Default append mode for bare write calls. False for backwards compatibility.

See Also:
Constant Field Values
Constructor Detail

Redirect

public Redirect()
Method Detail

open

public void open(XSLProcessorContext context,
                 ElemExtensionCall elem)
          throws java.net.MalformedURLException,
                 java.io.FileNotFoundException,
                 java.io.IOException,
                 javax.xml.transform.TransformerException
Open the given file and put it in the XML, HTML, or Text formatter listener's table.

Throws:
java.net.MalformedURLException
java.io.FileNotFoundException
java.io.IOException
javax.xml.transform.TransformerException

write

public void write(XSLProcessorContext context,
                  ElemExtensionCall elem)
           throws java.net.MalformedURLException,
                  java.io.FileNotFoundException,
                  java.io.IOException,
                  javax.xml.transform.TransformerException
Write the evalutation of the element children to the given file. Then close the file unless it was opened with the open extension element and is in the formatter listener's table.

Throws:
java.net.MalformedURLException
java.io.FileNotFoundException
java.io.IOException
javax.xml.transform.TransformerException

close

public void close(XSLProcessorContext context,
                  ElemExtensionCall elem)
           throws java.net.MalformedURLException,
                  java.io.FileNotFoundException,
                  java.io.IOException,
                  javax.xml.transform.TransformerException
Close the given file and remove it from the formatter listener's table.

Throws:
java.net.MalformedURLException
java.io.FileNotFoundException
java.io.IOException
javax.xml.transform.TransformerException

getFilename

private java.lang.String getFilename(XSLProcessorContext context,
                                     ElemExtensionCall elem)
                              throws java.net.MalformedURLException,
                                     java.io.FileNotFoundException,
                                     java.io.IOException,
                                     javax.xml.transform.TransformerException
Get the filename from the 'select' or the 'file' attribute.

Throws:
java.net.MalformedURLException
java.io.FileNotFoundException
java.io.IOException
javax.xml.transform.TransformerException

urlToFileName

private java.lang.String urlToFileName(java.lang.String base)

makeFormatterListener

private org.xml.sax.ContentHandler makeFormatterListener(XSLProcessorContext context,
                                                         ElemExtensionCall elem,
                                                         java.lang.String fileName,
                                                         boolean shouldPutInTable,
                                                         boolean mkdirs,
                                                         boolean append)
                                                  throws java.net.MalformedURLException,
                                                         java.io.FileNotFoundException,
                                                         java.io.IOException,
                                                         javax.xml.transform.TransformerException
Create a new ContentHandler, based on attributes of the current ContentHandler.

Throws:
java.net.MalformedURLException
java.io.FileNotFoundException
java.io.IOException
javax.xml.transform.TransformerException

startRedirection

public void startRedirection(TransformerImpl transf,
                             org.xml.sax.ContentHandler formatter)
A class that extends this class can over-ride this public method and recieve a callback that redirection is about to start

Parameters:
transf - The transformer.
formatter - The handler that receives the redirected output

endRedirection

public void endRedirection(TransformerImpl transf)
A class that extends this class can over-ride this public method and receive a callback that redirection to the ContentHandler specified in the startRedirection() call has ended

Parameters:
transf - The transformer.

createSerializationHandler

public SerializationHandler createSerializationHandler(TransformerImpl transformer,
                                                       java.io.FileOutputStream ostream,
                                                       java.io.File file,
                                                       OutputProperties format)
                                                throws java.io.IOException,
                                                       javax.xml.transform.TransformerException
A class that extends this one could over-ride this public method and receive a callback for the creation of the serializer used in the redirection.

Parameters:
transformer - The transformer
ostream - The output stream that the serializer wraps
file - The file associated with the ostream
format - The format parameter used to create the serializer
Returns:
the serializer that the redirection will go to.
Throws:
java.io.IOException
javax.xml.transform.TransformerException