Skip to content

Commit

Permalink
Use Apache Commons Text library to escape JSON content in xslt proces…
Browse files Browse the repository at this point in the history
…sing (geonetwork#7525)

* Use Apache Commons Text library to escape JSON content in xslt processing
* Maintain compatibility with plugins using the old funciton

Co-authored-by: Jose García <[email protected]>

---------

Co-authored-by: Juan Luis Rodríguez <[email protected]>
  • Loading branch information
josegar74 and juanluisrp authored Dec 11, 2023
1 parent 6d04991 commit ded6da6
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 169 deletions.
9 changes: 7 additions & 2 deletions core/src/main/java/org/fao/geonet/util/XslUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2001-2020 Food and Agriculture Organization of the
* Copyright (C) 2001-2023 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
Expand Down Expand Up @@ -39,6 +39,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
Expand Down Expand Up @@ -469,7 +470,7 @@ public static String getNodeId() {
return ApplicationContextHolder.get().getBean(org.fao.geonet.NodeInfo.class).getId();
}


public static String getNodeLogo(String key) {
Optional<Source> source = getSource(key);
return source.isPresent() ? source.get().getLogo() : "";
Expand Down Expand Up @@ -1706,4 +1707,8 @@ private static List<Element> buildRecordLink(SearchHits hits, String type) {
});
return listOfLinks;
}

public static String escapeForJson(String value) {
return StringEscapeUtils.escapeJson(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<xsl:variable name="resourceTitleObject" as="xs:string"
select="concat('{',
$doubleQuote, 'default', $doubleQuote, ':',
$doubleQuote, gn-fn-index:json-escape($mainTitle) ,$doubleQuote,
$doubleQuote, util:escapeForJson($mainTitle) ,$doubleQuote,
'}')"/>

<xsl:variable name="identifier" as="xs:string?"
Expand Down Expand Up @@ -150,30 +150,30 @@
</xsl:for-each>

<xsl:for-each select="dc:format">
<format><xsl:value-of select="gn-fn-index:json-escape(.)"/></format>
<format><xsl:value-of select="util:escapeForJson(.)"/></format>
</xsl:for-each>

<xsl:for-each select="dc:type[. != '']">
<resourceType><xsl:value-of select="gn-fn-index:json-escape(.)"/></resourceType>
<resourceType><xsl:value-of select="util:escapeForJson(.)"/></resourceType>
</xsl:for-each>

<xsl:for-each select="dc:source">
<lineage><xsl:value-of select="gn-fn-index:json-escape(.)"/></lineage>
<lineage><xsl:value-of select="util:escapeForJson(.)"/></lineage>
</xsl:for-each>

<!-- TODO Change mapping of dc:relation -->
<xsl:for-each select="dc:relation">
<related><xsl:value-of select="gn-fn-index:json-escape(.)"/></related>
<related><xsl:value-of select="util:escapeForJson(.)"/></related>
</xsl:for-each>

<!-- TODO Change mapping of dct:accessRights -->
<xsl:for-each select="dct:accessRights">
<useLimitation><xsl:value-of select="gn-fn-index:json-escape(.)"/></useLimitation>
<useLimitation><xsl:value-of select="util:escapeForJson(.)"/></useLimitation>
</xsl:for-each>

<!-- TODO Change mapping of dct:rights -->
<xsl:for-each select="dct:rights">
<useLimitation><xsl:value-of select="gn-fn-index:json-escape(.)"/></useLimitation>
<useLimitation><xsl:value-of select="util:escapeForJson(.)"/></useLimitation>
</xsl:for-each>

<xsl:variable name="allKeywords">
Expand All @@ -187,7 +187,7 @@
<keyword>
<values>
<value>
"default": <xsl:value-of select="concat($doubleQuote, gn-fn-index:json-escape(.), $doubleQuote)"/>
"default": <xsl:value-of select="concat($doubleQuote, util:escapeForJson(.), $doubleQuote)"/>
</value>
</values>
</keyword>
Expand All @@ -206,7 +206,7 @@
<keyword>
<values>
<value>
"default": <xsl:value-of select="concat($doubleQuote, gn-fn-index:json-escape(.), $doubleQuote)"/>
"default": <xsl:value-of select="concat($doubleQuote, util:escapeForJson(.), $doubleQuote)"/>
</value>
</values>
</keyword>
Expand All @@ -227,8 +227,8 @@
<!-- Index link where last token after the last / is the link name. -->
<link type="object">{
"protocol":"<xsl:value-of select="'WWW:LINK'"/>",
"urlObject":{"default": "<xsl:value-of select="gn-fn-index:json-escape(.)"/>"},
"nameObject":{"default": "<xsl:value-of select="gn-fn-index:json-escape($name)"/>"},
"urlObject":{"default": "<xsl:value-of select="util:escapeForJson(.)"/>"},
"nameObject":{"default": "<xsl:value-of select="util:escapeForJson($name)"/>"},
"descriptionObject":{"default": ""},
"function": ""
}</link>
Expand Down
32 changes: 16 additions & 16 deletions schemas/iso19110/src/main/plugin/iso19110/index-fields/index.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<xsl:variable name="resourceTitleObject" as="xs:string"
select="concat('{',
$doubleQuote, 'default', $doubleQuote, ':',
$doubleQuote, gn-fn-index:json-escape(.) ,$doubleQuote,
$doubleQuote, util:escapeForJson(.) ,$doubleQuote,
'}')"/>

