| 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.io.BufferedReader; |
|---|
| 36 | import java.io.IOException; |
|---|
| 37 | import java.io.StringReader; |
|---|
| 38 | import java.util.StringTokenizer; |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * @author Albrecht Messner |
|---|
| 42 | */ |
|---|
| 43 | public final class SpecialStatementComment |
|---|
| 44 | { |
|---|
| 45 | public static final int TYPE_BEAN_NAME = 1; |
|---|
| 46 | public static final int TYPE_JAVADOC = 2; |
|---|
| 47 | public static final int TYPE_OPTIMISTIC_VERSION_COUNT = 3; |
|---|
| 48 | public static final int TYPE_SKIP_APPSERVER_SUPPORT = 4; |
|---|
| 49 | |
|---|
| 50 | private final String mContent; |
|---|
| 51 | private final Token mToken; |
|---|
| 52 | private final int mType; |
|---|
| 53 | |
|---|
| 54 | private SpecialStatementComment (String content, Token token, int type) |
|---|
| 55 | { |
|---|
| 56 | mContent = content; |
|---|
| 57 | mToken = token; |
|---|
| 58 | mType = type; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | /** {@inheritDoc} */ |
|---|
| 62 | public String toString () |
|---|
| 63 | { |
|---|
| 64 | return "[SpecialStatementComment: type = " |
|---|
| 65 | + mType + ", content = " + mContent + "]"; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | public static SpecialStatementComment parseComment (Token t) |
|---|
| 69 | throws ParseException |
|---|
| 70 | { |
|---|
| 71 | final String content; |
|---|
| 72 | final int type; |
|---|
| 73 | final SpecialStatementComment result; |
|---|
| 74 | final String s = t.getValue(); |
|---|
| 75 | if (s.indexOf("@cmpgen") == -1) |
|---|
| 76 | { |
|---|
| 77 | // not a special comment |
|---|
| 78 | result = null; |
|---|
| 79 | } |
|---|
| 80 | else |
|---|
| 81 | { |
|---|
| 82 | if (s.indexOf("bean-name") != -1) |
|---|
| 83 | { |
|---|
| 84 | content = getValue(s); |
|---|
| 85 | type = TYPE_BEAN_NAME; |
|---|
| 86 | result = new SpecialStatementComment(content, t, type); |
|---|
| 87 | } |
|---|
| 88 | else if (s.indexOf("optimistic-version-count") != -1) |
|---|
| 89 | { |
|---|
| 90 | content = ""; |
|---|
| 91 | type = TYPE_OPTIMISTIC_VERSION_COUNT; |
|---|
| 92 | result = new SpecialStatementComment(content, t, type); |
|---|
| 93 | } |
|---|
| 94 | else if (s.indexOf("skip-appserver-support") != -1) |
|---|
| 95 | { |
|---|
| 96 | content = ""; |
|---|
| 97 | type = TYPE_SKIP_APPSERVER_SUPPORT; |
|---|
| 98 | result = new SpecialStatementComment(content, t, type); |
|---|
| 99 | } |
|---|
| 100 | else if (s.indexOf("javadoc") != -1) |
|---|
| 101 | { |
|---|
| 102 | final StringReader sr = new StringReader(s); |
|---|
| 103 | final BufferedReader br = new BufferedReader(sr); |
|---|
| 104 | |
|---|
| 105 | final StringBuffer sbuf = new StringBuffer(); |
|---|
| 106 | try |
|---|
| 107 | { |
|---|
| 108 | String line; |
|---|
| 109 | while ((line = br.readLine()) != null) |
|---|
| 110 | { |
|---|
| 111 | if (line.indexOf("/*") == -1 |
|---|
| 112 | && line.indexOf("*/") == -1) |
|---|
| 113 | { |
|---|
| 114 | if (sbuf.length() > 0) |
|---|
| 115 | { |
|---|
| 116 | sbuf.append(System.getProperty("line.separator")); |
|---|
| 117 | } |
|---|
| 118 | sbuf.append(line); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | catch (IOException e) |
|---|
| 123 | { |
|---|
| 124 | // unexpected, we are reading from a string reader! |
|---|
| 125 | throw new RuntimeException(e); |
|---|
| 126 | } |
|---|
| 127 | content = sbuf.toString(); |
|---|
| 128 | type = TYPE_JAVADOC; |
|---|
| 129 | result = new SpecialStatementComment(content, t, type); |
|---|
| 130 | } |
|---|
| 131 | else |
|---|
| 132 | { |
|---|
| 133 | throw new ParseException("Invalid special comment " + s, -1, -1); |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | return result; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | private static String getValue (String s) |
|---|
| 140 | { |
|---|
| 141 | final StringTokenizer tok = new StringTokenizer(s, "="); |
|---|
| 142 | tok.nextToken(); // just skip first token |
|---|
| 143 | String secondPart = tok.nextToken(); |
|---|
| 144 | secondPart = secondPart.trim(); |
|---|
| 145 | if (secondPart.startsWith("\"")) |
|---|
| 146 | { |
|---|
| 147 | secondPart = secondPart.substring(1); |
|---|
| 148 | } |
|---|
| 149 | if (secondPart.endsWith("\"")) |
|---|
| 150 | { |
|---|
| 151 | secondPart = secondPart.substring(0, secondPart.length() - 1); |
|---|
| 152 | } |
|---|
| 153 | return secondPart.trim(); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | /** |
|---|
| 158 | * @return Returns the content of this special comment. |
|---|
| 159 | */ |
|---|
| 160 | public String getContent () |
|---|
| 161 | { |
|---|
| 162 | return mContent; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | public int getType () |
|---|
| 166 | { |
|---|
| 167 | return mType; |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | |
|---|