View Javadoc

1   /***
2    * 
3    */
4   package ar.com.epere4.java2excel.parser;
5   
6   import java.io.IOException;
7   import java.io.InputStream;
8   
9   import org.xml.sax.SAXException;
10  
11  import ar.com.epere4.java2excel.parser.impl.BodyDtdImpl;
12  
13  /***
14   * This class allows the construction of several kinds of
15   * {@link ar.com.epere4.java2excel.parser.Java2ExcelDtd}.
16   * 
17   * @author epere4
18   * @since 19/02/2006
19   */
20  public final class Java2ExcelDtdFactory {
21      /*** Singleton. */
22      public static final Java2ExcelDtdFactory INSTANCE = 
23                          new Java2ExcelDtdFactory();
24  
25      /***
26       * @return the only instance of this class.
27       * @since 19/02/2006
28       */
29      public static Java2ExcelDtdFactory getInstance() {
30          return INSTANCE;
31      }
32  
33      /***
34       * Private constructor for Singleton.
35       * 
36       * @since 19/02/2006
37       */
38      private Java2ExcelDtdFactory() {
39  
40      }
41  
42      /***
43       * This returns a {@link Java2ExcelDtd} implementation that will be
44       * configured with the xml received as input. This xml must be valid to the
45       * {@link Parser#JAVA2EXCEL_LOCAL_DTD_URL}.
46       * 
47       * @param xmlInput the xml file.
48       * @return a fully configured instance of {@link Java2ExcelDtd}.
49       * @throws IOException if an Input/Output exception occurs.
50       * @throws SAXException if a parsing exception occurs.
51       * @since 19/02/2006
52       */
53      public Java2ExcelDtd readFromFile(final InputStream xmlInput)
54              throws IOException, SAXException {
55          Java2ExcelDtd java2ExcelDtd = new Parser().parse(xmlInput);
56          return java2ExcelDtd;
57      }
58      
59      /***
60       * Similar to calling
61       * <code>readFromFile(xmlInput, typesToExcludeInAutoComplete, false)</code>.
62       * 
63       * @param xmlInput
64       *            see {@link #readFromFile(InputStream, Class[], boolean)}
65       * @param typesToExcludeInAutoComplete
66       *            see {@link #readFromFile(InputStream, Class[], boolean)}
67       * @return see {@link #readFromFile(InputStream, Class[], boolean)}
68       * @throws IOException
69       *             see {@link #readFromFile(InputStream, Class[], boolean)}
70       * @throws SAXException
71       *             see {@link #readFromFile(InputStream, Class[], boolean)}
72       * @since 22/02/2006
73       */
74      public Java2ExcelDtd readFromFile(final InputStream xmlInput,
75              final Class[] typesToExcludeInAutoComplete) throws IOException,
76              SAXException {
77          return readFromFile(xmlInput, typesToExcludeInAutoComplete, false);
78      }
79      
80      /***
81       * 
82       * @param xmlInput the xml file.
83       * @param typesToExcludeInAutoComplete the array of types to be excluded
84       * when searching for getters by reflection. No property that returns an
85       * instance of any of the types in this array will be printed in the 
86       * excel file.
87       * @param ignoreDefaultsExcludes if true, then the values of the
88       * typesToExcludeInAutoComplete parameter will overwrite the default ones.
89       * @return a fully configured instance of {@link Java2ExcelDtd}.
90       * @throws IOException if an Input/Output exception occurs.
91       * @throws SAXException if a parsing exception occurs.
92       * @since 21/02/2006
93       */
94      public Java2ExcelDtd readFromFile(final InputStream xmlInput,
95              final Class[] typesToExcludeInAutoComplete,
96              final boolean ignoreDefaultsExcludes) throws IOException,
97              SAXException {
98          Java2ExcelDtd java2ExcelDtd = readFromFile(xmlInput);
99          
100         adjustTypesToExclude((BodyDtdImpl) java2ExcelDtd.getBody(),
101                 typesToExcludeInAutoComplete, ignoreDefaultsExcludes);
102         
103         return java2ExcelDtd;
104     }
105 
106     /***
107      * 
108      * @return a default implementation of {@link Java2ExcelDtd} with no
109      *         properties asociated.
110      * @since 21/02/2006
111      */
112     public Java2ExcelDtd defaultJava2ExcelDtd() {
113         InputStream xmlInput = getClass().getResourceAsStream(
114                 "emptyJava2Excel.xml");
115         Java2ExcelDtd java2ExcelDtd;
116         try {
117             java2ExcelDtd = new Parser().parse(xmlInput);
118         } catch (RuntimeException e) {
119             throw e;
120         } catch (Exception e) {
121             // Nor IOException nor SAXException should occur normally.
122             throw new RuntimeException(e);
123         }
124         return java2ExcelDtd;
125     }
126     
127     /***
128      * Default typesToExcludeInAutoComplete are those in
129      * {@link BodyDtdImpl#DEFAULT_TYPES_TO_EXCLUDE_IN_AUTO_COMPLETE}.
130      * @param includeExtraLevel the number of levels in which to
131      * search for properties by reflexion.
132      * @return similar to {@link #defaultJava2ExcelDtd()}, but setting
133      * the {@link BodyDtd#setIncludeExtraLevel(int)}. 
134      * @since 21/02/2006
135      */
136     public Java2ExcelDtd defaultJava2ExcelDtd(final int includeExtraLevel) {
137         Java2ExcelDtd java2ExcelDtd = defaultJava2ExcelDtd();
138         ((BodyDtdImpl) java2ExcelDtd.getBody())
139                 .setIncludeExtraLevel(includeExtraLevel);
140         return java2ExcelDtd;
141     }
142     
143     /***
144      * Similar to calling <code>defaultJava2ExcelDtd(includeExtraLevel, 
145      * typesToExcludeInAutoComplete, false)</code>.
146      * 
147      * @param includeExtraLevel
148      *            see {@link #defaultJava2ExcelDtd(int, Class[], boolean)}
149      * @param typesToExcludeInAutoComplete
150      *            see {@link #defaultJava2ExcelDtd(int, Class[], boolean)}
151      * @return see {@link #defaultJava2ExcelDtd(int, Class[], boolean)}
152      * @see #defaultJava2ExcelDtd(int, Class[], boolean)
153      * @since 22/02/2006
154      */
155     public Java2ExcelDtd defaultJava2ExcelDtd(final int includeExtraLevel,
156             final Class[] typesToExcludeInAutoComplete) {
157         return defaultJava2ExcelDtd(includeExtraLevel,
158                 typesToExcludeInAutoComplete, false);
159     }
160     
161     /***
162      * 
163      * @param includeExtraLevel the number of levels in which to
164      * search for properties by reflexion.
165      * @param typesToExcludeInAutoComplete the array of types to be excluded
166      * when searching for getters by reflection. No property that returns an
167      * instance of any of the types in this array will be printed in the 
168      * excel file.
169      * @param ignoreDefaultsExcludes if true, then the values of the
170      * typesToExcludeInAutoComplete parameter will overwrite the default ones.
171      * @return similar to {@link #defaultJava2ExcelDtd(int)}, but setting
172      * the types to exclude in auto complete. 
173      * @since 21/02/2006
174      */
175     public Java2ExcelDtd defaultJava2ExcelDtd(final int includeExtraLevel,
176             final Class[] typesToExcludeInAutoComplete,
177             final boolean ignoreDefaultsExcludes) {
178         Java2ExcelDtd java2ExcelDtd = defaultJava2ExcelDtd();
179         ((BodyDtdImpl) java2ExcelDtd.getBody())
180                 .setIncludeExtraLevel(includeExtraLevel);
181 
182         
183         adjustTypesToExclude((BodyDtdImpl) java2ExcelDtd.getBody(),
184                 typesToExcludeInAutoComplete, ignoreDefaultsExcludes);
185         
186         return java2ExcelDtd;
187     }
188     
189     /***
190      * Adds or set the typesToExcludeInAutoComplete to the body, depending
191      * on the ignoreDefaultsExcludes parameter.
192      * @param body the body where to add or set.
193      * @param typesToExcludeInAutoComplete the types to set.
194      * @param ignoreDefaultsExcludes if true, then the set is called, if false,
195      * then de add is called.
196      * @since 22/02/2006
197      */
198     private void adjustTypesToExclude(final BodyDtdImpl body,
199             final Class[] typesToExcludeInAutoComplete,
200             final boolean ignoreDefaultsExcludes) {
201         if (ignoreDefaultsExcludes) {
202             body.setTypesToExcludeInAutoComplete(typesToExcludeInAutoComplete);
203         } else {
204             body.addTypesToExcludeInAutoComplete(typesToExcludeInAutoComplete);
205         }
206     }
207 }