Skip to content
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

Xhtml visualization #497

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 54 additions & 62 deletions src/main/java/digital/slovensko/autogram/core/eforms/EFormUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Properties;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
Expand Down Expand Up @@ -204,6 +207,10 @@ public static String getNamespaceFromEformXml(Document xml) {
if (xmlns != null && !xmlns.getNodeValue().contains("justice.gov.sk"))
return xmlns.getNodeValue();

var xmlnsUri = xml.getDocumentElement().getNamespaceURI();
if (xmlnsUri != null && !xmlnsUri.contains("justice.gov.sk"))
return xmlnsUri;

xmlns = xml.getDocumentElement().getAttributes().getNamedItemNS("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation");
if (xmlns != null)
return xmlns.getNodeValue();
Expand Down Expand Up @@ -409,57 +416,44 @@ public static ArrayList<ManifestXsltEntry> getManifestXsltEntries(NodeList nodes
for (int i = 0; i < nodes.getLength(); i++) {
var node = nodes.item(i);

var fullPathNode = node.getAttributes().getNamedItem("full-path");
if (fullPathNode == null)
var fullPath = nullOrNodeValue(node.getAttributes().getNamedItem("full-path"));
if (fullPath == null)
continue;
var fullPath = fullPathNode.getNodeValue().replace("\\", "/");

var mediaTypeNode = node.getAttributes().getNamedItem("media-type");
var mediaType = mediaTypeNode != null ? mediaTypeNode.getNodeValue() : null;
fullPath = fullPath.replace("\\", "/");

var mediaDestination = "";
var mediaDestinationNode = node.getAttributes().getNamedItem("media-destination");
if (mediaDestinationNode != null) {
mediaDestination = mediaDestinationNode.getNodeValue();
if(!mediaDestination.equals("sign") && !mediaDestination.equals("x-xslt-ro"))
continue;
var mediaType = nullOrNodeValue(node.getAttributes().getNamedItem("media-type"));
var mediaDestinationTypeDescription = nullOrNodeValue(node.getAttributes().getNamedItem("media-destination-type-description"));
var mediaDestination = nullOrNodeValue(node.getAttributes().getNamedItem("media-destination"));
var mediaDestinationType = nullOrNodeValue(node.getAttributes().getNamedItem("media-destination-type"));

if (mediaType == null)
if (mediaDestination == null) {
if (Stream.of(".sb..xslt", ".html.xslt").noneMatch(fullPath::endsWith))
continue;

if (!mediaType.equals("application/xslt+xml") && !mediaType.equals("text/xsl"))
if (!(
(mediaType.equals("text/xml") || mediaDestination.equals("application/xml"))
&& (fullPath.contains(".xsl") || fullPath.contains(".xslt"))
))
continue;
mediaDestination = fullPath.contains(".sb.xslt") ? "sign" : "view";

} else {
if (!fullPath.contains(".sb.xslt") && !fullPath.contains(".html.xslt"))
if (mediaType == null)
continue;

mediaDestination = fullPath.contains(".sb.xslt") ? "sign" : "view";
}

var languageNode = node.getAttributes().getNamedItem("media-language");
var language = languageNode != null ? languageNode.getNodeValue() : null;

var mediaDestinationTypeDescriptionNode = node.getAttributes().getNamedItem("media-destination-type-description");
var mediaDestinationTypeDescription = mediaDestinationTypeDescriptionNode != null ? mediaDestinationTypeDescriptionNode.getNodeValue() : null;
if (Stream.of("sign", "x-xslt-ro", "view").noneMatch(mediaDestination::contains))
continue;

if (mediaDestinationTypeDescription == null) {
var mediaDestinationTypeNode = node.getAttributes().getNamedItem("media-destination-type");
var mediaDestinationType = mediaDestinationTypeNode != null ? mediaDestinationTypeNode.getNodeValue() : null;

if (mediaDestinationType != null)
mediaDestinationTypeDescription = switch (mediaDestinationType) {
case "text/plain" -> "TXT";
case "text/html" -> "HTML";
case "application/xhtml+xml" -> "XHTML";
default -> null;
};
if (Stream.of("application/xslt+xml", "text/xsl").noneMatch(mediaType::equals) &&
(Stream.of("text/xml", "application/xml").noneMatch(mediaType::equals) ||
Stream.of(".xsl", ".xslt").noneMatch(fullPath::endsWith)))
continue;
}

if (mediaDestinationTypeDescription == null && mediaDestinationType != null)
mediaDestinationTypeDescription = switch (mediaDestinationType) {
case "text/plain" -> "TXT";
case "text/html" -> "HTML";
case "application/xhtml+xml" -> "XHTML";
default -> null;
};

if (mediaDestinationTypeDescription == null) {
// need to get output method from xslt
var xsltString = getResource(source_url + form_url + "/" + fullPath);
Expand All @@ -473,16 +467,22 @@ public static ArrayList<ManifestXsltEntry> getManifestXsltEntries(NodeList nodes
}
}

var targetEnvironmentNode = node.getAttributes().getNamedItem("target-environment");
var targetEnvironment = targetEnvironmentNode != null ? targetEnvironmentNode.getNodeValue() : null;

entries.add(new ManifestXsltEntry(mediaType, language, mediaDestinationTypeDescription, targetEnvironment,
fullPath, mediaDestination));
entries.add(new ManifestXsltEntry(
mediaType,
nullOrNodeValue(node.getAttributes().getNamedItem("media-language")),
mediaDestinationTypeDescription,
nullOrNodeValue(node.getAttributes().getNamedItem("target-environment")),
fullPath,
mediaDestination));
}

return entries;
}

private static String nullOrNodeValue(Node node) {
return node != null ? node.getNodeValue() : null;
}

public static ManifestXsltEntry selectXslt(ArrayList<ManifestXsltEntry> entries, String xsltDestinationType, String xsltLanguage, String xsltTarget) {
if (xsltDestinationType != null)
entries.removeIf(entry -> !xsltDestinationType.equals(entry.destinationType()));
Expand All @@ -493,27 +493,19 @@ public static ManifestXsltEntry selectXslt(ArrayList<ManifestXsltEntry> entries,
if (xsltTarget != null)
entries.removeIf(entry -> !xsltTarget.equals(entry.target()));

if (entries.size() == 1)
return entries.get(0);
filterIfExist(entries, e -> e.mediaDestination().equals("sign"));
filterIfExist(entries, e -> List.of("HTML", "XHTML").contains(e.destinationType()));
filterIfExist(entries, e -> e.mediaDestination().equals("view"));
filterIfExist(entries, e -> e.destinationType().equals("XHTML"));

if (entries.stream().filter(entry -> entry.mediaDesination().equals("sign")).count() > 0)
entries.removeIf(entry -> !entry.mediaDesination().equals("sign"));
filterIfExist(entries, e -> e.language().equals("sk"));
filterIfExist(entries, e -> e.language().equals("en"));

if (entries.stream().filter(entry -> entry.destinationType().equals("XHTML")).count() > 0)
entries.removeIf(entry -> !entry.destinationType().equals("XHTML"));

else if (entries.stream().filter(entry -> entry.destinationType().equals("HTML")).count() > 0)
entries.removeIf(entry -> !entry.destinationType().equals("HTML"));

else if (entries.stream().filter(entry -> entry.destinationType().equals("TXT")).count() > 0)
entries.removeIf(entry -> !entry.destinationType().equals("TXT"));

if (entries.stream().filter(entry -> entry.language().equals("sk")).count() > 0)
entries.removeIf(entry -> !entry.language().equals("sk"));

else if (entries.stream().filter(entry -> entry.language().equals("en")).count() > 0)
entries.removeIf(entry -> !entry.language().equals("en"));
return entries.stream().findFirst().orElse(null);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Toto pre mna je trosku zle citatelne kedze ta metoda "mutuje" entries. Predtym som videl, ze najprv sa hlada sign potom xhtml potom html a potom txt. Tu to nevidim.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mne tam vadilo, že sa príliš veľa opakovalo. Rozumiem, že metódy, ktoré menia parametre sú nešťastné. Ako kompromis by som metóde filterIfExist dal return type entries a nebude meniť tie entries v parametri ale ich kópiu.


return entries.get(0);
private static void filterIfExist(ArrayList<ManifestXsltEntry> entries, Predicate<ManifestXsltEntry> l) {
if (entries.stream().anyMatch(l))
entries.removeIf(l.negate());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package digital.slovensko.autogram.core.eforms.dto;

public record ManifestXsltEntry(String mediaType, String language, String destinationType, String target,
String fullPath, String mediaDesination) {
String fullPath, String mediaDestination) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ private Visualization createVisualization(SigningJob job, UserSettings userSetti
if (transformationOutputMimeType.equals("HTML"))
return new HTMLVisualization(EFormUtils.transform(documentToDisplay, transformation), job);

if (transformationOutputMimeType.equals("XHTML"))
return new HTMLVisualization(EFormUtils.transform(documentToDisplay, transformation), job);

if (transformationOutputMimeType.equals("TXT"))
return new PlainTextVisualization(EFormUtils.transform(documentToDisplay, transformation), job);

Expand Down
Loading