1 14 package org.jmanage.core.management.data; 15 16 import org.jmanage.core.util.Loggers; 17 18 import javax.management.openmbean.CompositeData ; 19 import javax.management.openmbean.CompositeType ; 20 import java.util.*; 21 import java.util.logging.Logger ; 22 import java.util.logging.Level ; 23 import java.io.FileInputStream ; 24 import java.io.InputStream ; 25 import java.io.IOException ; 26 import java.lang.reflect.Array ; 27 28 35 public class DataFormatUtil { 36 37 private static final Logger logger = Loggers.getLogger(DataFormatUtil.class); 38 39 private static final String FORMAT_PREFIX = "format."; 40 private static final String COMPOSITE_TYPE_FORMAT_PREFIX = "CompositeTypeFormat."; 41 42 private static final String LIST_DELIMITER = "list.delimiter"; 43 private static final String ESCAPE_HTML = "escape.html"; 44 private static final String NULL_VALUE = "null.value"; 45 46 private static Object [][] classToFormatMapping = new Object [0][2]; 47 private static Map compositeTypeToFormatMapping = new HashMap(); 48 49 private static String listDelimiter = System.getProperty("line.separator"); 51 private static boolean escapeHtml = true; 52 private static String nullValue = ""; 53 private static int formatCount = 0; 54 55 static{ 56 String formatFile = 57 System.getProperty("org.jmanage.core.management.data.formatConfig"); 58 if(formatFile != null){ 59 try { 60 InputStream is = new FileInputStream (formatFile); 61 Properties props = new Properties(); 62 props.load(is); 63 is.close(); 64 classToFormatMapping = new Object [props.keySet().size()][2]; 66 for(Iterator it=props.keySet().iterator(); it.hasNext();){ 67 String property = (String )it.next(); 68 if(property.startsWith(FORMAT_PREFIX)){ 69 String className = property.substring(FORMAT_PREFIX.length()); 70 Class 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 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 e) { 95 logger.log(Level.SEVERE, "Error reading data format config file:" + 96 formatFile + ". DataFormatUtil is not initialized.", e); 97 } catch (Exception e) { 98 logger.log(Level.SEVERE, "Config file:" + formatFile + 99 ". DataFormatUtil is not initialized.", e); 100 } 101 } 102 } 103 104 public static String getListDelimiter(){ 105 return listDelimiter; 106 } 107 108 public static boolean isEscapeHtml(){ 109 return escapeHtml; 110 } 111 112 public static String getNullDisplayValue(){ 113 return nullValue; 114 } 115 116 public static String format(Object data){ 117 118 if(data == null) return nullValue; 119 120 121 DataFormat formatter = findDataFormat(data); 122 if(formatter != defaultFormatter){ 123 return formatter.format(data); 124 }else if(data.getClass().isArray()){ 125 127 return formatArray(data); 128 } 129 130 return defaultFormatter.format(data); 131 } 132 133 private static String formatArray(Object 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 buff = new StringBuffer (); 141 for(int i=0; i<arrayLength; i++){ 142 if(i > 0){ 143 buff.append(listDelimiter); 144 } 145 Object 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 data){ 158 159 if(CompositeData .class.isInstance(data)){ 160 CompositeType type = ((CompositeData )data).getCompositeType(); 161 DataFormat dataFormat = 162 (DataFormat)compositeTypeToFormatMapping.get(type.getTypeName()); 163 if(dataFormat != null) 164 return dataFormat; 165 } 166 167 for(int i=0; i<formatCount; i++){ 168 Class clazz = (Class )classToFormatMapping[i][0]; 169 if(clazz.isInstance(data)){ 170 return (DataFormat)classToFormatMapping[i][1]; 171 } 172 } 173 return defaultFormatter; 174 } 175 } 176 | Popular Tags |