KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > common > CombinedPatternMatcher


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.common;
25
26 //Admin imports
27
import com.sun.enterprise.admin.util.IPatternMatcher;
28 import com.sun.enterprise.admin.util.RegExpMatcher;
29 import com.sun.enterprise.admin.util.GeneralPatternMatcher;
30 import com.sun.enterprise.admin.util.Logger;
31
32 // i18n import
33
import com.sun.enterprise.admin.util.SOMLocalStringsManager;
34
35 public class CombinedPatternMatcher implements IPatternMatcher
36 {
37     private String JavaDoc mPattern = null;
38     private String JavaDoc mTestString = null;
39     private IPatternMatcher mRealMatcher = null;
40
41     // i18n SOMLocalStringsManager
42
private static SOMLocalStringsManager localizedStrMgr =
43         SOMLocalStringsManager.getManager( CombinedPatternMatcher.class );
44
45     /**
46         Creates new CombinedPatternMatcher with pattern string and
47         string on which the patten has to be matched. None of the
48         strings may be null.
49         
50         @param patternString the Pattern string.
51         @param testString the string on which pattern should be applied.
52     */

53     
54     public CombinedPatternMatcher(String JavaDoc patternString, String JavaDoc testString)
55     {
56         if (patternString == null || testString == null)
57         {
58             String JavaDoc msg = localizedStrMgr.getString( "admin.common.combinedpattermatcher_null_arg" );
59             throw new IllegalArgumentException JavaDoc( msg );
60         }
61         mPattern = patternString;
62         mTestString = testString;
63         if (isJDK14())
64         {
65 // Logger.log("using the jdk 1.4 regular expression facility");
66
mPattern = translateFromJMXToJDK14(patternString);
67             mRealMatcher = new RegExpMatcher(mPattern, testString);
68         }
69         else
70         {
71 // Logger.log("using pattern matcher without jdk 1.4");
72
mRealMatcher = new GeneralPatternMatcher(patternString, testString);
73         }
74     }
75
76     public boolean matches()
77     {
78         return ( mRealMatcher.matches() );
79     }
80     
81     public boolean isJDK14()
82     {
83         String JavaDoc javaSpecVersion = System.getProperty("java.specification.version");
84         
85         return ( javaSpecVersion.startsWith("1.4") );
86     }
87     
88     private String JavaDoc translateFromJMXToJDK14(String JavaDoc aString)
89     {
90         String JavaDoc dotEscpapedString = escapeDots(aString);
91         String JavaDoc starReplacedString = insertDotBeforeStar(dotEscpapedString);
92         String JavaDoc qmReplacedString = insertDotBeforeQM(starReplacedString);
93         return ( qmReplacedString );
94     }
95     
96     private String JavaDoc escapeDots(String JavaDoc aString)
97     {
98         char escape = Tokens.kEscapeChar;
99         char dot = Tokens.kDelimiterChar;
100         return ( insertCharBefore(aString, escape, dot) );
101     }
102     private String JavaDoc insertDotBeforeStar(String JavaDoc aString)
103     {
104         char dot = Tokens.kDelimiterChar;
105         char star = Tokens.kWildCardChar;
106         
107         return ( insertCharBefore(aString, dot, star) );
108     }
109     private String JavaDoc insertDotBeforeQM(String JavaDoc aString)
110     {
111         char dot = Tokens.kDelimiterChar;
112         char qm = ObjectNames.kSingleMatchChar;
113         
114         return ( insertCharBefore(aString, dot, qm) );
115     }
116     private String JavaDoc insertCharBefore(String JavaDoc aString, char insChar, char beforeChar)
117     {
118         StringBuffer JavaDoc destBuffer = new StringBuffer JavaDoc();
119         char[] srcArray = aString.toCharArray();
120         for (int i = 0 ; i < srcArray.length ; i++)
121         {
122             char ch = srcArray[i];
123             if (ch == beforeChar)
124             {
125                 destBuffer.append(insChar);
126             }
127             destBuffer.append(ch);
128         }
129         return ( destBuffer.toString() );
130     }
131 }
132
Popular Tags