implement-saxon-collation-uri-resolvers.dita 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE topic PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd">
  3. <!-- This file is part of the DITA Open Toolkit project. See the accompanying LICENSE file for applicable license. -->
  4. <topic id="implement-saxon-collation-uri-resolvers">
  5. <title>Implementing custom Saxon collation URI resolvers</title>
  6. <titlealts>
  7. <navtitle>Custom collation URI resolvers</navtitle>
  8. </titlealts>
  9. <shortdesc>Plug-ins can provide custom URI resolvers that provide collators for specific collation URIs.</shortdesc>
  10. <prolog>
  11. <metadata>
  12. <keywords>
  13. <indexterm>Saxon
  14. <indexterm><xmlelement>service</xmlelement></indexterm></indexterm>
  15. <indexterm>Ant
  16. <indexterm><xmlelement>jar</xmlelement></indexterm></indexterm>
  17. <indexterm><xmlatt>xsl:sort</xmlatt></indexterm>
  18. <indexterm>Chinese</indexterm>
  19. <indexterm>I18N
  20. <indexterm>plug-in</indexterm></indexterm>
  21. <indexterm>XSLT
  22. <indexterm>URI resolver</indexterm></indexterm>
  23. </keywords>
  24. </metadata>
  25. </prolog>
  26. <body>
  27. <p>To do custom sorting and grouping in XSLT, you identify collators using URIs that Saxon resolves to collator
  28. implementations. You implement the mapping from collation URIs to collators through custom collation URI
  29. resolvers.</p>
  30. <p>For example, the DITA Community I18N plugin provides a custom collator for doing dictionary-based sorting and
  31. grouping of Simplified Chinese. </p>
  32. <p>To allow multiple plug-ins to contribute collation URI resolvers, DITA-OT defines a superinterface of Saxon’s
  33. <codeph>CollationUriResolver</codeph> interface,
  34. <codeph>org.dita.dost.module.saxon.DelegatingCollationUriResolver</codeph>, that takes a base resolver.</p>
  35. <p>Implementations of <codeph>DelegatingCollationUriResolver</codeph> should delegate to their base resolver if they
  36. do not resolve the URI specified on the resolve request. When multiple plug-ins provide resolvers it results in a
  37. chain of resolvers, ending with the built-in Saxon default resolver.</p>
  38. <note>The order in which plug-ins will be processed during collation URI resolver configuration is variable, so two
  39. plug-ins should not try to resolve the same collation URI. In that case the first one configured will be used at
  40. run time.</note>
  41. <p>A typical delegating collation URI resolver looks like
  42. this:<codeblock outputclass="language-java">public class DCI18nCollationUriResolver implements DelegatingCollationUriResolver {
  43. public static final String DITA_COMMUNITY_I18N_ZH_CNAWARE_COLLATOR =
  44. "http://org.dita-community.i18n.zhCNawareCollator";
  45. public static final String LANG_URI_PARAM = "lang";
  46. private CollationURIResolver baseResolver;
  47. public DCI18nCollationUriResolver() {
  48. super();
  49. this.baseResolver = StandardCollationURIResolver.getInstance();
  50. }
  51. public net.sf.saxon.lib.StringCollator resolve(String uri, Configuration configuration)
  52. throws XPathException {
  53. ZhCnAwareCollator collator = resolveToZhCnAwareCollator(uri, null, configuration);
  54. if (null == collator) {
  55. return baseResolver.resolve(uri, configuration);
  56. }
  57. return (StringCollator) collator;
  58. }
  59. @Override
  60. public void setBaseResolver(CollationURIResolver baseResolver) {
  61. this.baseResolver = baseResolver;
  62. }
  63. /* ... Code to evaluate the collation URI and provide the appropriate collator goes here */
  64. }</codeblock></p>
  65. <p>To implement a custom collation URI resolver:
  66. <ol>
  67. <li>Add your plugin’s JAR file in the DITA-OT class path as described in
  68. <xref keyref="plugin-javalib"/>.</li>
  69. <li>Implement an instance of <codeph>org.dita.dost.module.saxon.DelegatingCollationUriResolver</codeph> as
  70. described above.</li>
  71. <li>Include a file named <filepath>org.dita.dost.module.saxon.DelegatingCollationUriResolver</filepath> in the
  72. directory <filepath>META-INF/services</filepath> in the compiled JAR that your plug-in provides. Each line of
  73. the file must be the name of a class that implements
  74. <codeph>org.dita.dost.module.saxon.DelegatingCollationUriResolver</codeph>:<codeblock>org.example.i18n.saxon.MyCollationUriResolver</codeblock>
  75. <p>You can create the services file using <xmlelement>service</xmlelement> elements in an Ant
  76. <xmlelement>jar</xmlelement>
  77. task:<codeblock outputclass="language-xml">&lt;jar destfile="${basedir}/target/lib/example-saxon.jar">
  78. &lt;service type="org.dita.dost.module.saxon.DelegatingCollationUriResolver">
  79. &lt;provider classname="org.example.i18n.saxon.MyCollationUriResolver"/>
  80. &lt;/service>
  81. &lt;/jar></codeblock></p></li>
  82. <li>To use the collator in XSLT style sheets, specify the collation URI on <xmlatt>xsl:sort</xmlatt> elements
  83. (or anywhere a collator URI can be
  84. specified):<codeblock outputclass="language-xml">&lt;xsl:apply-templates select="word">
  85. &lt;xsl:sort collation="http://org.example.i18n.MyCollator"/>
  86. &lt;/xsl:apply-templates></codeblock></li>
  87. </ol></p>
  88. </body>
  89. </topic>