View Javadoc

1   /***
2    * Licensed under the Artistic License; you may not use this file
3    * except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *      http://displaytag.sourceforge.net/license.html
7    *
8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10   * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11   */
12  package org.displaytag.export;
13  
14  import java.util.HashMap;
15  import java.util.Map;
16  
17  import org.apache.commons.lang.ArrayUtils;
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.displaytag.Messages;
21  import org.displaytag.exception.WrappedRuntimeException;
22  import org.displaytag.model.TableModel;
23  import org.displaytag.properties.MediaTypeEnum;
24  import org.displaytag.properties.TableProperties;
25  import org.displaytag.util.ReflectHelper;
26  
27  
28  /***
29   * Factory for export views.
30   * @author Fabrizio Giustina
31   * @version $Revision: 1.13 $ ($Author: fgiust $)
32   */
33  public final class ExportViewFactory
34  {
35  
36      /***
37       * Singleton.
38       */
39      private static ExportViewFactory instance;
40  
41      /***
42       * logger.
43       */
44      private static Log log = LogFactory.getLog(ExportViewFactory.class);
45  
46      /***
47       * Map containing MediaTypeEnum - View class.
48       */
49      private final Map viewClasses = new HashMap();
50  
51      /***
52       * Private constructor.
53       */
54      private ExportViewFactory()
55      {
56          TableProperties properties = TableProperties.getInstance(null);
57          String[] exportTypes = properties.getExportTypes();
58  
59          if (log.isInfoEnabled())
60          {
61              log.info(Messages.getString("ExportViewFactory.initializing", //$NON-NLS-1$
62                  new Object[]{ArrayUtils.toString(exportTypes)}));
63          }
64          for (int j = 0; j < exportTypes.length; j++)
65          {
66              String className = properties.getExportClass(exportTypes[j]);
67              registerExportView(exportTypes[j], className);
68          }
69      }
70  
71      /***
72       * Returns the simgleton for this class.
73       * @return ExportViewFactory instance
74       */
75      public static synchronized ExportViewFactory getInstance()
76      {
77          if (instance == null)
78          {
79              instance = new ExportViewFactory();
80          }
81          return instance;
82      }
83  
84      /***
85       * Register a new Export View, associated with a Media Type. If another export view is currently associated with the
86       * given media type it's replaced.
87       * @param name media name
88       * @param viewClassName export view class name
89       */
90      public void registerExportView(String name, String viewClassName)
91      {
92          Class exportClass;
93          try
94          {
95              exportClass = ReflectHelper.classForName(viewClassName);
96          }
97          catch (ClassNotFoundException e)
98          {
99              log.error(Messages.getString("ExportViewFactory.classnotfound", //$NON-NLS-1$
100                 new Object[]{name, viewClassName}));
101             return;
102         }
103         catch (NoClassDefFoundError e)
104         {
105             log.warn(Messages.getString("ExportViewFactory.noclassdef" //$NON-NLS-1$
106                 , new Object[]{name, viewClassName, e.getMessage()}));
107             return;
108         }
109 
110         try
111         {
112             exportClass.newInstance();
113         }
114         catch (InstantiationException e)
115         {
116             log.error(Messages.getString("ExportViewFactory.instantiationexception", //$NON-NLS-1$
117                 new Object[]{name, viewClassName, e.getMessage()}));
118             return;
119         }
120         catch (IllegalAccessException e)
121         {
122             log.error(Messages.getString("ExportViewFactory.illegalaccess", //$NON-NLS-1$
123                 new Object[]{name, viewClassName, e.getMessage()}));
124             return;
125         }
126         catch (NoClassDefFoundError e)
127         {
128             log.warn(Messages.getString("ExportViewFactory.noclassdef" //$NON-NLS-1$
129                 , new Object[]{name, viewClassName, e.getMessage()}));
130             return;
131         }
132 
133         MediaTypeEnum media = MediaTypeEnum.registerMediaType(name);
134         viewClasses.put(media, exportClass);
135 
136         if (log.isDebugEnabled())
137         {
138             log.debug(Messages.getString("ExportViewFactory.added", //$NON-NLS-1$ 
139                 new Object[]{media, viewClassName}));
140         }
141     }
142 
143     /***
144      * returns an instance of export view associated with the given export type.
145      * @param exportType MediaTypeEnum
146      * @param tableModel table model containing data to render
147      * @param exportFullList should the complete list be exported?
148      * @param includeHeader should header be included in export?
149      * @param decorateValues should ouput be decorated?
150      * @return specialized instance of BaseExportView
151      */
152     public ExportView getView(MediaTypeEnum exportType, TableModel tableModel, boolean exportFullList,
153         boolean includeHeader, boolean decorateValues)
154     {
155         ExportView view;
156 
157         Class viewClass = (Class) viewClasses.get(exportType);
158 
159         try
160         {
161             view = (ExportView) viewClass.newInstance();
162         }
163         catch (InstantiationException e)
164         {
165             // should not happen (class has already been instantiated before)
166             throw new WrappedRuntimeException(getClass(), e);
167         }
168         catch (IllegalAccessException e)
169         {
170             // should not happen (class has already been instantiated before)
171             throw new WrappedRuntimeException(getClass(), e);
172         }
173 
174         view.setParameters(tableModel, exportFullList, includeHeader, decorateValues);
175         return view;
176     }
177 
178 }