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 * @version $Id: ITextUtil.html 905940 2014-04-12 16:27:29Z hboutemy $
039 */
040public class ITextUtil
041{
042    /**
043     * Set the default page size for the document depending the user's country.
044     * TODO Maybe more generic?
045     *
046     * @return the page size
047     * @see com.lowagie.text.PageSize
048     */
049    public static Rectangle getDefaultPageSize()
050    {
051        String defaultCountry = Locale.getDefault().getCountry();
052        if ( defaultCountry != null
053            && ( defaultCountry.equals( Locale.US.getCountry() )
054                            || defaultCountry.equals( Locale.CANADA.getCountry() ) ) )
055        {
056            return PageSize.LETTER;
057        }
058
059        return PageSize.A4;
060    }
061
062    /**
063     * Return a page size as String.
064     *
065     * @param rect a Rectangle defined in {@link PageSize}.
066     * @return a page size as String or A4 if not found.
067     * @see com.lowagie.text.PageSize
068     */
069    public static String getPageSize( Rectangle rect )
070    {
071        Field[] fields = PageSize.class.getFields();
072        for ( int i = 0; i < fields.length; i++ )
073        {
074            Field currentField = fields[i];
075            try
076            {
077                if ( currentField.getType().equals( Rectangle.class ) )
078                {
079                    Rectangle fPageSize = (Rectangle) currentField.get( null );
080                    if ( ( rect.width() == fPageSize.width() ) && ( rect.height() == fPageSize.height() ) )
081                    {
082                        return currentField.getName();
083                    }
084                }
085            }
086            catch ( Exception e )
087            {
088                // nop
089            }
090        }
091
092        return "A4";
093    }
094
095    /**
096     * Return <code>true</code> if the page size is supported by {@link PageSize} class, <code>false</code> otherwise.
097     *
098     * @param aPageSize a page size
099     * @return <code>true</code> if the page size is supported, <code>false</code> otherwise
100     * @see com.lowagie.text.PageSize
101     */
102    public static boolean isPageSizeSupported( String aPageSize )
103    {
104        Field[] fields = PageSize.class.getFields();
105        for ( int i = 0; i < fields.length; i++ )
106        {
107            Field currentField = fields[i];
108            if ( ( currentField.getName().equalsIgnoreCase( aPageSize ) )
109                && ( currentField.getType().equals( Rectangle.class ) ) )
110            {
111                return true;
112            }
113        }
114
115        return false;
116    }
117
118    /**
119     * Parse an iText XML from the specified <CODE>InputStream</CODE>, writing an Pdf document
120     * specified <CODE>OutputStream</CODE>.
121     *
122     * @param is the <CODE>InputStream</CODE> from which the XML is read.
123     * @param os the <CODE>OutputStream</CODE> to which the result as Pdf is written.
124     * @see com.lowagie.text.xml.XmlToPdf
125     */
126    public static void writePdf( InputStream is, OutputStream os )
127    {
128        try
129        {
130            XmlToPdf x = new XmlToPdf();
131
132            x.parse( is, os );
133        }
134        catch ( DocumentException e )
135        {
136            throw new RuntimeException( "DocumentException : " + e.getMessage(), e );
137        }
138    }
139
140    /**
141     * Parse an iText XML from the specified <CODE>InputStream</CODE>, writing an rtf document
142     * specified <CODE>OutputStream</CODE>.
143     *
144     * @param is the <CODE>InputStream</CODE> from which the XML is read.
145     * @param os the <CODE>OutputStream</CODE> to which the result as RTF is written.
146     * @see com.lowagie.text.xml.XmlToRtf
147     */
148    public static void writeRtf( InputStream is, OutputStream os )
149    {
150        try
151        {
152            XmlToRtf x = new XmlToRtf();
153            x.parse( is, os );
154        }
155        catch ( DocumentException e )
156        {
157            throw new RuntimeException( "DocumentException : " + e.getMessage(), e );
158        }
159    }
160
161    /**
162     * Parse an iText XML from the specified <CODE>InputStream</CODE>, writing an html document
163     * specified <CODE>OutputStream</CODE>.
164     *
165     * @param is the <CODE>InputStream</CODE> from which the XML is read.
166     * @param os the <CODE>OutputStream</CODE> to which the result as Html is written.
167     * @see com.lowagie.text.xml.XmlToHtml
168     */
169    public static void writeHtml( InputStream is, OutputStream os )
170    {
171        try
172        {
173            XmlToHtml x = new XmlToHtml();
174            x.parse( is, os );
175        }
176        catch ( DocumentException e )
177        {
178            throw new RuntimeException( "DocumentException : " + e.getMessage(), e );
179        }
180    }
181}