Skip to content

Commit 84294cb

Browse files
committed
Replaced URLs with contstants
1 parent 07ae455 commit 84294cb

7 files changed

Lines changed: 26 additions & 16 deletions

File tree

ts/Constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export class Constants {
3434
// namespace constants
3535
static readonly XMLNS_NS: string = 'http://www.w3.org/2000/xmlns/';
3636
static readonly XML_NS: string = 'http://www.w3.org/XML/1998/namespace';
37+
static readonly XML_SCHEMA_NS: string = 'http://www.w3.org/2001/XMLSchema';
38+
static readonly XML_SCHEMA_NS_SECURE: string = 'https://www.w3.org/2001/XMLSchema';
39+
static readonly XML_SCHEMA_INSTANCE_NS: string = 'http://www.w3.org/2001/XMLSchema-instance';
3740
static readonly XSD_NS: string = 'http://www.w3.org/2001/XMLSchema-datatypes';
3841
static readonly RELAXNG_NS_URI: string = 'http://relaxng.org/ns/structure/1.0';
3942
}

ts/grammar/CompositeGrammar.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import { existsSync, readFileSync } from 'fs';
1414
import { resolve } from 'path';
15+
import { Constants } from '../Constants';
1516
import { XMLUtils } from '../XMLUtils';
1617
import { AttributeGroup } from '../schema/AttributeGroup';
1718
import { BuiltinTypes } from '../schema/BuiltinTypes';
@@ -93,7 +94,7 @@ export class CompositeGrammar implements Grammar {
9394
const data: any = JSON.parse(readFileSync(grammarPath, 'utf8'));
9495
const grammar: XMLSchemaGrammar = XMLSchemaGrammar.fromJSON(data);
9596

96-
this.grammars.set('http://www.w3.org/2001/XMLSchema', grammar);
97+
this.grammars.set(Constants.XML_SCHEMA_NS, grammar);
9798
}
9899
} catch (error) {
99100
// Silently continue - this is an optimization, not required
@@ -107,7 +108,7 @@ export class CompositeGrammar implements Grammar {
107108
const data: any = JSON.parse(readFileSync(grammarPath, 'utf8'));
108109
const grammar: XMLSchemaGrammar = XMLSchemaGrammar.fromJSON(data);
109110

110-
this.grammars.set('http://www.w3.org/2001/XMLSchema-instance', grammar);
111+
this.grammars.set(Constants.XML_SCHEMA_INSTANCE_NS, grammar);
111112

112113
}
113114
} catch (error) {
@@ -137,14 +138,15 @@ export class CompositeGrammar implements Grammar {
137138
const resolvedNamespace: string = namespace;
138139

139140
if (prefix === '') {
140-
const isSchemaDefault: boolean = resolvedNamespace === 'http://www.w3.org/2001/XMLSchema';
141+
const isSchemaDefault: boolean = resolvedNamespace === Constants.XML_SCHEMA_NS || resolvedNamespace === Constants.XML_SCHEMA_NS_SECURE;
141142
if (isSchemaDefault) {
142143
// Avoid persisting the XML Schema default namespace as the
143144
// active default for instance documents.
144-
if (this.prefixToNamespace.get('') === 'http://www.w3.org/2001/XMLSchema') {
145+
const defaultPrefixNs = this.prefixToNamespace.get('');
146+
if (defaultPrefixNs === Constants.XML_SCHEMA_NS || defaultPrefixNs === Constants.XML_SCHEMA_NS_SECURE) {
145147
this.prefixToNamespace.delete('');
146148
}
147-
if (this.defaultNamespace === 'http://www.w3.org/2001/XMLSchema') {
149+
if (this.defaultNamespace === Constants.XML_SCHEMA_NS || this.defaultNamespace === Constants.XML_SCHEMA_NS_SECURE) {
148150
this.defaultNamespace = '';
149151
}
150152
return;
@@ -640,7 +642,7 @@ export class CompositeGrammar implements Grammar {
640642
if (!elementDecl) {
641643
const targetNamespace: string | undefined = grammar.getTargetNamespace?.();
642644

643-
if (targetNamespace === 'http://www.w3.org/2001/XMLSchema') {
645+
if (targetNamespace === Constants.XML_SCHEMA_NS || targetNamespace === Constants.XML_SCHEMA_NS_SECURE) {
644646
// Allow schema documents to pass through structural validation.
645647
// Semantic validation is handled separately by XMLSchemaParser.
646648
return ValidationResult.success();
@@ -1042,7 +1044,7 @@ export class CompositeGrammar implements Grammar {
10421044
const name: string | undefined = simpleType.getName();
10431045
if (name) {
10441046
const localName: string = name.includes(':') ? name.substring(name.indexOf(':') + 1) : name;
1045-
if (namespace === 'http://www.w3.org/2001/XMLSchema' || name.startsWith('xsd:') || name.startsWith('xs:')) {
1047+
if (namespace === Constants.XML_SCHEMA_NS || namespace === Constants.XML_SCHEMA_NS_SECURE || name.startsWith('xsd:') || name.startsWith('xs:')) {
10461048
return localName;
10471049
}
10481050
}

ts/grammar/GrammarHandler.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { existsSync } from 'fs';
1414
import { dirname, isAbsolute, relative, resolve } from 'path';
1515
import { fileURLToPath } from 'url';
1616
import { Catalog } from '../Catalog';
17+
import { Constants } from '../Constants';
1718
import { DTDGrammar } from '../dtd/DTDGrammar';
1819
import { DTDParser } from '../dtd/DTDParser';
1920
import { EntityDecl } from '../dtd/EntityDecl';
@@ -419,7 +420,7 @@ export class GrammarHandler {
419420
hintLocation?: string
420421
): void {
421422
// Skip XMLSchema-instance namespace - it's handled by pre-compiled grammar
422-
if (namespace === 'http://www.w3.org/2001/XMLSchema-instance') {
423+
if (namespace === Constants.XML_SCHEMA_INSTANCE_NS) {
423424
this.trace(`Skipping schema load for reserved namespace '${namespace}'`);
424425
return;
425426
}
@@ -616,8 +617,8 @@ export class GrammarHandler {
616617
this.shouldPerformSemanticValidation();
617618

618619
if (shouldValidateSemantics && this.currentFile) {
619-
const hasXSDNamespace = namespaceInfo.defaultNamespace === 'http://www.w3.org/2001/XMLSchema' ||
620-
Array.from(namespaceInfo.prefixMappings.values()).includes('http://www.w3.org/2001/XMLSchema');
620+
const hasXSDNamespace = namespaceInfo.defaultNamespace === Constants.XML_SCHEMA_NS ||
621+
Array.from(namespaceInfo.prefixMappings.values()).some(ns => ns === Constants.XML_SCHEMA_NS || ns === Constants.XML_SCHEMA_NS_SECURE);
621622

622623
if (hasXSDNamespace) {
623624
const schemaParser = XMLSchemaParser.getInstance();

ts/grammar/GrammarPrecompiler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import { XMLSchemaParser } from "../schema/XMLSchemaParser";
1414
import { Catalog } from "../Catalog";
15+
import { Constants } from "../Constants";
1516
import { writeFileSync } from 'fs';
1617
import { resolve } from 'path';
1718

@@ -32,7 +33,7 @@ export class GrammarPrecompiler {
3233
const xmlSchemaPath = resolve(__dirname, '../../catalog/xml/xml.xsd');
3334
console.log(' Source: ' + xmlSchemaPath);
3435

35-
const xmlGrammar = this.schemaParser.parseSchema(xmlSchemaPath, 'http://www.w3.org/XML/1998/namespace');
36+
const xmlGrammar = this.schemaParser.parseSchema(xmlSchemaPath, Constants.XML_NS);
3637

3738
if (xmlGrammar) {
3839
const serialized = xmlGrammar.toJSON();
@@ -60,7 +61,7 @@ export class GrammarPrecompiler {
6061
const xmlSchemaPath = resolve(__dirname, '../../catalog/xml/XMLSchema.xsd');
6162
console.log(' Source: ' + xmlSchemaPath);
6263

63-
const schemaGrammar = this.schemaParser.parseSchema(xmlSchemaPath, 'http://www.w3.org/2001/XMLSchema');
64+
const schemaGrammar = this.schemaParser.parseSchema(xmlSchemaPath, Constants.XML_SCHEMA_NS);
6465

6566
if (schemaGrammar) {
6667
const serialized = schemaGrammar.toJSON();

ts/schema/BuiltinTypes.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Maxprograms - initial API and implementation
1111
*******************************************************************************/
1212

13+
import { Constants } from "../Constants";
1314
import { ValidationResult } from "../grammar/Grammar";
1415
import { XMLUtils } from "../XMLUtils";
1516
import { SimpleType } from "./SimpleType";
@@ -23,7 +24,7 @@ export class BuiltinTypes {
2324
return;
2425
}
2526

26-
const xsNamespace = "http://www.w3.org/2001/XMLSchema";
27+
const xsNamespace = Constants.XML_SCHEMA_NS;
2728

2829
// Base types
2930
this.createType("anyType", xsNamespace, () => ValidationResult.success());
@@ -92,7 +93,7 @@ export class BuiltinTypes {
9293
const colonIndex = name.indexOf(':');
9394
const localName = colonIndex !== -1 ? name.substring(colonIndex + 1) : name;
9495
// For built-in types, assume XML Schema namespace
95-
const key = `{http://www.w3.org/2001/XMLSchema}${localName}`;
96+
const key = `{${Constants.XML_SCHEMA_NS}}${localName}`;
9697
return this.types.get(key);
9798
}
9899

ts/schema/SchemaParsingHandler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*******************************************************************************/
1212

1313
import { Catalog } from "../Catalog";
14+
import { Constants } from "../Constants";
1415
import { ContentHandler } from "../ContentHandler";
1516
import { GrammarHandler } from "../grammar/GrammarHandler";
1617
import { XMLAttribute } from "../XMLAttribute";
@@ -653,7 +654,7 @@ export class SchemaParsingHandler implements ContentHandler {
653654
attrType.setTypeName(typeQName);
654655
} else {
655656
// Default to xs:string when no type is specified
656-
const stringType = '{http://www.w3.org/2001/XMLSchema}string';
657+
const stringType = `{${Constants.XML_SCHEMA_NS}}string`;
657658
attrType = new SimpleType(stringType);
658659
attrType.setTypeName(stringType);
659660
}

ts/schema/XMLSchemaGrammar.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Maxprograms - initial API and implementation
1111
*******************************************************************************/
1212

13+
import { Constants } from "../Constants";
1314
import { NotationDecl } from "../dtd/NotationDecl";
1415
import { AttributeInfo, Grammar, GrammarType, ValidationContext, ValidationResult } from "../grammar/Grammar";
1516
import { XMLUtils } from "../XMLUtils";
@@ -997,7 +998,7 @@ export class XMLSchemaGrammar implements Grammar {
997998
namespaceForType = this.targetNamespace || undefined;
998999
}
9991000

1000-
if (namespaceForType === 'http://www.w3.org/2001/XMLSchema') {
1001+
if (namespaceForType === Constants.XML_SCHEMA_NS || namespaceForType === Constants.XML_SCHEMA_NS_SECURE) {
10011002
continue;
10021003
}
10031004

0 commit comments

Comments
 (0)