UNPKG

ern-api-gen

Version:

Electrode Native API generator

211 lines 8.24 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ModelImpl_1 = __importDefault(require("../models/ModelImpl")); const RefModel_1 = __importDefault(require("../models/RefModel")); const properties_1 = require("../models/properties"); const StringUtils_1 = __importDefault(require("../java/StringUtils")); const javaUtil_1 = require("../java/javaUtil"); const AbstractModel_1 = __importDefault(require("../models/AbstractModel")); const StringBuilder_1 = __importDefault(require("../java/StringBuilder")); class XmlExampleGenerator { constructor(examples) { this.examples = examples; if (examples == null) { this.examples = javaUtil_1.newHashMap(); } } modelImplToXml(model, indent, path) { const modelName = model.getName(); if (path.contains(modelName)) { return XmlExampleGenerator.EMPTY; } const selfPath = javaUtil_1.newHashSet(...path); selfPath.add(modelName); const sb = StringBuilder_1.default(); const attributes = javaUtil_1.newHashMap(); const elements = javaUtil_1.newHashMap(); let name = modelName; const xml = model.getXml(); if (xml != null) { if (xml.getName() != null) { name = xml.getName(); } } if (model.getProperties() != null) { for (const [pName, p] of model.getProperties()) { if (p != null && p.getXml() != null && p.getXml().getAttribute() != null && p.getXml().getAttribute()) { attributes.put(pName, p); } else { elements.put(pName, p); } } } sb.append(this.indent(indent)).append(XmlExampleGenerator.TAG_START); sb.append(name); for (const [p, pName] of attributes) { sb.append(' ') .append(pName) .append('=') .append(this.quote(this.toXml(null, p, 0, selfPath))); } sb.append(XmlExampleGenerator.CLOSE_TAG); sb.append(XmlExampleGenerator.NEWLINE); for (const [pName, p] of elements) { const asXml = this.toXml(pName, p, indent + 1, selfPath); if (StringUtils_1.default.isEmpty(asXml)) { continue; } sb.append(asXml); sb.append(XmlExampleGenerator.NEWLINE); } sb.append(this.indent(indent)) .append(XmlExampleGenerator.TAG_END) .append(name) .append(XmlExampleGenerator.CLOSE_TAG); return sb.toString(); } quote(str) { return '"' + str + '"'; } toXml(name, property, indent, path) { if ((typeof name === 'string' || name === null) && (property instanceof properties_1.Property || property === null) && (typeof indent === 'number' || indent === null) && (path === null || 'size' in path)) { if (property == null) { return ''; } const sb = StringBuilder_1.default(); if (property != null && property instanceof properties_1.ArrayProperty) { const p = property; const inner = p.getItems(); let wrapped = false; if (property.getXml() != null && property.getXml().getWrapped() != null && property.getXml().getWrapped()) { wrapped = true; } if (wrapped) { let prefix = XmlExampleGenerator.EMPTY; if (name != null) { sb.append(this.indent(indent)); sb.append(this.openTag(name)); prefix = XmlExampleGenerator.NEWLINE; } const asXml = this.toXml(name, inner, indent + 1, path); if (StringUtils_1.default.isNotEmpty(asXml)) { sb.append(prefix).append(asXml); } if (name != null) { sb.append(XmlExampleGenerator.NEWLINE); sb.append(this.indent(indent)); sb.append(this.closeTag(name)); } } else { sb.append(this.toXml(name, inner, indent, path)); } } else if (property != null && property instanceof properties_1.RefProperty) { const ref = property; const actualModel = this.examples.get(ref.getSimpleRef()); sb.append(this.toXml(actualModel, indent, path)); } else { if (name != null) { sb.append(this.indent(indent)); sb.append(this.openTag(name)); } sb.append(this.getExample(property)); if (name != null) { sb.append(this.closeTag(name)); } } return sb.toString(); } else if (name instanceof properties_1.Property) { return this.toXml(null, name, 0, javaUtil_1.Collections.emptySet()); } else if (name instanceof AbstractModel_1.default) { const model = name; if (model != null && model instanceof RefModel_1.default) { const ref = model; const actualModel = this.examples.get(ref.getSimpleRef()); if (actualModel != null && actualModel instanceof ModelImpl_1.default) { return this.modelImplToXml(actualModel, indent, path); } } else if (model != null && model instanceof ModelImpl_1.default) { return this.modelImplToXml(model, property, indent); } return null; } throw new Error(`unknown overload`); } getExample(property) { if (property instanceof properties_1.DateTimeProperty) { if (property.getExample() != null) { return property.getExample().toString(); } return '2000-01-23T04:56:07.000Z'; } if (property instanceof properties_1.StringProperty) { if (property.getExample() != null) { return property.getExample().toString(); } return 'string'; } if (property instanceof properties_1.DateProperty) { if (property.getExample() != null) { return property.getExample().toString(); } return '2000-01-23T04:56:07.000Z'; } if (property instanceof properties_1.IntegerProperty) { if (property.getExample() != null) { return property.getExample().toString(); } return '0'; } if (property instanceof properties_1.BooleanProperty) { if (property.getExample() != null) { return property.getExample().toString(); } return 'true'; } if (property instanceof properties_1.LongProperty) { if (property.getExample() != null) { return property.getExample().toString(); } return '123456'; } return 'not implemented ' + property; } openTag(name) { return '<' + name + '>'; } closeTag(name) { return '</' + name + '>'; } indent(indent) { const sb = StringBuilder_1.default(); for (let i = 0; i < indent; i++) { sb.append(' '); } return sb.toString(); } } XmlExampleGenerator.NEWLINE = '\n'; XmlExampleGenerator.TAG_START = '<'; XmlExampleGenerator.CLOSE_TAG = '>'; XmlExampleGenerator.TAG_END = '</'; XmlExampleGenerator.EMPTY = ''; exports.default = XmlExampleGenerator; //# sourceMappingURL=XmlExampleGenerator.js.map