KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > util > RegexUtil


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.util;
21
22 import java.util.regex.Matcher JavaDoc;
23 import java.util.regex.Pattern JavaDoc;
24
25 /**
26  * A collection of utility methods related to regular expressions processing.
27  *
28  * @since 1.2
29  * @author Andrus Adamchik
30  */

31 class RegexUtil {
32
33     static final Pattern JavaDoc BACKSLASH = Pattern.compile("\\\\");
34     static final Pattern JavaDoc DOT = Pattern.compile("\\.");
35
36     /**
37      * Replaces all backslashes "\" with forward slashes "/". Convenience method to
38      * convert path Strings to URI format.
39      */

40     static String JavaDoc substBackslashes(String JavaDoc string) {
41         if (string == null) {
42             return null;
43         }
44
45         Matcher JavaDoc matcher = BACKSLASH.matcher(string);
46         return matcher.find() ? matcher.replaceAll("\\/") : string;
47     }
48
49     /**
50      * Returns package name for the Java class as a path separated with forward slash
51      * ("/"). Method is used to lookup resources that are located in package
52      * subdirectories. For example, a String "a/b/c" will be returned for class name
53      * "a.b.c.ClassName".
54      */

55     static String JavaDoc getPackagePath(String JavaDoc className) {
56         if (className == null) {
57             return "";
58         }
59
60         Matcher JavaDoc matcher = DOT.matcher(className);
61         if (matcher.find()) {
62             String JavaDoc path = matcher.replaceAll("\\/");
63             return path.substring(0, path.lastIndexOf("/"));
64         }
65         else {
66             return "";
67         }
68     }
69
70     /**
71      * Converts a SQL-style pattern to a valid Perl regular expression. E.g.:
72      * <p>
73      * <code>"billing_%"</code> will become <code>^billing_.*$</code>
74      * <p>
75      * <code>"user?"</code> will become <code>^user.?$</code>
76      */

77     static String JavaDoc sqlPatternToRegex(String JavaDoc pattern) {
78         if (pattern == null) {
79             throw new NullPointerException JavaDoc("Null pattern.");
80         }
81
82         if (pattern.length() == 0) {
83             throw new IllegalArgumentException JavaDoc("Empty pattern.");
84         }
85
86         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
87
88         // convert * into regex syntax
89
// e.g. abc*x becomes ^abc.*x$
90
// or abc?x becomes ^abc.?x$
91
buffer.append("^");
92         for (int j = 0; j < pattern.length(); j++) {
93             char nextChar = pattern.charAt(j);
94             if (nextChar == '%') {
95                 nextChar = '*';
96             }
97
98             if (nextChar == '*' || nextChar == '?') {
99                 buffer.append('.');
100             }
101             // escape special chars
102
else if (nextChar == '.'
103                     || nextChar == '/'
104                     || nextChar == '$'
105                     || nextChar == '^') {
106                 buffer.append('\\');
107             }
108
109             buffer.append(nextChar);
110         }
111
112         buffer.append("$");
113         return buffer.toString();
114     }
115
116     private RegexUtil() {
117         super();
118     }
119 }
120
Popular Tags