1 package com.daffodilwoods.daffodildb.utils.parser; 2 3 import java.lang.reflect.Field ; 4 import com.daffodilwoods.database.resource.DException; 5 import com.daffodilwoods.database.resource.DRuntimeException; 6 7 public class BinaryStringLiteralProductionRules extends ProductionRules{ 8 9 public static final int BINARYSTRINGTYPE = 1; 10 public static final int BITSTRINGTYPE = 2; 11 public static final int HEXSTRINGTYPE = 3; 12 int typeOfField ; 13 14 BinaryStringLiteralProductionRules(ClassLoader classLoader0) { 15 super(classLoader0); 16 ruleKey = nameOfRule; 17 typeOfField = -1; 18 } 19 20 void setClassName(int type){ 21 typeOfField = type; 22 switch(type){ 23 case BINARYSTRINGTYPE : 24 className = "binarystringliteral"; 25 break; 26 case BITSTRINGTYPE : 27 className = "bitstringliteralrepresentation"; 28 break; 29 case HEXSTRINGTYPE : 30 className = "hexitstringliteralrepresentation"; 31 break; 32 default : 33 throw new DRuntimeException("DSE0",new Object []{"Invalid Type for this Object"}); 34 } 35 } 36 37 Object parsePart(ParseElements pe) throws DException{ 38 if(typeOfField == -1) 39 throw new DException("DSE0",new Object []{"Type of rule is not set. First set the Type"}); 40 int position = pe.position; 41 char[] val = pe.queryArray; 42 int length = val.length; 43 char c = '\''; 44 45 46 while(position < pe.queryArray.length && Character.isWhitespace(val[position])) 47 position++; 48 49 if(position == pe.queryArray.length || val[position] != c){ 50 return pe.parseException; 51 } 52 position++; 53 return typeOfField == BITSTRINGTYPE ? 54 parsePartBitType(pe,position,length,val,c) : 55 parsePartHexitType(pe,position,length,val,c); 56 } 57 58 private Object parsePartBitType(ParseElements pe,int position,int length,char[] val,char c){ 59 StringBuffer stbfr = new StringBuffer (); 60 while(position < length){ 61 if(val[position] == c){ 62 position++; 63 pe.position = position; 64 String sa = stbfr.toString(); 65 return sa; 66 } 67 int a = (int)val[position]; 68 if(a == 48 || a == 49){ 69 stbfr.append(val[position]); 70 position++; 71 }else{ 72 return pe.parseException; 73 } 74 } 75 return pe.parseException; 76 } 77 78 private Object parsePartHexitType(ParseElements pe,int position,int length,char[] val,char c){ 79 StringBuffer stbfr = new StringBuffer (); 80 while(position < length){ 81 if(val[position] == c){ 82 position++; 83 pe.position = position; 84 String sa = stbfr.toString(); 85 return sa; 86 } 87 if(Character.isDigit(val[position]) || checkHex(val[position])){ 88 stbfr.append(val[position]); 89 position++; 90 }else{ 91 return pe.parseException; 92 } 93 } 94 return pe.parseException; 95 } 96 97 private boolean checkHex(char c){ 98 int a = (int)c; 99 return (a > 64 && a < 71) || (a > 96 && a < 103); 100 } 101 } 102 | Popular Tags |