1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import java.util.ArrayList ; 28 import java.util.StringTokenizer ; 29 import java.text.CharacterIterator ; 30 import java.text.StringCharacterIterator ; 31 32 import com.sun.enterprise.util.LocalStringManagerImpl; 33 34 import com.sun.enterprise.config.ConfigContext; 36 import com.sun.enterprise.config.ConfigException; 37 import com.sun.enterprise.config.ConfigBean; 38 import com.sun.enterprise.config.serverbeans.*; 39 40 import javax.management.Attribute ; 42 import javax.management.AttributeList ; 43 44 74 75 public abstract class XPathHelper { 76 77 81 public static int getNumberOfElemTokens(String xpath) 82 { 83 int[] cuts = getElemDelimitersPositions(xpath); 84 int iTokens = cuts.length; 85 if(iTokens>0 && cuts[0]==0) 86 iTokens--; 87 if(cuts.length>0 && cuts[cuts.length-1]<xpath.length()-1) 88 iTokens++; 89 return iTokens; 90 } 91 92 public static String getXPathPrefix(String xpath, int numberOfTokensInPrefix) 93 { 94 if(numberOfTokensInPrefix<=0) 95 return ""; 96 int[] cuts = getElemDelimitersPositions(xpath); 97 int delimIdx = numberOfTokensInPrefix; 98 if(cuts.length>0 && cuts[0]!=0) 100 delimIdx--; 101 if(delimIdx<cuts.length) 102 return xpath.substring(0, cuts[delimIdx]); 103 if(delimIdx==cuts.length && cuts[cuts.length-1]<xpath.length()-1) 104 return xpath; 105 return ""; 106 } 107 108 public static String [] extractTokens(String xpath) 109 { 110 ArrayList arr = new ArrayList (); 111 int[] cuts = getElemDelimitersPositions(xpath); 112 int iTokenPos = 0; 113 for(int i=0; i<cuts.length; i++) 114 { 115 if(iTokenPos==cuts[i]) { 117 arr.add(""); 118 } 119 else 120 { 121 arr.add(xpath.substring(iTokenPos, cuts[i])); 122 } 123 iTokenPos = cuts[i] +1; 124 } 125 if(iTokenPos<xpath.length()-1) 126 { 127 arr.add(xpath.substring(iTokenPos)); 128 } 129 return (String [])arr.toArray(new String [arr.size()]); 130 } 131 132 public static int[] getElemDelimitersPositions(String xpath) 133 { 134 return getElemDelimitersPositions(xpath, -1); 135 } 136 137 public static int[] getElemDelimitersPositions(String xpath, int maxDeep) 138 { 139 int[] arr = new int[100]; 140 int length = 0; 141 char[] buf = xpath.toCharArray(); 142 int i=0; 143 int iNext; 144 while(maxDeep!=0 && (iNext=getNextElemDelimiterPos(buf, i))>=0) 145 { 146 arr[length++] = iNext; 148 maxDeep--; 149 i = iNext+1; 150 } 151 int[] ret_arr = new int[length]; 152 for(i=0; i<length; i++) 153 ret_arr[i] = arr[i]; 154 return ret_arr; 155 } 156 157 public static boolean isAbsoluteXPath(String xpath) 158 { 159 return (xpath!=null && xpath.startsWith("/")); 160 } 161 162 private static int getNextElemDelimiterPos(char[] buf, int iFrom) 163 { 164 int i = iFrom; 165 while(i>=0 && i<buf.length) 166 { 167 if(buf[i]=='/') 168 return i; 169 if(buf[i]=='\'' || buf[i]=='"') 170 { 171 i = indexOf(buf, i+1, buf[i]); if(i>0) 173 i++; 174 continue; 175 } 176 i++; 177 } 178 return -1; 179 } 180 181 private static int indexOf(char[] buf, int iFrom, char chr) 182 { 183 for(int i=iFrom; i<buf.length; i++) 184 { 185 if(buf[i]==chr) 186 return i; 187 } 188 return -1; 189 } 190 191 192 203 public static String convertName(String name) { 204 CharacterIterator ci; 205 StringBuffer n = new StringBuffer (); 206 boolean up = true; 207 boolean keepCase = false; 208 char c; 209 210 if(name.equals("property")) { 212 name = "element-property"; 213 } 214 215 ci = new StringCharacterIterator (name); 216 c = ci.first(); 217 218 while (c != CharacterIterator.DONE) { 220 if (Character.isLowerCase(c)) { 221 keepCase = true; 222 break; 223 } 224 c = ci.next(); 225 } 226 227 c = ci.first(); 228 while (c != CharacterIterator.DONE) { 229 if (c == '-' || c == '_') 230 up = true; 231 else { 232 if (up) 233 c = Character.toUpperCase(c); 234 else 235 if (!keepCase) 236 c = Character.toLowerCase(c); 237 n.append(c); 238 up = false; 239 } 240 c = ci.next(); 241 } 242 return n.toString(); 243 } 244 245 static AttributeList resolve(ConfigContext ctx, ArrayList xpathes, String onlyPrefix) 246 { 247 AttributeList merge_list = null; 248 if(xpathes!=null) 249 { 250 merge_list = new AttributeList (); 251 for(int i=0; i<xpathes.size(); i++) 252 { 253 AttributeList list = resolve(ctx, (String )xpathes.get(i), onlyPrefix); 254 if(list!=null) 255 merge_list.addAll(list); 256 } 257 } 258 return merge_list; 259 } 260 261 static AttributeList resolve(ConfigContext ctx, String xpath, String prefixFilter) 262 { 263 if(xpath==null) 264 return null; 265 String [] elems = extractTokens(xpath); 266 if(elems.length==0 || elems[0].length()!=0) 267 return null; 268 ConfigBean cb = null; 269 try { 270 cb = ctx.getRootConfigBean(); 271 } catch (ConfigException ce){ 272 } 273 AttributeList list = new AttributeList (); 274 navigate(cb, elems, 1, prefixFilter, list); 275 return list; 276 277 } 278 279 private static void addAttributeToList(String attrName, ConfigBean cb, AttributeList list, String xpath) 280 { 281 String attrValue = getBeanAttributeValue(cb, attrName); 282 if(attrValue!=null) 283 { 284 list.add(new Attribute (xpath+"/@"+attrName, attrValue)); 286 } 287 } 288 289 private static ConfigBean getNextBean(ConfigBean cb, String name) 290 { 291 try{ 292 if("..".equals(name)) 293 return (ConfigBean)cb.parent(); 294 return (ConfigBean)cb.getValue(convertName(name)); 295 } catch(Throwable t){ 296 return null; 297 } 298 } 299 300 private static ConfigBean[] getNextBeans(ConfigBean cb, String name) 301 { 302 try{ 303 if("..".equals(name)) 304 { 305 ConfigBean parent = (ConfigBean)cb.parent(); 306 if(parent==null) 307 return null; 308 ConfigBean grand = (ConfigBean)parent.parent(); 309 if(grand==null) 310 return new ConfigBean[]{parent}; 311 return (ConfigBean[])grand.getValue(parent.name()); 312 } 313 return (ConfigBean[])cb.getValues(convertName(name)); 314 } catch(Throwable t){ 315 return null; 316 } 317 } 318 319 public static String getBeanAttributeValue(ConfigBean cb, String name) 320 { 321 try{ 322 return cb.getAttributeValue(convertName(name)); 323 } catch(Throwable t){ 324 return null; 325 } 326 } 327 328 private static void navigate(ConfigBean cb, String [] tokens, int iCurrentToken, String prefixFilter, AttributeList list) 329 { 330 if(cb==null || tokens==null || tokens.length<=iCurrentToken) 331 return; 332 String xpath = cb.getXPath(); 333 if(xpath==null) 334 return; 335 336 if(prefixFilter!=null && prefixFilter.length()>0) 338 { 339 if(xpath.length()>=prefixFilter.length()) 340 { 341 if(!xpath.startsWith(prefixFilter)) 342 return; 343 } 344 else 345 { 346 if(!prefixFilter.startsWith(xpath)) 347 return; 348 } 349 } 350 351 if(iCurrentToken==tokens.length-1) 355 { 356 list.add(new Attribute (xpath, cb)); 357 return; 358 } 359 360 int iNextToken = iCurrentToken+1; 362 String nextToken = tokens[iNextToken]; 363 if(iNextToken==(tokens.length-1) && nextToken.startsWith("@")) 366 { 367 if("@*".equals(nextToken)) { 370 String [] names = cb.getAttributeNames(); 371 if(names.length>0) 372 for(int i=0; i<names.length; i++) 373 addAttributeToList(names[i], cb, list, xpath); 374 } 375 else addAttributeToList(nextToken.substring(1), cb, list, xpath); 377 return; 380 } 381 if(nextToken.length()==0 || 384 "*".equals(nextToken)) 385 { 386 ConfigBean[] children = cb.getAllChildBeans(); 387 if(children!=null) 388 for(int i=0; i<children.length; i++) 389 { 390 if(nextToken.length()==0) 391 navigate(children[i], tokens, iCurrentToken , prefixFilter, list); 392 393 navigate(children[i], tokens, iNextToken , prefixFilter, list); 394 } 395 return; 396 } 397 398 if(!nextToken.endsWith("]")) 401 { 402 ConfigBean cbNext = getNextBean(cb, nextToken); 403 if(cbNext!=null) 404 navigate((ConfigBean)cbNext, tokens, iNextToken , prefixFilter, list); 405 return; 406 } 407 408 int indexStartPos = nextToken.indexOf("["); 410 String indexStr = nextToken.substring(indexStartPos+1, nextToken.length()-1); 411 String baseName = nextToken.substring(0, indexStartPos); 412 413 if("*".equals(indexStr)) { 417 ConfigBean[] beans = getNextBeans(cb, baseName); 418 if(beans!=null) 419 { 420 for(int i=0; i<beans.length; i++) 421 { 422 navigate(beans[i], tokens, iNextToken , prefixFilter, list); 423 } 424 } 425 return; 426 } 427 428 if(indexStr.startsWith("@") && indexStr.startsWith("'")) { 432 String attrName = indexStr.substring(1, indexStr.indexOf('=')); 433 String attrValue = indexStr.substring(indexStr.indexOf("='")+2, indexStr.length()-2); 434 ConfigBean[] beans = getNextBeans(cb, baseName); 435 if(beans!=null) 436 { 437 for(int i=0; i<beans.length; i++) 438 { 439 String value = getBeanAttributeValue(beans[i], convertName(attrName)); 440 if(value==null) 441 continue; 442 if(attrValue.equals("*") || attrValue.equals(value)) 443 navigate(beans[i], tokens, iNextToken , prefixFilter, list); 444 } 445 } 446 return; 447 } 448 } 450 451 454 470 } | Popular Tags |