tiagobento commented on code in PR #2105:
URL: 
https://github.com/apache/incubator-kie-tools/pull/2105#discussion_r1439991385


##########
packages/dmn-language-service/src/DmnLanguageService.ts:
##########
@@ -20,61 +20,165 @@
 import { DmnDocumentData } from "./DmnDocumentData";
 import { DmnDecision } from "./DmnDecision";
 import * as path from "path";
+import { DmnMarshaller, getMarshaller } from "@kie-tools/dmn-marshaller";
 
-const IMPORT = "import";
 const INPUT_DATA = "inputData";
 const XML_MIME = "text/xml";
-const LOCATION_URI_ATTRIBUTE = "locationURI";
 const DECISION_NAME_ATTRIBUTE = "name";
 const NAMESPACE = "namespace";
 const DMN_NAME = "name";
 const DECISION = "decision";
 const DEFINITIONS = "definitions";
 
+interface ImportedModelsByModelResources {
+  modelResources: DmnLanguageServiceImportedModelResources[];
+  normalizedPosixPathRelativeToWorkspaceRoot?: undefined;
+}
+
+interface ImportedModelsByNormalizedPosixPathRelativeToWorkspaceRoot {
+  modelResources?: undefined;
+  normalizedPosixPathRelativeToWorkspaceRoot: string;
+}
+
+type ImportedModelsParams = ImportedModelsByModelResources | 
ImportedModelsByNormalizedPosixPathRelativeToWorkspaceRoot;
+
 export interface DmnLanguageServiceImportedModelResources {
   content: string;
-  pathRelativeToWorkspaceRoot: string;
+  normalizedPosixPathRelativeToWorkspaceRoot: string;
 }
 
 export class DmnLanguageService {
   private readonly parser = new DOMParser();
-  private readonly importTagRegExp = new RegExp(`([a-z]*:)?(${IMPORT})`);
   private readonly inputDataRegEx = new RegExp(`([a-z]*:)?(${INPUT_DATA})`);
   private readonly decisionsTagRegExp = new RegExp(`([a-z]*:)?(${DECISION})`);
   private readonly definitionsTagRegExp = new 
RegExp(`([a-z]*:)?(${DEFINITIONS})`);
 
   constructor(
     private readonly args: {
-      getDmnImportedModelResource: (
-        importedModelPathRelativeToWorkspaceRoot: string
-      ) => Promise<DmnLanguageServiceImportedModelResources | undefined>;
+      getModelContent: (args: {
+        normalizedPosixPathRelativeToWorkspaceRoot: string;
+      }) => Promise<DmnLanguageServiceImportedModelResources | undefined>;
     }
   ) {}
 
-  private getImportedModelPathRelativeToWorkspaceRoot(
-    modelResources: DmnLanguageServiceImportedModelResources
+  private getImportedModelByNormalizedPosixPathRelativeToWorkspaceRoot(
+    modelResources: DmnLanguageServiceImportedModelResources,
+    importedModelsNormalizedPosixPathRelativeToWorkspaceRoot: Set<string>
   ): string[] {
-    const xmlContent = this.parser.parseFromString(modelResources.content, 
XML_MIME);
-    const importTag = this.importTagRegExp.exec(modelResources.content);
-    const importedModels = xmlContent.getElementsByTagName(importTag?.[0] ?? 
IMPORT);
-    return Array.from(importedModels)
-      .map((importedModel) =>
-        path.posix.join(
-          path.dirname(modelResources.pathRelativeToWorkspaceRoot),
-          path.normalize(importedModel.getAttribute(LOCATION_URI_ATTRIBUTE) ?? 
"")
-        )
-      )
+    if (!modelResources.content) {
+      return [];
+    }
+
+    const marshaller: DmnMarshaller = getMarshaller(modelResources.content);
+    const definitions = marshaller.parser.parse()?.definitions;
+
+    if (!definitions) {
+      return [];
+    }
+    if (!definitions.import) {
+      return [];
+    }
+
+    return definitions.import
+      .flatMap((importedModel) => {
+        const importedModelNormalizedPosixPathRelativeToWorkspaceRoot = 
path.posix.normalize(
+          path.posix.join(
+            
path.dirname(modelResources.normalizedPosixPathRelativeToWorkspaceRoot),
+            path.normalize(importedModel["@_locationURI"] ?? "")
+          )
+        );

Review Comment:
   Right, right, that makes total sense. Yep, we should be careful then to only 
use 'path.posix' when dealing with POSIX paths then. The DMN LS especially runs 
both on Node and browsers, so it makes sense to force POSIX everywhere. As 
we've been discussing as part of the Paths Standardizatin initiative, dealing 
with FS paths should be restricted to the last moment possible, when directly 
interaction with the platform's FS. 
   
   Thx!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to