1 23 24 package com.sun.enterprise.config; 25 26 import java.util.ArrayList ; 27 28 import java.lang.reflect.Method ; 29 import java.text.CharacterIterator ; 30 import java.text.StringCharacterIterator ; 31 32 import java.util.logging.Logger ; 33 import java.util.logging.Level ; 34 36 37 40 public class ConfigBeansFactory { 41 42 44 51 52 public static ConfigBean getConfigBeanByXPath(ConfigContext ctx, String xpath) 53 throws ConfigException { 54 ConfigBean cb = ctx.getRootConfigBean(); 55 56 if(cb == null) throw new ConfigException("getConfigBeanByXPath: null context"); 57 cb.setConfigContext(ctx); 58 return getConfigBeanByXPath(cb, xpath); 59 } 60 61 62 66 public static String getConfigBeanNameByXPath(String xpath) { 67 XpathSupport xp = new XpathSupport(xpath); 68 ArrayList arr = xp.getClassNameArray(); 69 70 if(arr.size() == 0) return null; 71 return (String )arr.get(arr.size()-1); 72 } 73 74 public static ConfigBean getConfigBeanByXPath(ConfigBean server, String xpath) 75 throws ConfigException { 76 Object ret = server; 77 try { 78 XpathSupport xp = new XpathSupport(xpath); 79 ArrayList arr = xp.getMethodArray(); 80 81 if(arr.size() == 0 || arr.size() == 1) { 82 ((ConfigBean) ret).setXPath(xpath); return (ConfigBean) ret; 84 } 85 86 for(int i=1;i<arr.size();i++) { 87 MethodCaller methodCaller = (MethodCaller) arr.get(i); 88 Method m = ret.getClass().getMethod(methodCaller.getMethodName(), methodCaller.getParamClasses()); 90 ret = m.invoke(ret, methodCaller.getParams()); 92 } 93 94 ((ConfigBean) ret).setConfigContext(server.getConfigContext()); 95 ((ConfigBean) ret).setXPath(xpath); 96 return (ConfigBean) ret; 97 } catch(Throwable t) { 98 } 100 return null; 101 102 } 103 104 private static class XpathSupport { 105 private String xpath; 106 private ArrayList arr = new ArrayList (); 107 private ArrayList nameArr = new ArrayList (); 108 109 XpathSupport(String xpath) { 110 this.xpath = xpath; 111 process(); 112 } 113 ArrayList getMethodArray() { 114 return arr; 115 } 116 ArrayList getClassNameArray() { 117 return nameArr; 118 } 119 120 private static char SEPARATOR_CHAR = '/'; 121 private static char OPENBRACKET_CHAR = '['; 122 private static char CLOSEBRACKET_CHAR = ']'; 123 private static char ESCAPE_CHAR = '\\'; 124 125 private static String [] getListOfNodes(String xpath) { 126 ArrayList arr = new ArrayList (); 128 char ch; 129 boolean insideBracket = false; 130 StringBuffer sb = new StringBuffer (); 131 132 if (xpath == null) 133 return new String []{}; 134 135 for(int i=0;i<xpath.length();i++) { 136 ch = xpath.charAt(i); 137 if(ch == SEPARATOR_CHAR && !insideBracket) { 138 if(sb.length() > 0) { 139 arr.add(sb.toString()); 140 sb = new StringBuffer (); 141 } 142 } else if(ch == SEPARATOR_CHAR && insideBracket) { 143 sb.append(ch); 144 } else if(ch == OPENBRACKET_CHAR) { 145 sb.append(ch); 146 insideBracket = true; 147 } else if (ch == CLOSEBRACKET_CHAR) { 148 sb.append(ch); 149 insideBracket = false; 150 } else { 151 sb.append(ch); 152 } 153 } 154 if(sb.length() >0) 155 arr.add(sb.toString()); 156 157 String [] ret = new String [arr.size()]; 159 for(int j=0;j<arr.size();j++) { 160 ret[j] = (String ) arr.get(j); 161 } 162 163 return ret; 164 } 165 166 private static String getTagName(String node) { 167 int n = node.indexOf('['); 168 if(n == -1) n = node.length(); 169 String tag = node.substring(0, n); 170 return tag; 172 } 173 private static String getParamName(String node) { 174 int n = node.indexOf('['); 175 if(n == -1) return null; 176 String p = node.substring(n+2); 177 int eq = p.indexOf("="); 178 String ret = p.substring(0, eq); 179 return ret; 181 } 182 private static String getParamValue(String node) { 183 int n = node.indexOf('['); 184 if(n == -1) return null; 185 String p = node.substring(n+2); 186 int eq = p.indexOf("="); 187 String ret = p.substring(eq+2, p.length()-2); 188 return ret; 190 } 191 void process() { 192 String [] n = XpathSupport.getListOfNodes(xpath); 194 for(int i = 0;i<n.length;i++) { 198 String node = n[i]; 199 MethodCaller mc = new MethodCaller(); 200 String tagName = XpathSupport.getTagName(node); 201 String methodName = "get" + convertName(tagName); 202 203 String paramName = XpathSupport.getParamName(node); 204 String paramValue = XpathSupport.getParamValue(node); 205 206 if(paramName!=null && !paramName.equals("")) { 207 methodName += "By" + convertName(paramName); 208 mc.setMethodName(methodName); 209 mc.setParamClasses(new Class [] {String .class}); 210 mc.setParams(new Object [] {paramValue}); 211 } else { 212 mc.setMethodName(methodName); 213 mc.setParamClasses(new Class [] {}); 214 mc.setParams(new Object [] {}); 215 } 216 arr.add(mc); 217 nameArr.add(convertName(tagName)); 218 } 219 } 220 } 221 private static class MethodCaller { 222 private String methodName; 223 private Class [] c; 224 private Object [] o; 225 226 public String toString() { 227 return "MethodCaller: " + methodName + ",o=" + o; 228 } 229 void setMethodName(String m) { 230 methodName = m; 231 } 232 String getMethodName(){ 233 return methodName; 234 } 235 void setParamClasses(Class [] cl) { 236 c = cl; 237 } 238 Class [] getParamClasses() { 239 return c; 240 } 241 void setParams(Object [] ob) { 242 o=ob; 243 } 244 Object [] getParams() { 245 return o; 246 } 247 } 248 259 private static String convertName(String name) { 260 CharacterIterator ci; 261 StringBuffer n = new StringBuffer (); 262 boolean up = true; 263 boolean keepCase = false; 264 char c; 265 266 if(name.equals("property")) { 268 name = "element-property"; 269 } 270 271 ci = new StringCharacterIterator (name); 272 c = ci.first(); 273 274 while (c != CharacterIterator.DONE) { 276 if (Character.isLowerCase(c)) { 277 keepCase = true; 278 break; 279 } 280 c = ci.next(); 281 } 282 283 c = ci.first(); 284 while (c != CharacterIterator.DONE) { 285 if (c == '-' || c == '_') 286 up = true; 287 else { 288 if (up) 289 c = Character.toUpperCase(c); 290 else 291 if (!keepCase) 292 c = Character.toLowerCase(c); 293 n.append(c); 294 up = false; 295 } 296 c = ci.next(); 297 } 298 return n.toString(); 299 } 300 } 301 | Popular Tags |