KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.utils.parser;
2
3 import java.util.*;
4 import com.daffodilwoods.database.resource.*;
5
6 public class Parser {
7
8    static ProductionRules productionRules;
9    static ProductionRules bveRule1;
10    static ProductionRules defaultOption;
11    static ProductionRules triggeredSQL;
12
13    public Parser() {
14    }
15
16    public static ProductionRules findProductionRule(String JavaDoc ruleToFind) throws DException{
17       initialisePR();
18       HashMap arr = new HashMap();
19       ProductionRules pre = findProductionRule(ruleToFind,productionRules,arr);
20       ProductionRules ttt = (ProductionRules) arr.get("999");
21       ttt.tokenRule = productionRules.tokenRule;
22       ttt.hashMap = productionRules.hashMap;
23       return ttt;
24    }
25
26    private static ProductionRules findProductionRule(String JavaDoc ruleToFind,ProductionRules pr, HashMap arr) {
27       String JavaDoc name1 = pr.nameOfRule;
28       ProductionRules tttt = (ProductionRules)arr.get(name1);
29       if (tttt != null) {
30          return tttt;
31       }
32       if (name1.equalsIgnoreCase(ruleToFind)) {
33          arr.put("999", pr);
34          return pr;
35       }
36       arr.put(name1,pr);
37       ProductionRules ttt = (ProductionRules) arr.get("999");
38       if (ttt != null) {
39          return ttt;
40       }
41       Object JavaDoc array[] = pr.rules;
42       array = array == null ? new ProductionRules[0] : array;
43       for (int i = 0 ; i < array.length ; i++) {
44          if (array[i] instanceof ProductionRules) {
45             ProductionRules tt = ( (ProductionRules) array[i]);
46             findProductionRule(ruleToFind,tt,arr);
47          }
48       }
49       return null;
50    }
51
52    public static void initialisePR() throws DException {
53       if (productionRules == null)
54          productionRules = getRules("SQL 99");
55    }
56
57    public static Object JavaDoc parseBatch(String JavaDoc query) throws DException {
58       if (productionRules == null)
59          productionRules = getRules("SQL 99"); //readFromFile("rules","SQL 99");
60
return productionRules.parseBatchUsingTokens(query);
61    }
62
63    public static Object JavaDoc parseBVE(String JavaDoc query) throws DException {
64       if (bveRule1 == null)
65         bveRule1 = findProductionRule("boolean value expression");//getRules("boolean value expression"); //readFromFile("rules","SQL 99");
66
return bveRule1.parseUsingTokens(query);
67    }
68
69
70    public static Object JavaDoc parseQuery(String JavaDoc query) throws DException {
71       if (productionRules == null)
72          productionRules = getRules("SQL 99"); //readFromFile("rules","SQL 99");
73
return productionRules.parseUsingTokens(query);
74    }
75
76
77
78    private static ProductionRules getRules(String JavaDoc ruleName) throws DException {
79       ProductionRuleTokenParser tokenParser = new ProductionRuleTokenParser(DaffodilClassLoader.getInstance());
80       ProductionRules productionRule = tokenParser.getProductionRule1(ruleName);
81       return productionRule;
82    }
83
84    /*private static ProductionRules readFromFile(String fileName,String ruleName) throws DException {
85      try{
86         URL url = (new Parser()).getClass().getResource("/"+fileName);
87         long tt1 = System.currentTimeMillis();
88         BufferedInputStream bufferedInputStream = new BufferedInputStream(url.openStream());
89         ObjectInputStream oin = new ObjectInputStream(bufferedInputStream);
90         ProductionRules productionRule1 = (ProductionRules)oin.readObject();
91         oin.close();
92         return productionRule1;
93      }catch(Exception e){
94         try{
95             return writeToFile(ruleName,fileName);
96         }catch(Exception e1){
97         }
98      }
99      return null;
100        }
101        private static ProductionRules writeToFile(String ruleName,String fileName) throws DException {
102     try{
103      ProductionRuleTokenParser tokenParser = new ProductionRuleTokenParser(DaffodilClassLoader.getInstance());
104      ProductionRules productionRule = tokenParser.getProductionRule1(ruleName);
105      URL url = (new Parser()).getClass().getResource("/");
106      String filePath = url.getPath()+fileName;
107      FileOutputStream fout = new FileOutputStream(filePath);
108      BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fout);
109      ObjectOutputStream oout = new ObjectOutputStream(bufferedOutputStream);
110      oout.writeObject(productionRule);
111      oout.close();
112      return productionRule;
113     }catch(Exception e){
114         e.printStackTrace();
115     }
116     return null;
117        }*/

118
119    public static Object JavaDoc parseDefaultOption(String JavaDoc query) throws DException {
120       if (defaultOption == null)
121          defaultOption = findProductionRule("default option");//getRules("default option"); //readFromFile("rules","SQL 99");
122
return defaultOption.parseUsingTokens(query);
123    }
124
125    public static Object JavaDoc parseTriggeredSQL(String JavaDoc query) throws DException {
126       if (triggeredSQL == null)
127          triggeredSQL = findProductionRule("triggered SQL statement");//getRules("triggered SQL statement"); //readFromFile("rules","SQL 99");
128
return triggeredSQL.parseUsingTokens(query);
129    }
130 }
131
Popular Tags