Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added BOM when writing UTF-16LE #4

Merged
merged 18 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Open source XML library written in TypeScript

Implements a SAX parser that exposes the these methods from the `ContentHandler` interface:
Implements a SAX parser that exposes these methods from the `ContentHandler` interface:

* initialize(): void;
* setCatalog(catalog: Catalog): void;
Expand All @@ -26,7 +26,7 @@ Class `DOMBuilder` implements the `ContentHandler` interface and builds a DOM tr

## Features currently in development

* Parsing of the Internal Subset specified in the <!DOCTYPE> declaration
* Parsing of DTDs and internal subsets from <!DOCTYPE>

## Limitations

Expand Down
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
{
"name": "typesxml",
"productName": "TypesXML",
"version": "1.2.1",
"version": "1.3.0",
"description": "Open source XML library written in TypeScript",
"keywords": [
"XML",
"Parser",
"DOM",
"SAX",
"DTD"
],
"scripts": {
"build": "tsc"
},
Expand All @@ -22,7 +29,7 @@
"url": "https://github.com/rmraya/TypesXML.git"
},
"devDependencies": {
"@types/node": "^20.10.0",
"typescript": "^5.3.2"
"@types/node": "^20.10.4",
"typescript": "^5.3.3"
}
}
2 changes: 1 addition & 1 deletion ts/CData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Maxprograms.
* Copyright (c) 2023 - 2024 Maxprograms.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse License 1.0
Expand Down
16 changes: 8 additions & 8 deletions ts/Catalog.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Maxprograms.
* Copyright (c) 2023 - 2024 Maxprograms.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse License 1.0
Expand All @@ -10,8 +10,8 @@
* Maxprograms - initial API and implementation
*******************************************************************************/

import path = require("path");
import { existsSync } from "fs";
import * as path from "node:path";
import { ContentHandler } from "./ContentHandler";
import { DOMBuilder } from "./DOMBuilder";
import { SAXParser } from "./SAXParser";
Expand Down Expand Up @@ -84,7 +84,7 @@ export class Catalog {
let uri: string = this.makeAbsolute(child.getAttribute("uri").getValue());
if (existsSync(uri)) {
this.publicCatalog.set(publicId, uri);
if (uri.endsWith(".dtd")) {
if (uri.endsWith(".dtd") || uri.endsWith(".ent") || uri.endsWith(".mod")) {
let name: string = path.basename(uri);
if (!this.dtdCatalog.has(name)) {
this.dtdCatalog.set(name, uri);
Expand All @@ -109,7 +109,7 @@ export class Catalog {
let uri: string = this.makeAbsolute(child.getAttribute("uri").getValue());
if (existsSync(uri)) {
this.uriCatalog.set(child.getAttribute("name").getValue(), uri);
if (uri.endsWith(".dtd")) {
if (uri.endsWith(".dtd") || uri.endsWith(".ent") || uri.endsWith(".mod")) {
let name: string = path.basename(uri);
if (!this.dtdCatalog.has(name)) {
this.dtdCatalog.set(name, uri);
Expand All @@ -135,25 +135,25 @@ export class Catalog {
let nextCatalog: string = this.makeAbsolute(child.getAttribute("catalog").getValue());
let catalog: Catalog = new Catalog(nextCatalog);
let map: Map<string, string> = catalog.getSystemCatalog();
map.forEach((key, value) => {
map.forEach((value, key) => {
if (!this.systemCatalog.has(key)) {
this.systemCatalog.set(key, value);
}
});
map = catalog.getPublicCatalog();
map.forEach((key, value) => {
map.forEach((value, key) => {
if (!this.publicCatalog.has(key)) {
this.publicCatalog.set(key, value);
}
});
map = catalog.getUriCatalog();
map.forEach((key, value) => {
map.forEach((value, key) => {
if (!this.uriCatalog.has(key)) {
this.uriCatalog.set(key, value);
}
});
map = catalog.getDtdCatalog();
map.forEach((key, value) => {
map.forEach((value, key) => {
if (!this.dtdCatalog.has(key)) {
this.dtdCatalog.set(key, value);
}
Expand Down
9 changes: 8 additions & 1 deletion ts/Constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Maxprograms.
* Copyright (c) 2023 - 2024 Maxprograms.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse License 1.0
Expand All @@ -23,4 +23,11 @@ export class Constants {
static readonly XML_DECLARATION_NODE: number = 8;
static readonly ATTRIBUTE_LIST_DECL_NODE: number = 9;
static readonly DOCUMENT_TYPE_NODE: number = 10;

// constants for DTD parser

static readonly ATTRIBUTE_DECL_NODE: number = 11;
static readonly ELEMENT_DECL_NODE: number = 12;
static readonly INTERNAL_SUBSET_NODE: number = 13;
static readonly NOTATION_DECL_NODE: number = 14;
}
2 changes: 1 addition & 1 deletion ts/ContentHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Maxprograms.
* Copyright (c) 2023 - 2024 Maxprograms.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse License 1.0
Expand Down
15 changes: 15 additions & 0 deletions ts/DOMBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { CData } from "./CData";
import { XMLDocumentType } from "./XMLDocumentType";
import { Catalog } from "./Catalog";
import { XMLUtils } from "./XMLUtils";
import { DTDParser } from "./dtd/DTDParser";
import { Grammar } from "./grammar/Grammar";

export class DOMBuilder implements ContentHandler {

Expand All @@ -18,7 +20,9 @@ export class DOMBuilder implements ContentHandler {
document: XMLDocument;
stack: Array<XMLElement>;
catalog: Catalog;
dtdParser: DTDParser;
grammarUrl: string;
grammar: Grammar;

initialize(): void {
this.document = new XMLDocument();
Expand All @@ -30,6 +34,10 @@ export class DOMBuilder implements ContentHandler {
this.catalog = catalog;
}

setDTDParser(dtdParser: DTDParser): void {
this.dtdParser = dtdParser;
}

getDocument(): XMLDocument {
return this.document;
}
Expand Down Expand Up @@ -175,6 +183,13 @@ export class DOMBuilder implements ContentHandler {
this.document.setDocumentType(docType);
if (this.catalog) {
this.grammarUrl = this.catalog.resolveEntity(publicId, systemId);
// TODO check grammar type (DTD, XDS or RelaxNG) and use the ritght parser
if (this.dtdParser && this.grammarUrl) {
let dtdGrammar :Grammar = this.dtdParser.parseDTD(this.grammarUrl);
if (dtdGrammar) {
this.grammar = dtdGrammar;
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion ts/FileReader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Maxprograms.
* Copyright (c) 2023 - 2024 Maxprograms.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse License 1.0
Expand Down
2 changes: 1 addition & 1 deletion ts/Indenter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Maxprograms.
* Copyright (c) 2023 - 2024 Maxprograms.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse License 1.0
Expand Down
2 changes: 1 addition & 1 deletion ts/ProcessingInstruction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Maxprograms.
* Copyright (c) 2023 - 2024 Maxprograms.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse License 1.0
Expand Down
Loading
Loading