View Javadoc

1   /*
2    * Copyright 2002-2009 the original author or authors.
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  
17  package net.sf.json.regexp;
18  
19  /**
20   * Convenience utility for working withRegexpMatcher.<br>
21   * 
22   * @author Andres Almiray <aalmiray@users.sourceforge.net>
23   */
24  public class RegexpUtils {
25     private static String javaVersion = "1.3.1";
26     static{
27        javaVersion = System.getProperty( "java.version" );
28     }
29  
30     /**
31      * Returns a RegexpMatcher that works in a specific environment.<br>
32      * When in a JVM 1.3.1 it will return a Perl5RegexpMatcher, if the JVM is
33      * younger (1.4+) it will return a JdkRegexpMatcher.
34      */
35     public static RegexpMatcher getMatcher( String pattern ) {
36        if( isJDK13() ){
37           return new Perl5RegexpMatcher( pattern );
38        }else{
39           return new JdkRegexpMatcher( pattern );
40        }
41     }
42  
43     /**
44      * Returns a RegexpMatcher that works in a specific environment.<br>
45      * When in a JVM 1.3.1 it will return a Perl5RegexpMatcher, if the JVM is
46      * younger (1.4+) it will return a JdkRegexpMatcher.
47      */
48     public static RegexpMatcher getMatcher( String pattern, boolean multiline ) {
49        if( isJDK13() ){
50           return new Perl5RegexpMatcher( pattern, true );
51        }else{
52           return new JdkRegexpMatcher( pattern, true );
53        }
54     }
55     
56     /**
57      * Queries the environment for the supported JDK version.
58      */
59     public static boolean isJDK13() {
60        return javaVersion.indexOf( "1.3" ) != -1;
61     }
62  
63     private RegexpUtils() {
64  
65     }
66  }