/doc/tools/xslt/extensions/saxon643/com/nwalsh/saxon/FormatCallout.java

http://power-architect.googlecode.com/ · Java · 111 lines · 69 code · 18 blank · 24 comment · 14 complexity · fe0440bc804956bce66ab076537609af MD5 · raw file

  1. package com.nwalsh.saxon;
  2. import org.xml.sax.SAXException;
  3. import org.w3c.dom.*;
  4. import javax.xml.transform.TransformerException;
  5. import com.icl.saxon.om.NamePool;
  6. import com.icl.saxon.output.Emitter;
  7. import com.icl.saxon.tree.AttributeCollection;
  8. import com.nwalsh.saxon.Callout;
  9. /**
  10. * <p>Utility class for the Verbatim extension (ignore this).</p>
  11. *
  12. * <p>$Id: FormatCallout.java 903 2006-06-21 22:32:15Z johnson $</p>
  13. *
  14. * <p>Copyright (C) 2000, 2001 Norman Walsh.</p>
  15. *
  16. * <p><b>Change Log:</b></p>
  17. * <dl>
  18. * <dt>1.0</dt>
  19. * <dd><p>Initial release.</p></dd>
  20. * </dl>
  21. *
  22. * @author Norman Walsh
  23. * <a href="mailto:ndw@nwalsh.com">ndw@nwalsh.com</a>
  24. *
  25. * @see Verbatim
  26. *
  27. * @version $Id: FormatCallout.java 903 2006-06-21 22:32:15Z johnson $
  28. **/
  29. public abstract class FormatCallout {
  30. protected static final String foURI = "http://www.w3.org/1999/XSL/Format";
  31. protected static final String xhURI = "http://www.w3.org/1999/xhtml";
  32. protected boolean foStylesheet = false;
  33. protected NamePool namePool = null;
  34. public FormatCallout(NamePool nPool, boolean fo) {
  35. namePool = nPool;
  36. foStylesheet = fo;
  37. }
  38. public String areaLabel(Element area) {
  39. String label = null;
  40. if (area.hasAttribute("label")) {
  41. // If this area has a label, use it
  42. label = area.getAttribute("label");
  43. } else {
  44. // Otherwise, if its parent is an areaset and it has a label, use that
  45. Element parent = (Element) area.getParentNode();
  46. if (parent != null
  47. && parent.getLocalName().equalsIgnoreCase("areaset")
  48. && parent.hasAttribute("label")) {
  49. label = parent.getAttribute("label");
  50. }
  51. }
  52. return label;
  53. }
  54. public void startSpan(Emitter rtf)
  55. throws TransformerException {
  56. // no point in doing this for FO, right?
  57. if (!foStylesheet && namePool != null) {
  58. int spanName = namePool.allocate("", "", "span");
  59. AttributeCollection spanAttr = new AttributeCollection(namePool);
  60. int namespaces[] = new int[1];
  61. spanAttr.addAttribute("", "", "class", "CDATA", "co");
  62. rtf.startElement(spanName, spanAttr, namespaces, 0);
  63. }
  64. }
  65. public void endSpan(Emitter rtf)
  66. throws TransformerException {
  67. // no point in doing this for FO, right?
  68. if (!foStylesheet && namePool != null) {
  69. int spanName = namePool.allocate("", "", "span");
  70. rtf.endElement(spanName);
  71. }
  72. }
  73. public void formatTextCallout(Emitter rtfEmitter,
  74. Callout callout) {
  75. Element area = callout.getArea();
  76. int num = callout.getCallout();
  77. String userLabel = areaLabel(area);
  78. String label = "(" + num + ")";
  79. if (userLabel != null) {
  80. label = userLabel;
  81. }
  82. char chars[] = label.toCharArray();
  83. try {
  84. startSpan(rtfEmitter);
  85. rtfEmitter.characters(chars, 0, label.length());
  86. endSpan(rtfEmitter);
  87. } catch (TransformerException e) {
  88. System.out.println("Transformer Exception in formatTextCallout");
  89. }
  90. }
  91. public abstract void formatCallout(Emitter rtfEmitter,
  92. Callout callout);
  93. }