1 21 22 package org.continuent.sequoia.controller.sql.macros; 23 24 import java.util.regex.Pattern ; 25 26 import org.continuent.sequoia.common.xml.XmlComponent; 27 28 34 public abstract class AbstractMacro implements XmlComponent 35 { 36 private Pattern macroPattern; 37 protected String macroName; 38 39 45 public AbstractMacro(String macroName) 46 { 47 this.macroName = macroName; 48 macroPattern = generatePatternForMacroName(macroName); 49 } 50 51 57 public abstract String generateMacroValue(long currentTimeInMs); 58 59 64 public final String getMacroName() 65 { 66 return macroName; 67 } 68 69 74 public final Pattern getMacroPattern() 75 { 76 return macroPattern; 77 } 78 79 86 private Pattern generatePatternForMacroName(String macroName) 87 { 88 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 Pattern.compile("\\b" + macroName + "\\s*", Pattern.CASE_INSENSITIVE 113 | Pattern.DOTALL); 114 } 115 116 119 public boolean equals(Object 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 132 public int hashCode() 133 { 134 return macroName.hashCode(); 135 } 136 137 } 138 | Popular Tags |