KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > exp > parser > PatternMatchNode


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.exp.parser;
21
22 import java.util.regex.Pattern JavaDoc;
23
24 import org.apache.cayenne.util.Util;
25
26 /**
27  * Superclass of pattern matching nodes. Assumes that subclass is a binary expression with
28  * the second operand being a pattern.
29  *
30  * @since 1.1
31  * @author Andrus Adamchik
32  */

33 public abstract class PatternMatchNode extends ConditionNode {
34
35     protected Pattern JavaDoc pattern;
36     protected boolean patternCompiled;
37     protected boolean ignoringCase;
38
39     PatternMatchNode(int i, boolean ignoringCase) {
40         super(i);
41         this.ignoringCase = ignoringCase;
42     }
43
44     protected boolean matchPattern(String JavaDoc string) {
45         return (string != null) ? getPattern().matcher(string).find() : false;
46     }
47
48     protected Pattern JavaDoc getPattern() {
49         // compile pattern on demand
50
if (!patternCompiled) {
51
52             synchronized (this) {
53
54                 if (!patternCompiled) {
55                     pattern = null;
56
57                     if (jjtGetNumChildren() < 2) {
58                         patternCompiled = true;
59                         return null;
60                     }
61
62                     // precompile pattern
63
ASTScalar patternNode = (ASTScalar) jjtGetChild(1);
64                     if (patternNode == null) {
65                         patternCompiled = true;
66                         return null;
67                     }
68
69                     String JavaDoc srcPattern = (String JavaDoc) patternNode.getValue();
70                     if (srcPattern == null) {
71                         patternCompiled = true;
72                         return null;
73                     }
74
75                     pattern = Util.sqlPatternToPattern(srcPattern, ignoringCase);
76                     patternCompiled = true;
77                 }
78             }
79         }
80
81         return pattern;
82     }
83
84     public void jjtAddChild(Node n, int i) {
85         // reset pattern if the node is modified
86
if (i == 1) {
87             patternCompiled = false;
88         }
89
90         super.jjtAddChild(n, i);
91     }
92 }
93
Popular Tags