Skip to content

Commit e3f69f2

Browse files
authored
API / XSL / Use XSL output configuration (formatter, editor). (geonetwork#7929)
* API / Formatter / Use XSL output configuration. Formatter API was using an XMLOutputter formatter overriding all configuration made in `xsl:output`. This was producing "invalid" HTML and some workaround were used so far. Now, return the output produced by saxon XMLIndenter or HTMLIndenter directly. * Editor / Fix formatting. The editor is an HTML page so don't use an XML formatter for the output. To format the XML in XML view, saxon is taking care of it using serialize and default-indent-mode. Use an HTML `pre` element which is not formatted by HTMLIndenter. * Formatter / Fix conflicting output when citation is used in xsl-view. * API / Formatter / iText for PDF expect xhtml document. If HTML, complains about not closed tags like links.
1 parent 7e83b2e commit e3f69f2

File tree

16 files changed

+139
-160
lines changed

16 files changed

+139
-160
lines changed

common/src/main/java/org/fao/geonet/utils/Xml.java

+16-46
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,7 @@
3939
import org.fao.geonet.utils.nio.NioPathAwareEntityResolver;
4040
import org.fao.geonet.utils.nio.NioPathHolder;
4141
import org.fao.geonet.utils.nio.PathStreamSource;
42-
import org.jdom.Attribute;
43-
import org.jdom.Content;
44-
import org.jdom.DocType;
45-
import org.jdom.Document;
46-
import org.jdom.Element;
47-
import org.jdom.JDOMException;
48-
import org.jdom.Namespace;
49-
import org.jdom.Text;
42+
import org.jdom.*;
5043
import org.jdom.filter.ElementFilter;
5144
import org.jdom.input.SAXBuilder;
5245
import org.jdom.output.Format;
@@ -64,27 +57,14 @@
6457
import javax.xml.XMLConstants;
6558
import javax.xml.bind.JAXBContext;
6659
import javax.xml.bind.Unmarshaller;
67-
import javax.xml.transform.Result;
68-
import javax.xml.transform.Source;
69-
import javax.xml.transform.Transformer;
70-
import javax.xml.transform.TransformerException;
71-
import javax.xml.transform.TransformerFactory;
72-
import javax.xml.transform.URIResolver;
60+
import javax.xml.transform.*;
7361
import javax.xml.transform.sax.SAXResult;
7462
import javax.xml.transform.stream.StreamResult;
7563
import javax.xml.transform.stream.StreamSource;
7664
import javax.xml.validation.Schema;
7765
import javax.xml.validation.SchemaFactory;
7866
import javax.xml.validation.ValidatorHandler;
79-
import java.io.BufferedOutputStream;
80-
import java.io.ByteArrayInputStream;
81-
import java.io.DataInputStream;
82-
import java.io.File;
83-
import java.io.IOException;
84-
import java.io.InputStream;
85-
import java.io.OutputStream;
86-
import java.io.PrintStream;
87-
import java.io.StringReader;
67+
import java.io.*;
8868
import java.net.HttpURLConnection;
8969
import java.net.URI;
9070
import java.net.URISyntaxException;
@@ -99,14 +79,7 @@
9979
import java.nio.file.Files;
10080
import java.nio.file.NoSuchFileException;
10181
import java.nio.file.Path;
102-
import java.util.ArrayList;
103-
import java.util.HashMap;
104-
import java.util.HashSet;
105-
import java.util.Iterator;
106-
import java.util.List;
107-
import java.util.Map;
108-
import java.util.Set;
109-
import java.util.UUID;
82+
import java.util.*;
11083
import java.util.regex.Matcher;
11184
import java.util.regex.Pattern;
11285

@@ -403,22 +376,16 @@ public static Element transform(Element xml, Path styleSheetPath, Map<String, Ob
403376
/**
404377
* Transforms an xml tree putting the result to a stream (uses a stylesheet on disk).
405378
*/
406-
public static void transform(Element xml, Path styleSheetPath, OutputStream out) throws Exception {
379+
public static void transform(Element xml, Path styleSheetPath, Map<String, Object> params, OutputStream out) throws Exception {
407380
StreamResult resStream = new StreamResult(out);
408-
transform(xml, styleSheetPath, resStream, null);
381+
transform(xml, styleSheetPath, resStream, params);
409382
out.flush();
410383
}
411384

412-
413-
public static void transformXml(Element xml, Path styleSheetPath, OutputStream out) throws Exception {
414-
StreamResult resStream = new StreamResult(out);
415-
Map<String, Object> map = new HashMap<>();
416-
map.put("geonet-force-xml", "xml");
417-
transform(xml, styleSheetPath, resStream, map);
418-
out.flush();
385+
public static void transform(Element xml, Path styleSheetPath, OutputStream out) throws Exception {
386+
transform(xml, styleSheetPath, new HashMap<>(), out);
419387
}
420388

421-
//--------------------------------------------------------------------------
422389

423390
/**
424391
* Transforms an xml tree putting the result to a stream - no parameters.
@@ -484,6 +451,9 @@ protected static Path resolvePath(Source s) throws URISyntaxException {
484451

485452
/**
486453
* Transforms an xml tree putting the result to a stream with optional parameters.
454+
* <p>
455+
* Add a geonet-force-xml parameter to force the formatting to be xml.
456+
* The preferred method is to define it using xsl:output.
487457
*/
488458
public static void
489459
transform(Element xml, Path styleSheetPath, Result result, Map<String, Object> params) throws Exception {
@@ -515,13 +485,13 @@ protected static Path resolvePath(Source s) throws URISyntaxException {
515485
t.setParameter(param.getKey(), param.getValue());
516486
}
517487

518-
if (params.containsKey("geonet-force-xml")) {
519-
((Controller) t).setOutputProperty("indent", "yes");
520-
((Controller) t).setOutputProperty("method", "xml");
521-
((Controller) t).setOutputProperty("{http://saxon.sf.net/}indent-spaces", "3");
488+
if (params.containsKey("geonet-force-xml")) {
489+
((Controller) t).setOutputProperty("indent", "yes");
490+
((Controller) t).setOutputProperty("method", "xml");
491+
((Controller) t).setOutputProperty("{http://saxon.sf.net/}indent-spaces", "2");
492+
}
522493
}
523494

524-
}
525495
t.transform(srcXml, result);
526496
}
527497
}

schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/formatter/citation/common.xsl

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
extension-element-prefixes="saxon"
1313
exclude-result-prefixes="#all">
1414

15+
1516
<xsl:param name="format"
1617
select="'html'"/>
1718

@@ -24,6 +25,7 @@
2425
<!-- What entity is responsible for producing and/or distributing the data set? Also, is there a physical location associated with the publisher? -->
2526
<xsl:param name="publisherRoles"
2627
select="'publisher'"/>
28+
2729
<xsl:variable name="publisherRolesList"
2830
select="tokenize($publisherRoles, ',')"/>
2931

@@ -115,7 +117,7 @@
115117
<blockquote>
116118
<div class="row">
117119
<div class="col-md-3">
118-
<i class="fa fa-quote-left pull-right"><xsl:comment select="'icon'"/></i>
120+
<i class="fa fa-quote-left pull-right"><xsl:comment/></i>
119121
</div>
120122
<div class="col-md-9">
121123
<p>

schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/formatter/citation/view.xsl

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
extension-element-prefixes="saxon"
1414
exclude-result-prefixes="#all">
1515

16+
<xsl:output omit-xml-declaration="yes"
17+
method="xml"
18+
indent="yes"
19+
saxon:indent-spaces="2"
20+
encoding="UTF-8"/>
21+
1622
<xsl:import href="base.xsl"/>
1723
<xsl:import href="common.xsl"/>
1824

schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/formatter/xsl-view/view.xsl

+26-26
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@
150150
<xsl:if test="count($tags/*) > 0">
151151
<section class="gn-md-side-social">
152152
<h2>
153-
<i class="fa fa-fw fa-tag"><xsl:comment select="'image'"/></i>
154-
<span><xsl:comment select="name()"/>
153+
<i class="fa fa-fw fa-tag"></i>
154+
<span>
155155
<xsl:value-of select="$schemaStrings/noThesaurusName"/>
156156
</span>
157157
</h2>
@@ -219,8 +219,8 @@
219219
<xsl:if test=".//mdb:identificationInfo/*/mri:extent">
220220
<section class="gn-md-side-extent">
221221
<h2>
222-
<i class="fa fa-fw fa-map-marker"><xsl:comment select="'image'"/></i>
223-
<span><xsl:comment select="name()"/>
222+
<i class="fa fa-fw fa-map-marker"></i>
223+
<span>
224224
<xsl:value-of select="$schemaStrings/spatialExtent"/>
225225
</span>
226226
</h2>
@@ -243,7 +243,7 @@
243243
<xsl:if test="mdb:identificationInfo/*/mri:graphicOverview">
244244
<section class="gn-md-side-overview">
245245
<h2>
246-
<i class="fa fa-fw fa-image"><xsl:comment select="'.'"/></i><xsl:comment select="'.'"/>
246+
<i class="fa fa-fw fa-image"></i>
247247
<span>
248248
<xsl:value-of select="$schemaStrings/overviews"/>
249249
</span>
@@ -295,7 +295,7 @@
295295
<xsl:if test="$withJsonLd = 'true'">
296296
<script type="application/ld+json">
297297
<xsl:apply-templates mode="getJsonLD"
298-
select="$metadata"/><xsl:comment select="'.'"/>
298+
select="$metadata"/>
299299
</script>
300300
</xsl:if>
301301
</xsl:template>
@@ -332,10 +332,10 @@
332332
<blockquote>
333333
<div class="row">
334334
<div class="col-md-1">
335-
<i class="fa fa-quote-left"><xsl:comment>Cite</xsl:comment></i>
335+
<i class="fa fa-quote-left"></i>
336336
</div>
337337
<div class="col-md-11">
338-
<h2 title="{$schemaStrings/citationProposal-help}"><xsl:comment select="name()"/>
338+
<h2 title="{$schemaStrings/citationProposal-help}">
339339
<xsl:value-of select="$schemaStrings/citationProposal"/>
340340
</h2>
341341

@@ -370,7 +370,7 @@
370370
<div data-ng-if="showCitation"
371371
data-gn-metadata-citation=""
372372
data-text="{$citation}">
373-
<xsl:comment>citation</xsl:comment>
373+
374374
</div>
375375
</xsl:when>
376376
<xsl:otherwise>
@@ -444,7 +444,7 @@
444444
<xsl:otherwise>
445445
<xsl:apply-templates mode="render-value" select="."/>
446446
</xsl:otherwise>
447-
</xsl:choose><xsl:comment select="'.'"/>
447+
</xsl:choose>
448448
<xsl:apply-templates mode="render-value" select="@*"/>
449449
</dd>
450450
</dl>
@@ -554,9 +554,9 @@
554554
<xsl:with-param name="languages" select="$allLanguages"/>
555555
</xsl:call-template>
556556
<xsl:apply-templates mode="render-value"
557-
select="@*"/><xsl:comment select="'.'"/>
557+
select="@*"/>
558558
</h2>
559-
<div class="target"><xsl:comment select="'.'"/>
559+
<div class="target">
560560
<xsl:apply-templates mode="render-field" select="*"/>
561561
</div>
562562
</div>
@@ -627,7 +627,7 @@
627627
<xsl:otherwise>
628628
<div class="gn-contact">
629629
<h4>
630-
<i class="fa fa-envelope"><xsl:comment select="'.'"/></i>
630+
<i class="fa fa-fw fa-envelope"></i>
631631
<xsl:apply-templates mode="render-value"
632632
select="*/cit:role/*/@codeListValue"/>
633633
</h4>
@@ -678,7 +678,7 @@
678678
<xsl:for-each select=".//cit:electronicMailAddress[not(gco:nilReason)]">
679679
<xsl:apply-templates mode="render-value-no-breaklines"
680680
select="."/>
681-
<xsl:if test="position() != last()">,<xsl:comment select="'.'"/></xsl:if>
681+
<xsl:if test="position() != last()">,</xsl:if>
682682
</xsl:for-each>
683683
</xsl:variable>
684684

@@ -699,7 +699,7 @@
699699

700700
<xsl:if test="$email != ''">
701701
<a href="mailto:{normalize-space($email)}">
702-
<sup class="fa fa-envelope">&#160;</sup>
702+
<sup class="fa fa-fw fa-envelope">&#160;</sup>
703703
</a>
704704
</xsl:if>
705705
</xsl:when>
@@ -712,11 +712,11 @@
712712
<xsl:choose>
713713
<xsl:when test="normalize-space($email) != ''">
714714
<a href="mailto:{normalize-space($email)}">
715-
<xsl:value-of select="$displayName"/><xsl:comment select="'.'"/>
715+
<xsl:value-of select="$displayName"/>
716716
</a>
717717
</xsl:when>
718718
<xsl:otherwise>
719-
<xsl:value-of select="$displayName"/><xsl:comment select="'.'"/>
719+
<xsl:value-of select="$displayName"/>
720720
</xsl:otherwise>
721721
</xsl:choose>
722722
</strong><br/>
@@ -741,7 +741,7 @@
741741
<xsl:variable name="phoneNumber">
742742
<xsl:apply-templates mode="render-value-no-breaklines" select="."/>
743743
</xsl:variable>
744-
<i class="fa fa-phone"><xsl:comment select="'.'"/></i>
744+
<i class="fa fa-fw fa-phone"></i>
745745
<a href="tel:{$phoneNumber}">
746746
<xsl:value-of select="$phoneNumber"/>
747747
</a>
@@ -752,9 +752,9 @@
752752
<xsl:variable name="phoneNumber">
753753
<xsl:apply-templates mode="render-value-no-breaklines" select="."/>
754754
</xsl:variable>
755-
<i class="fa fa-fax"><xsl:comment select="'.'"/></i>
755+
<i class="fa fa-fw fa-fax"></i>
756756
<a href="tel:{normalize-space($phoneNumber)}">
757-
<xsl:value-of select="normalize-space($phoneNumber)"/><xsl:comment select="'.'"/>(<xsl:value-of select="../cit:numberType/*/@codeListValue"/>)
757+
<xsl:value-of select="normalize-space($phoneNumber)"/> (<xsl:value-of select="../cit:numberType/*/@codeListValue"/>)
758758
</a>
759759
</div>
760760
</xsl:for-each>
@@ -770,7 +770,7 @@
770770
<xsl:variable name="linkage">
771771
<xsl:apply-templates mode="render-value-no-breaklines" select="."/>
772772
</xsl:variable>
773-
<i class="fa fa-link"><xsl:comment select="'.'"/></i>
773+
<i class="fa fa-fw fa-link"></i>
774774
<a href="{normalize-space($linkage)}" target="_blank">
775775
<xsl:value-of select="if (normalize-space($linkName) != '')
776776
then $linkName
@@ -838,7 +838,7 @@
838838
<xsl:apply-templates mode="render-value" select="@*"/>
839839

840840
<a class="btn btn-link" href="{$nodeUrl}api/records/{$metadataId}/formatters/xml" target="_blank">
841-
<i class="fa fa-file-code-o fa-2x"><xsl:comment select="'.'"/></i>
841+
<i class="fa fa-fw fa-file-code-o fa-2x"></i>
842842
<span><xsl:value-of select="$schemaStrings/metadataInXML"/></span>
843843
</a>
844844
</dd>
@@ -862,7 +862,7 @@
862862
</xsl:variable>
863863
<a href="{*/cit:linkage/*}" target="_blank">
864864
<xsl:apply-templates mode="render-value"
865-
select="if (*/cit:name != '') then */cit:name else */cit:linkage"/><xsl:comment select="'.'"/>
865+
select="if (*/cit:name != '') then */cit:name else */cit:linkage"/>
866866
</a>
867867
<p>
868868
<xsl:value-of select="normalize-space($linkDescription)"/>
@@ -1097,7 +1097,7 @@
10971097
<xsl:for-each select="parent::node()/*[name() = $nodeName]">
10981098
<li>
10991099
<a href="{$nodeUrl}api/records/{@uuidref}">
1100-
<i class="fa fa-link"><xsl:comment select="'.'"/></i>
1100+
<i class="fa fa-fw fa-link"></i>
11011101
<xsl:value-of select="gn-fn-render:getMetadataTitle(@uuidref, $language)"/>
11021102
</a>
11031103
</li>
@@ -1181,7 +1181,7 @@
11811181

11821182
<xsl:if test="@uom">
11831183
<!-- Display the unit value only -->
1184-
<xsl:comment select="'.'"/>&#160; <xsl:value-of select="if (contains(@uom, '#'))
1184+
&#160; <xsl:value-of select="if (contains(@uom, '#'))
11851185
then concat(., ' ', tokenize(@uom, '#')[2])
11861186
else concat(., ' ', @uom)"/>
11871187
</xsl:if>
@@ -1348,7 +1348,7 @@
13481348
<xsl:template mode="render-value"
13491349
match="@gco:nilReason[. = 'withheld']"
13501350
priority="100">
1351-
<i class="fa fa-lock text-warning" title="{{{{'withheld' | translate}}}}"><xsl:comment select="'.'"/></i>
1351+
<i class="fa fa-fw fa-lock text-warning" title="{{{{'withheld' | translate}}}}"></i>
13521352
</xsl:template>
13531353
<xsl:template mode="render-value"
13541354
match="@*"/>

schemas/iso19139/src/main/plugin/iso19139/formatter/citation/view.xsl

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
extension-element-prefixes="saxon"
1919
exclude-result-prefixes="#all">
2020

21+
<xsl:output omit-xml-declaration="yes"
22+
method="xml"
23+
indent="yes"
24+
saxon:indent-spaces="2"
25+
encoding="UTF-8"/>
26+
2127
<xsl:include href="base.xsl"/>
2228
<xsl:include href="../../../iso19115-3.2018/formatter/citation/common.xsl"/>
2329

0 commit comments

Comments
 (0)