View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  
19  package org.caleigo.core;
20  
21  import java.io.*;
22  import java.text.*;
23  import java.util.*;
24  
25  /*** The EntityWriter is a FilterWriter that adds write methods for wrting 
26   * entities, selections and related data.
27   *
28   * @author  Dennis Zikovic
29   * @version 1.00
30   * 
31   *//* 
32   *
33   * WHEN        WHO               WHY & WHAT
34   * -----------------------------------------------------------------------------
35   * 2003-02-24  Dennis Zikovic    Creation
36   */
37  public class EntityWriter extends FilterWriter
38  {
39      // Constants ---------------------------------------------------------------
40      public static final int DEFAULT_DECIMAL_COUNT;
41          
42      // Static data members -----------------------------------------------------
43      protected static DateFormat sDefaultDateFormat;
44      protected static NumberFormat sDefaultIntegerFormat;
45      protected static NumberFormat sDefaultFloatFormat;
46      
47      // Data members ------------------------------------------------------------
48      private int mFieldSeparator = ';';
49      private int mRecordSeparator = '\n';
50      private int mStringDelimiter = '"';
51      private String mNullValue = "";
52      
53      private Set mBlockedDataTypeSet;
54      private boolean mUsePrintFormat = false;
55      
56      // Static methods ----------------------------------------------------------
57      
58      static
59      {        
60          // Set default decimal count.
61          int decimal = 2;
62          try
63          {
64              // Try to base decimal count based on the local currency format.
65              decimal = Currency.getInstance(Locale.getDefault()).getDefaultFractionDigits();
66          }
67          catch(Exception e) 
68          {
69          }     
70          DEFAULT_DECIMAL_COUNT = decimal;
71          
72          // Create default number format.
73          sDefaultDateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
74          sDefaultIntegerFormat = NumberFormat.getNumberInstance();
75          sDefaultFloatFormat = NumberFormat.getNumberInstance();
76          sDefaultFloatFormat.setMinimumFractionDigits(DEFAULT_DECIMAL_COUNT);
77          sDefaultFloatFormat.setMaximumFractionDigits(DEFAULT_DECIMAL_COUNT);
78      }
79      
80      /*** Static help method that creates a string representation of a entity
81       * selection suitable for end-user display considering the default local.
82       */
83      public static String createDragString(ISelection selection)
84      {
85          StringWriter stringWriter = new StringWriter();
86          EntityWriter entityWriter = new EntityWriter(stringWriter, '\t', '\n');
87          try
88          {
89              // Configure EntityWriter.
90              entityWriter.addBlockedDataType(DataType.IMAGE);
91              entityWriter.setPrintFormat(true);
92              
93              // Write entity selection.
94              entityWriter.writeFieldTitles(selection.getEntityDescriptor());
95              entityWriter.writeSelection(selection);
96          }
97          catch(IOException e)
98          {
99              return null;
100         }
101         return stringWriter.getBuffer().toString();
102     }
103 
104     public static NumberFormat getDefaultIntegerFormat()
105     {
106         return sDefaultIntegerFormat;
107     }
108     
109     public static void setDefaultIntegerFormat(NumberFormat format)
110     {
111         sDefaultIntegerFormat = format;
112     }
113     
114     public static NumberFormat getDefaultFloatFormat()
115     {
116         return sDefaultFloatFormat;
117     }
118     
119     public static void setDefaultFloatFormat(NumberFormat format)
120     {
121         sDefaultFloatFormat = format;
122     }
123     
124     public static DateFormat getDefaultDateFormat()
125     {
126         return sDefaultDateFormat;
127     }
128     
129     public static void setDefaultDateFormat(DateFormat format)
130     {
131         sDefaultDateFormat = format;
132     }
133     
134     // Constructors ------------------------------------------------------------
135     public EntityWriter(Writer out)
136     {
137         super(out);
138     }
139     
140     public EntityWriter(Writer out, int fieldSeparator, int entitySeparator)
141     {
142         this(out);
143         mFieldSeparator = fieldSeparator;
144         mRecordSeparator = entitySeparator;
145     }
146     
147     // Action methods ----------------------------------------------------------
148         
149     /*** Writes a header row with titles using the display names of the fields 
150      * from the provided entity descriptor.
151      */
152     public void writeFieldTitles(IEntityDescriptor entityDescriptor) throws IOException
153     {
154         int fieldCount = 0;
155         for(int j=0; j<entityDescriptor.getFieldCount(); j++)
156         {
157             if(!this.isDataTypeBlocked(entityDescriptor.getFieldDescriptor(j).getDataType())
158                     && !(mUsePrintFormat && entityDescriptor.getFieldDescriptor(j).isHiddenField()))
159             {
160                 if(fieldCount>0)
161                     this.write(mFieldSeparator);
162                     
163                 if(this.useStringDelimiter(DataType.STRING))
164                     this.write(mStringDelimiter);
165                     
166                 this.write(entityDescriptor.getFieldDescriptor(j).getDisplayName());
167                 fieldCount++;
168                 
169                 if(this.useStringDelimiter(DataType.STRING))
170                     this.write(mStringDelimiter);
171             }
172         }
173         this.write(mRecordSeparator);
174     }
175     
176     /*** Writes a single entity to the writer.
177      */
178     public void writeEntity(IEntity entity) throws IOException
179     {
180         int fieldCount = 0;
181         for(int j=0; j<entity.getEntityDescriptor().getFieldCount(); j++)
182         {
183             if(!this.isDataTypeBlocked(entity.getEntityDescriptor().getFieldDescriptor(j).getDataType())
184                     && !(mUsePrintFormat && entity.getEntityDescriptor().getFieldDescriptor(j).isHiddenField())
185                     && !(entity.getEntityDescriptor().getFieldDescriptor(j) instanceof ICalculatedFieldDescriptor))
186             {
187                 if(fieldCount>0)
188                     this.write(mFieldSeparator);
189                 this.writeData(entity.getEntityDescriptor().getFieldDescriptor(j).getDataType(), entity.getData(entity.getEntityDescriptor().getFieldDescriptor(j)));
190                 fieldCount++;
191             }
192         }
193         this.write(mRecordSeparator);
194     }
195     
196     /*** Writes a selection to the writer.
197      */
198     public void writeSelection(ISelection selection) throws IOException
199     {
200         for(int j=0; j<selection.size(); j++)
201             this.writeEntity(selection.getEntity(j));
202     }
203     
204     /*** Writes a selection to the writer preceded with a row consisting of the
205      * source names for the fields defined by the selections entity descriptor.
206      */
207     public void writeMappedSelection(ISelection selection) throws IOException
208     {
209         int fieldCount = 0;
210         
211         // Write field descriptor map.
212         for(int j=0; j<selection.getEntityDescriptor().getFieldCount(); j++)
213         {
214             if(!this.isDataTypeBlocked(selection.getEntityDescriptor().getFieldDescriptor(j).getDataType())
215                     && !(mUsePrintFormat && selection.getEntityDescriptor().getFieldDescriptor(j).isHiddenField())
216                     && !(selection.getEntityDescriptor().getFieldDescriptor(j) instanceof ICalculatedFieldDescriptor))
217             {
218                 if(fieldCount>0)
219                     this.write(mFieldSeparator);
220                     
221                 if(this.useStringDelimiter(DataType.STRING))
222                     this.write(mStringDelimiter);
223                     
224                 this.write(selection.getEntityDescriptor().getFieldDescriptor(j).getSourceName());
225                 
226                 if(this.useStringDelimiter(DataType.STRING))
227                     this.write(mStringDelimiter);
228                     
229                 fieldCount++;
230             }
231         }
232         this.write(mRecordSeparator);
233         
234         // Write selection.
235         this.writeSelection(selection);
236     }
237     
238     // Access methods ----------------------------------------------------------
239     
240     public int getFieldSeparator()
241     {
242         return mFieldSeparator;
243     }
244     
245     public void setFieldSeparator(int character)
246     {
247         mFieldSeparator = character;
248     }
249     
250     public int getRecordSeparator()
251     {
252         return mRecordSeparator;
253     }
254     
255     public void setRecordSeparator(int character)
256     {
257         mRecordSeparator = character;
258     }
259     
260     public int getStringDelimiter()
261     {
262         return mStringDelimiter;
263     }
264     
265     public void setStringDelimiter(int character)
266     {
267         mStringDelimiter = character;
268     }
269     
270     public String getNullValue()
271     {
272         return mNullValue;
273     }
274     
275     /*** Setting the string mark to zero means that no string quotes will 
276      * be used.
277      */
278     public void setNullValue(String nullString)
279     {
280         mNullValue = nullString;
281     }
282     
283     /*** This method makes it possible to register data types that should be 
284      * ignored when writing.
285      */
286     public void addBlockedDataType(DataType dataType)
287     {
288         if(mBlockedDataTypeSet==null)
289             mBlockedDataTypeSet = new HashSet();
290         mBlockedDataTypeSet.add(dataType);            
291     }
292     
293     /*** Returns true if the provided data type is blocked.
294      */
295     public boolean isDataTypeBlocked(DataType dataType)
296     {
297         if(mBlockedDataTypeSet==null)
298             return false;
299         else
300             return mBlockedDataTypeSet.contains(dataType);            
301     }
302     
303     /*** Print format indicates that the data written should be formated for
304      * end-user display using the current locale settings. This affects the 
305      * format of dates and numeric values. It will also ignore all fields
306      * with the hidden flag set when writing. Print format is by default not 
307      * active.
308      */
309     public void setPrintFormat(boolean usePrintFormat)
310     {
311         mUsePrintFormat = usePrintFormat;
312     }
313     
314     /*** Returns true if print format is active.
315      */
316     public boolean isPrintFormat()
317     {
318         return mUsePrintFormat;
319     }
320     
321     // Help methods ------------------------------------------------------------
322     protected void writeData(DataType dataType, Object data) throws IOException
323     {
324         if(data!=null)
325         {
326             // Format data valute to a string.
327             if(mUsePrintFormat && (dataType==DataType.BYTE 
328                     || dataType==DataType.SHORT 
329                     || dataType==DataType.INTEGER         
330                     || dataType==DataType.LONG))
331                 data = sDefaultIntegerFormat.format(data);
332             else if(mUsePrintFormat && (dataType==DataType.FLOAT
333                     || dataType==DataType.DOUBLE
334                     || dataType==DataType.BIG_DECIMAL))           
335                 data = sDefaultFloatFormat.format(data);
336             else if(mUsePrintFormat && dataType==DataType.DATE)
337                 data = sDefaultDateFormat.format(data);
338             else
339                 data = dataType.convertToString(data);
340                     
341             // Write data value.
342             if(this.useStringDelimiter(dataType))
343             {
344                 this.write(mStringDelimiter);
345                 
346                 String stringValue = data.toString();
347                 for(int j=0; j<stringValue.length(); j++)
348                 {
349                     this.write(stringValue.charAt(j));
350                     if(stringValue.charAt(j)==mStringDelimiter)
351                         this.write(mStringDelimiter);
352                 }
353                 
354                 this.write(mStringDelimiter);
355             }
356             else
357                 this.write(data.toString());
358         }
359         else
360             this.write(mNullValue);
361     }
362     
363     protected boolean useStringDelimiter(DataType dataType)
364     {
365         return mStringDelimiter>0 && dataType==DataType.STRING;
366     }
367 }