Skip to content

Commit 339e40d

Browse files
committed
fn:transform - some sonarcloud observation fixes
Cleaned up the ones which make sense. Not cleaned up - functions with high complexity where the complexity seems essential to the function. Not cleaned up - old code we have just brushed past lightly, and which sonarcloud has consequently highlighted.
1 parent a27be64 commit 339e40d

File tree

4 files changed

+58
-58
lines changed

4 files changed

+58
-58
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/FnTransform.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.exist.xquery.value.Sequence;
3131
import org.exist.xquery.value.Type;
3232

33-
import static org.exist.Namespaces.XSL_NS;
3433
import static org.exist.xquery.FunctionDSL.param;
3534
import static org.exist.xquery.FunctionDSL.returnsOptMany;
3635
import static org.exist.xquery.functions.fn.FnModule.functionSignature;

exist-core/src/main/java/org/exist/xquery/functions/fn/transform/Convert.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@
4040

4141
/**
4242
* Type conversion to and from Saxon
43-
* <p>
44-
* TODO (AP) not yet remotely complete.
45-
* /p>
4643
*
4744
* <p>
4845
* Used to convert values to/from Saxon when we use Saxon as the XSLT transformer
@@ -64,6 +61,8 @@ private Convert() {
6461

6562
static class ToExist {
6663

64+
private ToExist() { super(); }
65+
6766
static Sequence of(final XdmValue xdmValue) throws XPathException {
6867
if (xdmValue.size() == 0) {
6968
return Sequence.EMPTY_SEQUENCE;
@@ -87,28 +86,26 @@ static Item ofItem(final XdmItem xdmItem) throws XPathException {
8786
} else if (atomicType == BuiltInAtomicType.DOUBLE) {
8887
return new DoubleValue(atomicValue.getStringValue());
8988
} else {
90-
// TODO (AP)
9189
throw new XPathException(ErrorCodes.XPTY0004,
92-
"net.sf.saxon.value.AtomicValue " + atomicValue +
93-
" could not be converted to an eXist Sequence");
90+
"net.sf.saxon.value.AtomicValue " + atomicValue + COULD_NOT_BE_CONVERTED + "atomic value");
9491
}
9592
} else if (xdmItem instanceof XdmNode) {
9693
return ToExist.ofNode((XdmNode)xdmItem);
9794
}
9895

9996
throw new XPathException(ErrorCodes.XPTY0004,
100-
"XdmItem " + xdmItem +
101-
" could not be converted to an eXist Sequence");
97+
"XdmItem " + xdmItem + COULD_NOT_BE_CONVERTED + "Sequence");
10298
}
10399

104100
static NodeValue ofNode(final XdmNode xdmNode) throws XPathException {
105101

106102
throw new XPathException(ErrorCodes.XPTY0004,
107-
"XdmNode " + xdmNode +
108-
" could not be converted to an eXist Node, Not yet implemented");
103+
"XdmNode " + xdmNode + COULD_NOT_BE_CONVERTED + " Node");
109104
}
110105
}
111106

107+
static final private String COULD_NOT_BE_CONVERTED = " could not be converted to an eXist ";
108+
112109
abstract static class ToSaxon {
113110

114111
abstract DocumentBuilder newDocumentBuilder();
@@ -129,8 +126,7 @@ XdmValue of(final Item item) throws XPathException {
129126
return ofNode((Node) item);
130127
}
131128
throw new XPathException(ErrorCodes.XPTY0004,
132-
"Item " + item + " of type " + Type.getTypeName(itemType) +
133-
" could not be converted to an XdmValue");
129+
"Item " + item + " of type " + Type.getTypeName(itemType) + COULD_NOT_BE_CONVERTED + "XdmValue");
134130
}
135131

136132
static private XdmValue ofAtomic(final AtomicValue atomicValue) throws XPathException {
@@ -147,7 +143,7 @@ static private XdmValue ofAtomic(final AtomicValue atomicValue) throws XPathExce
147143

148144
throw new XPathException(ErrorCodes.XPTY0004,
149145
"Atomic value " + atomicValue + " of type " + Type.getTypeName(itemType) +
150-
" could not be converted to an XdmValue");
146+
COULD_NOT_BE_CONVERTED + "XdmValue");
151147
}
152148

153149
private XdmValue ofNode(final Node node) throws XPathException {
@@ -158,18 +154,16 @@ private XdmValue ofNode(final Node node) throws XPathException {
158154
return sourceBuilder.build(new DOMSource(node));
159155
} else {
160156
//The source must be part of a document
161-
//TODO AP If it isn't, we don't know how to convert it
162157
final Document document = node.getOwnerDocument();
163158
if (document == null) {
164-
throw new XPathException(ErrorCodes.XPTY0004, "Node " + node + " could not be converted to an XdmValue, as it is not part of a document.");
159+
throw new XPathException(ErrorCodes.XPTY0004, "Node " + node + COULD_NOT_BE_CONVERTED + "XdmValue, as it is not part of a document.");
165160
}
166161
final List<Integer> nodeIndex = TreeUtils.treeIndex(node);
167162
final XdmNode xdmDocument = sourceBuilder.build(new DOMSource(document));
168-
final XdmNode xdmNode = TreeUtils.xdmNodeAtIndex(xdmDocument, nodeIndex);
169-
return xdmNode;
163+
return TreeUtils.xdmNodeAtIndex(xdmDocument, nodeIndex);
170164
}
171165
} catch (final SaxonApiException e) {
172-
throw new XPathException(ErrorCodes.XPTY0004, "Node " + node + " could not be converted to an XdmValue", e);
166+
throw new XPathException(ErrorCodes.XPTY0004, "Node " + node + COULD_NOT_BE_CONVERTED + "XdmValue", e);
173167
}
174168
}
175169

exist-core/src/main/java/org/exist/xquery/functions/fn/transform/SerializationParameters.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
class SerializationParameters {
4242

43-
static private class ParameterInfo {
43+
private static class ParameterInfo {
4444
final String defaultValue;
4545
final boolean hasMany;
4646
final List<Integer> types;
@@ -84,26 +84,33 @@ static String asValue(final Sequence sequence, final String defaultValue) throws
8484
}
8585
}
8686

87+
static final String ABSENT = "absent";
88+
static final String NONE = "none";
89+
static final String YES = "yes";
90+
static final String NO = "no";
91+
92+
static final String USE_CHARACTER_MAPS = "use-character-maps";
93+
8794
enum Param {
88-
ALLOW_DUPLICATE_NAMES(Type.BOOLEAN, "no"),
89-
BYTE_ORDER_MARK(Type.BOOLEAN, "no"),
95+
ALLOW_DUPLICATE_NAMES(Type.BOOLEAN, NO),
96+
BYTE_ORDER_MARK(Type.BOOLEAN, NO),
9097
CDATA_SECTION_ELEMENTS(Type.QNAME, "()", true),
91-
DOCTYPE_PUBLIC(Type.STRING, "absent"),
92-
DOCTYPE_SYSTEM(Type.STRING, "absent"),
98+
DOCTYPE_PUBLIC(Type.STRING, ABSENT),
99+
DOCTYPE_SYSTEM(Type.STRING, ABSENT),
93100
ENCODING(Type.STRING,"utf-8"),
94-
ESCAPE_URI_ATTRIBUTES(Type.BOOLEAN, "yes"),
101+
ESCAPE_URI_ATTRIBUTES(Type.BOOLEAN, YES),
95102
HTML_VERSION(Type.DECIMAL, "5"),
96-
INCLUDE_CONTENT_TYPE(Type.BOOLEAN, "yes"),
97-
INDENT(Type.BOOLEAN, "no"),
98-
ITEM_SEPARATOR(Type.STRING, "absent"),
103+
INCLUDE_CONTENT_TYPE(Type.BOOLEAN, YES),
104+
INDENT(Type.BOOLEAN, NO),
105+
ITEM_SEPARATOR(Type.STRING, ABSENT),
99106
//JSON_NODE_OUTPUT_METHOD
100107
MEDIA_TYPE(Type.STRING, ""),
101108
METHOD(Type.STRING, "xml"),
102-
NORMALIZATION_FORM(Type.STRING, "none"),
103-
OMIT_XML_DECLARATION(Type.BOOLEAN, "yes"),
109+
NORMALIZATION_FORM(Type.STRING, NONE),
110+
OMIT_XML_DECLARATION(Type.BOOLEAN, YES),
104111
STANDALONE(Type.BOOLEAN, "omit"),
105112
SUPPRESS_INDENTATION(Type.QNAME, "()", true),
106-
UNDECLARE_PREFIXES(Type.BOOLEAN, "no"),
113+
UNDECLARE_PREFIXES(Type.BOOLEAN, NO),
107114
USE_CHARACTER_MAPS(Type.MAP, "map{}"),
108115
VERSION(Type.STRING, "1.0");
109116

@@ -120,7 +127,7 @@ enum Param {
120127
}
121128
}
122129

123-
static private String getKeyValue(final IEntry<AtomicValue, Sequence> entry,
130+
private static String getKeyValue(final IEntry<AtomicValue, Sequence> entry,
124131
final BiFunction<ErrorCodes.ErrorCode, String, XPathException> errorBuilder) throws XPathException {
125132
if (!Type.subTypeOf(entry.key().getType(), Type.STRING)) {
126133
throw errorBuilder.apply(ErrorCodes.XPTY0004,
@@ -129,7 +136,7 @@ static private String getKeyValue(final IEntry<AtomicValue, Sequence> entry,
129136
return entry.key().getStringValue();
130137
}
131138

132-
static private Sequence getEntryValue(final IEntry<AtomicValue, Sequence> entry,
139+
private static Sequence getEntryValue(final IEntry<AtomicValue, Sequence> entry,
133140
final ParameterInfo parameterInfo,
134141
final BiFunction<ErrorCodes.ErrorCode, String, XPathException> errorBuilder) throws XPathException {
135142

@@ -160,7 +167,7 @@ static private Sequence getEntryValue(final IEntry<AtomicValue, Sequence> entry,
160167
"The value: " + entry.key() + " has multiple values in the sequence, and is required to have none or one.");
161168
}
162169

163-
static private Tuple3<String, Sequence, Param> getEntry(
170+
private static Tuple3<String, Sequence, Param> getEntry(
164171
final IEntry<AtomicValue, Sequence> entry,
165172
final BiFunction<ErrorCodes.ErrorCode, String, XPathException> errorBuilder) throws XPathException {
166173

@@ -258,7 +265,7 @@ static SerializationProperties combinePropertiesAndCharacterMaps(
258265
final SerializationProperties combinedProperties = overrideProperties.combineWith(baseProperties);
259266

260267
final List<String> baseCharacterMapKeys = new ArrayList<>();
261-
final Optional<String[]> baseCharacterMapString = Optional.ofNullable(baseProperties.getProperty("use-character-maps")).map(s -> s.split(" "));
268+
final Optional<String[]> baseCharacterMapString = Optional.ofNullable(baseProperties.getProperty(USE_CHARACTER_MAPS)).map(s -> s.split(" "));
262269
if (baseCharacterMapString.isPresent()) {
263270
for (final String s : baseCharacterMapString.get()) {
264271
if (!s.isEmpty()) {
@@ -267,7 +274,7 @@ static SerializationProperties combinePropertiesAndCharacterMaps(
267274
}
268275
}
269276

270-
final Optional<String> combinedCharacterMapKey = Optional.ofNullable(combinedProperties.getProperty("use-character-maps")).map(String::trim);
277+
final Optional<String> combinedCharacterMapKey = Optional.ofNullable(combinedProperties.getProperty(USE_CHARACTER_MAPS)).map(String::trim);
271278
if (combinedCharacterMapKey.isPresent()) {
272279
final List<CharacterMap> allMaps = new ArrayList<>();
273280
for (final String baseCharacterMapKey : baseCharacterMapKeys) {
@@ -280,7 +287,7 @@ static SerializationProperties combinePropertiesAndCharacterMaps(
280287
combinedProperties.getCharacterMapIndex().putCharacterMap(
281288
qNameCharacterMap,
282289
repairedCombinedMap);
283-
combinedProperties.setProperty("use-character-maps", qNameCharacterMap.getClarkName());
290+
combinedProperties.setProperty(USE_CHARACTER_MAPS, qNameCharacterMap.getClarkName());
284291
}
285292

286293
return combinedProperties;

exist-core/src/main/java/org/exist/xquery/functions/fn/transform/Transform.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
*/
8282
public class Transform {
8383

84-
private static final Logger LOGGER = LogManager.getLogger(org.exist.xquery.functions.fn.FnTransform.class);
84+
private static final Logger LOGGER = LogManager.getLogger(org.exist.xquery.functions.fn.transform.Transform.class);
8585
private static final org.exist.xquery.functions.fn.transform.Transform.ErrorListenerLog4jAdapter ERROR_LISTENER = new Transform.ErrorListenerLog4jAdapter(Transform.LOGGER);
8686

8787
//TODO(AR) if you want Saxon-EE features we need to set those in the Configuration
@@ -238,7 +238,7 @@ private XsltExecutable compileExecutable(final Options options) throws XPathExce
238238

239239
try {
240240
options.resolvedStylesheetBaseURI.ifPresent(anyURIValue -> options.xsltSource._2.setSystemId(anyURIValue.getStringValue()));
241-
return xsltCompiler.compile(options.xsltSource._2); // .compilePackage //TODO(AR) need to implement support for xslt-packages
241+
return xsltCompiler.compile(options.xsltSource._2); //TODO(AR) need to implement support for xslt-packages
242242
} catch (final SaxonApiException e) {
243243
final Optional<Exception> compilerException = errorListener.getWorst().map(e1 -> e1);
244244
throw originalXPathException("Could not compile stylesheet: ", compilerException.orElse(e), ErrorCodes.FOXT0003);
@@ -349,10 +349,10 @@ private MapType invokeCallTemplate() throws XPathException, SaxonApiException {
349349
"AND " + Options.INITIAL_TEMPLATE.name + " supplied indicating call-template invocation.");
350350
}
351351

352-
//TODO (AP) - Implement complete conversion in the {@link Convert} class
353-
//TODO (AP) - The saxDestination conversion loses type information in some cases
354-
//TODO (AP) - e.g. fn-transform-63 from XQTS has a <xsl:template name='main' as='xs:integer'>
355-
//TODO (AP) - which alongside "delivery-format":"raw" fails to deliver an int
352+
// Convert using our own {@link Convert} class
353+
// The saxDestination conversion loses type information in some cases
354+
// e.g. fn-transform-63 from XQTS has a <xsl:template name='main' as='xs:integer'>
355+
// which alongside "delivery-format":"raw" fails to deliver an int
356356

357357
final QName qName = options.initialTemplate.get().getQName();
358358
xslt30Transformer.callTemplate(Convert.ToSaxon.of(qName), destination);
@@ -392,29 +392,29 @@ private MapType invoke() throws XPathException, SaxonApiException {
392392
return invokeApplyTemplates();
393393
}
394394
}
395-
}
396395

