1//this js file parses the prefix into the endtag format to store information in an array (allows for indefinite mg/fieldsplit nesting) 2//requires the SAWs_prefix and the array of data that was already parsed 3 4//parses the prefix and returns an object containing the proper endtag and the newly encountered fieldsplit word (if any) 5function parsePrefix(data,SAWs_prefix) { 6 7 var endtag = "0"; 8 var newWord = ""; 9 10 while(SAWs_prefix != "") {//parse the entire prefix 11 12 var indexFirstUnderscore = SAWs_prefix.indexOf("_"); 13 var chunk = SAWs_prefix.substring(0,indexFirstUnderscore);//dont include the underscore 14 15 if(chunk == "mg") {//mg_ 16 indexFirstUnderscore = SAWs_prefix.indexOf("_",3); //index of the second underscore 17 chunk = SAWs_prefix.substring(0,indexFirstUnderscore);//updated chunk 18 19 //include more underscores here 20 if(chunk == "mg_levels") {//need to include yet another underscore 21 indexFirstUnderscore = SAWs_prefix.indexOf("_",10); //index of the third underscore 22 chunk = SAWs_prefix.substring(0,indexFirstUnderscore);//updated chunk 23 } 24 //otherwise, chunk == "mg_coarse" and we don't need to worry about it 25 } 26 27 else if(chunk == "fieldsplit") {//fieldsplit_ 28 29 var closest = SAWs_prefix.length;//the furthest a keyword could possibly be 30 //find index of next keyword (pc, ksp, sub, smoothing, coarse). we have to do it this way because the name of the fieldsplit may include an underscore. for example, x_velocity_ 31 32 var keywords = ["pc","ksp","sub","redundant","mg","asm"]; 33 var loc = SAWs_prefix.length; 34 for(var i=0; i<keywords.length; i++) { 35 loc = SAWs_prefix.indexOf(keywords[i]); 36 if(loc < closest && loc != -1) 37 closest = loc; 38 } 39 40 var theword = SAWs_prefix.substring(11,closest-1);//omit the first and last underscore 41 var existingEndtag = getEndtagByName(data, theword, endtag);//get the id (for example "001") associated with this fieldsplit word. need to pass in the existing endtag because we need to specify the parent of this fieldsplit 42 43 if(existingEndtag == "-1") { //new fieldsplit. this word has not been encountered yet. 44 var fieldsplitNumber = getNumChildren(data, endtag);//endtag = parent of this fieldsplit @TODO 45 endtag = endtag + "_" + fieldsplitNumber.toString(); 46 newWord = theword; 47 } 48 49 else { //we have encountered this word before 50 endtag = existingEndtag; 51 } 52 } 53 54 SAWs_prefix = SAWs_prefix.substring(indexFirstUnderscore+1, SAWs_prefix.length);//dont include the first underscore 55 56 if(chunk=="ksp" || chunk=="sub" || chunk=="mg_coarse" || chunk=="redundant") 57 endtag += "_0"; 58 else if(chunk.substring(0,10)=="mg_levels_") 59 endtag += "_" + chunk.substring(10,chunk.length); 60 } 61 62 var ret = new Object(); 63 ret.endtag = endtag; 64 ret.newWord = newWord; 65 66 return ret; 67} 68