KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > commands > FOPCommand


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

50
51 package org.openlaszlo.iv.flash.commands;
52
53 import org.openlaszlo.iv.flash.parser.*;
54 import org.openlaszlo.iv.flash.api.*;
55 import org.openlaszlo.iv.flash.api.image.*;
56 import org.openlaszlo.iv.flash.api.shape.*;
57 import org.openlaszlo.iv.flash.api.text.*;
58 import org.openlaszlo.iv.flash.util.*;
59 import org.openlaszlo.iv.flash.cache.*;
60 import org.openlaszlo.iv.flash.url.*;
61
62 import org.openlaszlo.iv.flash.fop.*;
63 import org.openlaszlo.iv.flash.context.Context;
64 import java.io.*;
65 import java.util.*;
66 import java.awt.geom.*;
67
68 /**
69  * Insert Text generator command<BR>
70  *
71  * @author James Taylor
72  */

73
74 public class FOPCommand extends GenericCommand
75 {
76     public FOPCommand() {}
77
78     public void doCommand( FlashFile file, Context context, Script parent, int frame ) throws IVException
79     {
80         String JavaDoc filename = getParameter( context, "filename" );
81         boolean cache = getBoolParameter( context, "cache", false );
82         boolean scale = getBoolParameter( context, "scale", true );
83         boolean maintainAspectRatio = getBoolParameter( context, "maintainAspectRatio", true );
84         String JavaDoc linkHandler = getParameter( context, "linkhandler", null );
85         String JavaDoc instancename = getParameter( context, "instancename" );
86
87         Instance inst = getInstance();
88
89         if( filename == null )
90         {
91             throw new IVException( Resource.NOTEXT );
92         }
93
94         String JavaDoc text = null;
95
96         IVUrl url = IVUrl.newUrl( filename, file );
97
98         // check in cache first
99

100         if( cache )
101         {
102             text = ( String JavaDoc ) MediaCache.getMedia( url );
103         }
104
105         if( text == null )
106         {
107             InputStream is = null;
108
109             try
110             {
111                 is = url.getInputStream();
112                 BufferedReader reader = new BufferedReader( new InputStreamReader(is) );
113                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
114                 String JavaDoc s = reader.readLine();
115
116                 while( s != null )
117                 {
118                     sb.append( s );
119                     sb.append( '\n' );
120                     s = reader.readLine();
121                 }
122
123                 text = sb.toString();
124
125             }
126             catch( IOException e )
127             {
128                 throw new IVException(Resource.ERRCMDFILEREAD, new Object JavaDoc[] {url.getName(), getCommandName()}, e);
129             }
130             finally
131             {
132                 try
133                 {
134                     if ( is != null ) is.close();
135                 }
136                 catch( IOException e ) {}
137             }
138
139             MediaCache.addMedia( url, text, text.length(), cache );
140         }
141
142         try
143         {
144             FOPHelper fopHelper = new FOPHelper();
145
146             fopHelper.setXSLFOInputSource( text );
147
148             if ( linkHandler != null )
149             {
150                 fopHelper.setLinkHandler( linkHandler );
151             }
152
153             fopHelper.render();
154
155             Script script = fopHelper.getScript();
156
157             inst.setScript( script );
158
159             if ( scale )
160             {
161                 Rectangle2D rect = fopHelper.getMaxPageBounds();
162                 AffineTransform m = inst.matrix;
163
164                 double width = rect.getWidth();
165                 double height = rect.getHeight();
166
167                 double widthFactor = ( m.getScaleX() / width );
168                 double heightFactor = ( m.getScaleY() / height );
169
170                 if ( maintainAspectRatio )
171                 {
172                     if ( widthFactor < heightFactor )
173                     {
174                         heightFactor = widthFactor;
175                     }
176                     else
177                     {
178                         widthFactor = heightFactor;
179                     }
180                 }
181
182                 double scaleX = 2048.0 * widthFactor;
183                 double scaleY = 2048.0 * heightFactor;
184                 inst.matrix = new AffineTransform( scaleX, 0, 0, scaleY,
185                     m.getTranslateX() - ( scaleX * width ) / 2,
186                     m.getTranslateY() - ( scaleY * height ) / 2
187                 );
188
189             }
190         }
191         catch ( Exception JavaDoc e )
192         {
193             throw new IVException( e );
194         }
195
196         if ( instancename != null )
197         {
198             inst.name = instancename;
199         }
200     }
201 }
202
203
Popular Tags