View Javadoc

1   /*
2    * POIStyleCreator.java
3    *
4    * Created on April 11, 2005, 4:17 PM
5    */
6   
7   package org.wiztools.xml2spreadsheet.poiimpl;
8   
9   import org.apache.poi.hssf.util.HSSFColor;
10  import org.wiztools.xml2spreadsheet.exception.OperationException;
11  import org.apache.poi.hssf.usermodel.HSSFCellStyle;
12  import org.apache.poi.hssf.usermodel.HSSFFont;
13  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
14  import org.wiztools.xml2spreadsheet.util.StyleHashCreator;
15  import org.wiztools.xml2spreadsheet.util.StyleRepository;
16  
17  
18  /***
19   *
20   * @author subhash
21   */
22  public final class POIStyleCreator {
23      
24      private static POIStyleCreator styleCreator = new POIStyleCreator();
25      
26      /*** Creates a new instance of POIStyleCreator */
27      private POIStyleCreator() {
28      }
29      
30      public static POIStyleCreator getInstance(){
31          return styleCreator;
32      }
33      
34      private void setBorder(HSSFCellStyle style, short border){
35          style.setBorderTop(border);
36          style.setBorderBottom(border);
37          style.setBorderLeft(border);
38          style.setBorderRight(border);
39      }
40      
41      private short getColor(String color) throws OperationException{
42          return POIColor.getInstance().getColor(color);
43      }
44      
45      public HSSFCellStyle getStyle(final HSSFWorkbook workBook,
46              final String cellStyleVal, final StyleRepository styleRepo)
47                          throws OperationException{
48          
49          String hash = StyleHashCreator.getHash(cellStyleVal);
50          // System.out.println("style hash: "+hash);
51          HSSFCellStyle style = (HSSFCellStyle)styleRepo.get(hash);
52          
53          if(style != null){
54              // System.out.println("Got style from repository for hash: "+hash);
55              return style;
56          }
57          
58          style = workBook.createCellStyle();
59          String[] arr = cellStyleVal.split("//s*;//s*");
60          for(int i=0; i<arr.length; i++){
61              String[] tarr = arr[i].split("//s*://s*");
62              if(tarr.length != 2){
63                  continue;
64              }
65              String key = tarr[0];
66              String val = tarr[1];
67              if("background".equals(key)){
68                  style.setFillBackgroundColor(getColor(val));
69                  //style.setFillForegroundColor(getColor(val));
70              }
71              else if("color".equals(key)){
72                  HSSFFont font = workBook.createFont();
73                  // font.setColor(HSSFFont.COLOR_RED);
74                  font.setColor(getColor(val));
75                  style.setFont(font);
76              }
77              else if("align".equals(key)){
78                  if("center".equals(val)){
79                      style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
80                  }
81                  else if("justify".equals(val)){
82                      style.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY);
83                  }
84                  else if("left".equals(val)){
85                      style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
86                  }
87                  else if("right".equals(val)){
88                      style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
89                  }
90                  else if("fill".equals(val)){
91                      style.setAlignment(HSSFCellStyle.ALIGN_FILL);
92                  }
93                  else if("general".equals(val)){
94                      style.setAlignment(HSSFCellStyle.ALIGN_GENERAL);
95                  }
96                  else if("center-selection".equals(val)){
97                      style.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);
98                  }
99                  else{
100                     throw new OperationException("Invalid value for style align: "+val);
101                 }
102             }
103             else if("valign".equals(key)){
104                 if("center".equals(val)){
105                     style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
106                 }
107                 else if("bottom".equals(val)){
108                     style.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);
109                 }
110                 else if("top".equals(val)){
111                     style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
112                 }
113                 else if("justify".equals(val)){
114                     style.setVerticalAlignment(HSSFCellStyle.VERTICAL_JUSTIFY);
115                 }
116                 else{
117                     throw new OperationException("Invalid value for style valign: "+val);
118                 }
119             }
120             else if("text-decoration".equals(key)){
121                 if("bold".equals(val)){
122                     HSSFFont font = workBook.createFont();
123                     font.setBoldweight(font.BOLDWEIGHT_BOLD);
124                     style.setFont(font);
125                 }
126                 else if("normal".equals(val)){
127                     HSSFFont font = workBook.createFont();
128                     font.setBoldweight(font.BOLDWEIGHT_NORMAL);
129                     style.setFont(font);
130                 }
131                 else{
132                     throw new OperationException(
133                             "Invalid value for style text-decoration: "+val);
134                 }
135             }
136             else if("border".equals(key)){
137                 if("dashed".equals(val)){
138                     short border = HSSFCellStyle.BORDER_DASHED;
139                     setBorder(style, border);
140                 }
141                 else if("dotted".equals(val)){
142                     short border = HSSFCellStyle.BORDER_DOTTED;
143                     setBorder(style, border);
144                 }
145                 else if("double".equals(val)){
146                     short border = HSSFCellStyle.BORDER_DOUBLE;
147                     setBorder(style, border);
148                 }
149                 else if("hair".equals(val)){
150                     short border = HSSFCellStyle.BORDER_HAIR;
151                     setBorder(style, border);
152                 }
153                 else if("medium".equals(val)){
154                     short border = HSSFCellStyle.BORDER_MEDIUM;
155                     setBorder(style, border);
156                 }
157                 else if("thick".equals(val)){
158                     short border = HSSFCellStyle.BORDER_THICK;
159                     setBorder(style, border);
160                 }
161                 else{
162                     throw new OperationException(
163                             "Invalid value for style border: "+val);
164                 }
165             }
166         }
167         
168         styleRepo.put(hash, style);
169         
170         return style;
171     }
172 }