25
25
import com .evolvedbinary .j8fu .tuple .Tuple2 ;
26
26
import com .github .benmanes .caffeine .cache .Cache ;
27
27
import com .github .benmanes .caffeine .cache .Caffeine ;
28
- import com .github .krukow .clj_lang .PersistentTreeMap ;
29
28
import io .lacuna .bifurcan .IEntry ;
30
29
import net .jpountz .xxhash .XXHash64 ;
31
30
import net .jpountz .xxhash .XXHashFactory ;
@@ -116,7 +115,7 @@ public class FnTransform extends BasicFunction {
116
115
private static final Processor SAXON_PROCESSOR = new Processor (FnTransform .SAXON_CONFIGURATION );
117
116
118
117
static class SystemProperties {
119
- private static RetainedStaticContext retainedStaticContext = new RetainedStaticContext (SAXON_CONFIGURATION );
118
+ private static final RetainedStaticContext retainedStaticContext = new RetainedStaticContext (SAXON_CONFIGURATION );
120
119
121
120
static String get (QName qName ) {
122
121
return SystemProperty .getProperty (qName .getNamespaceURI (), qName .getLocalPart (), retainedStaticContext );
@@ -252,9 +251,7 @@ private String executableHash(final Options options) {
252
251
options .stylesheetNodeDocumentPath ,
253
252
options .stylesheetNodeDocumentPath ).toString ();
254
253
255
- final String hash = Tuple (locationHash , paramHash ).toString ();
256
-
257
- return hash ;
254
+ return Tuple (locationHash , paramHash ).toString ();
258
255
}
259
256
260
257
@ Override
@@ -285,6 +282,9 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
285
282
// if we could not compile the xslt, rethrow the error
286
283
throw compileException .value ;
287
284
}
285
+ if (xsltExecutable == null ) {
286
+ throw new XPathException (this , ErrorCodes .FOXT0003 , "Unable to compile stylesheet (No error returned from compilation)" );
287
+ }
288
288
289
289
final Xslt30Transformer xslt30Transformer = xsltExecutable .load30 ();
290
290
@@ -365,7 +365,7 @@ private class TemplateInvocation {
365
365
final Xslt30Transformer xslt30Transformer ;
366
366
final Map <URI , Delivery > resultDocuments ;
367
367
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 ) {
369
369
this .options = options ;
370
370
this .sourceNode = sourceNode ;
371
371
this .delivery = delivery ;
@@ -511,11 +511,7 @@ private MapType makeResultMap(final Options options, final Delivery delivery, fi
511
511
512
512
final MapType outputMap = new MapType (context );
513
513
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" ));
519
515
520
516
final Sequence primaryValue = postProcess (outputKey , convertToDeliveryFormat (options , delivery ), options .postProcess );
521
517
outputMap .add (outputKey , primaryValue );
@@ -548,9 +544,12 @@ private Sequence convertToDeliveryFormat(final Options options, final Delivery d
548
544
final XdmValue xdmValue = delivery .getXdmValue ();
549
545
if (xdmValue != null ) {
550
546
return Convert .ToExist .of (xdmValue );
551
- } else {
547
+ }
548
+ final DocumentImpl document = delivery .getDocument ();
549
+ if (document != null ) {
552
550
return rawOutput (delivery .getDocument ());
553
551
}
552
+ throw new XPathException (ErrorCodes .FOXT0003 , "No RAW output has been constructed by the transformation." );
554
553
case DOCUMENT :
555
554
default :
556
555
return delivery .getDocument ();
@@ -612,13 +611,13 @@ private Source resolveStylesheetLocation(final String stylesheetLocation) throws
612
611
/**
613
612
* Resolve an absolute stylesheet location
614
613
*
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
618
617
*/
619
618
private Source resolvePossibleStylesheetLocation (final String location ) throws XPathException {
620
619
621
- Sequence document = null ;
620
+ Sequence document ;
622
621
try {
623
622
document = DocUtils .getDocument (context , location );
624
623
} catch (final PermissionDeniedException e ) {
@@ -653,7 +652,6 @@ private AnyURIValue resolveURI(final AnyURIValue relative, final AnyURIValue bas
653
652
} else {
654
653
return new AnyURIValue (baseURI .resolve (relativeURI ));
655
654
}
656
-
657
655
}
658
656
659
657
/**
@@ -677,9 +675,7 @@ private Tuple2<String, Source> getStylesheet(final MapType options) throws XPath
677
675
}
678
676
679
677
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 ))));
683
679
684
680
if (results .size () > 1 ) {
685
681
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() {
944
940
945
941
public Optional <TransformerException > getWorst () {
946
942
if (lastFatal .isPresent ()) return lastFatal ;
947
- if (lastError .isPresent ()) return lastError ;
948
- return Optional .empty ();
943
+ return lastError ;
949
944
}
950
945
951
946
public ErrorListenerLog4jAdapter (final Logger logger ) {
@@ -986,13 +981,12 @@ public Reader getReader() {
986
981
private enum DeliveryFormat {
987
982
DOCUMENT ,
988
983
SERIALIZED ,
989
- RAW ;
990
-
984
+ RAW
991
985
}
992
986
993
987
/**
994
988
* Read options into class values in a single place.
995
- *
989
+ * <p></p>
996
990
* This is a bit clearer where we need an option several times,
997
991
* we know we have read it up front.
998
992
*/
@@ -1008,7 +1002,7 @@ private Map<net.sf.saxon.s9api.QName, XdmValue> readParamsMap(final Optional<Map
1008
1002
if (!(key instanceof QNameValue )) {
1009
1003
throw new XPathException (FnTransform .this , ErrorCodes .FOXT0002 , "Supplied " + name + " is not a valid xs:qname: " + entry );
1010
1004
}
1011
- if (!( entry .value () instanceof Sequence ) ) {
1005
+ if (entry .value () == null ) {
1012
1006
throw new XPathException (FnTransform .this , ErrorCodes .FOXT0002 , "Supplied " + name + " is not a valid xs:sequence: " + entry );
1013
1007
}
1014
1008
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
1048
1042
if (!(entry .key () instanceof QNameValue )) {
1049
1043
throw new XPathException (FnTransform .this , ErrorCodes .FOXT0002 , "Supplied stylesheet-param is not a valid xs:qname: " + entry );
1050
1044
}
1051
- if (!( entry .value () instanceof Sequence ) ) {
1045
+ if (entry .value () == null ) {
1052
1046
throw new XPathException (FnTransform .this , ErrorCodes .FOXT0002 , "Supplied stylesheet-param is not a valid xs:sequence: " + entry );
1053
1047
}
1054
1048
}
@@ -1140,21 +1134,18 @@ private Optional<Long> getSourceTextChecksum(final MapType options) throws XPath
1140
1134
1141
1135
private String getStylesheetNodeDocumentPath (final MapType options ) throws XPathException {
1142
1136
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 ("" );
1147
1138
}
1148
1139
1149
1140
private void validateRequestedProperties (final MapType requestedProperties ) throws XPathException {
1150
1141
for (final IEntry <AtomicValue , Sequence > entry : requestedProperties ) {
1151
1142
final AtomicValue key = entry .key ();
1152
1143
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" );
1154
1145
}
1155
1146
final Sequence value = entry .value ();
1156
1147
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." );
1158
1149
}
1159
1150
final Item item = value .itemAt (0 );
1160
1151
final String requiredPropertyValue ;
0 commit comments