397-
private MapType makeResultMap(final Options options, final Delivery primaryDelivery, final Map<URI, Delivery> resultDocuments) throws XPathException {
396+
private MapType makeResultMap(final Options options, final Delivery primaryDelivery, final Map<URI, Delivery> resultDocuments) throws XPathException {
398397

399-
try (final MapType outputMap = new MapType(context)) {
400-
final AtomicValue outputKey;
401-
outputKey = options.baseOutputURI.orElseGet(() -> new StringValue("output"));
398+
try (final MapType outputMap = new MapType(context)) {
399+
final AtomicValue outputKey;
400+
outputKey = options.baseOutputURI.orElseGet(() -> new StringValue("output"));
402401

403-
final Sequence primaryValue = postProcess(outputKey, primaryDelivery.convert(), options.postProcess);
404-
outputMap.add(outputKey, primaryValue);
402+
final Sequence primaryValue = postProcess(outputKey, primaryDelivery.convert(), options.postProcess);
403+
outputMap.add(outputKey, primaryValue);
405404

406-
for (final Map.Entry<URI, Delivery> resultDocument : resultDocuments.entrySet()) {
407-
final AnyURIValue key = new AnyURIValue(resultDocument.getKey());
408-
final Delivery secondaryDelivery = resultDocument.getValue();
409-
final Sequence value = postProcess(key, secondaryDelivery.convert(), options.postProcess);
410-
outputMap.add(key, value);
411-
}
405+
for (final Map.Entry<URI, Delivery> resultDocument : resultDocuments.entrySet()) {
406+
final AnyURIValue key = new AnyURIValue(resultDocument.getKey());
407+
final Delivery secondaryDelivery = resultDocument.getValue();
408+
final Sequence value = postProcess(key, secondaryDelivery.convert(), options.postProcess);
409+
outputMap.add(key, value);
410+
}
412411

413-
return outputMap;
412+
return outputMap;
413+
}
414414
}
415415
}
416416

417-
private Sequence postProcess(final AtomicValue key, final Sequence before, final Optional<FunctionReference> postProcessingFunction) throws XPathException {
417+
private Sequence postProcess(final AtomicValue key, final Sequence before, final Optional<FunctionReference> postProcessingFunction) throws XPathException {
418418
if (postProcessingFunction.isPresent()) {
419419
FunctionReference functionReference = postProcessingFunction.get();
420420
return functionReference.evalFunction(null, null, new Sequence[]{key, before});

0 commit comments

Comments
 (0)