Skip to content

Commit e286f73

Browse files
committed
Use Apache Commons Text library to escape JSON content in xslt processing
1 parent bf64d58 commit e286f73

File tree

13 files changed

+159
-167
lines changed

13 files changed

+159
-167
lines changed

core/src/main/java/org/fao/geonet/util/XslUtil.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.commons.io.IOUtils;
4040
import org.apache.commons.lang.NotImplementedException;
4141
import org.apache.commons.lang.StringUtils;
42+
import org.apache.commons.text.StringEscapeUtils;
4243
import org.apache.http.HttpResponse;
4344
import org.apache.http.client.HttpClient;
4445
import org.apache.http.client.methods.HttpGet;
@@ -469,7 +470,7 @@ public static String getNodeId() {
469470
return ApplicationContextHolder.get().getBean(org.fao.geonet.NodeInfo.class).getId();
470471
}
471472

472-
473+
473474
public static String getNodeLogo(String key) {
474475
Optional<Source> source = getSource(key);
475476
return source.isPresent() ? source.get().getLogo() : "";
@@ -1706,4 +1707,8 @@ private static List<Element> buildRecordLink(SearchHits hits, String type) {
17061707
});
17071708
return listOfLinks;
17081709
}
1710+
1711+
public static String escapeForJson(String value) {
1712+
return StringEscapeUtils.escapeJson(value);
1713+
}
17091714
}

schemas/dublin-core/src/main/plugin/dublin-core/index-fields/index.xsl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<xsl:variable name="resourceTitleObject" as="xs:string"
7272
select="concat('{',
7373
$doubleQuote, 'default', $doubleQuote, ':',
74-
$doubleQuote, gn-fn-index:json-escape($mainTitle) ,$doubleQuote,
74+
$doubleQuote, util:escapeForJson($mainTitle) ,$doubleQuote,
7575
'}')"/>
7676

7777
<xsl:variable name="identifier" as="xs:string?"
@@ -150,30 +150,30 @@
150150
</xsl:for-each>
151151

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

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

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

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

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

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

179179
<xsl:variable name="allKeywords">
@@ -187,7 +187,7 @@
187187
<keyword>
188188
<values>
189189
<value>
190-
"default": <xsl:value-of select="concat($doubleQuote, gn-fn-index:json-escape(.), $doubleQuote)"/>
190+
"default": <xsl:value-of select="concat($doubleQuote, util:escapeForJson(.), $doubleQuote)"/>
191191
</value>
192192
</values>
193193
</keyword>
@@ -206,7 +206,7 @@
206206
<keyword>
207207
<values>
208208
<value>
209-
"default": <xsl:value-of select="concat($doubleQuote, gn-fn-index:json-escape(.), $doubleQuote)"/>
209+
"default": <xsl:value-of select="concat($doubleQuote, util:escapeForJson(.), $doubleQuote)"/>
210210
</value>
211211
</values>
212212
</keyword>
@@ -227,8 +227,8 @@
227227
<!-- Index link where last token after the last / is the link name. -->
228228
<link type="object">{
229229
"protocol":"<xsl:value-of select="'WWW:LINK'"/>",
230-
"urlObject":{"default": "<xsl:value-of select="gn-fn-index:json-escape(.)"/>"},
231-
"nameObject":{"default": "<xsl:value-of select="gn-fn-index:json-escape($name)"/>"},
230+
"urlObject":{"default": "<xsl:value-of select="util:escapeForJson(.)"/>"},
231+
"nameObject":{"default": "<xsl:value-of select="util:escapeForJson($name)"/>"},
232232
"descriptionObject":{"default": ""},
233233
"function": ""
234234
}</link>

schemas/iso19110/src/main/plugin/iso19110/index-fields/index.xsl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<xsl:variable name="resourceTitleObject" as="xs:string"
5151
select="concat('{',
5252
$doubleQuote, 'default', $doubleQuote, ':',
53-
$doubleQuote, gn-fn-index:json-escape(.) ,$doubleQuote,
53+
$doubleQuote, util:escapeForJson(.) ,$doubleQuote,
5454
'}')"/>
5555

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

