KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > util > Expression


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.core.util;
17
18 import java.util.StringTokenizer JavaDoc;
19
20 /**
21  * Provides a mechanism to specify application name, mbean name and
22  * attribute or operation name as a single String expression. The expression
23  * looks like the following:
24  * <br/>
25  * &lt;appName&gt;/&lt;mbeaName&gt;/&lt;attrName&gt;
26  * <p>
27  * The mbeanName could be the "configured" mbean name or the object name.
28  * This depends on the context in which Expression object is used.
29  * <p>
30  * This object is widely used in the jManage application specifically in
31  * the command line UI and the access control system.
32  *
33  * Date: Feb 26, 2005
34  * @author Rakesh Kalra
35  */

36 public class Expression {
37
38     public static final String JavaDoc WILDCARD = "*";
39     public static final String JavaDoc DELIMITER = "/";
40
41     private final String JavaDoc exprString;
42     private String JavaDoc appName;
43     private String JavaDoc mbeanName;
44     /* this could be attribute or operation name */
45     private String JavaDoc targetName;
46
47     public Expression(String JavaDoc appName, String JavaDoc mbeanName, String JavaDoc 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 JavaDoc buff = new StringBuffer JavaDoc(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 JavaDoc exprString){
63         this(exprString, (Expression)null);
64     }
65
66     public Expression(String JavaDoc exprString, Expression context){
67
68         this.exprString = exprString;
69         StringTokenizer JavaDoc 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             // we only expect max 3 tokens
76
String JavaDoc[] tokens = new String JavaDoc[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 JavaDoc("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 JavaDoc getAppName() {
110         return appName;
111     }
112
113     public String JavaDoc getMBeanName() {
114         return mbeanName;
115     }
116
117     public String JavaDoc getTargetName() {
118         return targetName;
119     }
120
121     public String JavaDoc toString(){
122         return exprString;
123     }
124
125     public String JavaDoc getHtmlEscaped(){
126         return exprString.replaceAll("\"","&quot;");
127     }
128
129     /**
130      * Handles the case where the delimiter "/" is within the expression.
131      * Note that this tokenizer doesn't return the right value for
132      * countTokens()
133      */

134
135     private class CustomStringTokenizer extends StringTokenizer JavaDoc{
136
137         public CustomStringTokenizer(String JavaDoc expr){
138             super(expr, DELIMITER);
139         }
140
141         /**
142          * Handles "/" within the expression.
143          */

144         public String JavaDoc nextToken(){
145             String JavaDoc token = super.nextToken();
146             if(token.startsWith("\"")){
147                 if(token.endsWith("\"")){
148                     // token ends with double quotes. just drop the double quotes
149
token = token.substring(1, token.length() -1);
150                 }else{
151                     /* token starts with double quotes, but doesn't end with it.
152                         Keep getting next token,
153                         till we find ending double quotes */

154                     StringBuffer JavaDoc buff = new StringBuffer JavaDoc(token.substring(1));
155
156                     while(true){
157                         String JavaDoc 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 JavaDoc("countTokens() is not supported");
174         }
175     }
176 }
177
Popular Tags