Skip to content

Commit

Permalink
#3636: cache
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 12, 2024
1 parent a7a76c4 commit 9e0b464
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 1 deletion.
79 changes: 78 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 @@ -23,19 +23,38 @@
*/
package org.eolang.parser;

import com.jcabi.log.Logger;
import com.jcabi.xml.StrictXML;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;
import javax.xml.namespace.NamespaceContext;
import org.cactoos.bytes.BytesOf;
import org.cactoos.bytes.IoCheckedBytes;
import org.cactoos.io.InputOf;
import org.w3c.dom.Node;
import org.xembly.Directives;
import org.xembly.Xembler;
import org.xml.sax.SAXParseException;

/**
* XMIR that validates itself right after construction.
*
* <p>This class is supposed to be used ONLY for testing, because
* it modifies the XML encapsulated: it replaces the location of
* XSD schema with a file, thus making testing much faster.</p>
*
* @since 0.49.0
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class StrictXmir implements XML {

/**
Expand All @@ -48,7 +67,7 @@ public final class StrictXmir implements XML {
* @param src The source
*/
public StrictXmir(final XML src) {
this.xml = new StrictXML(src);
this.xml = new StrictXML(StrictXmir.reset(src));
}

@Override
Expand Down Expand Up @@ -101,4 +120,62 @@ public Collection<SAXParseException> validate() {
public Collection<SAXParseException> validate(final XML xsd) {
return this.xml.validate(xsd);
}

/**
* Here, we check the location of the XSD in the XML
* and replace with a new one, if necessary.
* @param xml Original XML
* @return New XML with the same node
*/
private static XML reset(final XML xml) {
final Node node = xml.inner();
final List<String> location = xml.xpath("/program/@xsi:noNamespaceSchemaLocation");
if (!location.isEmpty()) {
String uri = location.get(0);
if (uri.startsWith("http")) {
uri = String.format(
"file:///%s",
StrictXmir.download(
uri,
Paths.get("target/xsd").resolve(
uri.substring(uri.lastIndexOf('/') + 1)
)
).toString().replace("\\", "/")
);
}
new Xembler(
new Directives().xpath("/program").attr(
"noNamespaceSchemaLocation xsi http://www.w3.org/2001/XMLSchema-instance",
uri
)
).applyQuietly(node);
}
return new XMLDocument(node);
}

/**
* Download URI from Internet and save to file.
* @param uri The URI
* @param path The file
* @return Where it was saved
*/
private static File download(final String uri, final Path path) {
final File abs = path.toFile().getAbsoluteFile();
if (!abs.exists()) {
if (abs.getParentFile().mkdirs()) {
Logger.debug(StrictXmir.class, "Directory for %[file]s created", path);
}
try {
Files.write(
path,
new IoCheckedBytes(
new BytesOf(new InputOf(new URI(uri)))
).asBytes()
);
} catch (final IOException | URISyntaxException ex) {
throw new IllegalArgumentException(ex);
}
}
return abs;
}
}
64 changes: 64 additions & 0 deletions eo-parser/src/test/java/org/eolang/parser/StrictXmirTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.parser;

import com.jcabi.xml.XMLDocument;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.xembly.Directives;
import org.xembly.Xembler;

/**
* Test case for {@link StrictXmir}.
*
* @since 0.5
*/
final class StrictXmirTest {

@Test
void validatesXmir() throws Exception {
MatcherAssert.assertThat(
"Result EO should be equal to original EO",
new StrictXmir(
new Xmir(
new XMLDocument(
new Xembler(
new Directives()
.append(new DrProgram("foo"))
.xpath("/program")
.attr(
"noNamespaceSchemaLocation xsi http://www.w3.org/2001/XMLSchema-instance",
"https://www.eolang.org/XMIR.xsd"
)
.add("objects")
).xml()
)
)
).validate(),
Matchers.emptyIterable()
);
}

}

0 comments on commit 9e0b464

Please sign in to comment.