1 package com.daffodilwoods.daffodildb.utils.parser; 2 3 import java.util.ArrayList ; 4 import java.util.Arrays ; 5 import com.daffodilwoods.database.utility.P; 6 import java.lang.reflect.Array ; 7 import java.lang.reflect.Field ; 8 import com.daffodilwoods.daffodildb.utils.DBStack; 9 import com.daffodilwoods.database.resource.*; 10 11 public class UnionProductionRulesWithHashMap extends ProductionRulesWithHashMap implements ProductionRuleParserConstants{ 12 13 UnionProductionRulesWithHashMap(ClassLoader classLoader0){ 14 super(classLoader0); 15 } 16 17 public String toString(){ 18 return ruleKey; 19 } 20 21 public Object getComparableObject(){ 22 int size = rules == null ? -1 : rules.length-1; 23 StringBuffer result = new StringBuffer (""); 24 for(int i = size ; i >= 0 ; i--){ 25 ProductionRules object = (ProductionRules)rules[i]; 26 if (object.getComparableObject()!=null) 27 result.append((String )object.getComparableObject()); 28 else 29 break; 30 } 31 if ( result.toString().equals("") ) 32 return null; 33 this.result = result.toString(); 34 return this.result; 35 } 36 37 38 public Object getComparableObjectArray(){ 39 int size = rules == null ? -1 : rules.length-1; 40 ArrayList res = new ArrayList (); 41 for(int i = size ; i >= 0 ; i--){ 42 ProductionRules object = (ProductionRules)rules[i]; 43 Object [] obj = (Object [])object.getComparableObjectArray(); 44 if ( obj != null) 45 res.addAll(Arrays.asList(obj)); 46 else 47 break; 48 } 49 if ( res.isEmpty() ) 50 return null; 51 String arr[] = (String [])res.toArray(new String [0]); 52 this.result = arr; 53 return arr; 54 } 55 56 public Object getRecursiveObject(String nameOfRule,DBStack occuredRules){ 57 if ( rules == null ) 58 return null; 59 ProductionRules production = (ProductionRules)rules[rules.length-1]; 60 if ( production.nameOfRule.equalsIgnoreCase(nameOfRule) ) 61 return new Object []{this}; 62 return null; 63 } 64 65 66 public Object getNonRecursiveObject(String nameOfRule,DBStack occuredRules){ 67 if ( rules == null ) 68 return null; 69 ProductionRules production = (ProductionRules)rules[rules.length-1]; 70 if ( ! (production.nameOfRule.equalsIgnoreCase(nameOfRule)) ) 71 return new Object []{this}; 72 return null; 73 } 74 75 79 Object parsePart(ParseElements pe)throws com.daffodilwoods.database.resource.DException{ 80 int rulesSize = rules.length; 81 Object [] parsedRule = new Object [rulesSize]; 82 int cmp = 1; 83 if(forTesting(pe)) return pe.parseException ; 85 for(int i = rulesSize - 1 ; i >= 0 ; i--){ 86 ProductionRules object = (ProductionRules)rules[i]; 87 String ruleName = object.getProductionRuleName(); 88 if (i == rulesSize - 1 && object.recursiveflag && ruleName.equals(pe.recursionState)){ 89 if(pe.recursiveObject != null ){ 90 parsedRule[i] = pe.recursiveObject; 91 pe.recursiveObject = null; 92 pe.recursionState = null; 93 continue; 94 } 95 else 96 return pe.parseException; 97 } 98 else if(pe.recursiveObject != null && !ruleName.equalsIgnoreCase(pe.recursionState)) 99 return pe.parseException; 100 Object value = object.parse(pe); 101 if(!(value instanceof ParseException)){ 102 parsedRule[i] = value; 103 cmp = 2; 104 } 105 else{ 106 if ( !pe.tokenFlag && ( cmp == 1 || cmp == 2 ) && (object instanceof OrProductionRules || object instanceof OrProductionRulesWithHashMap) ) 107 pe.parseException.setReturnType(2); 108 return pe.parseException; 109 } 110 } 111 ArrayList arr = new ArrayList (Arrays.asList(parsedRule)); 112 boolean ff = checkArrayList(arr); 113 return ff ? getObjectOfClass(arr.toArray()) : arr.get(0) ; 114 } 115 116 private boolean checkArrayList(ArrayList arr){ 117 int size = updatedRules.length - 1; 118 boolean ff = true; 119 for (int i = size; i >= 0; i--) { 120 if(updatedRules[i] == null){ 121 Object o = arr.remove(i); 122 ff = !ff; 123 } 124 } 125 if(repUnion){ 126 Object o1 = arr.remove(1); Object o0 = arr.remove(0); if(o0 == null){ 129 Class tempClass = o1.getClass(); 130 Object array = Array.newInstance(tempClass,1); 131 Array.set(array,0,o1); 132 arr.add(array); 133 }else{ 134 Class tempClass = o0.getClass(); 135 tempClass = tempClass.getComponentType(); 136 int length = Array.getLength(o0); 137 Object array = Array.newInstance(tempClass,length+1); 138 Array.set(array,0,o1); 139 for (int i = 0,j = 1; i < length; i++,j++) { 140 Array.set(array,j,Array.get(o0,i)); 141 } 142 arr.add(array); 143 } 144 return true; 145 } 146 return ff; 147 } 148 149 Object getObjectOfClass(Object [] parsedObject) throws DException{ 150 Object tempObject = null; 151 int i = parsedObject.length - 1; 152 try{ 153 if ( classMaker == null ){ 154 try{ 155 Class cls = classLoader.loadClass(className); 156 loadFields(i,cls); 157 classMaker = cls; 158 }catch(Exception ne){ 159 P.pln(ruleKey+" "+ P.print(rules) +" "+ P.print(updatedRules) ); 160 throw ne; 161 } 162 } 163 164 tempObject = classMaker.newInstance(); 165 166 for(; i >= 0 ; i--){ 167 ((Field )fields[i]).set(tempObject,parsedObject[i]); 168 } 169 }catch(Exception E){ 170 throw new DException("DSE0",new Object []{E.getMessage() }); 171 172 } 173 return tempObject; 174 } 175 boolean forTesting(ParseElements pe) throws DException{ 176 if(nameOfRule.equalsIgnoreCase("routine invocation") ){ 177 if(pe.position < pe.tokens.length ){ 178 String currentToken = pe.tokens[pe.position].toString(); 179 return vectorForBestAndNonBest.checkForSystemDefienedFunctions( 180 currentToken); 181 } 182 } 183 return false; 184 } 185 186 } 187 188 189 190 191 192 | Popular Tags |