KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > helper > JDK15Platform


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.helper;
23 import java.util.regex.Pattern JavaDoc;
24 import java.util.Hashtable JavaDoc;
25
26
27 /**
28  * INTERNAL:
29  * Implements operations specific to JDK 1.5
30  */

31 public class JDK15Platform implements JDKPlatform {
32
33     /**
34      * INTERNAL
35      * Get the Map to store the query cache in
36      */

37     public java.util.Map JavaDoc getQueryCacheMap() {
38         return new java.util.concurrent.ConcurrentHashMap JavaDoc();
39     }
40     
41     /**
42      * PERF: The regular expression compiled Pattern objects are cached
43      * to avoid recompilation on every usage.
44      */

45     protected static Hashtable JavaDoc patternCache = new Hashtable JavaDoc();
46
47     /**
48      * INTERNAL:
49      * An implementation of in memory queries with Like which uses the JDK 1.4
50      * regular expression framework.
51      */

52     public int conformLike(Object JavaDoc left, Object JavaDoc right) {
53         if ((left == null) && (right == null)) {
54             return JavaPlatform.TRUE;
55         } else if ((left == null) || (right == null)) {
56             return JavaPlatform.FALSE;
57         }
58         if (left instanceof String JavaDoc && right instanceof String JavaDoc) {
59             // PERF: First check the pattern cache for the pattern.
60
// Note that the original string is the key, to avoid having to translate it first.
61
Pattern JavaDoc pattern = (Pattern JavaDoc)patternCache.get(right);
62             if (pattern == null) {
63                 // Bug 3936427 - Replace regular expression reserved characters with escaped version of those characters
64
// For instance replace ? with \?
65
String JavaDoc convertedRight = ((String JavaDoc)right).replaceAll("\\?", "\\\\?");
66                 convertedRight = convertedRight.replaceAll("\\*", "\\\\*");
67                 convertedRight = convertedRight.replaceAll("\\.", "\\\\.");
68                 convertedRight = convertedRight.replaceAll("\\[", "\\\\[");
69                 convertedRight = convertedRight.replaceAll("\\)", "\\\\)");
70                 convertedRight = convertedRight.replaceAll("\\(", "\\\\(");
71                 convertedRight = convertedRight.replaceAll("\\{", "\\\\{");
72                 convertedRight = convertedRight.replaceAll("\\+", "\\\\+");
73                 convertedRight = convertedRight.replaceAll("\\^", "\\\\^");
74                 convertedRight = convertedRight.replaceAll("\\|", "\\\\|");
75
76                 // regular expressions to substitute SQL wildcards with regex wildcards
77
// replace "%" which is not preceded by "\" with ".*"
78
convertedRight = convertedRight.replaceAll("([^\\\\])%", "$1.*");
79
80                 // replace "_" which is not preceded by "\" with ".
81
convertedRight = convertedRight.replaceAll("([^\\\\])_", "$1.");
82
83                 // deal with wildcards at the beginning of a line.
84
convertedRight = convertedRight.replaceAll("^%", ".*");
85                 convertedRight = convertedRight.replaceAll("^_", ".");
86
87                 // replace "\%" with "%"
88
convertedRight = convertedRight.replaceAll("\\\\%", "%");
89
90                 // replace "\_" with "_"
91
convertedRight = convertedRight.replaceAll("\\\\_", "_");
92
93                 pattern = Pattern.compile(convertedRight);
94                 // Ensure cache does not grow beyond 100.
95
if (patternCache.size() > 100) {
96                     patternCache.remove(patternCache.keySet().iterator().next());
97                 }
98                 patternCache.put(right, pattern);
99             }
100             boolean match = pattern.matcher((String JavaDoc)left).matches();
101             if (match) {
102                 return JavaPlatform.TRUE;
103             } else {
104                 return JavaPlatform.FALSE;
105             }
106         }
107         return JavaPlatform.UNDEFINED;
108     }
109
110     /**
111      * INTERNAL:
112      * Get the milliseconds from a Calendar. In JDK 1.4, this can be accessed directly from the calendar.
113      */

114     public long getTimeInMillis(java.util.Calendar JavaDoc calendar) {
115         return calendar.getTimeInMillis();
116     }
117
118     /**
119      * INTERNAL:
120      * Set the milliseconds for a Calendar. In JDK 1.4, this can be set directly in the calendar.
121      */

122     public void setTimeInMillis(java.util.Calendar JavaDoc calendar, long millis) {
123         calendar.setTimeInMillis(millis);
124     }
125
126     /**
127      * INTERNAL:
128      * Use API first available in JDK 1.4 to set the cause of an exception.
129      */

130     public void setExceptionCause(Throwable JavaDoc exception, Throwable JavaDoc cause) {
131         if (exception.getCause() == null) {
132             exception.initCause(cause);
133         }
134     }
135
136     /**
137      * INTERNAL
138      * return a boolean which determines where TopLink should include the TopLink-stored
139      * Internal exception in it's stack trace. For JDK 1.4 VMs with exception chaining
140      * the Internal exception can be redundant and confusing.
141      * @return boolean will return false since JDK 1.4 does supports exception chaining
142      */

143     public boolean shouldPrintInternalException() {
144         return false;
145     }
146 }
147
Popular Tags