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   */
39  public class ITextUtil
40  {
41      /**
42       * Set the default page size for the document depending the user's country.
43       * TODO Maybe more generic?
44       *
45       * @return the page size
46       * @see com.lowagie.text.PageSize
47       */
48      public static Rectangle getDefaultPageSize()
49      {
50          String defaultCountry = Locale.getDefault().getCountry();
51          if ( defaultCountry != null
52              && ( defaultCountry.equals( Locale.US.getCountry() )
53                              || defaultCountry.equals( Locale.CANADA.getCountry() ) ) )
54          {
55              return PageSize.LETTER;
56          }
57  
58          return PageSize.A4;
59      }
60  
61      /**
62       * Return a page size as String.
63       *
64       * @param rect a Rectangle defined in {@link PageSize}.
65       * @return a page size as String or A4 if not found.
66       * @see com.lowagie.text.PageSize
67       */
68      public static String getPageSize( Rectangle rect )
69      {
70          Field[] fields = PageSize.class.getFields();
71          for ( Field currentField : fields )
72          {
73              try
74              {
75                  if ( currentField.getType().equals( Rectangle.class ) )
76                  {
77                      Rectangle fPageSize = (Rectangle) currentField.get( null );
78                      if ( ( rect.width() == fPageSize.width() ) && ( rect.height() == fPageSize.height() ) )
79                      {
80                          return currentField.getName();
81                      }
82                  }
83              }
84              catch ( Exception e )
85              {
86                  // nop
87              }
88          }
89  
90          return "A4";
91      }
92  
93      /**
94       * Return <code>true</code> if the page size is supported by {@link PageSize} class, <code>false</code> otherwise.
95       *
96       * @param aPageSize a page size
97       * @return <code>true</code> if the page size is supported, <code>false</code> otherwise
98       * @see com.lowagie.text.PageSize
99       */
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 }