Skip to content

Commit

Permalink
fn:transform - some sonarcloud observation fixes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alanpaxton committed Aug 9, 2022
1 parent a27be64 commit 339e40d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
Expand Up @@ -30,7 +30,6 @@
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.Type;

import static org.exist.Namespaces.XSL_NS;
import static org.exist.xquery.FunctionDSL.param;
import static org.exist.xquery.FunctionDSL.returnsOptMany;
import static org.exist.xquery.functions.fn.FnModule.functionSignature;
Expand Down
Expand Up @@ -40,9 +40,6 @@

/**
* Type conversion to and from Saxon
* <p>
* TODO (AP) not yet remotely complete.
* /p>
*
* <p>
* Used to convert values to/from Saxon when we use Saxon as the XSLT transformer
Expand All @@ -64,6 +61,8 @@ private Convert() {

static class ToExist {

private ToExist() { super(); }

static Sequence of(final XdmValue xdmValue) throws XPathException {
if (xdmValue.size() == 0) {
return Sequence.EMPTY_SEQUENCE;
Expand All @@ -87,28 +86,26 @@ static Item ofItem(final XdmItem xdmItem) throws XPathException {
} else if (atomicType == BuiltInAtomicType.DOUBLE) {
return new DoubleValue(atomicValue.getStringValue());
} else {
// TODO (AP)
throw new XPathException(ErrorCodes.XPTY0004,
"net.sf.saxon.value.AtomicValue " + atomicValue +
" could not be converted to an eXist Sequence");
"net.sf.saxon.value.AtomicValue " + atomicValue + COULD_NOT_BE_CONVERTED + "atomic value");
}
} else if (xdmItem instanceof XdmNode) {
return ToExist.ofNode((XdmNode)xdmItem);
}

throw new XPathException(ErrorCodes.XPTY0004,
"XdmItem " + xdmItem +
" could not be converted to an eXist Sequence");
"XdmItem " + xdmItem + COULD_NOT_BE_CONVERTED + "Sequence");
}

static NodeValue ofNode(final XdmNode xdmNode) throws XPathException {

throw new XPathException(ErrorCodes.XPTY0004,
"XdmNode " + xdmNode +
" could not be converted to an eXist Node, Not yet implemented");
"XdmNode " + xdmNode + COULD_NOT_BE_CONVERTED + " Node");
}
}

static final private String COULD_NOT_BE_CONVERTED = " could not be converted to an eXist ";

abstract static class ToSaxon {

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

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

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

private XdmValue ofNode(final Node node) throws XPathException {
Expand All @@ -158,18 +154,16 @@ private XdmValue ofNode(final Node node) throws XPathException {
return sourceBuilder.build(new DOMSource(node));
} else {
//The source must be part of a document
//TODO AP If it isn't, we don't know how to convert it
final Document document = node.getOwnerDocument();
if (document == null) {
throw new XPathException(ErrorCodes.XPTY0004, "Node " + node + " could not be converted to an XdmValue, as it is not part of a document.");
throw new XPathException(ErrorCodes.XPTY0004, "Node " + node + COULD_NOT_BE_CONVERTED + "XdmValue, as it is not part of a document.");
}
final List<Integer> nodeIndex = TreeUtils.treeIndex(node);
final XdmNode xdmDocument = sourceBuilder.build(new DOMSource(document));
final XdmNode xdmNode = TreeUtils.xdmNodeAtIndex(xdmDocument, nodeIndex);
return xdmNode;
return TreeUtils.xdmNodeAtIndex(xdmDocument, nodeIndex);
}
} catch (final SaxonApiException e) {
throw new XPathException(ErrorCodes.XPTY0004, "Node " + node + " could not be converted to an XdmValue", e);
throw new XPathException(ErrorCodes.XPTY0004, "Node " + node + COULD_NOT_BE_CONVERTED + "XdmValue", e);
}
}

Expand Down
Expand Up @@ -40,7 +40,7 @@

class SerializationParameters {

static private class ParameterInfo {
private static class ParameterInfo {
final String defaultValue;
final boolean hasMany;
final List<Integer> types;
Expand Down Expand Up @@ -84,26 +84,33 @@ static String asValue(final Sequence sequence, final String defaultValue) throws
}
}

static final String ABSENT = "absent";
static final String NONE = "none";
static final String YES = "yes";
static final String NO = "no";

static final String USE_CHARACTER_MAPS = "use-character-maps";

enum Param {
ALLOW_DUPLICATE_NAMES(Type.BOOLEAN, "no"),
BYTE_ORDER_MARK(Type.BOOLEAN, "no"),
ALLOW_DUPLICATE_NAMES(Type.BOOLEAN, NO),
BYTE_ORDER_MARK(Type.BOOLEAN, NO),
CDATA_SECTION_ELEMENTS(Type.QNAME, "()", true),
DOCTYPE_PUBLIC(Type.STRING, "absent"),
DOCTYPE_SYSTEM(Type.STRING, "absent"),
DOCTYPE_PUBLIC(Type.STRING, ABSENT),
DOCTYPE_SYSTEM(Type.STRING, ABSENT),
ENCODING(Type.STRING,"utf-8"),
ESCAPE_URI_ATTRIBUTES(Type.BOOLEAN, "yes"),
ESCAPE_URI_ATTRIBUTES(Type.BOOLEAN, YES),
HTML_VERSION(Type.DECIMAL, "5"),
INCLUDE_CONTENT_TYPE(Type.BOOLEAN, "yes"),
INDENT(Type.BOOLEAN, "no"),
ITEM_SEPARATOR(Type.STRING, "absent"),
INCLUDE_CONTENT_TYPE(Type.BOOLEAN, YES),
INDENT(Type.BOOLEAN, NO),
ITEM_SEPARATOR(Type.STRING, ABSENT),
//JSON_NODE_OUTPUT_METHOD
MEDIA_TYPE(Type.STRING, ""),
METHOD(Type.STRING, "xml"),
NORMALIZATION_FORM(Type.STRING, "none"),
OMIT_XML_DECLARATION(Type.BOOLEAN, "yes"),
NORMALIZATION_FORM(Type.STRING, NONE),
OMIT_XML_DECLARATION(Type.BOOLEAN, YES),
STANDALONE(Type.BOOLEAN, "omit"),
SUPPRESS_INDENTATION(Type.QNAME, "()", true),
UNDECLARE_PREFIXES(Type.BOOLEAN, "no"),
UNDECLARE_PREFIXES(Type.BOOLEAN, NO),
USE_CHARACTER_MAPS(Type.MAP, "map{}"),
VERSION(Type.STRING, "1.0");

Expand All @@ -120,7 +127,7 @@ enum Param {
}
}

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

static private Sequence getEntryValue(final IEntry<AtomicValue, Sequence> entry,
private static Sequence getEntryValue(final IEntry<AtomicValue, Sequence> entry,
final ParameterInfo parameterInfo,
final BiFunction<ErrorCodes.ErrorCode, String, XPathException> errorBuilder) throws XPathException {

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

static private Tuple3<String, Sequence, Param> getEntry(
private static Tuple3<String, Sequence, Param> getEntry(
final IEntry<AtomicValue, Sequence> entry,
final BiFunction<ErrorCodes.ErrorCode, String, XPathException> errorBuilder) throws XPathException {

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

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

final Optional<String> combinedCharacterMapKey = Optional.ofNullable(combinedProperties.getProperty("use-character-maps")).map(String::trim);
final Optional<String> combinedCharacterMapKey = Optional.ofNullable(combinedProperties.getProperty(USE_CHARACTER_MAPS)).map(String::trim);
if (combinedCharacterMapKey.isPresent()) {
final List<CharacterMap> allMaps = new ArrayList<>();
for (final String baseCharacterMapKey : baseCharacterMapKeys) {
Expand All @@ -280,7 +287,7 @@ static SerializationProperties combinePropertiesAndCharacterMaps(
combinedProperties.getCharacterMapIndex().putCharacterMap(
qNameCharacterMap,
repairedCombinedMap);
combinedProperties.setProperty("use-character-maps", qNameCharacterMap.getClarkName());
combinedProperties.setProperty(USE_CHARACTER_MAPS, qNameCharacterMap.getClarkName());
}

return combinedProperties;
Expand Down
Expand Up @@ -81,7 +81,7 @@
*/
public class Transform {

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

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

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

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

final QName qName = options.initialTemplate.get().getQName();
xslt30Transformer.callTemplate(Convert.ToSaxon.of(qName), destination);
Expand Down Expand Up @@ -392,29 +392,29 @@ private MapType invoke() throws XPathException, SaxonApiException {
return invokeApplyTemplates();
}
}
}

