Skip to content

Commit

Permalink
2024.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pvlasov committed Dec 30, 2024
1 parent d7c49b8 commit 70b94ed
Show file tree
Hide file tree
Showing 23 changed files with 169 additions and 48 deletions.
2 changes: 1 addition & 1 deletion capability-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>org.nasdanika.core</groupId>
<version>2024.11.1</version>
<version>2024.12.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>capability-tests</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions capability/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>org.nasdanika.core</groupId>
<version>2024.11.1</version>
<version>2024.12.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>capability</artifactId>
Expand All @@ -20,7 +20,7 @@
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>3.6.10</version>
<version>3.7.1</version>
</dependency>
</dependencies>
</project>
6 changes: 3 additions & 3 deletions cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>org.nasdanika.core</groupId>
<version>2024.11.1</version>
<version>2024.12.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>cli</artifactId>
Expand Down Expand Up @@ -39,7 +39,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.5.0</version>
<version>3.5.2</version>
<executions>
<execution>
<goals>
Expand Down Expand Up @@ -70,7 +70,7 @@
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
<version>3.26.3</version>
<version>3.28.0</version>
</dependency>
</dependencies>
</project>
133 changes: 127 additions & 6 deletions cli/src/main/java/org/nasdanika/cli/DrawioCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.function.Function;

