KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > management > data > DataFormatUtil


1 /**
2 * Copyright (c) 2004-2005 jManage.org
3 *
4 * This is a free software; you can redistribute it and/or
5 * modify it under the terms of the license at
6 * http://www.jmanage.org.
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */

14 package org.jmanage.core.management.data;
15
16 import org.jmanage.core.util.Loggers;
17
18 import javax.management.openmbean.CompositeData JavaDoc;
19 import javax.management.openmbean.CompositeType JavaDoc;
20 import java.util.*;
21 import java.util.logging.Logger JavaDoc;
22 import java.util.logging.Level JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.lang.reflect.Array JavaDoc;
27
28 /**
29  * Utility class to provide data formatting.
30  *
31  * <p>
32  * Date: Sep 27, 2005
33  * @author Rakesh Kalra
34  */

35 public class DataFormatUtil {
36
37     private static final Logger JavaDoc logger = Loggers.getLogger(DataFormatUtil.class);
38
39     private static final String JavaDoc FORMAT_PREFIX = "format.";
40     private static final String JavaDoc COMPOSITE_TYPE_FORMAT_PREFIX = "CompositeTypeFormat.";
41
42     private static final String JavaDoc LIST_DELIMITER = "list.delimiter";
43     private static final String JavaDoc ESCAPE_HTML = "escape.html";
44     private static final String JavaDoc NULL_VALUE = "null.value";
45
46     private static Object JavaDoc[][] classToFormatMapping = new Object JavaDoc[0][2];
47     private static Map compositeTypeToFormatMapping = new HashMap();
48
49     // properties
50
private static String JavaDoc listDelimiter = System.getProperty("line.separator");
51     private static boolean escapeHtml = true;
52     private static String JavaDoc nullValue = "";
53     private static int formatCount = 0;
54
55     static{
56         String JavaDoc formatFile =
57                 System.getProperty("org.jmanage.core.management.data.formatConfig");
58         if(formatFile != null){
59             try {
60                 InputStream JavaDoc is = new FileInputStream JavaDoc(formatFile);
61                 Properties props = new Properties();
62                 props.load(is);
63                 is.close();
64                 // note that array is bigger than number of formatters
65
classToFormatMapping = new Object JavaDoc[props.keySet().size()][2];
66                 for(Iterator it=props.keySet().iterator(); it.hasNext();){
67                     String JavaDoc property = (String JavaDoc)it.next();
68                     if(property.startsWith(FORMAT_PREFIX)){
69                         String JavaDoc className = property.substring(FORMAT_PREFIX.length());
70                         // todo: it will be better to load the data class using the application classloader
71
// the data format class can be loaded in the base classloader - rk
72
Class JavaDoc clazz = Class.forName(className);
73                         classToFormatMapping[formatCount][0] = clazz;
74                         classToFormatMapping[formatCount][1] =
75                                 Class.forName(props.getProperty(property)).newInstance();
76                         formatCount ++;
77                     }else if(property.startsWith(COMPOSITE_TYPE_FORMAT_PREFIX)){
78                         String JavaDoc compositeType = property.substring(COMPOSITE_TYPE_FORMAT_PREFIX.length());
79                         DataFormat formatter =
80                                 (DataFormat)Class.forName(props.getProperty(property)).newInstance();
81                         compositeTypeToFormatMapping.put(compositeType,
82                                 formatter);
83                     }else if(property.equals(LIST_DELIMITER)){
84                         listDelimiter = props.getProperty(property);
85                     }else if(property.equals(ESCAPE_HTML)){
86                         escapeHtml =
87                                 Boolean.valueOf(props.getProperty(property)).booleanValue();
88                     }else if(property.equals(NULL_VALUE)){
89                         nullValue = props.getProperty(property);
90                     }else{
91                         logger.warning("Unrecognized property=" + property);
92                     }
93                 }
94             } catch (IOException JavaDoc e) {
95                 logger.log(Level.SEVERE, "Error reading data format config file:" +
96                         formatFile + ". DataFormatUtil is not initialized.", e);
97             } catch (Exception JavaDoc e) {
98                 logger.log(Level.SEVERE, "Config file:" + formatFile +
99                         ". DataFormatUtil is not initialized.", e);
100             }
101         }
102     }
103
104     public static String JavaDoc getListDelimiter(){
105         return listDelimiter;
106     }
107
108     public static boolean isEscapeHtml(){
109         return escapeHtml;
110     }
111
112     public static String JavaDoc getNullDisplayValue(){
113         return nullValue;
114     }
115
116     public static String JavaDoc format(Object JavaDoc data){
117         /* if data is null, return the configured null value */
118         if(data == null) return nullValue;
119
120         /* check for a DataFormat for given type (normal object or array)*/
121         DataFormat formatter = findDataFormat(data);
122         if(formatter != defaultFormatter){
123             return formatter.format(data);
124         }else if(data.getClass().isArray()){
125             /* see if there is a formatter configured for the elements of the
126                 array. This assumes that the array has the same elements. */

127             return formatArray(data);
128         }
129         /* format with the default formatter */
130         return defaultFormatter.format(data);
131     }
132
133     private static String JavaDoc formatArray(Object JavaDoc data){
134         assert data.getClass().isArray();
135         final int arrayLength = Array.getLength(data);
136         if(arrayLength == 0){
137             return "There are no elements in the array.";
138         }
139         DataFormat formatter = findDataFormat(Array.get(data, 0));
140         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
141         for(int i=0; i<arrayLength; i++){
142             if(i > 0){
143                 buff.append(listDelimiter);
144             }
145             Object JavaDoc element = Array.get(data, i);
146             if(element != null)
147                 buff.append(formatter.format(element));
148             else
149                 buff.append(nullValue);
150         }
151         return buff.toString();
152     }
153
154     private static final DefaultDataFormat defaultFormatter =
155             new DefaultDataFormat();
156
157     private static DataFormat findDataFormat(Object JavaDoc data){
158         /* first check if this data is CompositeData type */
159         if(CompositeData JavaDoc.class.isInstance(data)){
160             CompositeType JavaDoc type = ((CompositeData JavaDoc)data).getCompositeType();
161             DataFormat dataFormat =
162                     (DataFormat)compositeTypeToFormatMapping.get(type.getTypeName());
163             if(dataFormat != null)
164                 return dataFormat;
165         }
166         /* now look for other formatters */
167         for(int i=0; i<formatCount; i++){
168             Class JavaDoc clazz = (Class JavaDoc)classToFormatMapping[i][0];
169             if(clazz.isInstance(data)){
170                 return (DataFormat)classToFormatMapping[i][1];
171             }
172         }
173         return defaultFormatter;
174     }
175 }
176
Popular Tags