|
| 1 | +/******************************************************************************* |
| 2 | + * SysML 2 Pilot Implementation |
| 3 | + * Copyright (c) 2026 Model Driven Solutions, Inc. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + * |
| 18 | + * @license LGPL-3.0-or-later <http://spdx.org/licenses/LGPL-3.0-or-later> |
| 19 | + * |
| 20 | + *******************************************************************************/ |
| 21 | + |
| 22 | +package org.omg.kerml.xtext.util; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import java.io.IOException; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | +import java.util.stream.Stream; |
| 31 | + |
| 32 | +import org.eclipse.emf.common.util.BasicEList; |
| 33 | +import org.eclipse.emf.ecore.resource.Resource; |
| 34 | +import org.omg.kerml.xtext.KerMLStandaloneSetup; |
| 35 | +import org.omg.sysml.lang.sysml.Membership; |
| 36 | +import org.omg.sysml.lang.sysml.Namespace; |
| 37 | +import org.omg.sysml.util.SysMLUtil; |
| 38 | + |
| 39 | +public class KerMLIndexUtil extends SysMLUtil { |
| 40 | + |
| 41 | + public KerMLIndexUtil() { |
| 42 | + super(); |
| 43 | + KerMLStandaloneSetup.doSetup(); |
| 44 | + addExtension(".kerml"); |
| 45 | + } |
| 46 | + |
| 47 | + protected static String formatEntry(String elementName, String resourcePath) { |
| 48 | + return " \"" + elementName + "\": \"" + resourcePath + "\""; |
| 49 | + } |
| 50 | + |
| 51 | + public String index(String inputPath) { |
| 52 | + read(inputPath); |
| 53 | + File inputFile = new File(inputPath); |
| 54 | + if (!inputFile.isDirectory()) { |
| 55 | + inputPath = inputFile.getParent(); |
| 56 | + if (inputPath == null) { |
| 57 | + inputPath = ""; |
| 58 | + } |
| 59 | + } |
| 60 | + List<String> entries = new ArrayList<>(); |
| 61 | + for (Resource resource: getInputResources()) { |
| 62 | + String resourcePath = resource.getURI().toFileString().replace(inputPath + "/", ""); |
| 63 | + Namespace rootNamespace = (Namespace)resource.getContents().get(0); |
| 64 | + List<Membership> memberships = rootNamespace.visibleMemberships(new BasicEList<>(), false, false); |
| 65 | + Stream<String> names = memberships.stream().map(Membership::getMemberName); |
| 66 | + Stream<String> shortNames = memberships.stream().map(Membership::getMemberShortName); |
| 67 | + Stream.concat(names, shortNames) |
| 68 | + .filter(name->name != null) |
| 69 | + .map(name->formatEntry(name, resourcePath)) |
| 70 | + .forEach(entries::add); |
| 71 | + } |
| 72 | + entries.sort(null); |
| 73 | + return String.join("\n", entries); |
| 74 | + } |
| 75 | + |
| 76 | + public void writeIndex(String inputPath, String outputPath) throws IOException { |
| 77 | + String index = index(inputPath); |
| 78 | + System.out.println("Writing " + outputPath + "..."); |
| 79 | + Files.writeString(Path.of(outputPath), index); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * <p>Usage: |
| 84 | + * |
| 85 | + * <p>KerMLIndexUtil input-path output-path |
| 86 | + * |
| 87 | + * <p>where: |
| 88 | + * |
| 89 | + * <ul> |
| 90 | + * <li>input-path is a path for reading input resources (file or directory)</li> |
| 91 | + * <li>output-path is a path for an output file</li> |
| 92 | + * </ul> |
| 93 | + */ |
| 94 | + public static void main(String[] args) { |
| 95 | + try { |
| 96 | + new KerMLIndexUtil().writeIndex(args[0], args[1]); |
| 97 | + } catch (IOException e) { |
| 98 | + e.printStackTrace(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments