Coverage Report - org.wiztools.xml2spreadsheet.jxlimpl.JXLStyleCreator
 
Classes in this File Line Coverage Branch Coverage Complexity
JXLStyleCreator
58% 
61% 
9.75
 
 1  
 /*
 2  
  * JXLStyleCreator.java
 3  
  *
 4  
  * Created on November 17, 2006, 5:18 PM
 5  
  *
 6  
  * To change this template, choose Tools | Template Manager
 7  
  * and open the template in the editor.
 8  
  */
 9  
 
 10  
 package org.wiztools.xml2spreadsheet.jxlimpl;
 11  
 
 12  
 import jxl.format.Alignment;
 13  
 import jxl.format.Border;
 14  
 import jxl.format.BorderLineStyle;
 15  
 import jxl.format.Colour;
 16  
 import jxl.format.Font;
 17  
 import jxl.format.VerticalAlignment;
 18  
 import jxl.write.WritableCell;
 19  
 import jxl.write.WritableCellFormat;
 20  
 import jxl.write.WritableFont;
 21  
 import jxl.write.WriteException;
 22  
 import org.wiztools.xml2spreadsheet.exception.OperationException;
 23  
 import org.wiztools.xml2spreadsheet.util.StyleHashCreator;
 24  
 import org.wiztools.xml2spreadsheet.util.StyleRepository;
 25  
 
 26  
 /**
 27  
  *
 28  
  * @author subhash
 29  
  */
 30  
 public class JXLStyleCreator {
 31  
     
 32  9
     private static JXLStyleCreator jsc = new JXLStyleCreator();
 33  
     
 34  
     /** Creates a new instance of JXLStyleCreator */
 35  9
     private JXLStyleCreator() {
 36  9
     }
 37  
     
 38  
     public static JXLStyleCreator getInstance(){
 39  34353
         return jsc;
 40  
     }
 41  
     
 42  
     public void setBackground(WritableCellFormat wcf, Colour color){
 43  
         
 44  0
     }
 45  
     
 46  
     public WritableCellFormat getStyle(final String cellStyleVal,
 47  
             final StyleRepository styleRepo)
 48  
                     throws OperationException{
 49  
         
 50  34353
         String hash = StyleHashCreator.getHash(cellStyleVal);
 51  
         // System.out.println("style hash: "+hash);
 52  34353
         WritableCellFormat style = (WritableCellFormat)styleRepo.get(hash);
 53  
         
 54  34353
         if(style != null){
 55  
             // System.out.println("Got style from repository for hash: "+hash);
 56  34245
             return style;
 57  
         }
 58  
         
 59  108
         style = new WritableCellFormat();
 60  
         
 61  108
         String[] arr = cellStyleVal.split("\\s*;\\s*");
 62  
         
 63  
         try{
 64  
         
 65  567
             for(int i=0; i<arr.length; i++){
 66  459
                 String[] tarr = arr[i].split("\\s*:\\s*");
 67  459
                 if(tarr.length != 2){
 68  0
                     continue;
 69  
                 }
 70  459
                 String key = tarr[0];
 71  459
                 String val = tarr[1];
 72  459
                 if("background".equals(key)){
 73  
                     // style.setFillBackgroundColor(HSSFColor.BLACK.index);
 74  54
                     style.setBackground(JXLColor.getInstance().getColor(val));
 75  
                 }
 76  405
                 else if("color".equals(key)){
 77  90
                     Font font = style.getFont();
 78  
                     // font.setColor(HSSFFont.COLOR_RED);
 79  
                     //font.setColor(getColor(val));
 80  
                     //style.setFont(font);
 81  90
                 }
 82  315
                 else if("align".equals(key)){
 83  117
                     if("center".equals(val)){
 84  90
                         style.setAlignment(Alignment.CENTRE);
 85  
                     }
 86  27
                     else if("justify".equals(val)){
 87  0
                         style.setAlignment(Alignment.JUSTIFY);
 88  
                     }
 89  27
                     else if("left".equals(val)){
 90  27
                         style.setAlignment(Alignment.LEFT);
 91  
                     }
 92  0
                     else if("right".equals(val)){
 93  0
                         style.setAlignment(Alignment.RIGHT);
 94  
                     }
 95  0
                     else if("fill".equals(val)){
 96  0
                         style.setAlignment(Alignment.FILL);
 97  
                     }
 98  0
                     else if("general".equals(val)){
 99  0
                         style.setAlignment(Alignment.GENERAL);
 100  
                     }
 101  0
                     else if("center-selection".equals(val)){
 102  0
                         style.setAlignment(Alignment.CENTRE);
 103  
                     }
 104  
                     else{
 105  0
                         throw new OperationException("Invalid value for style align: "+val);
 106  
                     }
 107  
                 }
 108  198
                 else if("valign".equals(key)){
 109  18
                     if("center".equals(val)){
 110  18
                         style.setVerticalAlignment(VerticalAlignment.CENTRE);
 111  
                     }
 112  0
                     else if("bottom".equals(val)){
 113  0
                         style.setVerticalAlignment(VerticalAlignment.BOTTOM);
 114  
                     }
 115  0
                     else if("top".equals(val)){
 116  0
                         style.setVerticalAlignment(VerticalAlignment.TOP);
 117  
                     }
 118  0
                     else if("justify".equals(val)){
 119  0
                         style.setVerticalAlignment(VerticalAlignment.JUSTIFY);
 120  
                     }
 121  
                     else{
 122  0
                         throw new OperationException("Invalid value for style valign: "+val);
 123  
                     }
 124  
                 }
 125  180
                 else if("text-decoration".equals(key)){
 126  72
                     if("bold".equals(val)){
 127  72
                         Font font = style.getFont();
 128  
                         //HSSFFont font = workBook.createFont();
 129  
                         //font.setBoldweight(font.BOLDWEIGHT_BOLD);
 130  
                         //style.setFont(font);
 131  72
                     }
 132  0
                     else if("normal".equals(val)){
 133  
                         //HSSFFont font = workBook.createFont();
 134  
                         //font.setBoldweight(font.BOLDWEIGHT_NORMAL);
 135  
                         //style.setFont(font);
 136  
                     }
 137  
                     else{
 138  0
                         throw new OperationException(
 139  
                                 "Invalid value for style text-decoration: "+val);
 140  
                     }
 141  
                 }
 142  108
                 else if("border".equals(key)){
 143  108
                     if("dashed".equals(val)){
 144  0
                         style.setBorder(Border.ALL, BorderLineStyle.DASHED);
 145  
                     }
 146  108
                     else if("dotted".equals(val)){
 147  90
                         style.setBorder(Border.ALL, BorderLineStyle.DOTTED);
 148  
                     }
 149  18
                     else if("double".equals(val)){
 150  18
                         style.setBorder(Border.ALL, BorderLineStyle.DOUBLE);
 151  
                     }
 152  0
                     else if("hair".equals(val)){
 153  0
                         style.setBorder(Border.ALL, BorderLineStyle.HAIR);
 154  
                     }
 155  0
                     else if("medium".equals(val)){
 156  0
                         style.setBorder(Border.ALL, BorderLineStyle.MEDIUM);
 157  
                     }
 158  0
                     else if("thick".equals(val)){
 159  0
                         style.setBorder(Border.ALL, BorderLineStyle.THICK);
 160  
                     }
 161  
                     else{
 162  0
                         throw new OperationException(
 163  
                                 "Invalid value for style border: "+val);
 164  
                     }
 165  
                 }
 166  
             }
 167  
         }
 168  0
         catch(WriteException we){
 169  0
             throw new OperationException("" + we.getMessage());
 170  108
         }
 171  
         
 172  108
         styleRepo.put(hash, style);
 173  
         
 174  108
         return style;
 175  
     }
 176  
     
 177  
 }