View Javadoc

1   package org.apache.maven.doxia.module.itext;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import com.lowagie.text.DocumentException;
23  import com.lowagie.text.PageSize;
24  import com.lowagie.text.Rectangle;
25  import com.lowagie.text.xml.XmlToHtml;
26  import com.lowagie.text.xml.XmlToPdf;
27  import com.lowagie.text.xml.XmlToRtf;
28  
29  import java.io.InputStream;
30  import java.io.OutputStream;
31  import java.lang.reflect.Field;
32  import java.util.Locale;
33  
34  /**
35   * A set of util methods for the <code>iText</code> framework
36   *
37   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
38   * @version $Id: ITextUtil.java 1003453 2010-10-01 09:45:16Z ltheussl $
39   */
40  public class ITextUtil
41  {
42      /**
43       * Set the default page size for the document depending the user's country.
44       * TODO Maybe more generic?
45       *
46       * @return the page size
47       * @see com.lowagie.text.PageSize
48       */
49      public static Rectangle getDefaultPageSize()
50      {
51          String defaultCountry = Locale.getDefault().getCountry();
52          if ( defaultCountry != null
53              && ( defaultCountry.equals( Locale.US.getCountry() )
54                              || defaultCountry.equals( Locale.CANADA.getCountry() ) ) )
55          {
56              return PageSize.LETTER;
57          }
58  
59          return PageSize.A4;
60      }
61  
62      /**
63       * Return a page size as String.
64       *
65       * @param rect a Rectangle defined in {@link PageSize}.
66       * @return a page size as String or A4 if not found.
67       * @see com.lowagie.text.PageSize
68       */
69      public static String getPageSize( Rectangle rect )
70      {
71          Field[] fields = PageSize.class.getFields();
72          for ( int i = 0; i < fields.length; i++ )
73          {
74              Field currentField = fields[i];
75              try
76              {
77                  if ( currentField.getType().equals( Rectangle.class ) )
78                  {
79                      Rectangle fPageSize = (Rectangle) currentField.get( null );
80                      if ( ( rect.width() == fPageSize.width() ) && ( rect.height() == fPageSize.height() ) )
81                      {
82                          return currentField.getName();
83                      }
84                  }
85              }
86              catch ( Exception e )
87              {
88                  // nop
89              }
90          }
91  
92          return "A4";
93      }
94  
95      /**
96       * Return <code>true</code> if the page size is supported by {@link PageSize} class, <code>false</code> otherwise.
97       *
98       * @param aPageSize a page size
99       * @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 }