UNPKG

@yellicode/elements

Version:

The meta model API for Yellicode - an extensible code generator.

70 lines (69 loc) 3.65 kB
import { DataToModelConverter } from './data-to-model-converter'; import { ElementTypeUtility } from './utils'; import { Document } from './document'; import { ModelDelegateImpl } from './model-delegate'; import { ElementMapImpl } from './element-map'; import { ElementComparerImpl } from './element-comparer'; var ModelReader = /** @class */ (function () { function ModelReader() { /** * Contains all profiles that were read from the main document or any referenced documents. */ this.allProfiles = []; this.elementMap = new ElementMapImpl(); this.modelDelegate = new ModelDelegateImpl(this.elementMap); this.converter = new DataToModelConverter(this.elementMap, this.modelDelegate, ElementComparerImpl.getInstance()); } ModelReader.canRead = function (data) { if (!data.modelTypeName) return false; return ("yellicode yml" === data.modelTypeName.toLowerCase()); }; ModelReader.readDocument = function (documentData) { var instance = new ModelReader(); return instance.readDocumentRecursive(documentData, false); }; ModelReader.prototype.readDocumentRecursive = function (documentData, isReferencedDocument) { // Obsolete: Ids must be prefixed if we are loading a referenced document, because references to it // are prefixed with the document id // const elementIdPrefix = isReferencedDocument ? documentData.id : null; var _this = this; // Convert the document's referenced documents first this.readReferencedDocumentsRecursive(documentData); // Convert the document's profiles first (this allows the converter to resolve references to them) var profilesInDocument = documentData.profiles ? this.converter.convert(documentData.profiles, null) : null; if (profilesInDocument) { profilesInDocument.packagedElements.forEach(function (p) { if (ElementTypeUtility.isProfile(p.elementType)) { // the element can also be a Enum or other type _this.allProfiles.push(p); } }); } // Then convert the model, using the profiles var modelInDocument = documentData.model ? this.converter.convert(documentData.model, this.allProfiles) : null; var document = new Document(this.modelDelegate); document.id = documentData.id; document.modelTypeName = documentData.modelTypeName; document.modelTypeVersion = documentData.modelTypeVersion; document.creator = documentData.creator; document.profiles = profilesInDocument; document.model = modelInDocument; return document; //return documentData.model ? ModelReader.converter.convert(documentData.model, ModelReader.profiles, elementIdPrefix) : null; }; ModelReader.prototype.readReferencedDocumentsRecursive = function (documentData) { var _this = this; if (!documentData.references) return; documentData.references.forEach(function (r) { if (!r.document) return; // Load the document. This will load the document's model elements into memory so that the converter can // resolve any references to it. We don't use the referenced document's main model here, it is not returned // through the API but may be referenced by the main document. _this.readDocumentRecursive(r.document, true); }); }; return ModelReader; }()); export { ModelReader };