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