UEA-Prodem

This commit is contained in:
2026-06-10 12:38:42 -03:00
parent 3f33154e16
commit c41625e542
9352 changed files with 1128292 additions and 14752 deletions
+131
View File
@@ -0,0 +1,131 @@
'use strict';
var xmlParser = require('./xml-parser');
const ATTR_ESCAPE_RE = /[&<>"]/g;
const ATTR_ESCAPE_MAP = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
};
function escapeAttribute(value) {
return value.replace(ATTR_ESCAPE_RE, (ch) => ATTR_ESCAPE_MAP[ch]);
}
const ELEMENT_ESCAPE_RE = /[&"'<>\r\n\u0085\u2028]/g;
const ELEMENT_ESCAPE_MAP = {
"&": "&amp;",
'"': "&quot;",
"'": "&apos;",
"<": "&lt;",
">": "&gt;",
"\r": "&#x0D;",
"\n": "&#x0A;",
"\u0085": "&#x85;",
"\u2028": "&#x2028;",
};
function escapeElement(value) {
return value.replace(ELEMENT_ESCAPE_RE, (ch) => ELEMENT_ESCAPE_MAP[ch]);
}
class XmlText {
value;
constructor(value) {
this.value = value;
}
toString() {
return escapeElement("" + this.value);
}
}
class XmlNode {
name;
children;
attributes = {};
static of(name, childText, withName) {
const node = new XmlNode(name);
if (childText !== undefined) {
node.addChildNode(new XmlText(childText));
}
if (withName !== undefined) {
node.withName(withName);
}
return node;
}
constructor(name, children = []) {
this.name = name;
this.children = children;
}
withName(name) {
this.name = name;
return this;
}
addAttribute(name, value) {
this.attributes[name] = value;
return this;
}
addChildNode(child) {
this.children.push(child);
return this;
}
removeAttribute(name) {
delete this.attributes[name];
return this;
}
n(name) {
this.name = name;
return this;
}
c(child) {
this.children.push(child);
return this;
}
a(name, value) {
if (value != null) {
this.attributes[name] = value;
}
return this;
}
cc(input, field, withName = field) {
if (input[field] != null) {
const node = XmlNode.of(field, input[field]).withName(withName);
this.c(node);
}
}
l(input, listName, memberName, valueProvider) {
if (input[listName] != null) {
const nodes = valueProvider();
nodes.map((node) => {
node.withName(memberName);
this.c(node);
});
}
}
lc(input, listName, memberName, valueProvider) {
if (input[listName] != null) {
const nodes = valueProvider();
const containerNode = new XmlNode(memberName);
nodes.map((node) => {
containerNode.c(node);
});
this.c(containerNode);
}
}
toString() {
const hasChildren = Boolean(this.children.length);
let xmlText = `<${this.name}`;
const attributes = this.attributes;
for (const attributeName of Object.keys(attributes)) {
const attribute = attributes[attributeName];
if (attribute != null) {
xmlText += ` ${attributeName}="${escapeAttribute("" + attribute)}"`;
}
}
return (xmlText += !hasChildren ? "/>" : `>${this.children.map((c) => c.toString()).join("")}</${this.name}>`);
}
}
exports.parseXML = xmlParser.parseXML;
exports.XmlNode = XmlNode;
exports.XmlText = XmlText;
@@ -0,0 +1,336 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityDecoderImpl = exports.CURRENCY = exports.COMMON_HTML = exports.XML = void 0;
exports.XML = {
amp: "&",
apos: "'",
gt: ">",
lt: "<",
quot: '"',
};
exports.COMMON_HTML = {
nbsp: "\u00a0",
copy: "\u00a9",
reg: "\u00ae",
trade: "\u2122",
mdash: "\u2014",
ndash: "\u2013",
hellip: "\u2026",
laquo: "\u00ab",
raquo: "\u00bb",
lsquo: "\u2018",
rsquo: "\u2019",
ldquo: "\u201c",
rdquo: "\u201d",
bull: "\u2022",
para: "\u00b6",
sect: "\u00a7",
deg: "\u00b0",
frac12: "\u00bd",
frac14: "\u00bc",
frac34: "\u00be",
};
exports.CURRENCY = {
cent: "\u00a2",
pound: "\u00a3",
curren: "\u00a4",
yen: "\u00a5",
euro: "\u20ac",
dollar: "$",
fnof: "\u0192",
inr: "\u20b9",
af: "\u060b",
birr: "\u1265\u122d",
peso: "\u20b1",
rub: "\u20bd",
won: "\u20a9",
yuan: "\u00a5",
cedil: "\u00b8",
};
const SPECIAL_CHARS = new Set("!?\\/[]$%{}^&*()<>|+");
function validateEntityName(name) {
if (name[0] === "#") {
throw new Error(`[EntityReplacer] Invalid character '#' in entity name: "${name}"`);
}
for (const ch of name) {
if (SPECIAL_CHARS.has(ch)) {
throw new Error(`[EntityReplacer] Invalid character '${ch}' in entity name: "${name}"`);
}
}
return name;
}
function mergeEntityMaps(...maps) {
const out = Object.create(null);
for (const map of maps) {
if (!map) {
continue;
}
for (const key of Object.keys(map)) {
const raw = map[key];
if (typeof raw === "string") {
out[key] = raw;
}
else if (raw && typeof raw === "object" && raw.val !== undefined) {
const val = raw.val;
if (typeof val === "string") {
out[key] = val;
}
}
}
}
return out;
}
const LIMIT_TIER_EXTERNAL = "external";
const LIMIT_TIER_BASE = "base";
const LIMIT_TIER_ALL = "all";
function parseLimitTiers(raw) {
if (!raw || raw === LIMIT_TIER_EXTERNAL) {
return new Set([LIMIT_TIER_EXTERNAL]);
}
if (raw === LIMIT_TIER_ALL) {
return new Set([LIMIT_TIER_ALL]);
}
if (raw === LIMIT_TIER_BASE) {
return new Set([LIMIT_TIER_BASE]);
}
if (Array.isArray(raw)) {
return new Set(raw);
}
return new Set([LIMIT_TIER_EXTERNAL]);
}
const NCR_LEVEL = Object.freeze({ allow: 0, leave: 1, remove: 2, throw: 3 });
const XML10_ALLOWED_C0 = new Set([0x09, 0x0a, 0x0d]);
function parseNCRConfig(ncr) {
if (!ncr) {
return { xmlVersion: 1.0, onLevel: NCR_LEVEL.allow, nullLevel: NCR_LEVEL.remove };
}
const xmlVersion = ncr.xmlVersion === 1.1 ? 1.1 : 1.0;
const onLevel = NCR_LEVEL[ncr.onNCR ?? "allow"] ?? NCR_LEVEL.allow;
const nullLevel = NCR_LEVEL[ncr.nullNCR ?? "remove"] ?? NCR_LEVEL.remove;
const clampedNull = Math.max(nullLevel, NCR_LEVEL.remove);
return { xmlVersion, onLevel, nullLevel: clampedNull };
}
const EntityDecoderImpl = class EntityDecoderImpl {
_limit;
_maxTotalExpansions;
_maxExpandedLength;
_postCheck;
_limitTiers;
_numericAllowed;
_baseMap;
_externalMap;
_inputMap;
_totalExpansions;
_expandedLength;
_removeSet;
_leaveSet;
_ncrXmlVersion;
_ncrOnLevel;
_ncrNullLevel;
constructor(options = {}) {
this._limit = options.limit || {};
this._maxTotalExpansions = this._limit.maxTotalExpansions || 0;
this._maxExpandedLength = this._limit.maxExpandedLength || 0;
this._postCheck = typeof options.postCheck === "function" ? options.postCheck : (r) => r;
this._limitTiers = parseLimitTiers(this._limit.applyLimitsTo ?? LIMIT_TIER_EXTERNAL);
this._numericAllowed = options.numericAllowed ?? true;
this._baseMap = mergeEntityMaps(exports.XML, options.namedEntities || null);
this._externalMap = Object.create(null);
this._inputMap = Object.create(null);
this._totalExpansions = 0;
this._expandedLength = 0;
this._removeSet = new Set(options.remove && Array.isArray(options.remove) ? options.remove : []);
this._leaveSet = new Set(options.leave && Array.isArray(options.leave) ? options.leave : []);
const ncrCfg = parseNCRConfig(options.ncr);
this._ncrXmlVersion = ncrCfg.xmlVersion;
this._ncrOnLevel = ncrCfg.onLevel;
this._ncrNullLevel = ncrCfg.nullLevel;
}
setExternalEntities(map) {
if (map) {
for (const key of Object.keys(map)) {
validateEntityName(key);
}
}
this._externalMap = mergeEntityMaps(map);
}
addExternalEntity(key, value) {
validateEntityName(key);
if (typeof value === "string" && value.indexOf("&") === -1) {
this._externalMap[key] = value;
}
}
addInputEntities(map) {
this._totalExpansions = 0;
this._expandedLength = 0;
this._inputMap = mergeEntityMaps(map);
}
reset() {
this._inputMap = Object.create(null);
this._totalExpansions = 0;
this._expandedLength = 0;
return this;
}
setXmlVersion(version) {
this._ncrXmlVersion = version === "1.1" || version === 1.1 ? 1.1 : 1.0;
}
decode(str) {
if (typeof str !== "string" || str.length === 0) {
return str;
}
const original = str;
const chunks = [];
const len = str.length;
let last = 0;
let i = 0;
const limitExpansions = this._maxTotalExpansions > 0;
const limitLength = this._maxExpandedLength > 0;
const checkLimits = limitExpansions || limitLength;
while (i < len) {
if (str.charCodeAt(i) !== 38) {
i++;
continue;
}
let j = i + 1;
while (j < len && str.charCodeAt(j) !== 59 && j - i <= 32) {
j++;
}
if (j >= len || str.charCodeAt(j) !== 59) {
i++;
continue;
}
const token = str.slice(i + 1, j);
if (token.length === 0) {
i++;
continue;
}
let replacement;
let tier;
if (this._removeSet.has(token)) {
replacement = "";
if (tier === undefined) {
tier = LIMIT_TIER_EXTERNAL;
}
}
else if (this._leaveSet.has(token)) {
i++;
continue;
}
else if (token.charCodeAt(0) === 35) {
const ncrResult = this._resolveNCR(token);
if (ncrResult === undefined) {
i++;
continue;
}
replacement = ncrResult;
tier = LIMIT_TIER_BASE;
}
else {
const resolved = this._resolveName(token);
replacement = resolved?.value;
tier = resolved?.tier;
}
if (replacement === undefined) {
i++;
continue;
}
if (i > last) {
chunks.push(str.slice(last, i));
}
chunks.push(replacement);
last = j + 1;
i = last;
if (checkLimits && this._tierCounts(tier)) {
if (limitExpansions) {
this._totalExpansions++;
if (this._totalExpansions > this._maxTotalExpansions) {
throw new Error(`[EntityReplacer] Entity expansion count limit exceeded: ` +
`${this._totalExpansions} > ${this._maxTotalExpansions}`);
}
}
if (limitLength) {
const delta = replacement.length - (token.length + 2);
if (delta > 0) {
this._expandedLength += delta;
if (this._expandedLength > this._maxExpandedLength) {
throw new Error(`[EntityReplacer] Expanded content length limit exceeded: ` +
`${this._expandedLength} > ${this._maxExpandedLength}`);
}
}
}
}
}
if (last < len) {
chunks.push(str.slice(last));
}
const result = chunks.length === 0 ? str : chunks.join("");
return this._postCheck(result, original);
}
_tierCounts(tier) {
if (this._limitTiers.has(LIMIT_TIER_ALL)) {
return true;
}
return this._limitTiers.has(tier);
}
_resolveName(name) {
if (name in this._inputMap) {
return { value: this._inputMap[name], tier: LIMIT_TIER_EXTERNAL };
}
if (name in this._externalMap) {
return { value: this._externalMap[name], tier: LIMIT_TIER_EXTERNAL };
}
if (name in this._baseMap) {
return { value: this._baseMap[name], tier: LIMIT_TIER_BASE };
}
return undefined;
}
_classifyNCR(cp) {
if (cp === 0) {
return this._ncrNullLevel;
}
if (cp >= 0xd800 && cp <= 0xdfff) {
return NCR_LEVEL.remove;
}
if (this._ncrXmlVersion === 1.0) {
if (cp >= 0x01 && cp <= 0x1f && !XML10_ALLOWED_C0.has(cp)) {
return NCR_LEVEL.remove;
}
}
return -1;
}
_applyNCRAction(action, token, cp) {
switch (action) {
case NCR_LEVEL.allow:
return String.fromCodePoint(cp);
case NCR_LEVEL.remove:
return "";
case NCR_LEVEL.leave:
return undefined;
case NCR_LEVEL.throw:
throw new Error(`[EntityDecoder] Prohibited numeric character reference ` +
`&${token}; (U+${cp.toString(16).toUpperCase().padStart(4, "0")})`);
default:
return String.fromCodePoint(cp);
}
}
_resolveNCR(token) {
const second = token.charCodeAt(1);
let cp;
if (second === 120 || second === 88) {
cp = parseInt(token.slice(2), 16);
}
else {
cp = parseInt(token.slice(1), 10);
}
if (Number.isNaN(cp) || cp < 0 || cp > 0x10ffff) {
return undefined;
}
const minimum = this._classifyNCR(cp);
if (!this._numericAllowed && minimum < NCR_LEVEL.remove) {
return undefined;
}
const effective = minimum === -1 ? this._ncrOnLevel : Math.max(this._ncrOnLevel, minimum);
return this._applyNCRAction(effective, token, cp);
}
};
exports.EntityDecoderImpl = EntityDecoderImpl;
+60
View File
@@ -0,0 +1,60 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseXML = parseXML;
let parser;
function parseXML(xmlString) {
if (!parser) {
parser = new DOMParser();
}
const xmlDocument = parser.parseFromString(xmlString, "application/xml");
if (xmlDocument.getElementsByTagName("parsererror").length > 0) {
throw new Error("DOMParser XML parsing error.");
}
const xmlToObj = (node) => {
if (node.nodeType === Node.TEXT_NODE) {
if (node.textContent?.trim()) {
return node.textContent;
}
}
if (node.nodeType === Node.ELEMENT_NODE) {
const element = node;
if (element.attributes.length === 0 && element.childNodes.length === 0) {
return "";
}
const obj = {};
const attributes = Array.from(element.attributes);
for (const attr of attributes) {
obj[`${attr.name}`] = attr.value;
}
const childNodes = Array.from(element.childNodes);
for (const child of childNodes) {
const childResult = xmlToObj(child);
if (childResult != null) {
const childName = child.nodeName;
if (childNodes.length === 1 && attributes.length === 0 && childName === "#text") {
return childResult;
}
if (obj[childName]) {
if (Array.isArray(obj[childName])) {
obj[childName].push(childResult);
}
else {
obj[childName] = [obj[childName], childResult];
}
}
else {
obj[childName] = childResult;
}
}
else if (childNodes.length === 1 && attributes.length === 0) {
return element.textContent;
}
}
return obj;
}
return null;
};
return {
[xmlDocument.documentElement.nodeName]: xmlToObj(xmlDocument.documentElement),
};
}
+47
View File
@@ -0,0 +1,47 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseXML = parseXML;
const fast_xml_parser_1 = require("fast-xml-parser");
const nodable_entities_1 = require("./xml-external/nodable_entities");
const entityDecoder = new nodable_entities_1.EntityDecoderImpl({
namedEntities: { ...nodable_entities_1.XML, ...nodable_entities_1.COMMON_HTML, ...nodable_entities_1.CURRENCY },
numericAllowed: true,
limit: {
maxTotalExpansions: Infinity,
},
ncr: {
xmlVersion: 1.1,
},
});
const parser = new fast_xml_parser_1.XMLParser({
attributeNamePrefix: "",
processEntities: {
enabled: true,
maxTotalExpansions: Infinity,
},
htmlEntities: true,
entityDecoder: {
setExternalEntities: (entities) => {
entityDecoder.setExternalEntities(entities);
},
addInputEntities: (entities) => {
entityDecoder.addInputEntities(entities);
},
reset: () => {
entityDecoder.reset();
},
decode: (text) => {
return entityDecoder.decode(text);
},
setXmlVersion: (version) => void {},
},
ignoreAttributes: false,
ignoreDeclaration: true,
parseTagValue: false,
trimValues: false,
tagValueProcessor: (_, val) => (val.trim() === "" && val.includes("\n") ? "" : undefined),
maxNestedTags: Infinity,
});
function parseXML(xmlString) {
return parser.parse(xmlString, true);
}