9898
<xsl:variable name="jsonFeatureTypes">[
9999
<xsl:for-each select=".//gfc:featureType">{
100-
"typeName" : "<xsl:value-of select="gn-fn-index:json-escape(gfc:FC_FeatureType/gfc:typeName/*/text())"/>",
101-
"definition" :"<xsl:value-of select="gn-fn-index:json-escape(gfc:FC_FeatureType/gfc:definition/*/text())"/>",
102-
"code" :"<xsl:value-of select="gn-fn-index:json-escape(gfc:FC_FeatureType/gfc:code/*/text())"/>",
100+
"typeName" : "<xsl:value-of select="util:escapeForJson(gfc:FC_FeatureType/gfc:typeName/*/text())"/>",
101+
"definition" :"<xsl:value-of select="util:escapeForJson(gfc:FC_FeatureType/gfc:definition/*/text())"/>",
102+
"code" :"<xsl:value-of select="util:escapeForJson(gfc:FC_FeatureType/gfc:code/*/text())"/>",
103103
"isAbstract" :"<xsl:value-of select="gfc:FC_FeatureType/gfc:isAbstract/*/text()"/>",
104-
"aliases" : "<xsl:value-of select="gn-fn-index:json-escape(gfc:FC_FeatureType/gfc:aliases/*/text())"/>"
104+
"aliases" : "<xsl:value-of select="util:escapeForJson(gfc:FC_FeatureType/gfc:aliases/*/text())"/>"
105105
<!--"inheritsFrom" : "<xsl:value-of select="gfc:FC_FeatureType/gfc:inheritsFrom/*/text()"/>",
106106
"inheritsTo" : "<xsl:value-of select="gfc:FC_FeatureType/gfc:inheritsTo/*/text()"/>",
107107
"constrainedBy" : "<xsl:value-of select="gfc:FC_FeatureType/gfc:constrainedBy/*/text()"/>",
@@ -112,9 +112,9 @@
112112
<xsl:if test="count($attributes) > 0">
113113
,"attributeTable" : [
114114
<xsl:for-each select="$attributes">
115-
{"name": "<xsl:value-of select="gn-fn-index:json-escape(*/gfc:memberName/*/text())"/>",
116-
"definition": "<xsl:value-of select="gn-fn-index:json-escape(*/gfc:definition/*/text())"/>",
117-
"code": "<xsl:value-of select="gn-fn-index:json-escape(*/gfc:code/*/text())"/>",
115+
{"name": "<xsl:value-of select="util:escapeForJson(*/gfc:memberName/*/text())"/>",
116+
"definition": "<xsl:value-of select="util:escapeForJson(*/gfc:definition/*/text())"/>",
117+
"code": "<xsl:value-of select="util:escapeForJson(*/gfc:code/*/text())"/>",
118118
"link": "<xsl:value-of select="*/gfc:code/*/@xlink:href"/>",
119119
"type": "<xsl:value-of select="*/gfc:valueType/gco:TypeName/gco:aName/*/text()"/>"
120120
<xsl:if test="*/gfc:cardinality">
@@ -130,9 +130,9 @@
130130
<xsl:if test="$codeList">
131131
,"values": [
132132
<xsl:for-each select="$codeList">{
133-
"label": "<xsl:value-of select="gn-fn-index:json-escape(*/gfc:label/*/text())"/>",
134-
"code": "<xsl:value-of select="gn-fn-index:json-escape(*/gfc:code/*/text())"/>",
135-
"definition": "<xsl:value-of select="gn-fn-index:json-escape(*/gfc:definition/*/text())"/>"}
133+
"label": "<xsl:value-of select="util:escapeForJson(*/gfc:label/*/text())"/>",
134+
"code": "<xsl:value-of select="util:escapeForJson(*/gfc:code/*/text())"/>",
135+
"definition": "<xsl:value-of select="util:escapeForJson(*/gfc:definition/*/text())"/>"}
136136
<xsl:if test="position() != last()">,</xsl:if>
137137
</xsl:for-each>
138138
]
@@ -204,13 +204,13 @@
204204
'organisation', $organisationName, $languages, true())"/>,
205205
</xsl:if>
206206
"role":"<xsl:value-of select="$role"/>",
207-
"email":"<xsl:value-of select="gn-fn-index:json-escape($email[1])"/>",
207+
"email":"<xsl:value-of select="util:escapeForJson($email[1])"/>",
208208
"website":"<xsl:value-of select="$website"/>",
209209
"logo":"<xsl:value-of select="$logo"/>",
210-
"individual":"<xsl:value-of select="gn-fn-index:json-escape($individualName)"/>",
211-
"position":"<xsl:value-of select="gn-fn-index:json-escape($positionName)"/>",
212-
"phone":"<xsl:value-of select="gn-fn-index:json-escape($phone[1])"/>",
213-
"address":"<xsl:value-of select="gn-fn-index:json-escape($address)"/>"
210+
"individual":"<xsl:value-of select="util:escapeForJson($individualName)"/>",
211+
"position":"<xsl:value-of select="util:escapeForJson($positionName)"/>",
212+
"phone":"<xsl:value-of select="util:escapeForJson($phone[1])"/>",
213+
"address":"<xsl:value-of select="util:escapeForJson($address)"/>"
214214
}
215215
</xsl:element>
216216
</xsl:template>

schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/formatter/jsonld/iso19115-3.2018-to-jsonld.xsl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@
305305
<xsl:variable name="p" select="normalize-space(cit:protocol/*/text())"/>
306306
{
307307
"@type":"DataDownload",
308-
"contentUrl": "<xsl:value-of select="gn-fn-index:json-escape((cit:linkage/*/text())[1])" />"
308+
"contentUrl": "<xsl:value-of select="util:escapeForJson((cit:linkage/*/text())[1])" />"
309309
<xsl:if test="cit:protocol">,
310-
"encodingFormat": "<xsl:value-of select="gn-fn-index:json-escape(if ($p != '') then $p else cit:protocol/*/@xlink:href)"/>"
310+
"encodingFormat": "<xsl:value-of select="util:escapeForJson(if ($p != '') then $p else cit:protocol/*/@xlink:href)"/>"
311311
</xsl:if>
312312
<xsl:if test="cit:name">,
313313
"name": <xsl:apply-templates mode="toJsonLDLocalized" select="cit:name"/>
@@ -426,7 +426,7 @@
426426
select="$metadata/gmd:locale/*[concat('#', @id) = $languageId]/gmd:languageCode/*/@codeListValue"/>
427427
{
428428
<xsl:value-of select="concat('&quot;@value&quot;: &quot;',
429-
gn-fn-index:json-escape(gmd:LocalisedCharacterString/text()),
429+
util:escapeForJson(gmd:LocalisedCharacterString/text()),
430430
'&quot;')"/>,
431431
<xsl:value-of select="concat('&quot;@language&quot;: &quot;',
432432
$languageCode,
@@ -441,14 +441,14 @@
441441
<xsl:variable name="requestedValue"
442442
select="lan:PT_FreeText/*/lan:LocalisedCharacterString[@id = $requestedLanguageId]/text()"/>
443443
<xsl:value-of select="concat('&quot;',
444-
gn-fn-index:json-escape(
444+
util:escapeForJson(
445445
if ($requestedValue != '') then $requestedValue else (gco:CharacterString|gcx:Anchor)),
446446
'&quot;')"/>
447447
</xsl:when>
448448
<xsl:otherwise>
449449
<!-- A simple property value -->
450450
<xsl:value-of select="concat('&quot;',
451-
gn-fn-index:json-escape(gco:CharacterString|gcx:Anchor),
451+
util:escapeForJson(gco:CharacterString|gcx:Anchor),
452452
'&quot;')"/>
453453
</xsl:otherwise>
454454
</xsl:choose>

schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/index-fields/index-subtemplate.xsl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@
113113
then concat(' (', $contactInfo, ')') else ''"/>
114114

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

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

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

146146
<resourceTitleObject type="object">{
147-
"default": "<xsl:value-of select="gn-fn-index:json-escape($org)"/>"
147+
"default": "<xsl:value-of select="util:escapeForJson($org)"/>"
148148
<xsl:for-each select="cit:name/lan:PT_FreeText/*/lan:LocalisedCharacterString[. != '']">
149149
,"lang<xsl:value-of select="$allLanguages/lang[
150150
@id = current()/@locale/substring(., 2, 2)
151-
]/@value"/>": "<xsl:value-of select="gn-fn-index:json-escape(.)"/>"
151+
]/@value"/>": "<xsl:value-of select="util:escapeForJson(.)"/>"
152152
</xsl:for-each>
153153
}
154154
</resourceTitleObject>
@@ -168,7 +168,7 @@
168168
<xsl:variable name="description"
169169
select="mrs:referenceSystemIdentifier/*/mcc:description/*/text()"/>
170170
<resourceTitleObject type="object">{
171-
"default": "<xsl:value-of select="gn-fn-index:json-escape(if ($description != '')
171+
"default": "<xsl:value-of select="util:escapeForJson(if ($description != '')
172172
then concat($description, ' (', $code, ')')
173173
else $code)"/>"
174174
}
@@ -191,9 +191,9 @@
191191

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

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

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

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

0 commit comments

Comments
 (0)