1 13 package com.tonbeller.jpivot.olap.model.impl; 14 15 import java.lang.reflect.Constructor ; 16 import java.util.ArrayList ; 17 import java.util.Collections ; 18 import java.util.HashMap ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 import org.apache.log4j.Logger; 23 import org.apache.regexp.RE; 24 import org.apache.regexp.RESyntaxException; 25 26 import com.tonbeller.jpivot.olap.model.Cell; 27 import com.tonbeller.jpivot.olap.model.CellFormatter; 28 import com.tonbeller.tbutils.res.Resources; 29 30 public class FormatStringParser { 31 private static Map cellFormatters = new HashMap (); 32 private RE regex1; 33 private RE regex2; 34 private Logger logger = Logger.getLogger(FormatStringParser.class); 35 Resources resources; 36 37 public FormatStringParser() { 38 try { 39 regex1 = new RE("\\s*([a-zA-Z][\\w\\.]*)\\s*=\\s*'([^']*)'"); 40 regex2 = new RE("\\s*([a-zA-Z][\\w\\.]*)\\s*=\\s*([^\\s]*)"); 41 resources = Resources.instance(FormatStringParser.class); 42 } catch (RESyntaxException e) { 43 logger.error(null, e); 44 } 45 } 46 47 public static class Result { 48 String formattedValue; 49 List properties; 50 51 private Result(String formattedValue, List properties) { 52 this.formattedValue = formattedValue; 53 this.properties = properties; 54 } 55 public String getFormattedValue() { 56 return formattedValue; 57 } 58 public List getProperties() { 59 return properties; 60 } 61 } 62 63 public Result parse(Cell cell, String formattedValue) { 64 if (formattedValue == null) { 65 return new Result("", Collections.EMPTY_LIST); 67 } 68 69 List properties = Collections.EMPTY_LIST; 70 if (formattedValue.startsWith("|")) { 71 properties = new ArrayList (); 72 String [] strs = formattedValue.substring(1).split("\\|"); 73 formattedValue = strs[0]; for (int i = 1; i < strs.length; i++) { 75 String propName = null; 76 String propValue = null; 77 if (regex1.match(strs[i])) { 78 propName = regex1.getParen(1); propValue = regex1.getParen(2); } else if (regex2.match(strs[i])) { 81 propName = regex2.getParen(1); propValue = regex2.getParen(2); } else { 84 formattedValue += strs[i]; 87 continue; 88 } 89 90 if (propName.equalsIgnoreCase("exit")) { 96 CellFormatter cf = getCellFormatter(propValue); 98 if (cf != null) { 99 formattedValue = cf.formatCell(cell); 100 } 101 } else { 102 PropertyImpl prop = new PropertyImpl(); 103 104 prop.setName(propName); 105 prop.setLabel(propName); 106 prop.setValue(propValue); 107 108 properties.add(prop); 109 } 110 } 112 } return new Result(formattedValue, properties); 114 } 115 116 119 private CellFormatter getCellFormatter(String propValue) { 120 synchronized (cellFormatters) { 121 CellFormatter cf = (CellFormatter) cellFormatters.get(propValue); 122 if (cf == null) { 123 try { 125 String className = resources.getString("cellfmt." + propValue); 126 Class clazz = Class.forName(className); 127 Constructor ctor = clazz.getConstructor(new Class [0]); 128 cf = (CellFormatter) ctor.newInstance(new Object [0]); 129 } catch (Exception e) { 130 logger.error("could not instantiate cell formatter " + propValue, e); 131 } 132 cellFormatters.put(propValue, cf); 133 } else { 134 logger.error("Could not find a property definition for exit = " + propValue); 135 } 136 return cf; 137 } 138 } 139 140 } | Popular Tags |