typesxml
Version:
Open source XML library written in TypeScript
60 lines • 2.04 kB
JavaScript
/*******************************************************************************
* Copyright (c) 2023-2026 Maxprograms.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-v10.html
*
* Contributors:
* Maxprograms - initial API and implementation
*******************************************************************************/
import { XMLUtils } from '../XMLUtils.js';
import { DTDElementNameParticle } from './DTDElementNameParticle.js';
export class DTDChoiceModel {
cardinality = '';
choices = [];
constructor(choices) {
if (choices) {
this.choices = choices;
}
}
getType() {
return 'choice';
}
addChoice(choice) {
this.choices.push(choice);
}
getChoices() {
return this.choices;
}
validate() {
// Cardinality must be '', '*', '+', or '?'
if (this.cardinality && !['*', '+', '?'].includes(this.cardinality)) {
return false;
}
// At least one choice required for valid model
if (this.choices.length === 0) {
return false;
}
// Validate each choice and its name if it's an element particle
for (const choice of this.choices) {
if (!choice.validate()) {
return false;
}
// Only check name if choice is DTDElementNameParticle
if (choice instanceof DTDElementNameParticle) {
const name = choice.getName();
if (name !== '#PCDATA' && !XMLUtils.isValidXMLName(name)) {
return false;
}
}
}
return true;
}
toBNF() {
let bnf = this.choices.map(choice => choice.toBNF()).join(' | ');
return bnf + (this.cardinality ? this.cardinality : '');
}
}
//# sourceMappingURL=DTDChoiceModel.js.map