001package org.apache.maven.doxia.module.itext; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import com.lowagie.text.DocumentException; 023import com.lowagie.text.PageSize; 024import com.lowagie.text.Rectangle; 025import com.lowagie.text.xml.XmlToHtml; 026import com.lowagie.text.xml.XmlToPdf; 027import com.lowagie.text.xml.XmlToRtf; 028 029import java.io.InputStream; 030import java.io.OutputStream; 031import java.lang.reflect.Field; 032import java.util.Locale; 033 034/** 035 * A set of util methods for the <code>iText</code> framework 036 * 037 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> 038 */ 039public class ITextUtil 040{ 041 /** 042 * Set the default page size for the document depending the user's country. 043 * TODO Maybe more generic? 044 * 045 * @return the page size 046 * @see com.lowagie.text.PageSize 047 */ 048 public static Rectangle getDefaultPageSize() 049 { 050 String defaultCountry = Locale.getDefault().getCountry(); 051 if ( defaultCountry != null 052 && ( defaultCountry.equals( Locale.US.getCountry() ) 053 || defaultCountry.equals( Locale.CANADA.getCountry() ) ) ) 054 { 055 return PageSize.LETTER; 056 } 057 058 return PageSize.A4; 059 } 060 061 /** 062 * Return a page size as String. 063 * 064 * @param rect a Rectangle defined in {@link PageSize}. 065 * @return a page size as String or A4 if not found. 066 * @see com.lowagie.text.PageSize 067 */ 068 public static String getPageSize( Rectangle rect ) 069 { 070 Field[] fields = PageSize.class.getFields(); 071 for ( Field currentField : fields ) 072 { 073 try 074 { 075 if ( currentField.getType().equals( Rectangle.class ) ) 076 { 077 Rectangle fPageSize = (Rectangle) currentField.get( null ); 078 if ( ( rect.width() == fPageSize.width() ) && ( rect.height() == fPageSize.height() ) ) 079 { 080 return currentField.getName(); 081 } 082 } 083 } 084 catch ( Exception e ) 085 { 086 // nop 087 } 088 } 089 090 return "A4"; 091 } 092 093 /** 094 * Return <code>true</code> if the page size is supported by {@link PageSize} class, <code>false</code> otherwise. 095 * 096 * @param aPageSize a page size 097 * @return <code>true</code> if the page size is supported, <code>false</code> otherwise 098 * @see com.lowagie.text.PageSize 099 */ 100 public static boolean isPageSizeSupported( String aPageSize ) 101 { 102 Field[] fields = PageSize.class.getFields(); 103 for ( Field currentField : fields ) 104 { 105 if ( ( currentField.getName().equalsIgnoreCase( aPageSize ) ) && ( currentField.getType().equals( 106 Rectangle.class ) ) ) 107 { 108 return true; 109 } 110 } 111 112 return false; 113 } 114 115 /** 116 * Parse an iText XML from the specified <CODE>InputStream</CODE>, writing an Pdf document 117 * specified <CODE>OutputStream</CODE>. 118 * 119 * @param is the <CODE>InputStream</CODE> from which the XML is read. 120 * @param os the <CODE>OutputStream</CODE> to which the result as Pdf is written. 121 * @see com.lowagie.text.xml.XmlToPdf 122 */ 123 public static void writePdf( InputStream is, OutputStream os ) 124 { 125 try 126 { 127 XmlToPdf x = new XmlToPdf(); 128 129 x.parse( is, os ); 130 } 131 catch ( DocumentException e ) 132 { 133 throw new RuntimeException( "DocumentException : " + e.getMessage(), e ); 134 } 135 } 136 137 /** 138 * Parse an iText XML from the specified <CODE>InputStream</CODE>, writing an rtf document 139 * specified <CODE>OutputStream</CODE>. 140 * 141 * @param is the <CODE>InputStream</CODE> from which the XML is read. 142 * @param os the <CODE>OutputStream</CODE> to which the result as RTF is written. 143 * @see com.lowagie.text.xml.XmlToRtf 144 */ 145 public static void writeRtf( InputStream is, OutputStream os ) 146 { 147 try 148 { 149 XmlToRtf x = new XmlToRtf(); 150 x.parse( is, os ); 151 } 152 catch ( DocumentException e ) 153 { 154 throw new RuntimeException( "DocumentException : " + e.getMessage(), e ); 155 } 156 } 157 158 /** 159 * Parse an iText XML from the specified <CODE>InputStream</CODE>, writing an html document 160 * specified <CODE>OutputStream</CODE>. 161 * 162 * @param is the <CODE>InputStream</CODE> from which the XML is read. 163 * @param os the <CODE>OutputStream</CODE> to which the result as Html is written. 164 * @see com.lowagie.text.xml.XmlToHtml 165 */ 166 public static void writeHtml( InputStream is, OutputStream os ) 167 { 168 try 169 { 170 XmlToHtml x = new XmlToHtml(); 171 x.parse( is, os ); 172 } 173 catch ( DocumentException e ) 174 { 175 throw new RuntimeException( "DocumentException : " + e.getMessage(), e ); 176 } 177 } 178}