KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > misc > RegexUtil


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 /*
25  * $Header: /cvs/glassfish/admin-core/mbeanapi/src/java/com/sun/appserv/management/util/misc/RegexUtil.java,v 1.3 2005/12/25 03:51:51 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:51:51 $
28  */

29  
30 package com.sun.appserv.management.util.misc;
31
32 import java.util.regex.Pattern JavaDoc;
33
34 /**
35     Useful utilities for regex handling
36  */

37 public final class RegexUtil
38 {
39         private
40     RegexUtil()
41     {
42         // disallow instantiation
43
}
44     
45     
46     private final static char BACKSLASH = '\\';
47     
48     /**
49         These characters will be escaped by wildcardToJavaRegex()
50      */

51     public static final String JavaDoc REGEX_SPECIALS = BACKSLASH + "[]^$?+{}()|-!";
52     
53     
54     
55     /**
56         Converts each String to a Pattern using wildcardToJavaRegex
57         
58         @param exprs String[] of expressions
59         @return Pattern[], one for each String
60      */

61         public static Pattern JavaDoc[]
62     exprsToPatterns( final String JavaDoc[] exprs )
63     {
64         return( exprsToPatterns( exprs, 0 ) );
65     }
66     
67     /**
68         Converts each String to a Pattern using wildcardToJavaRegex, passing the flags.
69         
70         @param exprs String[] of expressions
71         @param flags flags to pass to Pattern.compile
72         @return Pattern[], one for each String
73      */

74         public static Pattern JavaDoc[]
75     exprsToPatterns( final String JavaDoc[] exprs, int flags )
76     {
77         final Pattern JavaDoc[] patterns = new Pattern JavaDoc[ exprs.length ];
78         
79         for( int i = 0; i < exprs.length; ++i )
80         {
81             patterns[ i ] = Pattern.compile( wildcardToJavaRegex( exprs[ i ]), flags );
82         }
83         return( patterns );
84     }
85     
86     /**
87         Supports the single wildcard "*". There is no support for searching for
88         a literal "*".
89         
90         Convert a string to a form suitable for passing to java.util.regex.
91      */

92         public static String JavaDoc
93     wildcardToJavaRegex( String JavaDoc input )
94     {
95         String JavaDoc converted = input;
96         
97         if ( input != null )
98         {
99             final int length = input.length();
100             final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
101             
102             for( int i = 0; i < length; ++i )
103             {
104                 final char theChar = input.charAt( i );
105                 
106                 if ( theChar == '.' )
107                 {
108                     buf.append( "[.]" );
109                 }
110                 else if ( theChar == '*' )
111                 {
112                     buf.append( ".*" );
113                 }
114                 else if ( REGEX_SPECIALS.indexOf( theChar ) >= 0 )
115                 {
116                     // '[' begins a set of characters
117
buf.append( "" + BACKSLASH + theChar );
118                 }
119                 else
120                 {
121                     buf.append( theChar );
122                 }
123             }
124             
125             converted = buf.toString();
126             
127         }
128         return( converted );
129     }
130 }
131
132
Popular Tags