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