UNPKG

typesxml

Version:

Open source XML library written in TypeScript

130 lines 4.26 kB
"use strict"; /******************************************************************************* * 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 *******************************************************************************/ Object.defineProperty(exports, "__esModule", { value: true }); exports.SchemaAttributeDecl = void 0; const Grammar_js_1 = require("../grammar/Grammar.js"); const SchemaTypeValidator_js_1 = require("./SchemaTypeValidator.js"); class SchemaAttributeDecl { name; namespace; type; use; defaultValue; fixedValue; facets; unionAlternatives; constructor(name, type = 'string', use = Grammar_js_1.AttributeUse.OPTIONAL, defaultValue, fixedValue, namespace) { this.name = name; this.namespace = namespace; this.type = type; this.use = use; this.defaultValue = defaultValue; this.fixedValue = fixedValue; this.facets = {}; this.unionAlternatives = []; } getName() { return this.name; } getNamespace() { return this.namespace; } getType() { return this.type; } getUse() { return this.use; } getDefaultValue() { return this.defaultValue; } getFixedValue() { return this.fixedValue; } setEnumeration(values) { this.facets.enumeration = values.slice(); } getEnumeration() { return this.facets.enumeration || []; } setPatterns(values) { this.facets.patterns = values.slice(); } setMinInclusive(value) { this.facets.minInclusive = value; } setMaxInclusive(value) { this.facets.maxInclusive = value; } setMinExclusive(value) { this.facets.minExclusive = value; } setMaxExclusive(value) { this.facets.maxExclusive = value; } setLength(value) { this.facets.length = value; } setMinLength(value) { this.facets.minLength = value; } setMaxLength(value) { this.facets.maxLength = value; } setTotalDigits(value) { this.facets.totalDigits = value; } setFractionDigits(value) { this.facets.fractionDigits = value; } setWhiteSpace(value) { this.facets.whiteSpace = value; } setIsList(value) { this.facets.isList = value; } setUnionAlternatives(alts) { this.unionAlternatives = alts.slice(); } isValid(value) { if (this.fixedValue !== undefined && value !== this.fixedValue) { return false; } if (this.unionAlternatives.length > 0) { for (let i = 0; i < this.unionAlternatives.length; i++) { const alt = this.unionAlternatives[i]; if (SchemaTypeValidator_js_1.SchemaTypeValidator.validate(value, alt.baseType) && SchemaTypeValidator_js_1.SchemaTypeValidator.validateFacets(value, alt.facets, alt.baseType)) { return true; } } return false; } if (!SchemaTypeValidator_js_1.SchemaTypeValidator.validateFacets(value, this.facets, this.type)) { return false; } let normalized = value; const effectiveWhiteSpace = this.facets.whiteSpace ?? SchemaTypeValidator_js_1.SchemaTypeValidator.getBuiltInWhiteSpace(this.type); if (effectiveWhiteSpace === 'replace') { normalized = value.replaceAll(/[\t\n\r]/g, ' '); } else if (effectiveWhiteSpace === 'collapse') { normalized = value.replaceAll(/[\t\n\r ]+/g, ' ').trim(); } return SchemaTypeValidator_js_1.SchemaTypeValidator.validate(normalized, this.type); } toAttributeInfo() { return new Grammar_js_1.AttributeInfo(this.name, this.type, this.use, this.defaultValue, this.fixedValue, this.namespace); } } exports.SchemaAttributeDecl = SchemaAttributeDecl; //# sourceMappingURL=SchemaAttributeDecl.js.map