KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > olap > model > impl > FormatStringParser


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.olap.model.impl;
14
15 import java.lang.reflect.Constructor JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
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 JavaDoc cellFormatters = new HashMap JavaDoc();
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 JavaDoc formattedValue;
49     List JavaDoc properties;
50
51     private Result(String JavaDoc formattedValue, List JavaDoc properties) {
52       this.formattedValue = formattedValue;
53       this.properties = properties;
54     }
55     public String JavaDoc getFormattedValue() {
56       return formattedValue;
57     }
58     public List JavaDoc getProperties() {
59       return properties;
60     }
61   }
62
63   public Result parse(Cell cell, String JavaDoc formattedValue) {
64     if (formattedValue == null) {
65       // SAP
66
return new Result("", Collections.EMPTY_LIST);
67     }
68     
69     List JavaDoc properties = Collections.EMPTY_LIST;
70     if (formattedValue.startsWith("|")) {
71       properties = new ArrayList JavaDoc();
72       String JavaDoc[] strs = formattedValue.substring(1).split("\\|");
73       formattedValue = strs[0]; // original value
74
for (int i = 1; i < strs.length; i++) {
75         String JavaDoc propName = null;
76         String JavaDoc propValue = null;
77         if (regex1.match(strs[i])) {
78           propName = regex1.getParen(1); // property name
79
propValue = regex1.getParen(2); // property value
80
} else if (regex2.match(strs[i])) {
81           propName = regex2.getParen(1); // property name
82
propValue = regex2.getParen(2); // property value
83
} else {
84           // it is not a key=value pair
85
// we add the String to the formadded value
86
formattedValue += strs[i];
87           continue;
88         }
89
90         // call user defined function, if property key is "exit"
91
// exit = xxx
92
// where xxx is assigned to a class in user.properties or
93
// system.properties
94
// the class must implement the CellFormatter interface
95
if (propName.equalsIgnoreCase("exit")) {
96           // try to find the exit in the map
97
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       } // for
111

112     } // if (formattedValue.startsWith("|"))
113
return new Result(formattedValue, properties);
114   }
115
116   /**
117    * Threadsafe access to cell formatter cache
118    */

119   private CellFormatter getCellFormatter(String JavaDoc propValue) {
120     synchronized (cellFormatters) {
121       CellFormatter cf = (CellFormatter) cellFormatters.get(propValue);
122       if (cf == null) {
123         // not there, create new
124
try {
125           String JavaDoc className = resources.getString("cellfmt." + propValue);
126           Class JavaDoc clazz = Class.forName(className);
127           Constructor JavaDoc ctor = clazz.getConstructor(new Class JavaDoc[0]);
128           cf = (CellFormatter) ctor.newInstance(new Object JavaDoc[0]);
129         } catch (Exception JavaDoc 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