KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > sql > macros > AbstractMacro


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2006 Continuent, Inc.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet.
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.controller.sql.macros;
23
24 import java.util.regex.Pattern JavaDoc;
25
26 import org.continuent.sequoia.common.xml.XmlComponent;
27
28 /**
29  * This class defines an AbstractMacro for all macro definitions.
30  *
31  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
32  * @version 1.0
33  */

34 public abstract class AbstractMacro implements XmlComponent
35 {
36   private Pattern JavaDoc macroPattern;
37   protected String JavaDoc macroName;
38
39   /**
40    * Creates a new <code>AbstractMacro</code> object for a macro name.
41    * Automatically generates the corresponding macro pattern
42    *
43    * @param macroName the name of the macro
44    */

45   public AbstractMacro(String JavaDoc macroName)
46   {
47     this.macroName = macroName;
48     macroPattern = generatePatternForMacroName(macroName);
49   }
50
51   /**
52    * Generate the macro value to replace in the original SQL.
53    *
54    * @param currentTimeInMs current time in milliseconds
55    * @return the value of the macro
56    */

57   public abstract String JavaDoc generateMacroValue(long currentTimeInMs);
58
59   /**
60    * Returns the macro name.
61    *
62    * @return Returns the macro name.
63    */

64   public final String JavaDoc getMacroName()
65   {
66     return macroName;
67   }
68
69   /**
70    * Returns the macro pattern matcher.
71    *
72    * @return Returns the macro pattern.
73    */

74   public final Pattern JavaDoc getMacroPattern()
75   {
76     return macroPattern;
77   }
78
79   /**
80    * Generate a pattern matcher for a given macro name (performs the escaping of
81    * the parenthesis).
82    *
83    * @param macroName the macro name
84    * @return a pattern macher
85    */

86   private Pattern JavaDoc generatePatternForMacroName(String JavaDoc macroName)
87   {
88     // Add backslash and optional space characters before any parenthesis in the
89
// macro expression
90
int parenthesisIdx = macroName.indexOf('(');
91     while (parenthesisIdx != -1)
92     {
93       if (parenthesisIdx == 0)
94         macroName = "\\" + macroName;
95       else
96         macroName = macroName.substring(0, parenthesisIdx) + "\\s*\\"
97             + macroName.substring(parenthesisIdx) + "\\s*";
98       parenthesisIdx = macroName.indexOf('(', parenthesisIdx + 8);
99     }
100     parenthesisIdx = macroName.indexOf(')');
101     while (parenthesisIdx != -1)
102     {
103       if (parenthesisIdx == 0)
104         macroName = "\\" + macroName;
105       else
106         macroName = macroName.substring(0, parenthesisIdx) + "\\s*\\"
107             + macroName.substring(parenthesisIdx) + "\\s*";
108       parenthesisIdx = macroName.indexOf(')', parenthesisIdx + 8);
109     }
110
111     // Return the pattern
112
return Pattern.compile("\\b" + macroName + "\\s*", Pattern.CASE_INSENSITIVE
113         | Pattern.DOTALL);
114   }
115
116   /**
117    * @see java.lang.Object#equals(java.lang.Object)
118    */

119   public boolean equals(Object JavaDoc obj)
120   {
121     if (obj instanceof AbstractMacro)
122     {
123       AbstractMacro otherMacro = (AbstractMacro) obj;
124       return macroName.equals(otherMacro.getMacroName());
125     }
126     return false;
127   }
128
129   /**
130    * @see java.lang.Object#hashCode()
131    */

132   public int hashCode()
133   {
134     return macroName.hashCode();
135   }
136
137 }
138
Popular Tags