import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.emf.common.util.URI;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.nasdanika.capability.CapabilityLoader;
import org.nasdanika.common.DefaultConverter;
import org.nasdanika.common.NasdanikaException;
import org.nasdanika.common.ProgressMonitor;
import org.nasdanika.drawio.Document;
import org.xml.sax.SAXException;
import org.yaml.snakeyaml.Yaml;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;
Expand Down Expand Up @@ -55,6 +67,42 @@ protected DrawioCommand(CapabilityLoader capabilityLoader) {
@Mixin
PropertiesMixIn propertiesMixIn;


@Option(
names = {"-u", "--uri"},
description = {
"URI to URL mapping",
"Target URLs are resolved",
"relative to the document URL"})
private Map<String, String> uris = new LinkedHashMap<>();

@Option(
names = {"-U", "--uris"},
paramLabel = "URL of URI to URL mapping resource",
description = {
"URI map resource URL relative to the document file",
"YAML, JSON, or properties",
"Type is inferred from the content type header, if it is present, ",
"or extension"
})
private List<String> uriMapSources = new ArrayList<>();

protected Map<String,Object> loadJson(InputStream in) throws IOException {
JSONObject jo = new JSONObject(new JSONTokener(in));
return jo.toMap();
}

protected Map<Object,Object> loadYaml(InputStream in) throws IOException {
Yaml yaml = new Yaml();
return yaml.load(in);
}

protected Properties loadProperties(InputStream in) throws IOException {
Properties props = new Properties();
props.load(in);
return props;
}

@Override
public Document getDocument(ProgressMonitor progressMonitor) {
File currentDir = new File(".");
Expand All @@ -64,7 +112,7 @@ public Document getDocument(ProgressMonitor progressMonitor) {
if (isFile) {
File documentFile = new File(document);
try {
return Document.load(documentFile, getUriHandler(progressMonitor), getPropertySource(progressMonitor));
return Document.load(documentFile, getUriHandler(URI.createFileURI(documentFile.toString()), progressMonitor), getPropertySource(progressMonitor));
} catch (IOException | ParserConfigurationException | SAXException e) {
throw new NasdanikaException("Error loading document from '" + documentFile.getAbsolutePath() + "': " + e, e);
}
Expand All @@ -73,17 +121,90 @@ public Document getDocument(ProgressMonitor progressMonitor) {
URI baseURI = URI.createFileURI(currentDir.getAbsolutePath()).appendSegment("");
URI documentURI = URI.createURI(document).resolve(baseURI);
try {
return Document.load(documentURI, getUriHandler(progressMonitor), getPropertySource(progressMonitor));
return Document.load(documentURI, getUriHandler(documentURI, progressMonitor), getPropertySource(progressMonitor));
} catch (IOException | ParserConfigurationException | SAXException e) {
throw new NasdanikaException("Error loading document from '" + documentURI.toString() + "': " + e, e);
}
}

protected Function<URI, InputStream> getUriHandler(ProgressMonitor progressMonitor) {
// TODO
return null;
}
protected Function<URI, InputStream> getUriHandler(URI baseURI, ProgressMonitor progressMonitor) {
Map<Object, Object> data = new LinkedHashMap<>(uris);
File currentDir = new File(".");
for (String location: uriMapSources) {
try {
URL url = currentDir.toURI().resolve(location).toURL();
URLConnection connection = url.openConnection();

if (location.endsWith(".json")) {
try (InputStream in = connection.getInputStream()) {
data.putAll(loadJson(in));
}
} else if (location.endsWith(".yml") || location.endsWith(".yaml")) {
try (InputStream in = connection.getInputStream()) {
data.putAll(loadYaml(in));
}
} else if (location.endsWith(".properties")) {
try (InputStream in = connection.getInputStream()) {
data.putAll(loadProperties(in));
}
} else {
// Can't deduce content type from extension, attempting to use Content-Type header
if (connection instanceof HttpURLConnection && ((HttpURLConnection) connection).getResponseCode() == HttpURLConnection.HTTP_OK) {
String contentType = ((HttpURLConnection) connection).getHeaderField("Content-Type");
if ("application/json".equals(contentType)) {
try (InputStream in = connection.getInputStream()) {
data.putAll(loadJson(in));
}
} else if ("application/x-yaml".equals(contentType)) {
try (InputStream in = connection.getInputStream()) {
data.putAll(loadYaml(in));
}
} else if ("text/x-java-properties".equals(contentType)) {
try (InputStream in = connection.getInputStream()) {
data.putAll(loadProperties(in));
}
} else {
throw new CommandLine.ParameterException(spec.commandLine(), "Unsupported content type '" + contentType + "' for " + url);
}
} else {
throw new CommandLine.ParameterException(spec.commandLine(), "Unknown resource type " + url);
}
}
} catch (IOException e) {
throw new CommandLine.ParameterException(spec.commandLine(), "Cannot load properties from " + location + ": " + e, e);
}
}

if (data.isEmpty()) {
return null;
}

return uri -> {
for (Entry<Object, Object> dataEntry: data.entrySet()) {
String keyStr = dataEntry.getKey().toString();
URI keyURI = URI.createURI(keyStr);
URI valueURI = URI.createURI(dataEntry.getValue().toString());
if (valueURI.isRelative() && baseURI != null) {
valueURI = valueURI.resolve(baseURI);
}
if (keyURI.equals(uri)) {
uri = valueURI;
break;
}
if (uri.toString().startsWith(keyStr)) {
URI relative = uri.deresolve(keyURI, true, true, true);
uri = relative.resolve(valueURI);
break;
}
}

try {
return new URL(uri.toString()).openStream();
} catch (IOException e) {
throw new NasdanikaException("Error opening stream from " + uri + ": " + e, e);
}
};
}

protected Function<String, String> getPropertySource(ProgressMonitor progressMonitor) {
Map<Object, Object> properties = propertiesMixIn.getProperties();
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main/java/org/nasdanika/cli/PropertiesMixIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class PropertiesMixIn {

@Option(
names = {"-p", "--property"},
description = { "Properties" })
description = { "Property" })
private Map<String, String> properties = new LinkedHashMap<>();

@Option(
Expand Down
10 changes: 5 additions & 5 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>org.nasdanika.core</groupId>
<version>2024.11.1</version>
<version>2024.12.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>common</artifactId>
Expand All @@ -16,7 +16,7 @@
<dependency>
<groupId>org.eclipse.emf</groupId>
<artifactId>org.eclipse.emf.ecore</artifactId>
<version>2.37.0</version>
<version>2.38.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.emf</groupId>
Expand All @@ -37,7 +37,7 @@
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.18.1</version>
<version>1.18.3</version>
</dependency>

<dependency>
Expand All @@ -55,7 +55,7 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.12.0</version>
<version>1.13.0</version>
</dependency>

<dependency>
Expand All @@ -67,7 +67,7 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>6.1.14</version>
<version>6.2.1</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion diagram/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>org.nasdanika.core</groupId>
<version>2024.11.1</version>
<version>2024.12.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>diagram</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion drawio.model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>org.nasdanika.core</groupId>
<version>2024.11.1</version>
<version>2024.12.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>drawio-model</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion drawio/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>org.nasdanika.core</groupId>
<version>2024.11.1</version>
<version>2024.12.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>drawio</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions emf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>org.nasdanika.core</groupId>
<version>2024.11.1</version>
<version>2024.12.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>emf</artifactId>
Expand All @@ -24,7 +24,7 @@
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>7.0.0.202409031743-r</version>
<version>7.1.0.202411261347-r</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
Expand Down
2 changes: 1 addition & 1 deletion exec.gen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>org.nasdanika.core</groupId>
<version>2024.11.1</version>
<version>2024.12.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>exec-gen</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion exec/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>org.nasdanika.core</groupId>
<version>2024.11.1</version>
<version>2024.12.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>exec</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion graph.model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>org.nasdanika.core</groupId>
<version>2024.11.1</version>
<version>2024.12.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>graph-model</artifactId>
Expand Down
6 changes: 3 additions & 3 deletions graph/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>org.nasdanika.core</groupId>
<version>2024.11.1</version>
<version>2024.12.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>graph</artifactId>
Expand All @@ -28,9 +28,9 @@
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.nasdanika.core</groupId>
<groupId>${project.groupId}</groupId>
<artifactId>common</artifactId>
<version>2024.11.1</version>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit 70b94ed

Please sign in to comment.