KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > util > PatternWrapper


1 package org.jacorb.notification.util;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import org.jacorb.util.ObjectUtil;
25
26 /**
27  * This is a Wrapper around a PatternMatcher.
28  */

29
30 public abstract class PatternWrapper
31 {
32     private static final RuntimeException JavaDoc REGEXP_NOT_AVAILABLE = new RuntimeException JavaDoc(
33             "No RegExp Implementation available. "
34                     + "The package java.util.regex is part of the JDK since v1.4 "
35                     + "if you are running an older JDK you'll have to install "
36                     + "gnu.regexp or jakarta.regexp to run this NotificationService. Please refer "
37                     + "to the documentation for details.");
38
39     private static Class JavaDoc sDefaultInstance = null;
40
41     static
42     {
43         if (isClassAvailable("java.util.regex.Pattern"))
44         {
45             try
46             {
47                 sDefaultInstance = ObjectUtil
48                         .classForName("org.jacorb.notification.util.JDK14PatternWrapper");
49             } catch (ClassNotFoundException JavaDoc e)
50             {
51                 // no problem
52
// recoverable error
53
}
54         }
55
56         if (sDefaultInstance == null && isClassAvailable("org.apache.regexp.RE"))
57         {
58             try
59             {
60                 sDefaultInstance = ObjectUtil
61                         .classForName("org.jacorb.notification.util.JakartaRegexpPatternWrapper");
62             } catch (ClassNotFoundException JavaDoc e)
63             {
64                 // no problem
65
// recoverable error
66
}
67         }
68
69         if (sDefaultInstance == null && isClassAvailable("gnu.regexp.RE"))
70         {
71             try
72             {
73                 sDefaultInstance = ObjectUtil
74                         .classForName("org.jacorb.notification.util.GNUPatternWrapper");
75             } catch (ClassNotFoundException JavaDoc e)
76             {
77                 // this time its non recoverable ...
78
throw new RuntimeException JavaDoc(e.getMessage());
79             }
80         }
81
82         if (sDefaultInstance == null)
83         {
84             throw REGEXP_NOT_AVAILABLE;
85         }
86     }
87
88     public static PatternWrapper init(String JavaDoc patternString)
89     {
90         try
91         {
92             PatternWrapper _wrapper;
93             _wrapper = (PatternWrapper) sDefaultInstance.newInstance();
94             _wrapper.compile(patternString);
95
96             return _wrapper;
97         } catch (Exception JavaDoc e)
98         {
99             if (sDefaultInstance == null)
100             {
101                 throw REGEXP_NOT_AVAILABLE;
102             }
103
104             throw new RuntimeException JavaDoc(e.getMessage());
105         }
106     }
107
108     public abstract void compile(String JavaDoc pattern);
109
110     /**
111      * Match the given input against this pattern.
112      *
113      * @param text
114      * the input to be matched
115      * @return the index of the last character matched, plus one or zero if the pattern did not
116      * match.
117      */

118
119     public abstract int match(String JavaDoc text);
120
121     private static boolean isClassAvailable(String JavaDoc name)
122     {
123         try
124         {
125             ObjectUtil.classForName(name);
126
127             return true;
128         } catch (ClassNotFoundException JavaDoc e)
129         {
130             // ignore. just return false
131
}
132
133         return false;
134     }
135 }
Popular Tags