KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > fop > FOToSWF


1 /*
2  * $Id: FOToSWF.java,v 1.3 2002/02/24 02:10:19 skavish Exp $
3  *
4  * ==========================================================================
5  * The JGenerator Software License, Version 1.0
6  *
7  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution, if
21  * any, must include the following acknowlegement:
22  * "This product includes software developed by Dmitry Skavish
23  * (skavish@usa.net, http://www.flashgap.com/)."
24  * Alternately, this acknowlegement may appear in the software itself,
25  * if and wherever such third-party acknowlegements normally appear.
26  *
27  * 4. The name "The JGenerator" must not be used to endorse or promote
28  * products derived from this software without prior written permission.
29  * For written permission, please contact skavish@usa.net.
30  *
31  * 5. Products derived from this software may not be called "The JGenerator"
32  * nor may "The JGenerator" appear in their names without prior written
33  * permission of Dmitry Skavish.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
39  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  *
48  */

49
50 package org.openlaszlo.iv.flash.fop;
51
52 import org.openlaszlo.iv.flash.util.*;
53
54 import org.openlaszlo.iv.flash.api.*;
55
56 import org.apache.fop.apps.*;
57 import org.apache.fop.messaging.MessageHandler;
58 import org.apache.fop.viewer.*;
59
60 import org.xml.sax.*;
61 import org.apache.xerces.parsers.*;
62
63 import java.io.*;
64 import java.net.*;
65 import java.util.*;
66
67 /**
68  * A command line launcher using FOPHelper to convert FO input to SWF output.
69  * Mostly ripped out of "SWFStarter" right now.
70  *
71  * @author James Taylor
72  * @author Johan "Spocke" Sörlin
73  */

74
75 class FOToSWF
76 {
77     private InputSource xslFOInput;
78     private OutputStream swfOutputStream;
79
80     /**
81      * Changes the XSLFO InputSource, this input source is
82      * requierd in order to generate a SWF movie.
83      *
84      * @param input_source inputsource pointing to the XSLFO file/stream
85      */

86
87     public void setXSLFOInputSource( InputSource input_source )
88     {
89         this.xslFOInput = input_source;
90     }
91
92     /**
93      * Changes the output stream, this stream is where the output SWF movie
94      * will be stored.
95      *
96      * @param output_stream stream to write finished SWF movie to
97      */

98
99     public void setOutputStream( OutputStream output_stream )
100     {
101         this.swfOutputStream = output_stream;
102     }
103
104     /**
105      * Generates the SWF movie from the passed inputparameters using FOP.
106      */

107
108     public void generate( ) throws IVException, IOException, FOPException
109     {
110         FOPHelper fh = new FOPHelper();
111
112         fh.setXSLFOInputSource( this.xslFOInput );
113
114         fh.render();
115
116         Script s = fh.getScript();
117
118         FlashFile f = FlashFile.newFlashFile( );
119
120         s.setMain();
121
122         f.setMainScript( s );
123
124         f.setFrameSize( fh.getMaxPageBounds() );
125
126         FlashOutput fob = f.generate( );
127
128         BufferedOutputStream bos = new BufferedOutputStream( this.swfOutputStream );
129         bos.write( fob.getBuf( ), 0, fob.getSize( ) );
130         bos.flush( );
131         bos.close( );
132     }
133
134     /**
135      * Console main method, this method is executed when running in console mode.
136      *
137      * @param args application arguments
138      */

139
140     public static void main( String JavaDoc args[] )
141     {
142         FOToSWF starter = new FOToSWF();
143         String JavaDoc temp;
144         boolean inputFilePassed = false;
145
146         try
147         {
148             Util.init( );
149             Log.setLogToConsole( );
150
151             // Check num arguments
152
if ( args.length < 3 )
153             {
154                 printHelp( "Requierd arguments missing." );
155             }
156
157             // Get XSL-FO parameter
158
temp = getParameterByName( args, "-fo" );
159             if ( temp != null )
160             {
161                 starter.setXSLFOInputSource( new InputSource( temp ) );
162                 inputFilePassed = true;
163             }
164
165             // If no input file, pass error
166

167             if ( ! inputFilePassed )
168             {
169                 printHelp( "Input file missing, use argument \"-fo <infile>\"." );
170             }
171
172             // Get SWF output path
173

174             temp = getParameterByName( args, "-swf" );
175             if ( temp != null )
176             {
177                 starter.setOutputStream( new FileOutputStream( temp ) );
178             }
179             else
180             {
181                 printHelp( "Output file missing, use argument \"-swf <outfile>\"." );
182             }
183
184             // Do the generation
185

186             starter.generate( );
187
188             System.exit( 0 );
189         }
190         catch ( Exception JavaDoc e )
191         {
192             e.printStackTrace();
193         }
194     }
195
196
197     // * * Private class methods
198

199     /**
200      * Returns a parameter value by a parameter name.
201      *
202      * @param args application arguments
203      * @param name name of parameter to get
204      * @return parameter value or null if it wasn't found
205      */

206     private static String JavaDoc getParameterByName( String JavaDoc args[], String JavaDoc name )
207     {
208         for ( int i = 0; i < args.length; i++ )
209         {
210             if ( args[ i ].equals( name ) )
211             {
212                 if ( ( i + 1 ) < args.length )
213                     return args[ i + 1 ];
214                 else
215                     return null;
216             }
217         }
218
219         return null;
220     }
221
222     /**
223      * Checks the existance of a parameter by a parameter name.
224      *
225      * @param args application arguments
226      * @param name name of parameter to get
227      * @return true - it exists, false - it doesn't exist
228      */

229     private static boolean checkParameterByName( String JavaDoc args[], String JavaDoc name )
230     {
231         for ( int i = 0; i < args.length; i++ )
232         {
233             if ( args[ i ].equals( name ) )
234                 return true;
235         }
236
237         return false;
238     }
239
240     /**
241      * Prints the usage text.
242      *
243      * @param error_msg Error message to write with usage text
244      */

245     private static void printHelp( String JavaDoc error_msg )
246     {
247         System.out.println( "USAGE" );
248         System.out.println( "" );
249         System.out.println( "SWFStarter [options] -fo infile -swf outfile" );
250         System.out.println( "" );
251         System.out.println( " [INPUT]" );
252         System.out.println( " -fo infile xsl:fo input file" );
253         System.out.println( "" );
254         System.out.println( " [OUTPUT]" );
255         System.out.println( " -swf outfile input will be rendered as swf format into the outfile" );
256         System.out.println( "" );
257         System.out.println( " [Examples]" );
258         System.out.println( " SWFStarter -fo myfile.fo myfile.swf" );
259         System.out.println( "" );
260         System.out.println( "ERROR: " + error_msg );
261         System.out.println( "" );
262
263         System.exit( -1 );
264     }
265 }
266
Popular Tags