Skip to content

Commit

Permalink
Add XML parser (#1199)
Browse files Browse the repository at this point in the history
* Add file system utility to parse XML files

* Add test for file system utility to parse XML files

* Added separate module for XmlParser

* Add docs for the XML parser

* Set @scomans as codeowner of the XmlParser

---------

Co-authored-by: Oliver Schwendener <[email protected]>
  • Loading branch information
scomans and oliverschwendener authored Sep 13, 2024
1 parent 71bef4b commit a6bba02
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ vite.config.mts @oliverschwendener

src/main/Core/IniFileParser @ke1v
src/main/Core/LinuxDesktopEnvironment @ke1v
src/main/Core/XmlParser @scomans

# Extensions

Expand Down
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@fluentui/react-icons": "^2.0.249",
"better-sqlite3": "^11.2.1",
"color": "^4.2.3",
"fast-xml-parser": "^4.5.0",
"fuse.js": "^7.0.0",
"fuzzysort": "^3.0.1",
"i18next": "^23.8.1",
Expand Down
1 change: 1 addition & 0 deletions src/main/Core/Dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ export type Dependencies = {
Translator?: Core.Translator;
UeliCommandInvoker?: Core.UeliCommandInvoker;
UrlImageGenerator?: Core.UrlImageGenerator;
XmlParser?: Core.XmlParser;
};
14 changes: 14 additions & 0 deletions src/main/Core/XmlParser/Contract/XmlParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Offers a method to parse XML content.
*/
export interface XmlParser {
/**
* Parses the given XML string and returns the result as an object.
* @param xml - The XML string to be parsed.
* @param [options] - Optional settings for parsing.
* @param options.preserveOrder - Whether to preserve the order of elements.
* @param options.ignoreAttributes - Whether to ignore attributes in the XML.
* @returns {T} - The parsed result.
*/
parse<T>(xml: string, options?: { preserveOrder: boolean; ignoreAttributes: boolean }): T;
}
1 change: 1 addition & 0 deletions src/main/Core/XmlParser/Contract/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./XmlParser";
14 changes: 14 additions & 0 deletions src/main/Core/XmlParser/XmlParser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, it } from "vitest";
import { XmlParser } from "./XmlParser";

describe(XmlParser, () => {
describe(XmlParser.prototype.parse, () => {
it("should read and parse XML file content", async () => {
const xml = "<root><test>test</test></root>";
const actual = new XmlParser().parse(xml, { ignoreAttributes: true, preserveOrder: true });
const expected = [{ root: [{ test: [{ "#text": "test" }] }] }];

expect(actual).toEqual(expected);
});
});
});
8 changes: 8 additions & 0 deletions src/main/Core/XmlParser/XmlParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { XMLParser } from "fast-xml-parser";
import type { XmlParser as XmlParserInterface } from "./Contract";

export class XmlParser implements XmlParserInterface {
public parse<T>(xml: string, options?: { preserveOrder: boolean; ignoreAttributes: boolean }): T {
return new XMLParser(options).parse(xml);
}
}
9 changes: 9 additions & 0 deletions src/main/Core/XmlParser/XmlParserModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Dependencies } from "@Core/Dependencies";
import type { DependencyRegistry } from "@Core/DependencyRegistry";
import { XmlParser } from "./XmlParser";

export class XmlParserModule {
public static bootstrap(dependencyRegistry: DependencyRegistry<Dependencies>) {
dependencyRegistry.register("XmlParser", new XmlParser());
}
}
2 changes: 2 additions & 0 deletions src/main/Core/XmlParser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./Contract";
export * from "./XmlParserModule";
1 change: 1 addition & 0 deletions src/main/Core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ export * from "./Terminal";
export * from "./Translator";
export * from "./TrayIcon";
export * from "./UeliCommand";
export * from "./XmlParser";
1 change: 1 addition & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import * as Extensions from "./Extensions";
Core.EnvironmentVariableProviderModule.bootstrap(dependencyRegistry);
Core.LinuxDesktopEnvironmentModule.bootstrap(dependencyRegistry);
Core.IniFileParserModule.bootstrap(dependencyRegistry);
Core.XmlParserModule.bootstrap(dependencyRegistry);
Core.EventEmitterModule.bootstrap(dependencyRegistry);
Core.EventSubscriberModule.bootstrap(dependencyRegistry);
Core.BrowserWindowNotifierModule.bootstrap(dependencyRegistry);
Expand Down

0 comments on commit a6bba02

Please sign in to comment.