Author: ltheussl Date: Mon Jun 8 14:34:10 2009 New Revision: 782648 URL: http://svn.apache.org/viewvc?rev=782648&view=rev Log: Extract the image reading into a re-usable utility method.
Modified: maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/DoxiaUtils.java maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoAggregateSink.java Modified: maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/DoxiaUtils.java URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/DoxiaUtils.java?rev=782648&r1=782647&r2=782648&view=diff ============================================================================== --- maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/DoxiaUtils.java (original) +++ maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/DoxiaUtils.java Mon Jun 8 14:34:10 2009 @@ -19,8 +19,14 @@ * under the License. */ +import java.awt.image.BufferedImage; + +import java.io.File; +import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.net.URL; + import java.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; @@ -28,6 +34,12 @@ import java.util.Date; import java.util.Locale; +import javax.imageio.ImageIO; + +import javax.swing.text.MutableAttributeSet; + +import org.apache.maven.doxia.sink.SinkEventAttributeSet; + /** * General Doxia utility methods. The methods in this class should not assume * any specific Doxia module or document format. @@ -356,9 +368,44 @@ return ( c >= '0' && c <= '9' ); } + /** + * Determine width and height of an image. If successful, the returned SinkEventAttributes + * contain width and height attribute keys whose values are the width and height of the image (as a String). + * + * @param logo a String containing either a URL or a path to an image file. + * @return a set of SinkEventAttributes, or null if no ImageReader was found to read the image. + * @throws java.io.IOException if an error occurs during reading. + * @since 1.1.1 + */ + public static MutableAttributeSet getImageAttributes( String logo ) + throws IOException + { + BufferedImage img = null; + + if ( isExternalLink( logo ) ) + { + img = ImageIO.read( new URL( logo ) ); + } + else + { + img = ImageIO.read( new File( logo ) ); + } + + if ( img == null ) + { + return null; + } + + MutableAttributeSet atts = new SinkEventAttributeSet(); + atts.addAttribute( SinkEventAttributeSet.WIDTH, Integer.toString( img.getWidth() ) ); + atts.addAttribute( SinkEventAttributeSet.HEIGHT, Integer.toString( img.getHeight() ) ); + // add other attributes? + + return atts; + } + private DoxiaUtils() { // utility class } - } Modified: maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoAggregateSink.java URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoAggregateSink.java?rev=782648&r1=782647&r2=782648&view=diff ============================================================================== --- maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoAggregateSink.java (original) +++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoAggregateSink.java Mon Jun 8 14:34:10 2009 @@ -19,10 +19,8 @@ * under the License. */ -import java.awt.image.BufferedImage; -import java.io.File; +import java.io.IOException; import java.io.Writer; -import java.net.URL; import java.util.Calendar; import java.util.Date; @@ -32,7 +30,6 @@ import java.util.ResourceBundle; import java.util.Stack; -import javax.imageio.ImageIO; import javax.swing.text.MutableAttributeSet; import javax.swing.text.html.HTML.Tag; @@ -89,6 +86,9 @@ /** Used to get the current position in the TOC. */ private final Stack tocStack = new Stack(); + // TODO: make configurable + private static final String COVER_HEADER_HEIGHT = "1.5in"; + /** * Constructor. * @@ -958,7 +958,7 @@ String compLogo = cover.getCompanyLogo(); String projLogo = cover.getProjectLogo(); - writeStartTag( TABLE_ROW_TAG, "height", "1.5in" ); + writeStartTag( TABLE_ROW_TAG, "height", COVER_HEADER_HEIGHT ); writeStartTag( TABLE_CELL_TAG ); if ( StringUtils.isNotEmpty( compLogo ) ) @@ -1151,47 +1151,31 @@ private SinkEventAttributeSet getGraphicsAttributes( String logo ) { - SinkEventAttributeSet atts = new SinkEventAttributeSet(); + MutableAttributeSet atts = null; - BufferedImage img = null; - if ( ( logo.toLowerCase( Locale.ENGLISH ).startsWith( "http://" ) ) - || ( logo.toLowerCase( Locale.ENGLISH ).startsWith( "https://" ) ) ) + try { - try - { - img = ImageIO.read( new URL( logo ) ); - } - catch ( Exception e ) - { - getLog().debug( e ); - } + atts = DoxiaUtils.getImageAttributes( logo ); } - else + catch ( IOException e ) { - try - { - img = ImageIO.read( new File( logo ) ); - } - catch ( Exception e ) - { - getLog().debug( e ); - } + getLog().debug( e ); } - if ( img == null ) + if ( atts == null ) { - atts.addAttribute( SinkEventAttributes.HEIGHT, "1.5in" ); - return atts; + return new SinkEventAttributeSet( new String[] {SinkEventAttributes.HEIGHT, COVER_HEADER_HEIGHT} ); } // FOP dpi: 72 // Max width : 3.125 inch, table cell size, see #coverPage() - double maxWidth = 3.125 * 72; - if ( img.getWidth() > maxWidth ) + final int maxWidth = 225; // 3.125 * 72 + + if ( Integer.parseInt( atts.getAttribute( SinkEventAttributes.WIDTH ).toString() ) > maxWidth ) { atts.addAttribute( "content-width", "3.125in" ); } - return atts; + return new SinkEventAttributeSet( atts ); } }