1212
1313import { existsSync } from "fs" ;
1414import * as path from "node:path" ;
15- import { ContentHandler } from "./ContentHandler" ;
1615import { DOMBuilder } from "./DOMBuilder" ;
1716import { SAXParser } from "./SAXParser" ;
17+ import { XMLAttribute } from "./XMLAttribute" ;
1818import { XMLDocument } from "./XMLDocument" ;
1919import { XMLElement } from "./XMLElement" ;
2020import { XMLUtils } from "./XMLUtils" ;
@@ -48,12 +48,18 @@ export class Catalog {
4848 this . workDir = path . dirname ( catalogFile ) ;
4949 this . base = '' ;
5050
51- let contentHandler : ContentHandler = new DOMBuilder ( ) ;
51+ let contentHandler : DOMBuilder = new DOMBuilder ( ) ;
5252 let parser : SAXParser = new SAXParser ( ) ;
5353 parser . setContentHandler ( contentHandler ) ;
5454 parser . parseFile ( catalogFile ) ;
55- let catalogDocument : XMLDocument = ( contentHandler as DOMBuilder ) . getDocument ( ) ;
56- let catalogRoot : XMLElement = catalogDocument . getRoot ( ) ;
55+ let catalogDocument : XMLDocument | undefined = contentHandler . getDocument ( ) ;
56+ if ( ! catalogDocument ) {
57+ throw new Error ( 'Catalog file ' + catalogFile + ' is empty' ) ;
58+ }
59+ let catalogRoot : XMLElement | undefined = catalogDocument . getRoot ( ) ;
60+ if ( ! catalogRoot ) {
61+ throw new Error ( 'Catalog file ' + catalogFile + ' is empty' ) ;
62+ }
5763 if ( catalogRoot . getName ( ) !== 'catalog' ) {
5864 throw new Error ( 'Catalog root element must be <catalog>' ) ;
5965 }
@@ -63,8 +69,9 @@ export class Catalog {
6369 recurse ( catalogRoot : XMLElement ) {
6470 for ( let child of catalogRoot . getChildren ( ) ) {
6571 let currentBase : string = this . base ;
66- if ( child . hasAttribute ( 'xml:base' ) && child . getAttribute ( "xml:base" ) . getValue ( ) !== '' ) {
67- this . base = child . getAttribute ( "xml:base" ) . getValue ( ) ;
72+ let xmlBase : XMLAttribute | undefined = child . getAttribute ( "xml:base" ) ;
73+ if ( xmlBase ) {
74+ this . base = xmlBase . getValue ( ) ;
6875 if ( ! this . base . endsWith ( '/' ) ) {
6976 this . base += '/' ;
7077 }
@@ -76,12 +83,20 @@ export class Catalog {
7683 }
7784 }
7885 if ( child . getName ( ) === 'public' ) {
79- let publicId : string = child . getAttribute ( "publicId" ) . getValue ( ) ;
86+ let publicIdAttribute : XMLAttribute | undefined = child . getAttribute ( "publicId" ) ;
87+ if ( ! publicIdAttribute ) {
88+ throw new Error ( 'publicId attribute is required for <public>' ) ;
89+ }
90+ let publicId : string = publicIdAttribute . getValue ( ) ;
8091 if ( publicId . startsWith ( "urn:publicid:" ) ) {
8192 publicId = this . unwrapUrn ( publicId ) ;
8293 }
8394 if ( ! this . publicCatalog . has ( publicId ) ) {
84- let uri : string = this . makeAbsolute ( child . getAttribute ( "uri" ) . getValue ( ) ) ;
95+ let uriAttribute : XMLAttribute | undefined = child . getAttribute ( "uri" ) ;
96+ if ( ! uriAttribute ) {
97+ throw new Error ( 'uri attribute is required for <public>' ) ;
98+ }
99+ let uri : string = this . makeAbsolute ( uriAttribute . getValue ( ) ) ;
85100 if ( existsSync ( uri ) ) {
86101 this . publicCatalog . set ( publicId , uri ) ;
87102 if ( uri . endsWith ( ".dtd" ) || uri . endsWith ( ".ent" ) || uri . endsWith ( ".mod" ) ) {
@@ -94,9 +109,17 @@ export class Catalog {
94109 }
95110 }
96111 if ( child . getName ( ) === 'system' ) {
97- let uri : string = this . makeAbsolute ( child . getAttribute ( "uri" ) . getValue ( ) ) ;
112+ let uriAttribute : XMLAttribute | undefined = child . getAttribute ( "uri" ) ;
113+ if ( ! uriAttribute ) {
114+ throw new Error ( 'uri attribute is required for <system>' ) ;
115+ }
116+ let uri : string = this . makeAbsolute ( uriAttribute . getValue ( ) ) ;
98117 if ( existsSync ( uri ) ) {
99- this . systemCatalog . set ( child . getAttribute ( "systemId" ) . getValue ( ) , uri ) ;
118+ let systemId : XMLAttribute | undefined = child . getAttribute ( "systemId" ) ;
119+ if ( ! systemId ) {
120+ throw new Error ( 'systemId attribute is required for <system>' ) ;
121+ }
122+ this . systemCatalog . set ( systemId . getValue ( ) , uri ) ;
100123 if ( uri . endsWith ( ".dtd" ) ) {
101124 let name : string = path . basename ( uri ) ;
102125 if ( ! this . dtdCatalog . has ( name ) ) {
@@ -106,9 +129,17 @@ export class Catalog {
106129 }
107130 }
108131 if ( child . getName ( ) === 'uri' ) {
109- let uri : string = this . makeAbsolute ( child . getAttribute ( "uri" ) . getValue ( ) ) ;
132+ let uriAttribute : XMLAttribute | undefined = child . getAttribute ( "uri" ) ;
133+ if ( ! uriAttribute ) {
134+ throw new Error ( 'uri attribute is required for <uri>' ) ;
135+ }
136+ let uri : string = this . makeAbsolute ( uriAttribute . getValue ( ) ) ;
110137 if ( existsSync ( uri ) ) {
111- this . uriCatalog . set ( child . getAttribute ( "name" ) . getValue ( ) , uri ) ;
138+ let nameAttribute : XMLAttribute | undefined = child . getAttribute ( "name" ) ;
139+ if ( ! nameAttribute ) {
140+ throw new Error ( 'name attribute is required for <uri>' ) ;
141+ }
142+ this . uriCatalog . set ( nameAttribute . getValue ( ) , uri ) ;
112143 if ( uri . endsWith ( ".dtd" ) || uri . endsWith ( ".ent" ) || uri . endsWith ( ".mod" ) ) {
113144 let name : string = path . basename ( uri ) ;
114145 if ( ! this . dtdCatalog . has ( name ) ) {
@@ -118,21 +149,41 @@ export class Catalog {
118149 }
119150 }
120151 if ( child . getName ( ) === 'rewriteURI' ) {
121- let uri : string = this . makeAbsolute ( child . getAttribute ( "rewritePrefix" ) . getValue ( ) ) ;
122- let pair : string [ ] = [ child . getAttribute ( "uriStartString" ) . getValue ( ) , uri ] ;
152+ let rewritePrefix : XMLAttribute | undefined = child . getAttribute ( "rewritePrefix" ) ;
153+ if ( ! rewritePrefix ) {
154+ throw new Error ( 'rewritePrefix attribute is required for <rewriteURI>' ) ;
155+ }
156+ let uri : string = this . makeAbsolute ( rewritePrefix . getValue ( ) ) ;
157+ let uriStartString : XMLAttribute | undefined = child . getAttribute ( "uriStartString" ) ;
158+ if ( ! uriStartString ) {
159+ throw new Error ( 'uriStartString attribute is required for <rewriteURI>' ) ;
160+ }
161+ let pair : string [ ] = [ uriStartString . getValue ( ) , uri ] ;
123162 if ( ! this . uriRewrites . includes ( pair ) ) {
124163 this . uriRewrites . push ( pair ) ;
125164 }
126165 }
127166 if ( child . getName ( ) === 'rewriteSystem' ) {
128- let uri : string = this . makeAbsolute ( child . getAttribute ( "rewritePrefix" ) . getValue ( ) ) ;
129- let pair : string [ ] = [ child . getAttribute ( "systemIdStartString" ) . getValue ( ) , uri ] ;
167+ let rewritePrefix : XMLAttribute | undefined = child . getAttribute ( "rewritePrefix" ) ;
168+ if ( ! rewritePrefix ) {
169+ throw new Error ( 'rewritePrefix attribute is required for <rewriteSystem>' ) ;
170+ }
171+ let uri : string = this . makeAbsolute ( rewritePrefix . getValue ( ) ) ;
172+ let systemIdStartString : XMLAttribute | undefined = child . getAttribute ( "systemIdStartString" ) ;
173+ if ( ! systemIdStartString ) {
174+ throw new Error ( 'systemIdStartString attribute is required for <rewriteSystem>' ) ;
175+ }
176+ let pair : string [ ] = [ systemIdStartString . getValue ( ) , uri ] ;
130177 if ( ! this . systemRewrites . includes ( pair ) ) {
131178 this . systemRewrites . push ( pair ) ;
132179 }
133180 }
134181 if ( child . getName ( ) === 'nextCatalog' ) {
135- let nextCatalog : string = this . makeAbsolute ( child . getAttribute ( "catalog" ) . getValue ( ) ) ;
182+ let catalogAttribute : XMLAttribute | undefined = child . getAttribute ( "catalog" ) ;
183+ if ( ! catalogAttribute ) {
184+ throw new Error ( 'catalog attribute is required for <nextCatalog>' ) ;
185+ }
186+ let nextCatalog : string = this . makeAbsolute ( catalogAttribute . getValue ( ) ) ;
136187 let catalog : Catalog = new Catalog ( nextCatalog ) ;
137188 let map : Map < string , string > = catalog . getSystemCatalog ( ) ;
138189 map . forEach ( ( value , key ) => {
@@ -229,21 +280,17 @@ export class Catalog {
229280 return this . systemRewrites ;
230281 }
231282
232- resolveEntity ( publicId : string , systemId : string ) : string {
283+ resolveEntity ( publicId : string , systemId : string ) : string | undefined {
233284 if ( publicId ) {
234- let location : string = this . matchPublic ( publicId ) ;
285+ let location : string | undefined = this . matchPublic ( publicId ) ;
235286 if ( location ) {
236287 return location ;
237288 }
238289 }
239- let location : string = this . matchSystem ( systemId ) ;
240- if ( location ) {
241- return location ;
242- }
243- return undefined ;
290+ return this . matchSystem ( systemId ) ;
244291 }
245292
246- matchSystem ( systemId : string ) : string {
293+ matchSystem ( systemId : string ) : string | undefined {
247294 if ( systemId ) {
248295 for ( let pair of this . systemRewrites ) {
249296 if ( systemId . startsWith ( pair [ 0 ] ) ) {
@@ -261,7 +308,7 @@ export class Catalog {
261308 return undefined ;
262309 }
263310
264- matchPublic ( publicId : string ) : string {
311+ matchPublic ( publicId : string ) : string | undefined {
265312 if ( publicId . startsWith ( "urn:publicid:" ) ) {
266313 publicId = this . unwrapUrn ( publicId ) ;
267314 }
0 commit comments