KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > utils > parser > KeyWordProductionRules


1 package com.daffodilwoods.daffodildb.utils.parser;
2
3 import java.lang.reflect.Field JavaDoc;
4 import com.daffodilwoods.database.utility.P;
5 import com.daffodilwoods.database.resource.*;
6
7 /**
8  * Class for KeyWord ProductionRules.
9  */

10
11 public class KeyWordProductionRules
12     extends StringProductionRules {
13
14   private static char[] reserveWord = new char[] {
15       '+', '-', '*', '/', '=', '<', '>', '!', 13};
16
17   public KeyWordProductionRules(ClassLoader JavaDoc classLoader0) {
18     super(classLoader0);
19   }
20
21   /**
22    * Parse Part Method Of KeyWordProductionRules.
23    * KeyWord is recognised with the help of delimeters specified in ProductionRules.
24    */

25
26   Object JavaDoc parsePart(ParseElements pe) throws DException {
27      if (pe.position >= pe.query.length() || pe.recursiveObject != null) {
28        pe.parseException.setPosition(pe.getIndexPosition(pe.position));
29        return pe.parseException;
30      }
31      while (Character.isWhitespace(pe.queryArray[pe.position])) {
32        pe.position++;
33      }
34     Object JavaDoc ob = parseComments(pe);
35     if(ob instanceof ParseException )
36       return ob;
37     int prevPosition = pe.position = ((Integer JavaDoc)ob).intValue() ;
38
39 // case ---- :: AIOB caught & exception returned beacuse ; not found */
40

41      int cmp1[] = checkString(pe.queryArray, string, pe.position);
42      int cmp = cmp1[0];
43      boolean delimeter = false;
44      if (cmp == 0) {
45        pe.position += ((String JavaDoc)result).length();
46        if (pe.position <= pe.queryArray.length - 1)
47          delimeter = checkForDelimeters(pe);
48
49        if (delimeter) {
50          return getObjectOfClass(new String JavaDoc(pe.queryArray, prevPosition,pe.position- prevPosition));
51        }
52        if (pe.position == pe.queryArray.length) {
53          String JavaDoc saq = new String JavaDoc(pe.queryArray, prevPosition,
54                          pe.position - prevPosition);
55          return getObjectOfClass(saq);
56        }
57      }
58      cmp = cmp > 0 ? 1 : cmp < 0 ? -1 : 0;
59      if (!delimeter && (cmp == 0)) {
60        pe.position -= ( ( (String JavaDoc) result).length() + cmp1[1]);
61        cmp = 2;
62      }
63      pe.parseException.setReturnType(cmp);
64      pe.parseException.setPosition(pe.getIndexPosition(pe.position));
65      return pe.parseException;
66    }
67
68
69   /**
70    * If source is in small letter then we convert it into capital letter
71    * and then compare the result with query.
72    * Returns result as >0, <0, or =0
73    */

74   private int[] checkString(char[] source, char[] target, int position) { //,boolean flag,boolean flag1){
75
int compare = 0, i = position;
76     for (int j = 0; i < source.length && j < target.length && compare == 0; ++j,
77          ++i) {
78
79 /* done to parse "\n" in a keyword
80        if(source[i] == 10){
81             dddd++;
82             j--;
83             continue;
84          } */

85
86       if (source[i] >= 97 && source[i] < 123)
87         compare = (source[i] - 32) - target[j];
88       else
89         compare = source[i] - target[j];
90     }
91
92
93     return new int[] {
94     compare,0};
95   }
96
97   /**
98    * Checks for delimeters with respect to query for identifying keywords.
99    */

100   private boolean checkForDelimeters(ParseElements pe) {
101     for (int i = 0; i < delimitedTokens.length; ++i) {
102       if (pe.queryArray[pe.position] == delimitedTokens[i])
103         return true;
104     }
105     for (int i = 0; i < reserveWord.length; ++i) {
106       if (pe.queryArray[pe.position] == reserveWord[i])
107         return true;
108     }
109     return false;
110   }
111
112   public String JavaDoc toString() {
113     return nameOfRule;
114   }
115
116   public int hashCode() {
117     return keyWord.hashCode();
118   }
119
120   Object JavaDoc getObjectOfClass(Object JavaDoc parsedObject) throws DException {
121     Object JavaDoc tempObject = null;
122     int i = 0;
123     try {
124       if (classMaker == null) {
125         try {/* here using cls variable for comments refer to getObjectOfClass method of SimpleProductionRules */
126        Class JavaDoc cls = classLoader.loadClass(className);
127        loadFields(i,cls);
128        classMaker = cls;
129        }
130         catch (NoClassDefFoundError JavaDoc ne) {
131           throw ne;
132         }
133       }
134       /* Done by Kaushik on 09/09/2004 as part of Parser Optimization */
135       tempObject = classMaker.newInstance();
136       ( (Field JavaDoc) fields[0]).set(tempObject, parsedObject);
137     }
138     catch (Exception JavaDoc E) {
139         throw new DException("DSE0",new Object JavaDoc[]{E.getMessage() });
140
141     }
142     return tempObject;
143   }
144
145   /* private String getString(char value[], int offset, int count) {
146         StringBuffer valuea = new StringBuffer(count);
147         for (int i = 0,j = offset,k=count-1; i < count; j++) {
148               valuea.append(value[j]);
149               i++;
150         }
151         String aa = new String (valuea);
152         return aa;
153      }*/

154 }
155
Popular Tags