Source code: org/jfor/jfor/converter/ExternalGraphicBuilder.java
1 package org.jfor.jfor.converter;
2
3 import org.jfor.jfor.converter.AbstractBuilder;
4 import org.jfor.jfor.converter.BuilderContext;
5 import org.jfor.jfor.converter.IBuilder;
6
7 import org.jfor.jfor.rtflib.rtfdoc.RtfAttributes;
8 import org.jfor.jfor.rtflib.rtfdoc.RtfExternalGraphic;
9 import org.jfor.jfor.rtflib.rtfdoc.IRtfExternalGraphicContainer;
10
11 import org.xml.sax.Attributes;
12 import java.io.IOException;
13 import java.util.Hashtable;
14 import java.util.Enumeration;
15
16 /*-----------------------------------------------------------------------------
17 * jfor - Open-Source XSL-FO to RTF converter - see www.jfor.org
18 *
19 * ====================================================================
20 * jfor Apache-Style Software License.
21 * Copyright (c) 2002 by the jfor project. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 *
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 *
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in
32 * the documentation and/or other materials provided with the
33 * distribution.
34 *
35 * 3. The end-user documentation included with the redistribution,
36 * if any, must include the following acknowledgment:
37 * "This product includes software developed
38 * by the jfor project (http://www.jfor.org)."
39 * Alternately, this acknowledgment may appear in the software itself,
40 * if and wherever such third-party acknowledgments normally appear.
41 *
42 * 4. The name "jfor" must not be used to endorse
43 * or promote products derived from this software without prior written
44 * permission. For written permission, please contact info@jfor.org.
45 *
46 * 5. Products derived from this software may not be called "jfor",
47 * nor may "jfor" appear in their name, without prior written
48 * permission of info@jfor.org.
49 *
50 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
51 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 * DISCLAIMED. IN NO EVENT SHALL THE JFOR PROJECT OR ITS CONTRIBUTORS BE
54 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
57 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
58 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
59 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
60 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 * ====================================================================
62 * Contributor(s):
63 * @author <a href="mailto:a.putz@skynamics.com">Andreas Putz</a>
64 -----------------------------------------------------------------------------*/
65
66 /** An IBuilder that handles fo:external-graphic
67 * @author <a href="mailto:a.putz@skynamics.com">Andreas Putz</a>
68 */
69
70 //------------------------------------------------------------------------------
71 // $Id: ExternalGraphicBuilder.java,v 1.5 2002/07/12 08:08:31 bdelacretaz Exp $
72 // $Log: ExternalGraphicBuilder.java,v $
73 // Revision 1.5 2002/07/12 08:08:31 bdelacretaz
74 // License changed to jfor Apache-style license
75 //
76 // Revision 1.4 2001/09/26 13:48:51 putzi
77 // Availability to change the compression rate of the external graphics from the outside.
78 // Changes in Converter options concept.
79 //
80 // Revision 1.3 2001/09/06 14:19:43 putzi
81 // Optimized code
82 //
83 // Revision 1.2 2001/08/31 07:51:00 bdelacretaz
84 // MPL license text added + javadoc class comments corrected
85 //
86 // Revision 1.1 2001/08/29 13:27:51 bdelacretaz
87 // V0.4.1 - base package name changed to org.jfor.jfor
88 //
89 // Revision 1.2 2001/08/21 15:58:02 bdelacretaz
90 // V0.3.5, jpeg and gif support added
91 //
92 // Revision 1.1 2001/08/16 11:54:49 bdelacretaz
93 // V0.2.3 - fo:external-graphic support added (written by Andreas Putz of skynamics)
94 //
95 //------------------------------------------------------------------------------
96
97 class ExternalGraphicBuilder extends AbstractBuilder
98 {
99
100 //////////////////////////////////////////////////
101 // @@ Members
102 //////////////////////////////////////////////////
103
104 /**
105 * RTF graphic object
106 */
107 private RtfExternalGraphic graphic = null;
108
109
110 //////////////////////////////////////////////////
111 // @@ Construction
112 //////////////////////////////////////////////////
113
114 /**
115 * Default constructor.
116 *
117 * @param ctx a <code>BuilderContext</code> value
118 */
119 public ExternalGraphicBuilder (BuilderContext ctx)
120 {
121 super (ctx);
122 }
123
124
125 //////////////////////////////////////////////////
126 // @@ AbstractBuilder imlementation
127 //////////////////////////////////////////////////
128
129 /**
130 * Return the appropriate builder for given element if we have one.
131 * @param ctx a <code>BuilderContext</code> value
132 * @param rawName a <code>String</code> value
133 * @param attr an <code>Attributes</code> value
134 * @return an <code>IBuilder</code> value
135 */
136 public IBuilder getBuilder (BuilderContext ctx, String rawName, Attributes attr)
137 {
138 if (rawName.equals ("fo:external-graphic"))
139 {
140 return new ExternalGraphicBuilder (ctx);
141 }
142 else
143 {
144 return null;
145 }
146 }
147
148 /**
149 * Called by Converter at the start of an element.
150 * @param rawName a <code>String</code> value
151 * @param attr an <code>Attributes</code> value
152 * @exception IOException if an error occurs
153 */
154 public void start (String rawName, Attributes attr) throws IOException
155 {
156 final IRtfExternalGraphicContainer c =
157 (IRtfExternalGraphicContainer) m_context
158 .getContainer (IRtfExternalGraphicContainer.class, true, this);
159
160 final RtfExternalGraphic newGraphic = c.newImage ();
161 int len = attr.getLength ();
162
163 Hashtable required = new Hashtable ();
164 required.put ("src", Boolean.FALSE);
165
166 for (int i = 0; i < len; i++)
167 {
168 String name = attr.getQName (i);
169 setAttribute (newGraphic, name, attr.getValue (i));
170 required.put (name, Boolean.TRUE);
171 }
172
173 int compression = m_context.m_options.getRtfExternalGraphicCompressionRate ();
174 if (compression != 0)
175 {
176 if (! newGraphic.setCompressionRate (compression))
177 {
178 m_context.log.logWarning ("The compression rate " + compression + " is invalid. The value have to be between 1 and 100 %.");
179 }
180 }
181
182 for (Enumeration e = required.keys (); e.hasMoreElements (); )
183 {
184 if ((Boolean) required.get (e.nextElement ()) == Boolean.FALSE)
185 {
186 throw new IOException ("The attribute 'src' of <fo:external-graphic> is required.");
187 }
188 }
189 }
190
191 /**
192 * Called by Converter at the end of an element.
193 *
194 * @exception IOException if an error occurs
195 */
196 public void end () throws IOException
197 {
198 }
199
200 /**
201 * Called by the parser for Text nodes.
202 * @param str a <code>String</code> value
203 * @exception IOException if an error occurs
204 */
205 public void characters (String str) throws IOException
206 {
207 }
208
209 /**
210 * Ignores child nodes.
211 *
212 * @return
213 * true xsl:fo elements that are children of an
214 * element using this builder must be ignored\n
215 * false xsl:fo elements that are children of an
216 * element using this builder will not be ignored
217 */
218 public boolean ignoreChildren ()
219 {
220 return true;
221 }
222
223
224 /**
225 * Sets a attribute of the image.
226 *
227 * @param name Name of attribute
228 * @param value Value of attribute
229 *
230 * @throws IOException Required attributes are not complete set
231 */
232 private void setAttribute (RtfExternalGraphic graphicObject, String name, String value)
233 throws IOException
234 {
235 boolean successful = false;
236
237 if (name == null || value == null)
238 {
239 return;
240 }
241
242 try
243 {
244 if (name.equalsIgnoreCase ("src"))
245 {
246 graphicObject.setURL (value);
247 }
248 else if (name.equalsIgnoreCase ("width"))
249 {
250 graphicObject.setWidth (value);
251 }
252 else if (name.equalsIgnoreCase ("height"))
253 {
254 graphicObject.setHeight (value);
255 }
256 else if (name.equalsIgnoreCase ("scaling"))
257 {
258 graphicObject.setScaling (value);
259 }
260 else
261 {
262 m_context.log.logDebug ("Unsupported attribute of fo:external-graphic : '"
263 + name + "'");
264 }
265 }
266 catch (NumberFormatException e)
267 {
268
269 // no valid height or width
270 throw new IOException ("The attribute '" + name + "' of <fo:external-graphic> has a invalid value: '" + value + "'");
271 }
272 }
273 }