01: /*
02: * $Id: PatternUtils.java,v 1.4 2007/09/18 08:45:07 agoubard Exp $
03: *
04: * Copyright 2003-2007 Orange Nederland Breedband B.V.
05: * See the COPYRIGHT file for redistribution and use restrictions.
06: */
07: package org.xins.common.text;
08:
09: import org.apache.oro.text.regex.MalformedPatternException;
10: import org.apache.oro.text.regex.Pattern;
11: import org.apache.oro.text.regex.Perl5Compiler;
12: import org.xins.common.MandatoryArgumentChecker;
13: import org.xins.common.ProgrammingException;
14: import org.xins.common.Utils;
15:
16: /**
17: * Regular expressions related utility functions.
18: *
19: * @version $Revision: 1.4 $ $Date: 2007/09/18 08:45:07 $
20: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
21: *
22: * @since XINS 2.0
23: */
24: public final class PatternUtils {
25:
26: /**
27: * Perl 5 pattern compiler.
28: */
29: private static final Perl5Compiler PATTERN_COMPILER = new Perl5Compiler();
30:
31: /**
32: * Constructs a new <code>PatternUtils</code> object.
33: */
34: private PatternUtils() {
35: // empty
36: }
37:
38: /**
39: * Compiles the given regular expression to a Perl5 pattern object.
40: *
41: * @param regexp
42: * the String value of the Perl5 regular expresssion, cannot be <code>null</code>.
43: *
44: * @return
45: * the Perl5 pattern, never <code>null</code>
46: *
47: * @throws IllegalArgumentException
48: * if <code>regexp == null</code>.
49: *
50: * @throws ProgrammingException
51: * if the pattern cannot be complied.
52: *
53: * @since XINS 2.0.
54: */
55: public static Pattern createPattern(String regexp)
56: throws IllegalArgumentException, ProgrammingException {
57: MandatoryArgumentChecker.check("regexp", regexp);
58: try {
59: Pattern pattern = PATTERN_COMPILER.compile(regexp,
60: Perl5Compiler.READ_ONLY_MASK
61: | Perl5Compiler.CASE_INSENSITIVE_MASK);
62: return pattern;
63: } catch (MalformedPatternException exception) {
64: throw Utils.logProgrammingError(exception);
65: }
66: }
67: }
|