1 package com.daffodilwoods.daffodildb.utils.parser; 2 3 import java.util.ArrayList ; 4 import java.util.Comparator ; 5 import java.util.HashMap ; 6 import java.util.Arrays ; 7 import java.util.List ; 8 import com.daffodilwoods.database.utility.P; 9 import com.daffodilwoods.daffodildb.utils.DBStack; 10 import com.daffodilwoods.database.resource.DException; 11 import java.util.*; 12 13 23 24 public class ProductionRuleTokenParser extends ProductionRuleParser implements ProductionRuleParserConstants{ 25 26 29 HashMap tokenRules ; 30 31 34 List comparableToNonComparable; 35 36 public ProductionRuleTokenParser(java.net.URL urlPath,ClassLoader classLoader0) throws DException{ 37 super(urlPath,classLoader0); 38 } 39 40 public ProductionRuleTokenParser(ClassLoader classLoader0) throws DException{ 41 super(classLoader0); 42 } 43 44 48 49 private void getProductionRuleSupportingTokens(ProductionRules productionRules){ 50 if(productionRules instanceof StringProductionRules) 51 return; 52 String name = productionRules.ruleKey; 53 if ( name == null ){ 54 productionRules.ruleKey = productionRules.nameOfRule; 55 name = productionRules.ruleKey; 56 } 57 if ( recursion.contains(name) ){ 58 if (!(productionRules instanceof OrProductionRules || productionRules instanceof OrProductionRulesWithHashMap) ) 59 assiningTokens(productionRules.rules); 60 return; 61 } 62 if ( productionRules instanceof OrProductionRules ){ 63 recursion.add(name); 64 OrProductionRules orRule = (OrProductionRules)productionRules; 65 callRecursively(orRule.recursiveObjects); 66 callRecursively(orRule.nonRecursiveObjects); 67 callRecursively(orRule.comparableRules); 68 getComparableObjects((OrProductionRules)productionRules); 69 }else if ( productionRules instanceof OrProductionRulesWithHashMap ){ 70 recursion.add(name); 71 OrProductionRulesWithHashMap orRule = (OrProductionRulesWithHashMap)productionRules; 72 callRecursively(orRule.recursiveObjects); 73 callRecursively(orRule.nonRecursiveObjects); 74 callRecursively(orRule.comparableRules); 75 getComparableObjects((OrProductionRulesWithHashMap)productionRules); 76 } 77 else{ 78 recursion.add(name); 79 callRecursively(productionRules.rules); 80 } 81 } 82 86 private void assiningTokens(Object [] listOfRules){ 87 if ( listOfRules == null ) 88 return; 89 for(int i = 0; i < listOfRules.length; ++i){ 90 ProductionRules productionRule = (ProductionRules)listOfRules[i]; 91 if ( tokenRules.containsKey(productionRule.nameOfRule) ){ 92 String nameOfClass = productionRule.initailizeNameOfClass(productionRule.nameOfRule); 93 String name = nameOfClass.substring(nameOfClass.lastIndexOf(".")+1); 94 TokenProductionRule rule = null; if ( productionRule.nameOfRule.equalsIgnoreCase(REGULARIDENTIFIER) ){ 96 rule = new TokenProductionRule(nameOfClass,name,name.toCharArray(),classLoader,3); 97 }else if ( productionRule.nameOfRule.equalsIgnoreCase(DELIMITEDIDENTIFIER) ){ 98 rule = new TokenProductionRule(nameOfClass,name,name.toCharArray(),classLoader,4); 99 }else{ 100 rule = new TokenProductionRule(nameOfClass,name,name.toCharArray(),classLoader,0); 101 } 102 if ( productionRule.result instanceof String [] ) 103 rule.result = productionRule.nameOfRule; 104 else 105 rule.result = new String []{(String )productionRule.nameOfRule}; 106 listOfRules[i] = rule; 107 } 108 } 109 } 110 111 115 private void callRecursively(Object []listOfRules){ 116 if ( listOfRules == null ) 117 return; 118 for(int i = listOfRules.length - 1 ; i >= 0 ; --i){ 119 ProductionRules productionRule = (ProductionRules)listOfRules[i]; 120 if ( tokenRules.containsKey(productionRule.nameOfRule) ){ 121 String nameOfClass = productionRule.initailizeNameOfClass(productionRule.nameOfRule); 122 String name = nameOfClass.substring(nameOfClass.lastIndexOf(".")+1); 123 TokenProductionRule rule = null; if ( productionRule.nameOfRule.equalsIgnoreCase(REGULARIDENTIFIER) ){ 125 rule = new TokenProductionRule(nameOfClass,name,name.toCharArray(),classLoader,3); 126 } else if ( productionRule.nameOfRule.equalsIgnoreCase(DELIMITEDIDENTIFIER)){ 127 rule = new TokenProductionRule(nameOfClass,name,name.toCharArray(),classLoader,4); 128 }else{ 129 rule = new TokenProductionRule(nameOfClass,name,name.toCharArray(),classLoader,0); 130 } 131 if ( productionRule.result instanceof String [] ) 132 rule.result = productionRule.nameOfRule; 133 else 134 rule.result = new String []{(String )productionRule.nameOfRule}; 135 listOfRules[i] = rule; 136 } 137 else 138 getProductionRuleSupportingTokens(productionRule); 139 } 140 } 141 142 146 public void getRecursiveObjects(ProductionRules production){ 147 String ruleName = production.ruleKey; 148 if ( production instanceof StringProductionRules ) 149 return; 150 if ( recursion.contains(ruleName) ) 151 return; 152 recursion.add(ruleName); 153 DBStack occuredRules = new DBStack(); 154 Object [] rls = production.rules; 155 if ( rls == null ) 156 return; 157 int size = rls.length; 158 occuredRules.push(ruleName); 159 ArrayList rec = new ArrayList (25); 160 ArrayList nonrec = new ArrayList (25); 161 Comparator cmpg = new RuleComparator(); 162 TreeMap recMap = new TreeMap(cmpg); 163 TreeMap nonrecMap = new TreeMap(cmpg); 164 for(int i = 0; i < size; ++i){ 165 ProductionRules productionRule = (ProductionRules)rls[i]; 166 if ( production instanceof OrProductionRules ) 167 getRecursiveForRule((OrProductionRules)production,occuredRules,productionRule,ruleName,rec,nonrec,recMap,nonrecMap); 168 if ( production instanceof OrProductionRulesWithHashMap ) 169 getRecursiveForRule((OrProductionRulesWithHashMap)production,occuredRules,productionRule,ruleName,rec,nonrec,recMap,nonrecMap); 170 getRecursiveObjects(productionRule); 171 } 172 if ( production instanceof OrProductionRules ){ 173 ((OrProductionRules)production).recursiveObjects = rec.toArray(); 174 ((OrProductionRules)production).nonRecursiveObjects = nonrec.toArray(); 175 } 176 if ( production instanceof OrProductionRulesWithHashMap ){ 177 ((OrProductionRulesWithHashMap)production).nonRecursiveObjects = nonrec.toArray(); 178 ((OrProductionRulesWithHashMap)production).recursiveObjects = rec.toArray(); 179 } 180 } 181 182 185 private void getComparableObjects(OrProductionRulesWithHashMap or){ 186 ArrayList arr = new ArrayList (); ArrayList comparable = new ArrayList (); if(or.nonRecursiveObjects == null) 189 return; 190 for(int i = 0 ; i < or.nonRecursiveObjects.length ; i++){ 191 ProductionRules pq = (ProductionRules)or.nonRecursiveObjects[i]; 192 if ( !withHashMapWithBinary.contains(pq.ruleKey) ) 193 if ( pq instanceof OrProductionRulesWithHashMap || pq instanceof UnionProductionRulesWithHashMap 194 || pq instanceof SimpleProductionRulesWithHashMap || pq instanceof RepetitiveProductionRulesWithHashMap 195 || comparableToNonComparable.contains(pq.ruleKey) ){ 196 arr.add(pq); 197 continue; 198 } 199 if(pq.getComparableObjectArray()!= null ) 200 comparable.add(pq); 201 else 202 arr.add(pq); 203 } 204 or.nonRecursiveObjects = arr.toArray(); 205 if(!comparable.isEmpty()){ 206 if ( or.comparableRules != null && or.comparableRules.length > 0 ) 207 for(int i = 0; i < or.comparableRules.length; ++i){ 208 if ( !comparable.contains(or.comparableRules[i]) ) 209 comparable.add(or.comparableRules[i]); 210 } 211 sortComparableRules(comparable,or); 212 or.comparableRules = comparable.toArray(); 213 } 214 } 215 216 219 220 private void getComparableObjects(OrProductionRules or){ 221 ArrayList arr = new ArrayList (); ArrayList comparable = new ArrayList (); if ( or.nonRecursiveObjects == null ) return; 224 for(int i = 0 ; i < or.nonRecursiveObjects.length ; i++){ 225 ProductionRules pq = (ProductionRules)or.nonRecursiveObjects[i]; 226 if ( !withHashMapWithBinary.contains(pq.ruleKey) ) 227 if ( pq instanceof OrProductionRulesWithHashMap || pq instanceof UnionProductionRulesWithHashMap 228 || pq instanceof SimpleProductionRulesWithHashMap || pq instanceof RepetitiveProductionRulesWithHashMap 229 || comparableToNonComparable.contains(pq.ruleKey) ){ 230 arr.add(pq); 231 continue; 232 } 233 if(pq.getComparableObjectArray()!= null ) 234 comparable.add(pq); 235 else 236 arr.add(pq); 237 } 238 or.nonRecursiveObjects = arr.toArray(); 239 if(!comparable.isEmpty()){ 240 if ( or.comparableRules != null && or.comparableRules.length > 0 ) 241 for(int i = 0; i < or.comparableRules.length; ++i){ 242 if ( !comparable.contains(or.comparableRules[i]) ) 243 comparable.add(0,or.comparableRules[i]); 244 } 245 sortComparableRules(comparable,or); 246 or.comparableRules = comparable.toArray(); 247 } 248 } 249 250 253 private void sortComparableRules(ArrayList listOfComparableRules,ProductionRules rule){ 254 Object array[] = listOfComparableRules.toArray(); 255 Arrays.sort(array,new StringComparatorForSql()); 256 listOfComparableRules.clear(); 257 listOfComparableRules.addAll(Arrays.asList(array)); 258 } 259 260 263 264 private void getRecursiveForRule(OrProductionRules orRule,DBStack occuredRules, 265 ProductionRules productionRule,String ruleName, 266 ArrayList recursiveObjects,ArrayList nonRecursiveObjects, 267 Map recMap, Map nonrecMap) 268 { 269 occuredRules.clear(); 270 occuredRules.add(ruleName); 271 Object []object = (Object [])productionRule.getRecursiveObject(ruleName,occuredRules); 272 occuredRules.clear(); 273 occuredRules.add(ruleName); 274 Object []object1 = (Object [])productionRule.getNonRecursiveObject(ruleName,occuredRules); 275 if ( object != null ){ 276 for(int k = 0; k < object.length; ++k){ 277 if (! recMap.containsKey(object[k]) ){ 278 recursiveObjects.add(object[k]); 279 recMap.put(object[k],null); 280 } 281 } 282 } 283 if ( object1 != null ){ 284 for(int k = 0; k < object1.length; ++k){ 285 if (! nonrecMap.containsKey(object1[k]) ){ 286 nonRecursiveObjects.add(object1[k]); 287 nonrecMap.put(object1[k],null); 288 } 289 } 290 } 291 } 292 293 296 297 private void getRecursiveForRule(OrProductionRulesWithHashMap orRule,DBStack 298 occuredRules,ProductionRules productionRule, 299 String ruleName,ArrayList recursiveObjects, 300 ArrayList nonRecursiveObjects, 301 Map recMap, Map nonrecMap 302 ){ 303 occuredRules.clear(); 304 occuredRules.add(ruleName); 305 Object []object = (Object [])productionRule.getRecursiveObject(ruleName,occuredRules); 306 occuredRules.clear(); 307 occuredRules.add(ruleName); 308 Object []object1 = (Object [])productionRule.getNonRecursiveObject(ruleName,occuredRules); 309 if ( object != null ){ 310 for(int k = 0; k < object.length; ++k) 311 if (! recMap.containsKey(object[k]) ){ 312 recursiveObjects.add(object[k]); 313 recMap.put(object[k],null); 314 } 315 } 316 if ( object1 != null ){ 317 for(int k = 0; k < object1.length; ++k){ 318 if (! nonrecMap.containsKey(object1[k]) ){ 319 nonRecursiveObjects.add(object1[k]); 320 nonrecMap.put(object1[k],null); 321 } 322 } 323 } 324 } 325 326 329 private void getComparableObjectsForToken(ProductionRules productionRules){ 330 if(productionRules instanceof StringProductionRules) 331 return; 332 String name = productionRules.ruleKey; 333 Object []rules = productionRules.rules; 334 if (rules == null) 335 return; 336 testingForDigit(name,productionRules); 337 if ( recursion.contains(name) && !recursion.contains(IDENTIFIERPART) ) 338 return; 339 recursion.add(name); 340 for(int i = 0; i < rules.length; ++i){ 341 ProductionRules rule = (ProductionRules)rules[i]; 342 if ( productionRules instanceof OrProductionRules ) 343 getComparableObjectsToken((OrProductionRules)productionRules); 344 if ( productionRules instanceof OrProductionRulesWithHashMap ) 345 getComparableObjectsToken((OrProductionRulesWithHashMap)productionRules); 346 getComparableObjectsForToken(rule); 347 } 348 } 349 350 353 private void getComparableObjectsToken(OrProductionRulesWithHashMap or){ 354 ArrayList arr = new ArrayList (); 355 ArrayList comparable = new ArrayList (); 356 for(int i = 0 ; i < or.nonRecursiveObjects.length ; i++){ 357 ProductionRules pq = (ProductionRules)or.nonRecursiveObjects[i]; 358 if(pq.getComparableObject()!= null ) 359 comparable.add(pq); 360 else{ 361 makeDummyRule(comparable,arr); 362 arr.add(pq); 363 } 364 } 365 if ( ! comparable.isEmpty() ) 366 makeDummyRule(comparable,arr); 367 or.nonRecursiveObjects = arr.toArray(); 368 } 369 370 373 private void getComparableObjectsToken(OrProductionRules or){ 374 ArrayList arr = new ArrayList (); 375 ArrayList comparable = new ArrayList (); 376 for(int i = 0 ; i < or.nonRecursiveObjects.length ; i++){ 377 ProductionRules pq = (ProductionRules)or.nonRecursiveObjects[i]; 378 if(pq.getComparableObject()!= null ) 379 comparable.add(pq); 380 else{ 381 makeDummyRule(comparable,arr); 382 arr.add(pq); 383 } 384 } 385 if ( ! comparable.isEmpty() ) 386 makeDummyRule(comparable,arr); 387 or.nonRecursiveObjects = arr.toArray(); 388 } 389 390 private void makeDummyRule(ArrayList comparable,ArrayList nonRecursiveObjects){ 391 if ( comparable.isEmpty() ) 392 return; 393 String ruleName = DummyNameGetter.getNameOfOrProductionRule(); 394 OrProductionRules orRule = new OrProductionRules(classLoader); 395 orRule.nameOfRule = ruleName; 396 orRule.ruleKey = ruleName; 397 sortComparableRulesToken(comparable); 398 orRule.comparableRules = comparable.toArray(); 399 nonRecursiveObjects.add(orRule); 400 comparable.clear(); 401 } 402 403 406 407 private void sortComparableRulesToken(ArrayList listOfComparableRules){ 408 Object array[] = listOfComparableRules.toArray(); 409 Arrays.sort(array); 410 listOfComparableRules.clear(); 411 listOfComparableRules.addAll(Arrays.asList(array)); 412 } 413 414 private void testingForDigit(String name,ProductionRules productionRules){ 415 if ( name.equalsIgnoreCase(DIGIT) ){ 416 Arrays.sort(productionRules.rules); 417 ((OrProductionRulesWithHashMap)productionRules).comparableRules = productionRules.rules; 418 } 419 } 420 421 428 429 public ProductionRules getProductionRule1(String productionRuleName)throws DException{ 430 recursion.clear(); 431 ProductionRuleParser productionRuleparser = null; 432 if ( url == null ) 433 productionRuleparser = new ProductionRuleParser(classLoader); 434 else 435 productionRuleparser = new ProductionRuleParser(url,classLoader); 436 productionRuleparser.withHashMapWithBinary = vectorForBestAndNonBest.getRulesWithComparableWithHashMap(); 437 productionRuleparser.tokenFlag = true; 438 ProductionRules tokenRule = productionRuleparser.getProductionRule(TOKEN); 439 tokenRules = productionRuleparser.allRules; withHashMapWithBinary = vectorForBestAndNonBest.getRulesWithComparableWithHashMap(); 441 ProductionRules productionRules = super.getProductionRule(productionRuleName); 442 getRecursiveObjects(productionRules); recursion.clear(); 444 getRecursiveObjects(tokenRule); recursion.clear(); 446 getComparableObjectsForToken(tokenRule); 447 productionRules.hashMap = vectorForBestAndNonBest.getRulesWithBest(); 448 tokenRule.hashMap = productionRules.hashMap; 449 productionRules.tokenRule = tokenRule; comparableToNonComparable = vectorForBestAndNonBest.getRulesToBeIncludedInNonComparableRules(); 451 recursion.clear(); 452 getProductionRuleSupportingTokens(productionRules); return productionRules; 454 } 455 456 459 469 470 473 483 484 487 496 497 500 501 521 522 525 534 535 628 629 636 637 } 638 639 class StringComparatorForSql implements Comparator { 640 641 StringComparatorForSql(){ 642 } 643 644 public int compare(Object source,Object target){ 645 ProductionRules object1 = (ProductionRules)source; 646 ProductionRules object2 = (ProductionRules)target; 647 if ( object1.result == null ){ 648 if ( object2.result == null ) 649 return 0; 650 return -1; 651 }else if ( object2.result == null ) 652 return 1; 653 String []source1 = null; 654 String []target1 = null; 655 if ( object1.result instanceof String [] && object2.result instanceof String [] ){ 656 source1 = (String [])object1.result; 657 target1 = (String [])object2.result; 658 } 659 else if ( object1.result instanceof String [] && object2.result instanceof String ){ 660 source1 = (String [])object1.result; 661 target1 = new String []{(String )object2.result}; 662 } 663 else if ( object1.result instanceof String && object2.result instanceof String [] ){ 664 source1 = new String []{(String )object1.result}; 665 target1 = (String [])object2.result; 666 } 667 else { source1 = new String []{(String )object1.result}; 669 target1 = new String []{(String )object2.result}; 670 } 671 return source1 == null ? target1 == null ? 0 : -1 672 : target1 == null ? 1 : compareResult(source1,target1); 673 } 674 675 private int compareResult(String []source,String target[]){ 676 for(int i = 0, j = 0; i < source.length && j < target.length; ++i,++j){ 677 int cmp = source[i] == null ? target[j] == null ? 0 : -1 678 : target[j] == null ? 1 : checkString(source[i],target[i]); if ( cmp != 0 ) 680 return cmp; 681 } 682 return source.length - target.length; 683 } 684 685 private int checkString(String source1,String target1){ 686 char []source = source1.replace('-','_').toCharArray(); 687 char[] target = target1.replace('-','_').toCharArray(); 688 int compare = 0; 689 for(int i = 0,j = 0; i < source.length && j < target.length && compare == 0; ++j,++i) 690 compare = source[i] - target[j]; 691 if (compare != 0 ) 692 return compare; 693 return source.length - target.length; 694 } 695 } 696 697 class RuleComparator implements Comparator { 698 699 RuleComparator(){ 700 } 701 702 public int compare(Object source,Object target){ 703 return ((ProductionRules)source).ruleKey.compareToIgnoreCase(((ProductionRules)target).ruleKey); 704 } 705 } 706 | Popular Tags |