Skip to content

Commit 9543567

Browse files
committed
fn:transform - fixes to IntelliJ code inspections
Accept changes proposed by IntelliJ code inspections.
1 parent 9ce9b10 commit 9543567

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

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

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.evolvedbinary.j8fu.tuple.Tuple2;
2626
import com.github.benmanes.caffeine.cache.Cache;
2727
import com.github.benmanes.caffeine.cache.Caffeine;
28-
import com.github.krukow.clj_lang.PersistentTreeMap;
2928
import io.lacuna.bifurcan.IEntry;
3029
import net.jpountz.xxhash.XXHash64;
3130
import net.jpountz.xxhash.XXHashFactory;
@@ -116,7 +115,7 @@ public class FnTransform extends BasicFunction {
116115
private static final Processor SAXON_PROCESSOR = new Processor(FnTransform.SAXON_CONFIGURATION);
117116

118117
static class SystemProperties {
119-
private static RetainedStaticContext retainedStaticContext = new RetainedStaticContext(SAXON_CONFIGURATION);
118+
private static final RetainedStaticContext retainedStaticContext = new RetainedStaticContext(SAXON_CONFIGURATION);
120119

121120
static String get(QName qName) {
122121
return SystemProperty.getProperty(qName.getNamespaceURI(), qName.getLocalPart(), retainedStaticContext);
@@ -252,9 +251,7 @@ private String executableHash(final Options options) {
252251
options.stylesheetNodeDocumentPath,
253252
options.stylesheetNodeDocumentPath).toString();
254253

255-
final String hash = Tuple(locationHash, paramHash).toString();
256-
257-
return hash;
254+
return Tuple(locationHash, paramHash).toString();
258255
}
259256

260257
@Override
@@ -285,6 +282,9 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
285282
// if we could not compile the xslt, rethrow the error
286283
throw compileException.value;
287284
}
285+
if (xsltExecutable == null) {
286+
throw new XPathException(this, ErrorCodes.FOXT0003, "Unable to compile stylesheet (No error returned from compilation)");
287+
}
288288

289289
final Xslt30Transformer xslt30Transformer = xsltExecutable.load30();
290290

@@ -365,7 +365,7 @@ private class TemplateInvocation {
365365
final Xslt30Transformer xslt30Transformer;
366366
final Map<URI, Delivery> resultDocuments;
367367

368-
TemplateInvocation(final Options options, final Optional<Source> sourceNode, final Delivery delivery, final Xslt30Transformer xslt30Transformer, final Map<URI, Delivery> resultDocuments) throws XPathException {
368+
TemplateInvocation(final Options options, final Optional<Source> sourceNode, final Delivery delivery, final Xslt30Transformer xslt30Transformer, final Map<URI, Delivery> resultDocuments) {
369369
this.options = options;
370370
this.sourceNode = sourceNode;
371371
this.delivery = delivery;
@@ -511,11 +511,7 @@ private MapType makeResultMap(final Options options, final Delivery delivery, fi
511511

512512
final MapType outputMap = new MapType(context);
513513
final AtomicValue outputKey;
514-
if (options.baseOutputURI.isPresent()) {
515-
outputKey = options.baseOutputURI.get();
516-
} else {
517-
outputKey = new StringValue("output");
518-
}
514+
outputKey = options.baseOutputURI.orElseGet(() -> new StringValue("output"));
519515

520516
final Sequence primaryValue = postProcess(outputKey, convertToDeliveryFormat(options, delivery), options.postProcess);
521517
outputMap.add(outputKey, primaryValue);
@@ -548,9 +544,12 @@ private Sequence convertToDeliveryFormat(final Options options, final Delivery d
548544
final XdmValue xdmValue = delivery.getXdmValue();
549545
if (xdmValue != null) {
550546
return Convert.ToExist.of(xdmValue);
551-
} else {
547+
}
548+
final DocumentImpl document = delivery.getDocument();
549+
if (document != null) {
552550
return rawOutput(delivery.getDocument());
553551
}
552+
throw new XPathException(ErrorCodes.FOXT0003, "No RAW output has been constructed by the transformation.");
554553
case DOCUMENT:
555554
default:
556555
return delivery.getDocument();
@@ -612,13 +611,13 @@ private Source resolveStylesheetLocation(final String stylesheetLocation) throws
612611
/**
613612
* Resolve an absolute stylesheet location
614613
*
615-
* @param location
616-
* @return
617-
* @throws XPathException
614+
* @param location of the stylesheet
615+
* @return the resolved stylesheet as a source
616+
* @throws XPathException if the item does not exist, or is not a document
618617
*/
619618
private Source resolvePossibleStylesheetLocation(final String location) throws XPathException {
620619

621-
Sequence document = null;
620+
Sequence document;
622621
try {
623622
document = DocUtils.getDocument(context, location);
624623
} catch (final PermissionDeniedException e) {
@@ -653,7 +652,6 @@ private AnyURIValue resolveURI(final AnyURIValue relative, final AnyURIValue bas
653652
} else {
654653
return new AnyURIValue(baseURI.resolve(relativeURI));
655654
}
656-
657655
}
658656

659657
/**
@@ -677,9 +675,7 @@ private Tuple2<String, Source> getStylesheet(final MapType options) throws XPath
677675
}
678676

679677
final Optional<String> stylesheetText = FnTransform.STYLESHEET_TEXT.get(options).map(StringValue::getStringValue);
680-
if (stylesheetText.isPresent()) {
681-
results.add(Tuple("", new StringSource(stylesheetText.get())));
682-
}
678+
stylesheetText.ifPresent(s -> results.add(Tuple("", new StringSource(s))));
683679

684680
if (results.size() > 1) {
685681
throw new XPathException(this, ErrorCodes.FOXT0002, "More than one of stylesheet-location, stylesheet-node, and stylesheet-text was set");
@@ -944,8 +940,7 @@ public void clear() {
944940

945941
public Optional<TransformerException> getWorst() {
946942
if (lastFatal.isPresent()) return lastFatal;
947-
if (lastError.isPresent()) return lastError;
948-
return Optional.empty();
943+
return lastError;
949944
}
950945

951946
public ErrorListenerLog4jAdapter(final Logger logger) {
@@ -986,13 +981,12 @@ public Reader getReader() {
986981
private enum DeliveryFormat {
987982
DOCUMENT,
988983
SERIALIZED,
989-
RAW;
990-
984+
RAW
991985
}
992986

993987
/**
994988
* Read options into class values in a single place.
995-
*
989+
* <p></p>
996990
* This is a bit clearer where we need an option several times,
997991
* we know we have read it up front.
998992
*/
@@ -1008,7 +1002,7 @@ private Map<net.sf.saxon.s9api.QName, XdmValue> readParamsMap(final Optional<Map
10081002
if (!(key instanceof QNameValue)) {
10091003
throw new XPathException(FnTransform.this, ErrorCodes.FOXT0002, "Supplied " + name + " is not a valid xs:qname: " + entry);
10101004
}
1011-
if (!(entry.value() instanceof Sequence)) {
1005+
if (entry.value() == null) {
10121006
throw new XPathException(FnTransform.this, ErrorCodes.FOXT0002, "Supplied " + name + " is not a valid xs:sequence: " + entry);
10131007
}
10141008
result.put(Convert.ToSaxon.of((QNameValue) key), toSaxon.of(entry.value()));
@@ -1048,7 +1042,7 @@ private Map<net.sf.saxon.s9api.QName, XdmValue> readParamsMap(final Optional<Map
10481042
if (!(entry.key() instanceof QNameValue)) {
10491043
throw new XPathException(FnTransform.this, ErrorCodes.FOXT0002, "Supplied stylesheet-param is not a valid xs:qname: " + entry);
10501044
}
1051-
if (!(entry.value() instanceof Sequence)) {
1045+
if (entry.value() == null) {
10521046
throw new XPathException(FnTransform.this, ErrorCodes.FOXT0002, "Supplied stylesheet-param is not a valid xs:sequence: " + entry);
10531047
}
10541048
}
@@ -1140,21 +1134,18 @@ private Optional<Long> getSourceTextChecksum(final MapType options) throws XPath
11401134

11411135
private String getStylesheetNodeDocumentPath(final MapType options) throws XPathException {
11421136
final Optional<Node> stylesheetNode = FnTransform.STYLESHEET_NODE.get(options).map(NodeValue::getNode);
1143-
if (stylesheetNode.isPresent()) {
1144-
return TreeUtils.pathTo(stylesheetNode.get()).toString();
1145-
}
1146-
return "";
1137+
return stylesheetNode.map(node -> TreeUtils.pathTo(node).toString()).orElse("");
11471138
}
11481139

11491140
private void validateRequestedProperties(final MapType requestedProperties) throws XPathException {
11501141
for (final IEntry<AtomicValue, Sequence> entry : requestedProperties) {
11511142
final AtomicValue key = entry.key();
11521143
if (!Type.subTypeOf(key.getType(), Type.QNAME)) {
1153-
throw new XPathException(ErrorCodes.XPTY0004, "Type error: requested-properties key: " + key.toString() + " is not a QName");
1144+
throw new XPathException(ErrorCodes.XPTY0004, "Type error: requested-properties key: " + key + " is not a QName");
11541145
}
11551146
final Sequence value = entry.value();
11561147
if (!value.hasOne()) {
1157-
throw new XPathException(ErrorCodes.XPTY0004, "Type error: requested-properties " + key.toString() + " does not have a single item value.");
1148+
throw new XPathException(ErrorCodes.XPTY0004, "Type error: requested-properties " + key + " does not have a single item value.");
11581149
}
11591150
final Item item = value.itemAt(0);
11601151
final String requiredPropertyValue;

0 commit comments

Comments
 (0)