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

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