Skip to content

Commit

Permalink
#3636: local XSD used if matches
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 13, 2024
1 parent 04af5af commit d0bf3b8
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion eo-parser/src/main/java/org/eolang/parser/StrictXmir.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.eolang.parser;

import com.jcabi.log.Logger;
import com.jcabi.manifests.Manifests;
import com.jcabi.xml.StrictXML;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
Expand All @@ -40,6 +41,7 @@
import org.cactoos.bytes.BytesOf;
import org.cactoos.bytes.IoCheckedBytes;
import org.cactoos.io.InputOf;
import org.cactoos.io.ResourceOf;
import org.w3c.dom.Node;
import org.xembly.Directives;
import org.xembly.Xembler;
Expand Down Expand Up @@ -148,7 +150,7 @@ private static XML reset(final XML xml, final Path tmp) {
if (uri.startsWith("http")) {
uri = String.format(
"file:///%s",
StrictXmir.download(
StrictXmir.fetch(
uri,
tmp.resolve(
uri.substring(uri.lastIndexOf('/') + 1)
Expand All @@ -166,6 +168,42 @@ private static XML reset(final XML xml, final Path tmp) {
return new XMLDocument(node);
}

/**
* Fetch the XSD and place into the path.
* @param uri The URI
* @param path The file
* @return Where it was saved
*/
private static File fetch(final String uri, final Path path) {
final File ret;
final String mine = String.format(
"https://www.eolang.org/xsd/XMIR-%s.xsd",
Manifests.read("EO-Version")
);
if (uri.equals(mine)) {
if (path.toFile().getParentFile().mkdirs()) {
Logger.debug(StrictXmir.class, "Directory for %[file]s created", path);
}
try {
Files.write(
path,
new IoCheckedBytes(
new BytesOf(new ResourceOf("XMIR.xsd"))
).asBytes()
);
} catch (final IOException ex) {
throw new IllegalArgumentException(
String.format("Failed to save %s to %s", uri, path),
ex
);
}
ret = path.toFile();
} else {
ret = StrictXmir.download(uri, path);
}
return ret;
}

/**
* Download URI from Internet and save to file.
* @param uri The URI
Expand Down

0 comments on commit d0bf3b8

Please sign in to comment.