1 23 24 package com.sun.enterprise.config.util; 25 26 29 33 34 38 public final class ConfigXPathHelper { 39 public static final String XPATH_SEPARATOR = "/"; 40 41 47 public static String getAbsoluteIdXpathExpression(String childTagName, String nameId, String valueId) { 48 if(childTagName.startsWith(XPATH_SEPARATOR)) 49 return childTagName + "[@" + nameId + "='" + valueId + "']"; 50 else 51 return XPATH_SEPARATOR + childTagName + "[@" + nameId + "='" + valueId + "']"; 52 } 53 54 private static char SEPARATOR_CHAR = '/'; 55 private static char OPENBRACKET_CHAR = '['; 56 private static char CLOSEBRACKET_CHAR = ']'; 57 private static char ESCAPE_CHAR = '\\'; 58 59 62 public static String getLastNodeName(String xPath) { 63 66 int idx = xPath.length()-1; 67 char ch; 68 int idxEnd = -1; 69 if(idx>=0 && (ch=xPath.charAt(idx))==CLOSEBRACKET_CHAR) { 70 idxEnd = bypassBrackets(xPath, --idx); 71 idx = idxEnd; 72 } 73 74 while(idx>=0 && ((ch=xPath.charAt(idx))!=SEPARATOR_CHAR || isEscapedChar(xPath,idx))) { 75 idx--; 76 } 77 idx++; 78 if(idxEnd<=0 || idxEnd==xPath.length()-1) 79 return xPath.substring(idx); 80 else 81 return xPath.substring(idx, idxEnd+1); 82 } 83 84 88 public static String getParentXPath(String xPath) { 89 92 int idx = xPath.length()-1; 93 char ch; 94 if(idx>=0 && (ch=xPath.charAt(idx))==CLOSEBRACKET_CHAR) { 95 idx = bypassBrackets(xPath, --idx); 96 } 97 98 while(idx>=0 && ((ch=xPath.charAt(idx))!=SEPARATOR_CHAR || isEscapedChar(xPath,idx))) { 99 idx--; 100 } 101 if(idx<=0) 102 return "/"; 103 return xPath.substring(0, idx); 104 } 105 106 private static int bypassBrackets(String xPath, int idx) { 107 char ch; 108 while(idx>=0 && ((ch=xPath.charAt(idx))!=OPENBRACKET_CHAR || isEscapedChar(xPath,idx))) { 109 idx--; 110 } 111 return idx-1; 112 } 113 private static boolean isEscapedChar(String xPath, int idx) { 114 if(idx<=0) 115 return false; 116 int count = 0; 117 while(--idx>=0 && xPath.charAt(idx)==ESCAPE_CHAR) 118 count++; 119 return ((count%2)==1); 120 } 121 } 122 | Popular Tags |