public Converter(string sourceFolderXml, string targetFolderYaml, string targetFolderJson, string targetFolderResx, bool checkBSDD = false) { CheckBSDD = checkBSDD; _bsdd = new Bsdd(); string propertySetVersionList = string.Empty; string propertySetTemplateList = string.Empty; string propertyTypeList = string.Empty; string propertyUnitList = string.Empty; foreach (string sourceFile in Directory.EnumerateFiles(sourceFolderXml, "PSet*.xml").OrderBy(x => x).ToList())//.Where(x=>x.Contains("Pset_ConstructionResource"))) { numberOfPsets++; PropertySetDef pSet = PropertySetDef.LoadFromFile(sourceFile); log.Info("--------------------------------------------------"); log.Info($"Checking PSet {pSet.Name}"); log.Info($"Opened PSet-File {sourceFile.Replace(sourceFolderXml + @"\", string.Empty)}"); if (!propertySetVersionList.Contains(pSet.IfcVersion.version)) { propertySetVersionList += pSet.IfcVersion.version + ","; } if (!propertySetTemplateList.Contains(pSet.templatetype.ToString())) { propertySetTemplateList += pSet.templatetype.ToString() + ","; } PropertySet propertySet = new PropertySet() { name = pSet.Name, dictionaryReference = new DictionaryReference() { ifdGuid = "", legacyGuid = "" }, ifcVersion = new IfcVersion() { version = ConvertToSematicVersion(pSet.IfcVersion.version).ToString(), schema = pSet.IfcVersion.schema }, definition = pSet.Definition }; propertySet.applicableIfcClasses = new List <ApplicableIfcClass>(); foreach (var applicableClass in pSet.ApplicableClasses) { propertySet.applicableIfcClasses.Add(new ApplicableIfcClass() { name = applicableClass, type = pSet.ApplicableTypeValue }); } //Insert missing standard localizations as dummys propertySet.localizations = new List <Localization>(); foreach (string standardLanguage in StandardLanguages.OrderBy(x => x)) { if (propertySet.localizations.Where(x => x.language == standardLanguage).FirstOrDefault() == null) { propertySet.localizations.Add(new Localization() { language = standardLanguage, name = string.Empty, definition = string.Empty }); } } if (CheckBSDD) { if (propertySet.dictionaryReference.legacyGuid.Length == 0) { Console.ForegroundColor = ConsoleColor.Red; log.Info($" ERROR: The GUID is missing in PSet!"); Console.ResetColor(); } } IfdConceptList ifdConceptList = _bsdd.SearchNests(pSet.Name); if (ifdConceptList == null) { log.Info($" Could not find the PSet in bSDD"); } else { numberOfPsetsWithbSDDGuid++; IfdConcept bsddPSet = ifdConceptList.IfdConcept.FirstOrDefault(); log.Info($" Loaded Property from bSDD (1 out of {ifdConceptList.IfdConcept.Count})"); log.Info($" Loaded PSet from bSDD"); log.Info($" Guid: {bsddPSet.Guid}"); log.Info($" Status: {bsddPSet.Status}"); log.Info($" VersionDate: {bsddPSet.VersionDate}"); log.Info($" Web: http://bsdd.buildingsmart.org/#concept/browse/{bsddPSet.Guid}"); if (ifdConceptList.IfdConcept.Count == 1) { log.Info($" The GUID of the PSet in the file was changed {propertySet.dictionaryReference.legacyGuid} => {bsddPSet.Guid}"); propertySet.dictionaryReference.ifdGuid = bsddPSet.Guid; } } propertySet.dictionaryReference.dictionaryWebUri = $"http://bsdd.buildingsmart.org/#concept/browse/{propertySet.dictionaryReference.ifdGuid}"; propertySet.dictionaryReference.dictionaryApiUri = $"http://bsdd.buildingsmart.org/api/4.0/IfdConcept/{propertySet.dictionaryReference.ifdGuid}"; log.Info($" Now checking the properties within the PSet"); propertySet.properties = LoadProperties(pSet, pSet.PropertyDefs); propertySet = Utils.PrepareTexts(propertySet); string targetFileYaml = sourceFile.Replace("xml", "YAML").Replace(sourceFolderXml, targetFolderYaml); string targetFileJson = sourceFile.Replace("xml", "json").Replace(sourceFolderXml, targetFolderJson); string targetFileResx = sourceFile.Replace("xml", "resx").Replace(sourceFolderXml, targetFolderResx); var ScalarStyleSingleQuoted = new YamlMemberAttribute() { ScalarStyle = ScalarStyle.SingleQuoted }; var yamlSerializer = new SerializerBuilder() //.WithNamingConvention(new CamelCaseNamingConvention()) .WithAttributeOverride <PropertySet>(nc => nc.name, ScalarStyleSingleQuoted) .WithAttributeOverride <PropertySet>(nc => nc.definition, ScalarStyleSingleQuoted) .WithAttributeOverride <Localization>(nc => nc.name, ScalarStyleSingleQuoted) .WithAttributeOverride <Localization>(nc => nc.definition, ScalarStyleSingleQuoted) .Build(); string yamlContent = yamlSerializer.Serialize(propertySet); File.WriteAllText(targetFileYaml, yamlContent, Encoding.UTF8); log.Info("The PSet was saved as YAML file"); JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings(); jsonSerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore; string jsonContent = JsonConvert.SerializeObject(propertySet, Formatting.Indented, jsonSerializerSettings); File.WriteAllText(targetFileJson, jsonContent, Encoding.UTF8); log.Info("The PSet was saved as JSON file"); var yamlDeserializer = new DeserializerBuilder() .Build(); try { propertySet = yamlDeserializer.Deserialize <PropertySet>(new StringReader(File.ReadAllText(targetFileYaml))); log.Info("The YAML file is valid"); } catch (Exception ex) { Console.Write(" ERROR!"); log.Info(ex.Message); } ResxWriter resx = new ResxWriter(targetFileResx); resx.Write(propertySet, StandardLanguages); log.Info("The PSet was saved as RESX file"); } log.Info($"Number of PSets: {numberOfPsets}"); log.Info($" with not resolved bSDD Guid: {numberOfPsetsWithbSDDGuid}"); log.Info($"Number of Properties: {numberOfProperties}"); log.Info($" with not resolved bSDD Guid: {numberOfPropertiesWithbSDDGuid}"); }
public YamlTranslationWriter(string translationSourceFile, string folderYaml, string folderJson, string folderResx) { log.Info($"Inject the translation into the YAML files from this source table: {translationSourceFile}"); if (translationSourceFile != null) { log.Info($"Inject translations from {translationSourceFile}"); } if (folderYaml != null) { log.Info($"Inject into YAML files in this target folder: {folderYaml}"); } if (folderJson != null) { log.Info($"Inject into JSON files in this target folder: {folderJson}"); } if (folderResx != null) { log.Info($"Inject into RESX files in this target folder: {folderResx}"); } if (translationSourceFile == null) { log.Error($"ERROR - The parameter translationSourceFile does not exist. Exiting!"); return; } else if (!File.Exists(translationSourceFile)) { log.Error($"ERROR - File {translationSourceFile} does not exist. Exiting!"); return; } if (folderYaml == null) { log.Error($"ERROR - The parameter folderXml does not exist. Exiting!"); return; } else if (!Directory.Exists(folderYaml)) { log.Error($"ERROR - The Directory {folderYaml} does not exist. Exiting!"); return; } if (folderJson != null) { if (!Directory.Exists(folderJson)) { log.Error($"ERROR - The Directory {folderJson} does not exist. Exiting!"); return; } } if (folderResx != null) { if (!Directory.Exists(folderResx)) { log.Error($"ERROR - The Directory {folderResx} does not exist. Exiting!"); return; } } foreach (Translation translation in ReadTranslationDataFromExcel(translationSourceFile)) { string yamlFileName = Path.Combine(folderYaml, $"{translation.pset}.YAML"); var yamlDeserializer = new DeserializerBuilder().Build(); PropertySet propertySet; try { propertySet = yamlDeserializer.Deserialize <PropertySet>(new StringReader(File.ReadAllText(yamlFileName))); log.Info($"Opened the YAML file {yamlFileName}"); } catch (Exception ex) { log.Error(ex.Message); return; } var ScalarStyleSingleQuoted = new YamlMemberAttribute() { ScalarStyle = ScalarStyle.SingleQuoted }; var yamlSerializer = new SerializerBuilder() //.WithNamingConvention(new CamelCaseNamingConvention()) .WithAttributeOverride <PropertySet>(nc => nc.name, ScalarStyleSingleQuoted) .WithAttributeOverride <PropertySet>(nc => nc.definition, ScalarStyleSingleQuoted) .WithAttributeOverride <Localization>(nc => nc.name, ScalarStyleSingleQuoted) .WithAttributeOverride <Localization>(nc => nc.definition, ScalarStyleSingleQuoted) .Build(); switch (translation.type) { case "PSet": log.Info($"Translate PSet {translation.pset} => {translation.name_TL} [{translation.language}]"); var localizationPSet = propertySet.localizations.Where(x => x.language.ToLower() == translation.language.ToLower()).FirstOrDefault(); localizationPSet.name = translation.name_TL; localizationPSet.definition = translation.definition_tl; string yamlContentPSet = yamlSerializer.Serialize(propertySet); File.WriteAllText(yamlFileName, yamlContentPSet, Encoding.UTF8); break; case "Property": log.Info($"Translated Property {translation.pset}.{translation.name} => {translation.name_TL} [{translation.language}]"); try { var localizationProperty = propertySet.properties.Where(x => x.name == translation.name).FirstOrDefault().localizations.Where(x => x.language.ToLower() == translation.language.ToLower()).FirstOrDefault(); localizationProperty.name = translation.name_TL; localizationProperty.definition = translation.definition_tl; string yamlContentProperty = yamlSerializer.Serialize(propertySet); File.WriteAllText(yamlFileName, yamlContentProperty, Encoding.UTF8); } catch (Exception ex) { log.Error($"ERROR: {ex.Message}"); } break; } if (folderJson != null) { string targetFileJson = yamlFileName.Replace(".YAML", ".json").Replace(folderYaml, folderJson); JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings(); jsonSerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore; string jsonContent = JsonConvert.SerializeObject(propertySet, Formatting.Indented, jsonSerializerSettings); File.WriteAllText(targetFileJson, jsonContent, Encoding.UTF8); log.Info("The PSet was saved as JSON file"); } if (folderResx != null) { string targetFileResx = yamlFileName.Replace(".YAML", ".resx").Replace(folderYaml, folderResx); ResxWriter resx = new ResxWriter(targetFileResx); resx.Write(propertySet, StandardLanguages); log.Info("The PSet was saved as RESX file"); } } }