Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 7b7cbf6

Browse files
committed
#118 TDE validation now handles processing instructions and comments
In addition, an error from the server call is no longer caught, which is unlikely to be noticed during a deployment.
1 parent 3a14ef4 commit 7b7cbf6

File tree

4 files changed

+53
-27
lines changed

4 files changed

+53
-27
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
group=com.marklogic
22
javadocsDir=../gh-pages-marklogic-java/javadocs
3-
version=3.13.2
3+
version=3.13.3

src/main/java/com/marklogic/client/ext/schemasloader/impl/TdeDocumentFileProcessor.java

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import com.fasterxml.jackson.databind.node.ObjectNode;
44
import com.marklogic.client.DatabaseClient;
5-
import com.marklogic.client.FailedRequestException;
65
import com.marklogic.client.eval.ServerEvaluationCall;
76
import com.marklogic.client.ext.file.DocumentFile;
87
import com.marklogic.client.ext.file.DocumentFileProcessor;
98
import com.marklogic.client.ext.helper.LoggingObject;
109
import com.marklogic.client.io.Format;
1110
import com.marklogic.client.io.JacksonHandle;
11+
import com.marklogic.client.io.StringHandle;
1212
import org.springframework.util.FileCopyUtils;
1313

1414
import java.io.File;
@@ -75,48 +75,46 @@ protected void validateTdeTemplate(DocumentFile documentFile) {
7575
logger.warn("Could not read TDE template from file, will not validate; cause: " + e.getMessage());
7676
}
7777
if (fileContent != null) {
78-
try {
79-
ServerEvaluationCall call = null;
80-
if (Format.XML.equals(documentFile.getFormat())) {
81-
call = buildXqueryCall(documentFile, fileContent);
82-
} else if (Format.JSON.equals(documentFile.getFormat())) {
83-
call = buildJavascriptCall(documentFile, fileContent);
84-
} else {
85-
logger.info("Unrecognized file format, will not try to validate TDE template in file: " + file + "; format: " + documentFile.getFormat());
86-
}
78+
ServerEvaluationCall call = null;
79+
if (Format.XML.equals(documentFile.getFormat())) {
80+
call = buildXqueryCall(documentFile, fileContent);
81+
} else if (Format.JSON.equals(documentFile.getFormat())) {
82+
call = buildJavascriptCall(documentFile, fileContent);
83+
} else {
84+
logger.info("Unrecognized file format, will not try to validate TDE template in file: " + file + "; format: " + documentFile.getFormat());
85+
}
8786

88-
if (call != null) {
89-
ObjectNode node = (ObjectNode) call.eval(new JacksonHandle()).get();
90-
if (node.get("valid").asBoolean()) {
91-
logger.info("TDE template passed validation: " + file);
92-
} else {
93-
throw new RuntimeException(format("TDE template failed validation; file: %s; cause: %s", file, node.get("message").asText()));
94-
}
87+
if (call != null) {
88+
ObjectNode node = (ObjectNode) call.eval(new JacksonHandle()).get();
89+
if (node.get("valid").asBoolean()) {
90+
logger.info("TDE template passed validation: " + file);
91+
} else {
92+
throw new RuntimeException(format("TDE template failed validation; file: %s; cause: %s", file, node.get("message").asText()));
9593
}
96-
} catch (FailedRequestException e) {
97-
logger.warn("Unexpected error when trying to validate TDE template in file: " + file + "; cause: " + e.getMessage());
9894
}
9995
}
10096
}
10197
}
10298

10399
protected ServerEvaluationCall buildJavascriptCall(DocumentFile documentFile, String fileContent) {
104-
StringBuilder script = new StringBuilder("xdmp.invokeFunction(function() {var tde = require('/MarkLogic/tde.xqy');");
100+
StringBuilder script = new StringBuilder("var template; xdmp.invokeFunction(function() {var tde = require('/MarkLogic/tde.xqy');");
105101
script.append(format(
106-
"\nreturn tde.validate([xdmp.toJSON(%s)], ['%s'])}, {database: xdmp.database('%s')})",
107-
fileContent, documentFile.getUri(), tdeValidationDatabase
102+
"\nreturn tde.validate([xdmp.toJSON(template)], ['%s'])}, {database: xdmp.database('%s')})",
103+
documentFile.getUri(), tdeValidationDatabase
108104
));
109-
return databaseClient.newServerEval().javascript(script.toString());
105+
return databaseClient.newServerEval().javascript(script.toString())
106+
.addVariable("template", new StringHandle(fileContent).withFormat(Format.JSON));
110107
}
111108

112109
protected ServerEvaluationCall buildXqueryCall(DocumentFile documentFile, String fileContent) {
113110
StringBuilder script = new StringBuilder("import module namespace tde = 'http://marklogic.com/xdmp/tde' at '/MarkLogic/tde.xqy'; ");
114-
script.append(format("\nxdmp:invoke-function(function() { \nlet $t := %s ", fileContent));
111+
script.append("\ndeclare variable $template external; ");
112+
script.append("\nxdmp:invoke-function(function() { ");
115113
script.append(format(
116-
"\nreturn tde:validate($t, '%s')}, <options xmlns='xdmp:eval'><database>{xdmp:database('%s')}</database></options>)",
114+
"\ntde:validate($template, '%s')}, <options xmlns='xdmp:eval'><database>{xdmp:database('%s')}</database></options>)",
117115
documentFile.getUri(), tdeValidationDatabase
118116
));
119-
return databaseClient.newServerEval().xquery(script.toString());
117+
return databaseClient.newServerEval().xquery(script.toString()).addVariable("template", new StringHandle(fileContent).withFormat(Format.XML));
120118
}
121119

122120
public DatabaseClient getDatabaseClient() {

src/test/java/com/marklogic/client/ext/schemasloader/impl/ValidateTdeTemplatesTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,11 @@ public void validationDisabled() {
7171
List<DocumentFile> files = loader.loadSchemas(Paths.get("src", "test", "resources", "bad-schemas", "bad-json").toString());
7272
assertEquals("TDE validation is disabled, so the bad TDE template should have been loaded", 1, files.size());
7373
}
74+
75+
@Test
76+
public void mixedContent() {
77+
List<DocumentFile> files = loader.loadSchemas(Paths.get("src", "test", "resources", "good-schemas", "xml-schemas").toString());
78+
assertEquals("Verifying that the file still loads correctly even with processing instructions and comments in it",
79+
1, files.size());
80+
}
7481
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Hello top -->
3+
<template xmlns="http://marklogic.com/xdmp/tde">
4+
<!-- Hello middle -->
5+
<description>Sample description</description>
6+
<context>/Citation/Article</context>
7+
<rows>
8+
<row>
9+
<schema-name>Medical2</schema-name>
10+
<view-name>Publications</view-name>
11+
<columns>
12+
<column>
13+
<name>ID</name>
14+
<scalar-type>long</scalar-type>
15+
<val>../ID</val>
16+
</column>
17+
</columns>
18+
</row>
19+
</rows>
20+
</template>
21+
<!-- Hello bottom -->

0 commit comments

Comments
 (0)