<xsl:copy-of select="gn-fn-index:add-object-field(
Expand Down Expand Up @@ -97,11 +97,11 @@

<xsl:variable name="jsonFeatureTypes">[
<xsl:for-each select=".//gfc:featureType">{
"typeName" : "<xsl:value-of select="gn-fn-index:json-escape(gfc:FC_FeatureType/gfc:typeName/*/text())"/>",
"definition" :"<xsl:value-of select="gn-fn-index:json-escape(gfc:FC_FeatureType/gfc:definition/*/text())"/>",
"code" :"<xsl:value-of select="gn-fn-index:json-escape(gfc:FC_FeatureType/gfc:code/*/text())"/>",
"typeName" : "<xsl:value-of select="util:escapeForJson(gfc:FC_FeatureType/gfc:typeName/*/text())"/>",
"definition" :"<xsl:value-of select="util:escapeForJson(gfc:FC_FeatureType/gfc:definition/*/text())"/>",
"code" :"<xsl:value-of select="util:escapeForJson(gfc:FC_FeatureType/gfc:code/*/text())"/>",
"isAbstract" :"<xsl:value-of select="gfc:FC_FeatureType/gfc:isAbstract/*/text()"/>",
"aliases" : "<xsl:value-of select="gn-fn-index:json-escape(gfc:FC_FeatureType/gfc:aliases/*/text())"/>"
"aliases" : "<xsl:value-of select="util:escapeForJson(gfc:FC_FeatureType/gfc:aliases/*/text())"/>"
<!--"inheritsFrom" : "<xsl:value-of select="gfc:FC_FeatureType/gfc:inheritsFrom/*/text()"/>",
"inheritsTo" : "<xsl:value-of select="gfc:FC_FeatureType/gfc:inheritsTo/*/text()"/>",
"constrainedBy" : "<xsl:value-of select="gfc:FC_FeatureType/gfc:constrainedBy/*/text()"/>",
Expand All @@ -112,9 +112,9 @@
<xsl:if test="count($attributes) > 0">
,"attributeTable" : [
<xsl:for-each select="$attributes">
{"name": "<xsl:value-of select="gn-fn-index:json-escape(*/gfc:memberName/*/text())"/>",
"definition": "<xsl:value-of select="gn-fn-index:json-escape(*/gfc:definition/*/text())"/>",
"code": "<xsl:value-of select="gn-fn-index:json-escape(*/gfc:code/*/text())"/>",
{"name": "<xsl:value-of select="util:escapeForJson(*/gfc:memberName/*/text())"/>",
"definition": "<xsl:value-of select="util:escapeForJson(*/gfc:definition/*/text())"/>",
"code": "<xsl:value-of select="util:escapeForJson(*/gfc:code/*/text())"/>",
"link": "<xsl:value-of select="*/gfc:code/*/@xlink:href"/>",
"type": "<xsl:value-of select="*/gfc:valueType/gco:TypeName/gco:aName/*/text()"/>"
<xsl:if test="*/gfc:cardinality">
Expand All @@ -130,9 +130,9 @@
<xsl:if test="$codeList">
,"values": [
<xsl:for-each select="$codeList">{
"label": "<xsl:value-of select="gn-fn-index:json-escape(*/gfc:label/*/text())"/>",
"code": "<xsl:value-of select="gn-fn-index:json-escape(*/gfc:code/*/text())"/>",
"definition": "<xsl:value-of select="gn-fn-index:json-escape(*/gfc:definition/*/text())"/>"}
"label": "<xsl:value-of select="util:escapeForJson(*/gfc:label/*/text())"/>",
"code": "<xsl:value-of select="util:escapeForJson(*/gfc:code/*/text())"/>",
"definition": "<xsl:value-of select="util:escapeForJson(*/gfc:definition/*/text())"/>"}
<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
]
Expand Down Expand Up @@ -204,13 +204,13 @@
'organisation', $organisationName, $languages, true())"/>,
</xsl:if>
"role":"<xsl:value-of select="$role"/>",
"email":"<xsl:value-of select="gn-fn-index:json-escape($email[1])"/>",
"email":"<xsl:value-of select="util:escapeForJson($email[1])"/>",
"website":"<xsl:value-of select="$website"/>",
"logo":"<xsl:value-of select="$logo"/>",
"individual":"<xsl:value-of select="gn-fn-index:json-escape($individualName)"/>",
"position":"<xsl:value-of select="gn-fn-index:json-escape($positionName)"/>",
"phone":"<xsl:value-of select="gn-fn-index:json-escape($phone[1])"/>",
"address":"<xsl:value-of select="gn-fn-index:json-escape($address)"/>"
"individual":"<xsl:value-of select="util:escapeForJson($individualName)"/>",
"position":"<xsl:value-of select="util:escapeForJson($positionName)"/>",
"phone":"<xsl:value-of select="util:escapeForJson($phone[1])"/>",
"address":"<xsl:value-of select="util:escapeForJson($address)"/>"
}
</xsl:element>
</xsl:template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,9 @@
<xsl:variable name="p" select="normalize-space(cit:protocol/*/text())"/>
{
"@type":"DataDownload",
"contentUrl": "<xsl:value-of select="gn-fn-index:json-escape((cit:linkage/*/text())[1])" />"
"contentUrl": "<xsl:value-of select="util:escapeForJson((cit:linkage/*/text())[1])" />"
<xsl:if test="cit:protocol">,
"encodingFormat": "<xsl:value-of select="gn-fn-index:json-escape(if ($p != '') then $p else cit:protocol/*/@xlink:href)"/>"
"encodingFormat": "<xsl:value-of select="util:escapeForJson(if ($p != '') then $p else cit:protocol/*/@xlink:href)"/>"
</xsl:if>
<xsl:if test="cit:name">,
"name": <xsl:apply-templates mode="toJsonLDLocalized" select="cit:name"/>
Expand Down Expand Up @@ -426,7 +426,7 @@
select="$metadata/gmd:locale/*[concat('#', @id) = $languageId]/gmd:languageCode/*/@codeListValue"/>
{
<xsl:value-of select="concat('&quot;@value&quot;: &quot;',
gn-fn-index:json-escape(gmd:LocalisedCharacterString/text()),
util:escapeForJson(gmd:LocalisedCharacterString/text()),
'&quot;')"/>,
<xsl:value-of select="concat('&quot;@language&quot;: &quot;',
$languageCode,
Expand All @@ -441,14 +441,14 @@
<xsl:variable name="requestedValue"
select="lan:PT_FreeText/*/lan:LocalisedCharacterString[@id = $requestedLanguageId]/text()"/>
<xsl:value-of select="concat('&quot;',
gn-fn-index:json-escape(
util:escapeForJson(
if ($requestedValue != '') then $requestedValue else (gco:CharacterString|gcx:Anchor)),
'&quot;')"/>
</xsl:when>
<xsl:otherwise>
<!-- A simple property value -->
<xsl:value-of select="concat('&quot;',
gn-fn-index:json-escape(gco:CharacterString|gcx:Anchor),
util:escapeForJson(gco:CharacterString|gcx:Anchor),
'&quot;')"/>
</xsl:otherwise>
</xsl:choose>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@
then concat(' (', $contactInfo, ')') else ''"/>

<resourceTitleObject type="object">{
"default": "<xsl:value-of select="gn-fn-index:json-escape(
"default": "<xsl:value-of select="util:escapeForJson(
concat($org, $orgContactInfoSuffix))"/>"
<xsl:for-each
select="cit:party/cit:CI_Organisation/cit:name/lan:PT_FreeText/*/lan:LocalisedCharacterString[. != '']">
,"lang<xsl:value-of select="$allLanguages/lang[
@id = current()/@locale/substring(., 2, 2)
]/@value"/>": "<xsl:value-of select="gn-fn-index:json-escape(
]/@value"/>": "<xsl:value-of select="util:escapeForJson(
concat(., $orgContactInfoSuffix))"/>"
</xsl:for-each>
}
Expand All @@ -128,7 +128,7 @@
<xsl:copy-of select="gn-fn-index:add-field('Org', $org)"/>

<any type="object">{"common": "<xsl:value-of
select="gn-fn-index:json-escape(normalize-space(.))"/>"}
select="util:escapeForJson(normalize-space(.))"/>"}
</any>

<xsl:for-each
Expand All @@ -144,11 +144,11 @@
<xsl:variable name="org" select="normalize-space(cit:name/gco:CharacterString)"/>

<resourceTitleObject type="object">{
"default": "<xsl:value-of select="gn-fn-index:json-escape($org)"/>"
"default": "<xsl:value-of select="util:escapeForJson($org)"/>"
<xsl:for-each select="cit:name/lan:PT_FreeText/*/lan:LocalisedCharacterString[. != '']">
,"lang<xsl:value-of select="$allLanguages/lang[
@id = current()/@locale/substring(., 2, 2)
]/@value"/>": "<xsl:value-of select="gn-fn-index:json-escape(.)"/>"
]/@value"/>": "<xsl:value-of select="util:escapeForJson(.)"/>"
</xsl:for-each>
}
</resourceTitleObject>
Expand All @@ -168,7 +168,7 @@
<xsl:variable name="description"
select="mrs:referenceSystemIdentifier/*/mcc:description/*/text()"/>
<resourceTitleObject type="object">{
"default": "<xsl:value-of select="gn-fn-index:json-escape(if ($description != '')
"default": "<xsl:value-of select="util:escapeForJson(if ($description != '')
then concat($description, ' (', $code, ')')
else $code)"/>"
}
Expand All @@ -191,9 +191,9 @@

<resourceTitleObject type="object">{
"default": "<xsl:value-of select="if ($specifications != '' )
then gn-fn-index:json-escape($specifications)
then util:escapeForJson($specifications)
else if ($measures != '' )
then gn-fn-index:json-escape($measures)
then util:escapeForJson($measures)
else normalize-space(.)"/>"
}
</resourceTitleObject>
Expand All @@ -217,7 +217,7 @@
select="string-join(mco:useLimitations/*/text(), ', ')"/>

<resourceTitleObject type="object">{
"default": "<xsl:value-of select="gn-fn-index:json-escape(
"default": "<xsl:value-of select="util:escapeForJson(
if ($references != '')
then $references else if ($others != '')
then $others
Expand All @@ -238,7 +238,7 @@
select="concat('S:', .//gex:southBoundLatitude/*/text(), ', W:', .//gex:westBoundLongitude/*/text(), ', N:', .//gex:northBoundLatitude/*/text(), ', E:',.//gex:eastBoundLongitude/*/text())"/>

<resourceTitleObject type="object">{
"default": "<xsl:value-of select="gn-fn-index:json-escape(
"default": "<xsl:value-of select="util:escapeForJson(
if ($desc != '')
then $desc
else $name)"/>"
Expand All @@ -250,7 +250,7 @@

<xsl:template name="subtemplate-common-fields">
<any type="object">{"common": "<xsl:value-of
select="gn-fn-index:json-escape(normalize-space(.))"/>"}
select="util:escapeForJson(normalize-space(.))"/>"}
</any>
</xsl:template>

Expand Down
Loading

0 comments on commit ded6da6

Please sign in to comment.