KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.utils.parser;
2
3 import java.util.ArrayList JavaDoc;
4 import com.daffodilwoods.database.utility.P;
5 import java.lang.reflect.Field JavaDoc;
6 import com.daffodilwoods.daffodildb.utils.DBStack;
7 import com.daffodilwoods.database.resource.*;
8
9 public class StringProductionRules extends ProductionRules{
10
11    public String JavaDoc XYZ = "XYZ";//used in writing toString() method for classes
12

13    char[] string;
14    String JavaDoc keyWord;
15
16    StringProductionRules(ClassLoader JavaDoc classLoader0){
17      super(classLoader0);
18    }
19
20    public void setString(String JavaDoc result1,String JavaDoc keyWord0){
21       this.result = result1.equalsIgnoreCase("'\\u0020'") ? " " : result1;
22       XYZ = (String JavaDoc)this.result;
23       this.keyWord = keyWord0;
24       ArrayList JavaDoc rules = new ArrayList JavaDoc();
25       String JavaDoc xyz = nameOfRule == null ? (String JavaDoc)this.result : nameOfRule;
26       if ( keyWord == null ){
27         rules.add(xyz);
28         this.keyWord = nameOfRule;
29       }
30       else
31         rules.add(keyWord);
32       super.setProductionRules(rules);
33       string = ((String JavaDoc)result).toCharArray();
34    }
35
36    Object JavaDoc parsePart(ParseElements pe) throws DException{
37       if(pe.position >= pe.query.length() || pe.recursiveObject != null){
38          pe.parseException.setPosition(pe.getIndexPosition(pe.position));
39          return pe.parseException;
40       }
41
42    Object JavaDoc ob = parseComments(pe);
43    if(ob instanceof ParseException )
44      return ob;
45    pe.position = ((Integer JavaDoc)ob).intValue() ;
46 // case ---- :: AIOB caught & exception returned beacuse ; not found */
47

48
49
50       int cmp = checkString(pe.queryArray,string,pe.position);
51        if ( cmp == 0 ) {
52           pe.position += ((String JavaDoc)result).length();
53           if ( pe.position <= pe.queryArray.length){
54             return getObjectOfClass(result);
55           }
56       }
57      cmp = cmp > 0 ? 1 : cmp < 0 ? -1 : 0;
58      pe.parseException.setReturnType(cmp);
59      pe.parseException.setPosition(pe.getIndexPosition(pe.position));
60      return pe.parseException;
61    }
62
63    public void setProductionRuleName(String JavaDoc nameOfRule1){
64       String JavaDoc name = nameOfRule1.trim();
65       StringBuffer JavaDoc name1 = new StringBuffer JavaDoc("S");
66       name1.append(name).append(name.hashCode());
67       super.setProductionRuleName(name1.toString());
68    }
69
70    private int checkString(char []source, char[] target,int position){
71      int compare = 0;
72      int i = position;
73      for(int j = 0; i < source.length && j < target.length && compare == 0; ++j,++i)
74           compare = source[i] - target[j];
75      return compare;
76    }
77
78    public Object JavaDoc getComparableObject(){
79      String JavaDoc result1 = (String JavaDoc)this.result;
80      if ( result1.equalsIgnoreCase(">=") || result1.equalsIgnoreCase("<=") ||
81           result1.equalsIgnoreCase("<>") || result1.equalsIgnoreCase("!=") ||
82           result1.equalsIgnoreCase("==") || result1.equalsIgnoreCase("&&") ||
83           result1.equalsIgnoreCase("||") )
84         return null;
85      return result1;
86    }
87
88    public Object JavaDoc getComparableObjectArray(){
89      return new String JavaDoc[]{nameOfRule};
90    }
91
92
93    public String JavaDoc toString(){
94       return new StringBuffer JavaDoc("[").append(nameOfRule).append("] SPR [").append(result).append("]").toString();
95    }
96
97    public Object JavaDoc getRecursiveObject(String JavaDoc nameOfRule,DBStack occuredRules){
98       if ( this.nameOfRule.equalsIgnoreCase(nameOfRule) )
99          return new Object JavaDoc[]{this};
100       return null;
101    }
102
103    public Object JavaDoc getNonRecursiveObject(String JavaDoc nameOfRule,DBStack occuredRules){
104       if ( !(this.nameOfRule.equalsIgnoreCase(nameOfRule)) )
105          return new Object JavaDoc[]{this};
106       return null;
107    }
108
109    public int hashCode(){
110       return result.hashCode();
111    }
112
113    Object JavaDoc getObjectOfClass(Object JavaDoc parsedObject) throws DException{
114       Object JavaDoc tempObject = null;
115       int i = 0;
116       try{
117          if ( classMaker == null ){
118            try{ /* here using cls variable for comments refer to getObjectOfClass method of SimpleProductionRules */
119              Class JavaDoc cls = classLoader.loadClass(className);
120
121                 loadFields(i,cls);
122                 classMaker = cls;
123             }catch(NoClassDefFoundError JavaDoc ne){
124                throw ne;
125             }
126          }
127          /* Done by Kaushik on 09/09/2004 as part of Parser Optimization */
128          tempObject = classMaker.newInstance();
129
130          ((Field JavaDoc)fields[0]).set(tempObject,parsedObject);
131       }catch(Exception JavaDoc E){
132             throw new DException("DSE0",new Object JavaDoc[]{E.getMessage() });
133
134       }
135       return tempObject;
136    }
137 }
138
Popular Tags