1 16 package org.jmanage.core.util; 17 18 import java.util.StringTokenizer ; 19 20 36 public class Expression { 37 38 public static final String WILDCARD = "*"; 39 public static final String DELIMITER = "/"; 40 41 private final String exprString; 42 private String appName; 43 private String mbeanName; 44 45 private String targetName; 46 47 public Expression(String appName, String mbeanName, String targetName){ 48 49 this.appName = appName!=null && appName.length()>0?appName:WILDCARD; 50 this.mbeanName = mbeanName!=null && mbeanName.length()>0?mbeanName:WILDCARD; 51 this.targetName = targetName!=null && targetName.length()>0?targetName:WILDCARD; 52 StringBuffer buff = new StringBuffer (this.appName); 53 buff.append(DELIMITER); 54 buff.append("\""); 55 buff.append(this.mbeanName); 56 buff.append("\""); 57 buff.append(DELIMITER); 58 buff.append(targetName); 59 this.exprString = buff.toString(); 60 } 61 62 public Expression(String exprString){ 63 this(exprString, (Expression)null); 64 } 65 66 public Expression(String exprString, Expression context){ 67 68 this.exprString = exprString; 69 StringTokenizer tokenizer = new CustomStringTokenizer(exprString); 70 if(context != null){ 71 this.appName = context.getAppName(); 72 this.mbeanName = context.getMBeanName(); 73 this.targetName = context.getTargetName(); 74 int tokenCount = 0; 75 String [] tokens = new String [3]; 77 for(int i=0;tokenizer.hasMoreTokens() && i<3;i++){ 78 tokenCount ++; 79 tokens[i] = tokenizer.nextToken(); 80 } 81 if(tokenizer.hasMoreTokens()){ 82 throw new RuntimeException ("invalid expression"); 83 } 84 switch(tokenCount){ 85 case 1: 86 targetName = tokens[0]; 87 break; 88 case 2: 89 mbeanName = tokens[0]; 90 targetName = tokens[1]; 91 break; 92 case 3: 93 appName = tokens[0]; 94 mbeanName = tokens[1]; 95 targetName = tokens[2]; 96 break; 97 } 98 }else{ 99 if(tokenizer.hasMoreTokens()) 100 appName = tokenizer.nextToken(); 101 if(tokenizer.hasMoreTokens()) 102 mbeanName = tokenizer.nextToken(); 103 if(tokenizer.hasMoreTokens()) 104 targetName = tokenizer.nextToken(); 105 } 106 107 } 108 109 public String getAppName() { 110 return appName; 111 } 112 113 public String getMBeanName() { 114 return mbeanName; 115 } 116 117 public String getTargetName() { 118 return targetName; 119 } 120 121 public String toString(){ 122 return exprString; 123 } 124 125 public String getHtmlEscaped(){ 126 return exprString.replaceAll("\"","""); 127 } 128 129 134 135 private class CustomStringTokenizer extends StringTokenizer { 136 137 public CustomStringTokenizer(String expr){ 138 super(expr, DELIMITER); 139 } 140 141 144 public String nextToken(){ 145 String token = super.nextToken(); 146 if(token.startsWith("\"")){ 147 if(token.endsWith("\"")){ 148 token = token.substring(1, token.length() -1); 150 }else{ 151 154 StringBuffer buff = new StringBuffer (token.substring(1)); 155 156 while(true){ 157 String nextToken = super.nextToken(); 158 buff.append(DELIMITER); 159 if(nextToken.endsWith("\"")){ 160 buff.append(nextToken.substring(0, nextToken.length()-1)); 161 break; 162 }else{ 163 buff.append(nextToken); 164 } 165 } 166 token = buff.toString(); 167 } 168 } 169 return token; 170 } 171 172 public int countTokens(){ 173 throw new RuntimeException ("countTokens() is not supported"); 174 } 175 } 176 } 177 | Popular Tags |