Coverage Report - org.wiztools.xml2spreadsheet.poiimpl.POIStyleCreator
 
Classes in this File Line Coverage Branch Coverage Complexity
POIStyleCreator
60% 
62% 
7.6
 
 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  10
     
 24  9
     private static POIStyleCreator styleCreator = new POIStyleCreator();
 25  
     
 26  10
     /** Creates a new instance of POIStyleCreator */
 27  19
     private POIStyleCreator() {
 28  9
     }
 29  
     
 30  38140
     public static POIStyleCreator getInstance(){
 31  34353
         return styleCreator;
 32  
     }
 33  
     
 34  90
     private void setBorder(HSSFCellStyle style, short border){
 35  198
         style.setBorderTop(border);
 36  198
         style.setBorderBottom(border);
 37  198
         style.setBorderLeft(border);
 38  198
         style.setBorderRight(border);
 39  108
     }
 40  
     
 41  130
     private short getColor(String color) throws OperationException{
 42  144
         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  38140
         
 49  34353
         String hash = StyleHashCreator.getHash(cellStyleVal);
 50  38140
         // System.out.println("style hash: "+hash);
 51  34353
         HSSFCellStyle style = (HSSFCellStyle)styleRepo.get(hash);
 52  38140
         
 53  34353
         if(style != null){
 54  38050
             // System.out.println("Got style from repository for hash: "+hash);
 55  34245
             return style;
 56  
         }
 57  90
         
 58  198
         style = workBook.createCellStyle();
 59  608
         String[] arr = cellStyleVal.split("\\s*;\\s*");
 60  977
         for(int i=0; i<arr.length; i++){
 61  869
             String[] tarr = arr[i].split("\\s*:\\s*");
 62  459
             if(tarr.length != 2){
 63  0
                 continue;
 64  410
             }
 65  869
             String key = tarr[0];
 66  869
             String val = tarr[1];
 67  459
             if("background".equals(key)){
 68  104
                 style.setFillBackgroundColor(getColor(val));
 69  
                 //style.setFillForegroundColor(getColor(val));
 70  360
             }
 71  485
             else if("color".equals(key)){
 72  90
                 HSSFFont font = workBook.createFont();
 73  80
                 // font.setColor(HSSFFont.COLOR_RED);
 74  170
                 font.setColor(getColor(val));
 75  170
                 style.setFont(font);
 76  370
             }
 77  425
             else if("align".equals(key)){
 78  197
                 if("center".equals(val)){
 79  90
                     style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
 80  30
                 }
 81  27
                 else if("justify".equals(val)){
 82  0
                     style.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY);
 83  30
                 }
 84  57
                 else if("left".equals(val)){
 85  27
                     style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
 86  0
                 }
 87  0
                 else if("right".equals(val)){
 88  0
                     style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
 89  0
                 }
 90  0
                 else if("fill".equals(val)){
 91  0
                     style.setAlignment(HSSFCellStyle.ALIGN_FILL);
 92  0
                 }
 93  0
                 else if("general".equals(val)){
 94  0
                     style.setAlignment(HSSFCellStyle.ALIGN_GENERAL);
 95  0
                 }
 96  0
                 else if("center-selection".equals(val)){
 97  0
                     style.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);
 98  
                 }
 99  0
                 else{
 100  0
                     throw new OperationException("Invalid value for style align: "+val);
 101  
                 }
 102  170
             }
 103  208
             else if("valign".equals(key)){
 104  28
                 if("center".equals(val)){
 105  18
                     style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
 106  0
                 }
 107  0
                 else if("bottom".equals(val)){
 108  0
                     style.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);
 109  0
                 }
 110  0
                 else if("top".equals(val)){
 111  0
                     style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
 112  0
                 }
 113  0
                 else if("justify".equals(val)){
 114  0
                     style.setVerticalAlignment(HSSFCellStyle.VERTICAL_JUSTIFY);
 115  
                 }
 116  0
                 else{
 117  0
                     throw new OperationException("Invalid value for style valign: "+val);
 118  
                 }
 119  160
             }
 120  250
             else if("text-decoration".equals(key)){
 121  142
                 if("bold".equals(val)){
 122  142
                     HSSFFont font = workBook.createFont();
 123  142
                     font.setBoldweight(font.BOLDWEIGHT_BOLD);
 124  142
                     style.setFont(font);
 125  72
                 }
 126  0
                 else if("normal".equals(val)){
 127  0
                     HSSFFont font = workBook.createFont();
 128  0
                     font.setBoldweight(font.BOLDWEIGHT_NORMAL);
 129  0
                     style.setFont(font);
 130  0
                 }
 131  0
                 else{
 132  0
                     throw new OperationException(
 133  
                             "Invalid value for style text-decoration: "+val);
 134  
                 }
 135  90
             }
 136  198
             else if("border".equals(key)){
 137  108
                 if("dashed".equals(val)){
 138  0
                     short border = HSSFCellStyle.BORDER_DASHED;
 139  0
                     setBorder(style, border);
 140  90
                 }
 141  188
                 else if("dotted".equals(val)){
 142  170
                     short border = HSSFCellStyle.BORDER_DOTTED;
 143  170
                     setBorder(style, border);
 144  100
                 }
 145  28
                 else if("double".equals(val)){
 146  28
                     short border = HSSFCellStyle.BORDER_DOUBLE;
 147  28
                     setBorder(style, border);
 148  18
                 }
 149  0
                 else if("hair".equals(val)){
 150  0
                     short border = HSSFCellStyle.BORDER_HAIR;
 151  0
                     setBorder(style, border);
 152  0
                 }
 153  0
                 else if("medium".equals(val)){
 154  0
                     short border = HSSFCellStyle.BORDER_MEDIUM;
 155  0
                     setBorder(style, border);
 156  0
                 }
 157  0
                 else if("thick".equals(val)){
 158  0
                     short border = HSSFCellStyle.BORDER_THICK;
 159  0
                     setBorder(style, border);
 160  0
                 }
 161  0
                 else{
 162  0
                     throw new OperationException(
 163  
                             "Invalid value for style border: "+val);
 164  
                 }
 165  
             }
 166  
         }
 167  90
         
 168  108
         styleRepo.put(hash, style);
 169  90
         
 170  108
         return style;
 171  
     }
 172  
 }