ソースを参照

Update 'src/app/core/parser/file-parser.js'

To read GEO with intersection point
New debug help method "static printMap_fp (map)"
New parameter "vec_OD" in "parse (vec_OD)":
- from "parsed_object = this.parseObjectLine(str.trim());" to: "aux_vec = this.parseObjectLine(str.trim());" and additional "if"
- in "parseObjectLine (object_line)" added several additional comments and "try/catch" to help in debug
leo 2 年 前
コミット
16eeef2163
1 ファイル変更133 行追加59 行削除
  1. 133 59
      src/app/core/parser/file-parser.js

+ 133 - 59
src/app/core/parser/file-parser.js

@@ -26,106 +26,152 @@ export class FileParser {
 
   // @param {string} content
   constructor (content) {
-    this.content = content;
+    this.content = content; // String with GEO file content
+    this.lines = []; // to store each GEO line (help debug)
     }
 
+  //DEBUG: get String from map to check associated geometric object
+  static printMap_fp (map) {
+    var str = ""; // map.forEach((value: boolean, key: string) => { str += "[" + key + "=" + value + "] "; });
+    for (let [key, value] of map) { str += "[" + key + "=" + value + "] "; }
+    return str;
+    }
+
+  // Analyse GEO content building a Map with objects
+  // @param vec_OD is Array with entries {id:<value>, obj:<dynamic object>}
   // @calledby app/core/application/header-menu.js: onFileChanged()
-  parse () {
-    //D console.trace();
+  parse (vec_OD) {
     console.log("file-parser.js!parse(): start");
-    var parsed_object, parsed_result = this.content.split("\n").filter(str => !str.match(/^[#\[]/))
+    var parsed_object, obj, parsed_result = this.content.split("\n").filter(str => !str.match(/^[#\[]/))
       .map(str => {
         try {
+        var aux_vec, only_id = -1; // integer with ID only (to debug proces) returned by parseObjectLine(object_line): return [map, only_id];
+        this.lines.push(str);
         if (!OBJECT_LINE_REGEX.test(str.trim())) {
-          console.error("Malformed object string in iGeom file: ", str);
+          console.error("file-parser.js!parse(.): Malformed object string in iGeom file: ", str);
           return undefined;
           }
-        parsed_object = this.parseObjectLine(str.trim());
+        only_id = -1; // security to debug
+        aux_vec = this.parseObjectLine(str.trim()); // go to parseObjectLine(.)
+        if (aux_vec && aux_vec.length>1) { parsed_object = aux_vec[0]; only_id = aux_vec[1]; } else parsed_object = aux_vec;
+        if (parsed_object && parsed_object!="" &&  parsed_object!=undefined && only_id!=-1) {
+          try { // file-parser.js!parse(): vec_id.push error TypeError: parsed_object.get is not a function::""
+            obj = { id : only_id, obj : parsed_object };
+            vec_OD.push(obj); // by reference, change "effective parameter"
+            //vec_id.push(parsed_object.get("id"));
+          } catch (error) { console.log("file-parser.js!parse(): vec_id.push error " + error + "::" + JSON.stringify(parsed_object)); }
+          }
         //D console.log("file-parser.js!parse(): str=" + str + "\nparsed_object=" + JSON.stringify(parsed_object)); //D
         return parsed_object;
-	} catch (error) {
-	  // console.error("file-parser.js!parse(): error " + error + " in: " + str);
-	  console.log("file-parser.js!parse(): error " + error + " in: " + str);
-	  }
+        } catch (error) {
+          // console.error("file-parser.js!parse(): error " + error + " in: " + str);
+          console.log("file-parser.js!parse(): error " + error + " in: " + str);
+          }
         });
-    console.log("file-parser.js!parse(): final"); //D
+    var aux1 = ""; try { aux1 = JSON.stringify(Object.keys(parsed_result)) + " "; } catch (error) { aux1 = "error " + error + " "; }
+    var aux2 = ""; try { var tam = vec_OD.length; for (var ii=0; ii<tam; ii++) aux2 += "[" + vec_OD[ii].id + ":" + vec_OD[ii].obj + "],"; } catch (error) { aux2 += "error " + error + " "; }
+    console.log("file-parser.js!parse(): " + aux1); //D
+    console.log("file-parser.js!parse(): " + aux2 + "\n* final"); //D
     return parsed_result;
-    }
+    } // parse(vec_OD)
 
   // Function to analyse each line of a GEO file, currently with 8 itens.
   // A typical GEO line (to free Point) and its 8 itens:
   //   1:0, 0:0, 2:215.0 -145.0, 3:2, 4:A 4.0 -10.0 0, 6:1, 5:-16711936, 7:0
-  // in this line, following the order in line, the param code are
-  //   "1" ("1:0"),  "0" ("0:0"),  "2" ("2:215.0 -145.0"), "3" ("3:2"), "4" ("4:A 4.0 -10.0 0"), 
-  //   "6" ("6:1"), "5" ("5:-16711936") and "7" ("7:0")
+  // in this line, following the order in line, the param code are defined in "app/core/enums/geo-file-enum.js":
+  //  ID=1, TYPE=0, DEFINITION=2, LIST=3, LABEL=4, COLOR=5, DEFINED=6, HIDDEN=7, PIXEL=8, FONT=9, LABEL_COLOR=10
+  //  E.g: "1" ("1:0"),  "0" ("0:0"),  "2" ("2:215.0 -145.0"), "3" ("3:2"), "4" ("4:A 4.0 -10.0 0"), 
+  //       "6" ("6:1"), "5" ("5:-16711936") and "7" ("7:0")
   // @param {string} object_line
   parseObjectLine (object_line) {
+    var only_id = -1; // integer with ID only (to debug proces)
     const map = new Map();
-    object_line = object_line.substring(1);
+    object_line = object_line.substring(1); // remove initial character '{'
     if (object_line.endsWith("}!")) {
-      object_line = object_line.substring(0, object_line.length - 2);
+      object_line = object_line.substring(0, object_line.length - 2); // remove final char '}!'
     } else {
-      object_line = object_line.substring(0, object_line.length - 1);
+      object_line = object_line.substring(0, object_line.length - 1); // remove final char '}'
       }
-    //D console.log("file-parser.js: object_line = '" + object_line + "'"); //D
+    if (!object_line || object_line.length==0) return "";
+    console.log("file-parser.js: object_line = '" + object_line + "'"); //D
+    var str2Debug = ""; //D13
+    var prop, prop_info, key, type, id, value_id, definition_param_list, list, param, maybe_number,
+        id_list, array_list, label_param_list, defined_value, color_value,
+        hidden_value, pixel_position, font_param_list, label_color_value, label_color;
     const properties = object_line.split(",");
     for (const index in properties) {
-      const prop = properties[index];
-      const prop_info = prop.trim().split(":");
-      const key = parseInt(prop_info[0]);
+      prop = properties[index];
+      prop_info = prop.trim().split(":");
+      key = parseInt(prop_info[0]);
       if (prop_info[1] == undefined) continue;
       if (key == TYPE) { // TYPE = 0
-        const type = prop_info[1];
+        type = prop_info[1];
+        str2Debug += "TYPE=" + key + ": " + type + ", "; //D13
         map.set("type", ObjectClass.fromCodeToClass(parseInt(type)));
       } else if (key == ID) {
-        const id = prop_info[1];
-        map.set("id", parseInt(id));
+        id = prop_info[1];
+        value_id = parseInt(id);
+        str2Debug += "ID=" + key + ": " + (value_id+0) + ", "; //D13 "value_id+0" to identify error in "integer"
+        //D console.log("file-parser.js: ID: id=" + id + " = " + prop_info[1]); // is ID
+	only_id = value_id; // integer with ID only (to debug proces)
+        map.set("id", value_id);
       } else if (key == DEFINITION) { // DEFINITION = 2: "2:215.0 -145.0" or "2:0 1"...
         var aux = ""; //D_
-        const param_list = [];
+        str2Debug += "DEFINITION=" + key + ": ("; //D13
+        definition_param_list = [];
         const list = prop_info[1].trim().split(" ");
         aux += "index=" + index + ", #prop_info[1]=" + list.length + ", "; //D_
         var ii=0; //D_
         for (const i in list) {
           const param = list[i];
           const maybe_number = Number(param);
+        str2Debug += param + ", "; //D13
           aux += "(" + ii + "," + param + " == " + maybe_number + "?); "; //D_
           ii++; //D_
           if (Number.isNaN(maybe_number)) {
-            param_list.push(param);
+            definition_param_list.push(param);
           } else {
-            param_list.push(maybe_number);
+            definition_param_list.push(maybe_number);
             }
           } // for (const i in list)
         //DD console.log("file-parser.js: DEFINITION: " + aux); //D_
-        map.set("param", param_list);
+        map.set("param", definition_param_list);
+        str2Debug += "), "; //D13
       } else if (key == LIST) {
-        const id_list = [];
-        const list = prop_info[1].trim().split(" ");
-        for (const i in list) {
-          const id = list[i];
-          id_list.push(id);
+        str2Debug += "LIST=" + key + ": ("; //D13
+        id_list = [];
+        array_list = prop_info[1].trim().split(" ");
+        for (const i in array_list) {
+          const id1 = array_list[i];
+        str2Debug += id1 + ", "; //D13
+          id_list.push(id1);
           }
+        str2Debug += "), "; //D13
         map.set("deps", id_list);
       } else if (key == LABEL) {
-        const param_list = [];
+        str2Debug += "LABEL=" + key + ": ("; //D13
+        label_param_list = [];
         const list = prop_info[1].trim().split(" ");
         for (const i in list) {
           const param = list[i];
+        str2Debug += param + ", "; //D13
           const maybe_number = Number(param);
           if (Number.isNaN(maybe_number)) {
-            param_list.push(param);
+            label_param_list.push(param);
           } else {
-            param_list.push(maybe_number);
+            label_param_list.push(maybe_number);
             }
           }
-        map.set("label", param_list);
+        str2Debug += "), "; //D13
+        map.set("label", label_param_list);
       } else if (key == DEFINED) {
-        const value = prop_info[1];
-        map.set("defined", parseInt(value));
+        defined_value = prop_info[1];
+        str2Debug += "DEFINED=" + key + ": " + defined_value + ", "; //D13
+        map.set("defined", parseInt(defined_value));
       } else if (key == COLOR) {
-        const value = prop_info[1];
-        let number = Number(value);
+        color_value = prop_info[1];
+        str2Debug += "COLOR=" + key + ": " + color_value + ", "; //D13
+        let number = Number(color_value);
         const alpha = ((number & ALPHA_MASK) >>> 24) / 255;
         number = number ^ ALPHA_MASK;
         const r = (number >> 16) & 0xff;
@@ -133,33 +179,61 @@ export class FileParser {
         const b = number & 0xff;
         map.set("color", { alpha: alpha, r: r, g: g, b: b });
       } else if (key == HIDDEN) {
-        const value = prop_info[1];
-        map.set("hidden", parseInt(value));
-      } else if (key == PIXEL) {
-        const position = [];
+        hidden_value = prop_info[1];
+        str2Debug += "HIDDEN=" + key + ": " + hidden_value + ", "; //D13
+        if (hidden_value) // optional: if empty do not insert this property
+          map.set("hidden", parseInt(hidden_value));
+      } else if (key == PIXEL) { // PIXEL = 8
+        str2Debug += "PIXEL=" + key + ": ("; //D13
+        pixel_position = [];
         const list = prop_info[1].trim().split(" ");
+        var count_pix = 0;
         for (const index in list) {
-          const i = list[index];
-          position.push(parseInt(i));
+          const value_pos = list[index];
+          const value_pos_int = parseInt(value_pos);
+          str2Debug += value_pos + ", "; //D13
+          if (value_pos_int) { // optional: if empty do not insert this property
+            count_pix++;
+            pixel_position.push(value_pos_int);
+	    }
           }
-        map.set("position", position);
+        str2Debug += "), "; //D13
+        if (count_pix>0) // optional: if empty do not insert this property
+          map.set("position", pixel_position);
+        console.log("file-parser.js!parseObjectLine(): PIXEL=" + PIXEL + ", count_pix=" + count_pix);
       } else if (key == FONT) {
-        const param_list = prop_info[1].trim().split(" ");
-        map.set("font", param_list);
+        font_param_list = prop_info[1].trim().split(" ");
+        if (font_param_list && font_param_list.length>0) // optional: if empty do not insert this property
+          map.set("font", font_param_list);
+        str2Debug += "FONT=" + key + ": " + font_param_list + ", "; //D13
       } else if (key == LABEL_COLOR) {
-        const value = prop_info[1];
-        let number = Number(value);
+        label_color_value = prop_info[1];
+        str2Debug += "LABEL_COLOR=" + key + ": (" + label_color_value + "="; //D13
+        let number = Number(label_color_value);
         const alpha = ((number & ALPHA_MASK) >>> 24) / 255;
         number = number ^ ALPHA_MASK;
         const r = (number >> 16) & 0xff;
         const g = (number >> 8) & 0xff;
         const b = number & 0xff;
-        map.set("label_color", { alpha: alpha, r: r, g: g, b: b });
+        str2Debug += "RGB("+r+","+g+","+b+")), "; //D13
+        if (r && g && b) // optional: if empty do not insert this property
+          map.set("label_color", { alpha: alpha, r: r, g: g, b: b });
+        label_color = "(" + alpha + ", RGB[" + r + "," + g + "," + b + "])";
         }
-
       } // for (const index in properties)
+    console.log("file-parser.js: '" + str2Debug + "'"); //D
+    if (type==13) { //D
+      var auxDef = "";
+      for (var ii=0; ii<definition_param_list.length; ii++) auxDef += definition_param_list[ii] + ",";
+      console.log("file-parser.js!parseObjectLine(.): id=" + id + ", DEFINITION=(" + auxDef + ")");
+      console.log("file-parser.js!parseObjectLine(.): id=" + id + ", map=(" + FileParser.printMap_fp(map) + ")"); // JSON.stringify(Object.keys ...) // map.entries()
+      // map.entries() = [object Map Iterator]
+      }
+    var vec_aux;
+    try { // build Array() with {id:<value>, obj:<dynamic object>}
+      vec_aux = [map, only_id];
+    } catch (error) { vec_aux = map; console.log("file-parser.js!parseObjectLine(.): id=" + id + ", error " + error); }
+    return vec_aux;
+    } // parseObjectLine(object_line)
 
-    return map;
-    }
-
-  }
+  } // export class FileParser