-
Notifications
You must be signed in to change notification settings - Fork 26
feat: API записи объектов метаданных (Subsystem, Catalog, Configuration) в форматах EDT и Конфигуратор #595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
5d60e9b
1603f78
24d5cf8
3c4a54d
68141aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * This file is a part of MDClasses. | ||
| * | ||
| * Copyright (c) 2019 - 2026 | ||
| * Tymko Oleg <olegtymko@yandex.ru>, Maximov Valery <maximovvalery@gmail.com> and contributors | ||
| * | ||
| * SPDX-License-Identifier: LGPL-3.0-or-later | ||
| * | ||
| * MDClasses is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 3.0 of the License, or (at your option) any later version. | ||
| * | ||
| * MDClasses is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public | ||
| * License along with MDClasses. | ||
| */ | ||
| package com.github._1c_syntax.bsl.mdclasses; | ||
|
|
||
| import lombok.Builder; | ||
|
|
||
| /** | ||
| * Настройки записи MDC | ||
| * | ||
| * @param encoding Кодировка файлов (по умолчанию UTF-8) | ||
| */ | ||
| @Builder | ||
| public record MDCWriteSettings(String encoding) { | ||
|
|
||
| /** | ||
| * Настройки по умолчанию (UTF-8) | ||
| */ | ||
| public static final MDCWriteSettings DEFAULT = MDCWriteSettings.builder() | ||
| .encoding("UTF-8") | ||
| .build(); | ||
|
|
||
| public String encoding() { | ||
| return encoding != null ? encoding : "UTF-8"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * This file is a part of MDClasses. | ||
| * | ||
| * Copyright (c) 2019 - 2026 | ||
| * Tymko Oleg <olegtymko@yandex.ru>, Maximov Valery <maximovvalery@gmail.com> and contributors | ||
| * | ||
| * SPDX-License-Identifier: LGPL-3.0-or-later | ||
| * | ||
| * MDClasses is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 3.0 of the License, or (at your option) any later version. | ||
| * | ||
| * MDClasses is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public | ||
| * License along with MDClasses. | ||
| */ | ||
| package com.github._1c_syntax.bsl.writer; | ||
|
|
||
| import com.github._1c_syntax.bsl.mdclasses.MDCWriteSettings; | ||
| import com.github._1c_syntax.bsl.writer.designer.DesignerWriter; | ||
| import com.github._1c_syntax.bsl.writer.edt.EDTWriter; | ||
| import lombok.experimental.UtilityClass; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.commons.io.FilenameUtils; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
|
|
||
| /** | ||
| * Фасад записи объектов метаданных в файлы (EDT .mdo, в перспективе Designer .xml). | ||
| */ | ||
| @UtilityClass | ||
| @Slf4j | ||
| public class MDOWriter { | ||
|
|
||
| /** | ||
| * Записывает объект метаданных в файл. | ||
| * Формат определяется по расширению пути: .mdo — EDT. | ||
| * | ||
| * @param path Путь к файлу (например, .../Subsystems/Name/Name.mdo) | ||
| * @param object Объект метаданных (поддерживается Subsystem для EDT) | ||
| * @throws IOException при ошибке записи | ||
| * @throws UnsupportedOperationException если формат или тип объекта не поддерживается | ||
| */ | ||
| public void writeObject(Path path, Object object) throws IOException { | ||
| writeObject(path, object, MDCWriteSettings.DEFAULT); | ||
| } | ||
|
|
||
| /** | ||
| * Записывает объект метаданных в файл с настройками. | ||
| * | ||
| * @param path Путь к файлу | ||
| * @param object Объект метаданных | ||
| * @param writeSettings Настройки записи | ||
| * @throws IOException при ошибке записи | ||
| * @throws UnsupportedOperationException если формат или тип объекта не поддерживается | ||
| */ | ||
| public void writeObject(Path path, Object object, MDCWriteSettings writeSettings) throws IOException { | ||
| if (path == null || object == null) { | ||
| throw new IllegalArgumentException("path and object must not be null"); | ||
| } | ||
| if (FilenameUtils.isExtension(path.toString(), "mdo")) { | ||
| var writer = new EDTWriter(writeSettings); | ||
| writer.write(path, object); | ||
| } else if (FilenameUtils.isExtension(path.toString(), "xml")) { | ||
| var writer = new DesignerWriter(writeSettings); | ||
| writer.write(path, object); | ||
| } else { | ||
| throw new UnsupportedOperationException("Write is supported only for EDT (.mdo) or Designer (.xml) format, got: " + path); | ||
| } | ||
|
Comment on lines
+61
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normalize unsupported-type handling before dispatch. The facade documents one failure mode, but the 🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /* | ||
| * This file is a part of MDClasses. | ||
| * | ||
| * Copyright (c) 2019 - 2026 | ||
| * Tymko Oleg <olegtymko@yandex.ru>, Maximov Valery <maximovvalery@gmail.com> and contributors | ||
| * | ||
| * SPDX-License-Identifier: LGPL-3.0-or-later | ||
| * | ||
| * MDClasses is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 3.0 of the License, or (at your option) any later version. | ||
| * | ||
| * MDClasses is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public | ||
| * License along with MDClasses. | ||
| */ | ||
| package com.github._1c_syntax.bsl.writer; | ||
|
|
||
| import com.github._1c_syntax.bsl.mdclasses.Configuration; | ||
| import com.github._1c_syntax.bsl.mdclasses.MDClasses; | ||
| import com.github._1c_syntax.bsl.mdclasses.MDCReadSettings; | ||
| import com.github._1c_syntax.bsl.mdo.Catalog; | ||
| import com.github._1c_syntax.bsl.mdo.Subsystem; | ||
| import com.github._1c_syntax.bsl.reader.MDOReader; | ||
| import com.github._1c_syntax.bsl.reader.edt.EDTReader; | ||
| import com.github._1c_syntax.bsl.types.MultiLanguageString; | ||
|
|
||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
|
|
||
| /** | ||
| * Демонстрация чтения и записи метаданных: три типа объектов (Subsystem, Catalog, Configuration) | ||
| * в двух форматах — EDT (.mdo) и Конфигуратор (Designer .xml). Артефакты раскладываются по каталогам: | ||
| * build/read-write-demo-output/edt/ и build/read-write-demo-output/designer/. | ||
| * Запуск: {@code ./gradlew runReadWriteDemo} или указать путь в аргументе. | ||
| */ | ||
| public final class ReadWriteDemo { | ||
|
|
||
| private ReadWriteDemo() { | ||
| } | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| var baseDir = args.length > 0 ? Paths.get(args[0]) : Paths.get("build", "read-write-demo-output"); | ||
| var edtDir = baseDir.resolve("edt"); | ||
| var designerDir = baseDir.resolve("designer"); | ||
| var edtSrc = edtDir.resolve("src"); | ||
| var designerSrc = designerDir.resolve("src").resolve("cf"); | ||
| Files.createDirectories(edtSrc); | ||
| Files.createDirectories(designerSrc); | ||
|
|
||
| var subsystem = Subsystem.builder() | ||
| .name("DemoSubsystem") | ||
| .uuid("0421b67e-ed26-491d-ab98-ec59002ed4ce") | ||
| .synonym(MultiLanguageString.create("ru", "Демо подсистема")) | ||
| .build(); | ||
| var catalog = Catalog.builder() | ||
| .name("DemoCatalog") | ||
| .uuid("c6c26c3c-de7a-4ed4-944d-ada62cf1ab8f") | ||
| .synonym(MultiLanguageString.create("ru", "Демо справочник")) | ||
| .build(); | ||
| var config = Configuration.builder() | ||
| .name("DemoConfiguration") | ||
| .uuid("46c7c1d0-b04d-4295-9b04-ae3207c18d29") | ||
| .build(); | ||
|
|
||
| var ok = true; | ||
|
|
||
| // --- EDT (.mdo) — каталог edt/ --- | ||
| System.out.println("=== EDT (edt/) ==="); | ||
| var subsystemMdo = edtSrc.resolve("Subsystems").resolve("DemoSubsystem").resolve("DemoSubsystem.mdo"); | ||
| MDClasses.writeObject(subsystemMdo, subsystem); | ||
| System.out.println("Written: " + subsystemMdo.toAbsolutePath()); | ||
| var readSub = new EDTReader(subsystemMdo, MDCReadSettings.SKIP_SUPPORT).read(subsystemMdo); | ||
| ok &= checkReadBack("Subsystem", readSub instanceof Subsystem s ? s.getName() : null, subsystem.getName()); | ||
|
|
||
| var catalogMdo = edtSrc.resolve("Catalogs").resolve("DemoCatalog").resolve("DemoCatalog.mdo"); | ||
| MDClasses.writeObject(catalogMdo, catalog); | ||
| System.out.println("Written: " + catalogMdo.toAbsolutePath()); | ||
| var readCat = new EDTReader(catalogMdo, MDCReadSettings.SKIP_SUPPORT).read(catalogMdo); | ||
| ok &= checkReadBack("Catalog", readCat instanceof Catalog c ? c.getName() : null, catalog.getName()); | ||
|
|
||
| var configMdo = edtSrc.resolve("Configuration").resolve("Configuration.mdo"); | ||
| MDClasses.writeObject(configMdo, config); | ||
| System.out.println("Written: " + configMdo.toAbsolutePath()); | ||
| var readConfig = MDOReader.readConfiguration(configMdo, MDCReadSettings.SKIP_SUPPORT); | ||
| if (readConfig != null && readConfig instanceof Configuration) { | ||
| System.out.println(" Read back Configuration: " + readConfig.getClass().getSimpleName()); | ||
| } else { | ||
| System.out.println(" ERROR: read back Configuration failed"); | ||
| ok = false; | ||
| } | ||
|
|
||
| // --- Конфигуратор (Designer .xml) — каталог designer/ --- | ||
| System.out.println("=== Designer (designer/) ==="); | ||
| var subsystemXml = designerSrc.resolve("Subsystems").resolve("DemoSubsystem.xml"); | ||
| MDClasses.writeObject(subsystemXml, subsystem); | ||
| System.out.println("Written: " + subsystemXml.toAbsolutePath()); | ||
| ok &= Files.exists(subsystemXml) && Files.size(subsystemXml) > 0; | ||
|
|
||
| var catalogXml = designerSrc.resolve("Catalogs").resolve("DemoCatalog.xml"); | ||
| MDClasses.writeObject(catalogXml, catalog); | ||
| System.out.println("Written: " + catalogXml.toAbsolutePath()); | ||
| ok &= Files.exists(catalogXml) && Files.size(catalogXml) > 0; | ||
|
|
||
| var configXml = designerSrc.resolve("Configuration.xml"); | ||
| MDClasses.writeObject(configXml, config); | ||
| System.out.println("Written: " + configXml.toAbsolutePath()); | ||
| ok &= Files.exists(configXml) && Files.size(configXml) > 0; | ||
|
|
||
| if (ok) { | ||
| System.out.println("OK: all objects written to edt/ and designer/ (Subsystem, Catalog, Configuration)."); | ||
| } else { | ||
| System.exit(1); | ||
| } | ||
| } | ||
|
|
||
| private static boolean checkReadBack(String type, String readName, String expectedName) { | ||
| if (readName != null && readName.equals(expectedName)) { | ||
| System.out.println(" Read back " + type + ": " + readName); | ||
| return true; | ||
| } | ||
| System.out.println(" ERROR: read back " + type + " failed (expected " + expectedName + ", got " + readName + ")"); | ||
| return false; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.