private MapType makeResultMap(final Options options, final Delivery primaryDelivery, final Map<URI, Delivery> resultDocuments) throws XPathException {
private MapType makeResultMap(final Options options, final Delivery primaryDelivery, final Map<URI, Delivery> resultDocuments) throws XPathException {

try (final MapType outputMap = new MapType(context)) {
final AtomicValue outputKey;
outputKey = options.baseOutputURI.orElseGet(() -> new StringValue("output"));
try (final MapType outputMap = new MapType(context)) {
final AtomicValue outputKey;
outputKey = options.baseOutputURI.orElseGet(() -> new StringValue("output"));

final Sequence primaryValue = postProcess(outputKey, primaryDelivery.convert(), options.postProcess);
outputMap.add(outputKey, primaryValue);
final Sequence primaryValue = postProcess(outputKey, primaryDelivery.convert(), options.postProcess);
outputMap.add(outputKey, primaryValue);

for (final Map.Entry<URI, Delivery> resultDocument : resultDocuments.entrySet()) {
final AnyURIValue key = new AnyURIValue(resultDocument.getKey());
final Delivery secondaryDelivery = resultDocument.getValue();
final Sequence value = postProcess(key, secondaryDelivery.convert(), options.postProcess);
outputMap.add(key, value);
}
for (final Map.Entry<URI, Delivery> resultDocument : resultDocuments.entrySet()) {
final AnyURIValue key = new AnyURIValue(resultDocument.getKey());
final Delivery secondaryDelivery = resultDocument.getValue();
final Sequence value = postProcess(key, secondaryDelivery.convert(), options.postProcess);
outputMap.add(key, value);
}

return outputMap;
return outputMap;
}
}
}

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

0 comments on commit 339e40d

Please sign in to comment.