| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.phoenix.sqlparser; |
|---|
| 34 | |
|---|
| 35 | import java.util.StringTokenizer; |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * This class represents a special comment in the following form |
|---|
| 39 | * -- &cmpgen.java-type org.jcoderz.ipp.Msisdn |
|---|
| 40 | * -- &cmpgen.storeMethod="toString()" |
|---|
| 41 | * -- &cmpgen.loadMethod="fromString(java.lang.String)". |
|---|
| 42 | * |
|---|
| 43 | * @author Albrecht Messner |
|---|
| 44 | */ |
|---|
| 45 | public class SpecialColumnComment |
|---|
| 46 | { |
|---|
| 47 | private String mJavaType; |
|---|
| 48 | private String mStoreMethod; |
|---|
| 49 | private String mLoadMethod; |
|---|
| 50 | private String mCurrencyColumn; |
|---|
| 51 | private String mPeriodFieldName; |
|---|
| 52 | private String mPeriodEndDateColumn; |
|---|
| 53 | private String mWeblogicColumnType; |
|---|
| 54 | private boolean mSkipInInterface = false; |
|---|
| 55 | |
|---|
| 56 | /** {@inheritDoc} */ |
|---|
| 57 | public final String toString () |
|---|
| 58 | { |
|---|
| 59 | return "[SpecialColumnComment " |
|---|
| 60 | + "java-type=" + mJavaType |
|---|
| 61 | + ", store-method=" + mStoreMethod |
|---|
| 62 | + ", load-method=" + mLoadMethod |
|---|
| 63 | + "]"; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * Parse a special comment. |
|---|
| 68 | * @param t the token to parse |
|---|
| 69 | * @throws ParseException if the comment has a syntax error |
|---|
| 70 | */ |
|---|
| 71 | public final void parseComment (Token t) throws ParseException |
|---|
| 72 | { |
|---|
| 73 | final String s = t.getValue(); |
|---|
| 74 | if (s.indexOf("@cmpgen") != -1) |
|---|
| 75 | { |
|---|
| 76 | if (s.indexOf("java-type") != -1) |
|---|
| 77 | { |
|---|
| 78 | if (isSetJavaType()) |
|---|
| 79 | { |
|---|
| 80 | throw new ParseException( |
|---|
| 81 | "Duplicate special comment 'java-type'", -1, -1); |
|---|
| 82 | } |
|---|
| 83 | mJavaType = getValue(s); |
|---|
| 84 | } |
|---|
| 85 | else if (s.indexOf("store-method") != -1) |
|---|
| 86 | { |
|---|
| 87 | if (isSetStoreMethod()) |
|---|
| 88 | { |
|---|
| 89 | throw new ParseException( |
|---|
| 90 | "Duplicate special comment 'store-method'", -1, -1); |
|---|
| 91 | } |
|---|
| 92 | mStoreMethod = getValue(s); |
|---|
| 93 | } |
|---|
| 94 | else if (s.indexOf("load-method") != -1) |
|---|
| 95 | { |
|---|
| 96 | if (isSetLoadMethod()) |
|---|
| 97 | { |
|---|
| 98 | throw new ParseException( |
|---|
| 99 | "Duplicate special comment 'load-method'", -1, -1); |
|---|
| 100 | } |
|---|
| 101 | mLoadMethod = getValue(s); |
|---|
| 102 | } |
|---|
| 103 | else if (s.indexOf("currency-column") != -1) |
|---|
| 104 | { |
|---|
| 105 | if (isSetCurrencyColumn()) |
|---|
| 106 | { |
|---|
| 107 | throw new ParseException( |
|---|
| 108 | "Duplicate special comment 'currency-column'", -1, -1); |
|---|
| 109 | } |
|---|
| 110 | mCurrencyColumn = getValue(s); |
|---|
| 111 | } |
|---|
| 112 | else if (s.indexOf("period-field-name") != -1) |
|---|
| 113 | { |
|---|
| 114 | if (isSetPeriodFieldName()) |
|---|
| 115 | { |
|---|
| 116 | throw new ParseException( |
|---|
| 117 | "Duplicate special comment 'period-field-name'", -1, -1); |
|---|
| 118 | } |
|---|
| 119 | mPeriodFieldName = getValue(s); |
|---|
| 120 | } |
|---|
| 121 | else if (s.indexOf("period-end-date-column") != -1) |
|---|
| 122 | { |
|---|
| 123 | if (isSetPeriodEndDateColumn()) |
|---|
| 124 | { |
|---|
| 125 | throw new ParseException( |
|---|
| 126 | "Duplicate special comment 'period-end-date-column'", |
|---|
| 127 | -1, -1); |
|---|
| 128 | } |
|---|
| 129 | mPeriodEndDateColumn = getValue(s); |
|---|
| 130 | } |
|---|
| 131 | else if (s.indexOf("skip-in-interface") != -1) |
|---|
| 132 | { |
|---|
| 133 | mSkipInInterface = true; |
|---|
| 134 | } |
|---|
| 135 | else if (s.indexOf("weblogic-dbms-column-type") != -1) |
|---|
| 136 | { |
|---|
| 137 | if (isSetWeblogicColumnType()) |
|---|
| 138 | { |
|---|
| 139 | throw new ParseException( |
|---|
| 140 | "Duplicate special comment 'weblogic.dbms-column-type'", |
|---|
| 141 | -1, -1); |
|---|
| 142 | } |
|---|
| 143 | mWeblogicColumnType = getValue(s); |
|---|
| 144 | } |
|---|
| 145 | else |
|---|
| 146 | { |
|---|
| 147 | throw new ParseException("Invalid special comment", -1, -1); |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | private String getValue (String s) |
|---|
| 153 | { |
|---|
| 154 | final StringTokenizer tok = new StringTokenizer(s, "="); |
|---|
| 155 | tok.nextToken(); // just skip first token |
|---|
| 156 | String secondPart = tok.nextToken(); |
|---|
| 157 | secondPart = secondPart.trim(); |
|---|
| 158 | if (secondPart.startsWith("\"")) |
|---|
| 159 | { |
|---|
| 160 | secondPart = secondPart.substring(1); |
|---|
| 161 | } |
|---|
| 162 | if (secondPart.endsWith("\"")) |
|---|
| 163 | { |
|---|
| 164 | secondPart = secondPart.substring(0, secondPart.length() - 1); |
|---|
| 165 | } |
|---|
| 166 | return secondPart.trim(); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | * Returns the period field name. |
|---|
| 171 | * @return the period field name |
|---|
| 172 | */ |
|---|
| 173 | public final String getPeriodFieldName () |
|---|
| 174 | { |
|---|
| 175 | return mPeriodFieldName; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | /** |
|---|
| 179 | * Returns the end date column. |
|---|
| 180 | * @return the end date column |
|---|
| 181 | */ |
|---|
| 182 | public final String getPeriodEndDateColumn () |
|---|
| 183 | { |
|---|
| 184 | return mPeriodEndDateColumn; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | /** |
|---|
| 188 | * Returns the currency column. |
|---|
| 189 | * @return the currency column |
|---|
| 190 | */ |
|---|
| 191 | public final String getCurrencyColumn () |
|---|
| 192 | { |
|---|
| 193 | return mCurrencyColumn; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | /** |
|---|
| 197 | * Returns the java type. |
|---|
| 198 | * @return the java type |
|---|
| 199 | */ |
|---|
| 200 | public final String getJavaType () |
|---|
| 201 | { |
|---|
| 202 | return mJavaType; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | /** |
|---|
| 206 | * Returns the load method. |
|---|
| 207 | * @return the load method |
|---|
| 208 | */ |
|---|
| 209 | public final String getLoadMethod () |
|---|
| 210 | { |
|---|
| 211 | return mLoadMethod; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | /** |
|---|
| 215 | * Returns the store method. |
|---|
| 216 | * @return the store method |
|---|
| 217 | */ |
|---|
| 218 | public final String getStoreMethod () |
|---|
| 219 | { |
|---|
| 220 | return mStoreMethod; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | /** |
|---|
| 224 | * Check whether all required fields have been set. |
|---|
| 225 | * @throws ParseException if the comment is invalid |
|---|
| 226 | */ |
|---|
| 227 | public final void validate () throws ParseException |
|---|
| 228 | { |
|---|
| 229 | if (isSetLoadMethod() || isSetStoreMethod()) |
|---|
| 230 | { |
|---|
| 231 | // if one is set, both must be present |
|---|
| 232 | if (! (isSetLoadMethod() && isSetStoreMethod())) |
|---|
| 233 | { |
|---|
| 234 | throw new ParseException( |
|---|
| 235 | "'store-method' and 'load-method' must be " |
|---|
| 236 | + "both present or both absent", |
|---|
| 237 | -1, |
|---|
| 238 | -1); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | // if we have load and store methods, we also need the |
|---|
| 242 | // java type |
|---|
| 243 | if (! isSetJavaType()) |
|---|
| 244 | { |
|---|
| 245 | throw new ParseException( |
|---|
| 246 | "Invalid special comment, mandatory field missing", |
|---|
| 247 | -1, |
|---|
| 248 | -1); |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | if (isSetPeriodFieldName()) |
|---|
| 253 | { |
|---|
| 254 | // if one is set, both must be present |
|---|
| 255 | if (! (isSetPeriodEndDateColumn())) |
|---|
| 256 | { |
|---|
| 257 | throw new ParseException( |
|---|
| 258 | "'period-end-date-column' must be" |
|---|
| 259 | + " present when 'period-field-name' has been specified.", |
|---|
| 260 | -1, |
|---|
| 261 | -1); |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | public final boolean isSkipInInterface () |
|---|
| 267 | { |
|---|
| 268 | return mSkipInInterface; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | /** |
|---|
| 272 | * Check whether the field mJavaType has been set. |
|---|
| 273 | * @return true if the field is not-null, false otherwise |
|---|
| 274 | */ |
|---|
| 275 | public final boolean isSetJavaType () |
|---|
| 276 | { |
|---|
| 277 | return mJavaType != null; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | /** |
|---|
| 281 | * Check whether the field mStoreMethod has been set. |
|---|
| 282 | * @return true if the field is not-null, false otherwise |
|---|
| 283 | */ |
|---|
| 284 | public final boolean isSetStoreMethod () |
|---|
| 285 | { |
|---|
| 286 | return mStoreMethod != null; |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | /** |
|---|
| 290 | * Check whether the field mLoadMethod has been set. |
|---|
| 291 | * @return true if the field is not-null, false otherwise |
|---|
| 292 | */ |
|---|
| 293 | public final boolean isSetLoadMethod () |
|---|
| 294 | { |
|---|
| 295 | return mLoadMethod != null; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | /** |
|---|
| 299 | * Check whether the field mCurrencyColumn has been set. |
|---|
| 300 | * @return true if the field is not-null, false otherwise |
|---|
| 301 | */ |
|---|
| 302 | public final boolean isSetCurrencyColumn () |
|---|
| 303 | { |
|---|
| 304 | return mCurrencyColumn != null; |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | /** |
|---|
| 308 | * Check whether the field mPeriodFieldName has been set. |
|---|
| 309 | * @return true if the field is not-null, false otherwise |
|---|
| 310 | */ |
|---|
| 311 | public final boolean isSetPeriodFieldName () |
|---|
| 312 | { |
|---|
| 313 | return mPeriodFieldName != null; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | /** |
|---|
| 317 | * Check whether the field mPeriodEndDateColumn has been set. |
|---|
| 318 | * @return true if the field is not-null, false otherwise |
|---|
| 319 | */ |
|---|
| 320 | public final boolean isSetPeriodEndDateColumn () |
|---|
| 321 | { |
|---|
| 322 | return mPeriodEndDateColumn != null; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | public String getWeblogicColumnType () |
|---|
| 326 | { |
|---|
| 327 | return mWeblogicColumnType; |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | public boolean isSetWeblogicColumnType () |
|---|
| 331 | { |
|---|
| 332 | return mWeblogicColumnType != null; |
|---|
| 333 | } |
|---|
| 334 | } |
|---|