Debian Codegraph @

src/com/nwalsh/saxon/Verbatim.java

raw symbols SCIP
1 // Verbatim.java - Saxon extensions supporting DocBook verbatim environments
2
3 package com.nwalsh.saxon;
4
5 import java.util.Stack;
6 import java.util.StringTokenizer;
7 import org.xml.sax.*;
8 import org.w3c.dom.*;
9 import javax.xml.transform.TransformerException;
10 import com.icl.saxon.Controller;
11 import com.icl.saxon.expr.*;
12 import com.icl.saxon.om.*;
13 import com.icl.saxon.pattern.*;
14 import com.icl.saxon.Context;
15 import com.icl.saxon.tree.*;
16 import com.icl.saxon.functions.Extensions;
17 import com.nwalsh.saxon.NumberLinesEmitter;
18 import com.nwalsh.saxon.CalloutEmitter;
19
20 /**
21 * <p>Saxon extensions supporting DocBook verbatim environments</p>
22 *
23 * <p>$Id: Verbatim.java 7155 2007-07-26 21:34:48Z mzjn $</p>
24 *
25 * <p>Copyright (C) 2000 Norman Walsh.</p>
26 *
27 * <p>This class provides a
28 * <a href="http://saxon.sourceforge.net/">Saxon</a>
29 * implementation of two features that would be impractical to
30 * implement directly in XSLT: line numbering and callouts.</p>
31 *
32 * <p><b>Line Numbering</b></p>
33 * <p>The <tt>numberLines</tt> method takes a result tree
34 * fragment (assumed to contain the contents of a formatted verbatim
35 * element in DocBook: programlisting, screen, address, literallayout,
36 * or synopsis) and returns a result tree fragment decorated with
37 * line numbers.</p>
38 *
39 * <p><b>Callouts</b></p>
40 * <p>The <tt>insertCallouts</tt> method takes an
41 * <tt>areaspec</tt> and a result tree fragment
42 * (assumed to contain the contents of a formatted verbatim
43 * element in DocBook: programlisting, screen, address, literallayout,
44 * or synopsis) and returns a result tree fragment decorated with
45 * callouts.</p>
46 *
47 * <p><b>Change Log:</b></p>
48 * <dl>
49 * <dt>1.0</dt>
50 * <dd><p>Initial release.</p></dd>
51 * </dl>
52 *
53 * @author Norman Walsh
54 * <a href="mailto:[email protected]">[email protected]</a>
55 *
56 * @version $Id: Verbatim.java 7155 2007-07-26 21:34:48Z mzjn $
57 *
58 */
59 public class Verbatim {
60 /** True if the stylesheet is producing formatting objects */
61 private static boolean foStylesheet = false;
62 /** The modulus for line numbering (every 'modulus' line is numbered). */
63 private static int modulus = 0;
64 /** The width (in characters) of line numbers (for padding). */
65 private static int width = 0;
66 /** The starting line number. */
67 private static int startinglinenumber = 1;
68 /** The separator between the line number and the verbatim text. */
69 private static String separator = "";
70
71 /** True if callouts have been setup */
72 private static boolean calloutsSetup = false;
73 /** The default column for callouts that have only a line or line range */
74 private static int defaultColumn = 60;
75 /** The path to use for graphical callout decorations. */
76 private static String graphicsPath = null;
77 /** The extension to use for graphical callout decorations. */
78 private static String graphicsExt = null;
79 /** The largest callout number that can be represented graphically. */
80 private static int graphicsMax = 10;
81 /** The size of the callout icon. */
82 private static String iconSize = null;
83
84 /** The FormatCallout object to use for formatting callouts. */
85 private static FormatCallout fCallout = null;
86
87 /**
88 * <p>Constructor for Verbatim</p>
89 *
90 * <p>All of the methods are static, so the constructor does nothing.</p>
91 */
92 public Verbatim() {
93 }
94
95 /**
96 * <p>Find the string value of a stylesheet variable or parameter</p>
97 *
98 * <p>Returns the string value of <code>varName</code> in the current
99 * <code>context</code>. Returns the empty string if the variable is
100 * not defined.</p>
101 *
102 * @param context The current stylesheet context
103 * @param varName The name of the variable (without the dollar sign)
104 *
105 * @return The string value of the variable
106 */
107 protected static String getVariable(Context context, String varName) {
108 Value variable = null;
109 String varString = null;
110
111 try {
112 variable = Extensions.evaluate(context, "$" + varName);
113 varString = variable.asString();
114 return varString;
115 } catch (TransformerException te) {
116 System.out.println("Undefined variable: " + varName);
117 return "";
118 } catch (IllegalArgumentException iae) {
119 System.out.println("Undefined variable: " + varName);
120 return "";
121 }
122 }
123
124 /**
125 * <p>Setup the parameters associated with line numbering</p>
126 *
127 * <p>This method queries the stylesheet for the variables
128 * associated with line numbering. It is called automatically before
129 * lines are numbered. The context is used to retrieve the values,
130 * this allows templates to redefine these variables.</p>
131 *
132 * <p>The following variables are queried. If the variables do not
133 * exist, builtin defaults will be used (but you may also get a bunch
134 * of messages from the Java interpreter).</p>
135 *
136 * <dl>
137 * <dt><code>linenumbering.everyNth</code></dt>
138 * <dd>Specifies the lines that will be numbered. The first line is
139 * always numbered. (builtin default: 5).</dd>
140 * <dt><code>linenumbering.width</code></dt>
141 * <dd>Specifies the width of the numbers. If the specified width is too
142 * narrow for the largest number needed, it will automatically be made
143 * wider. (builtin default: 3).</dd>
144 * <dt><code>linenumbering.separator</code></dt>
145 * <dd>Specifies the string that separates line numbers from lines
146 * in the program listing. (builtin default: " ").</dd>
147 * <dt><code>linenumbering.startinglinenumber</code></dt>
148 * <dd>Specifies the initial line number
149 * in the program listing. (builtin default: "1").</dd>
150 * <dt><code>stylesheet.result.type</code></dt>
151 * <dd>Specifies the stylesheet result type. The value is either 'fo'
152 * (for XSL Formatting Objects) or it isn't. (builtin default: html).</dd>
153 * </dl>
154 *
155 * @param context The current stylesheet context
156 *
157 */
158 private static void setupLineNumbering(Context context) {
159 // Hardcoded defaults
160 modulus = 5;
161 width = 3;
162 startinglinenumber = 1;
163 separator = " ";
164 foStylesheet = false;
165
166 String varString = null;
167
168 // Get the modulus
169 varString = getVariable(context, "linenumbering.everyNth");
170 try {
171 modulus = Integer.parseInt(varString);
172 } catch (NumberFormatException nfe) {
173 System.out.println("$linenumbering.everyNth is not a number: " + varString);
174 }
175
176 // Get the width
177 varString = getVariable(context, "linenumbering.width");
178 try {
179 width = Integer.parseInt(varString);
180 } catch (NumberFormatException nfe) {
181 System.out.println("$linenumbering.width is not a number: " + varString);
182 }
183
184 // Get the startinglinenumber
185 varString = getVariable(context, "linenumbering.startinglinenumber");
186 try {
187 startinglinenumber = Integer.parseInt(varString);
188 } catch (NumberFormatException nfe) {
189 System.out.println("$linenumbering.startinglinenumber is not a number: " + varString);
190 }
191
192 // Get the separator
193 varString = getVariable(context, "linenumbering.separator");
194 separator = varString;
195
196 // Get the stylesheet type
197 varString = getVariable(context, "stylesheet.result.type");
198 foStylesheet = (varString.equals("fo"));
199 }
200
201 /**
202 * <p>Number lines in a verbatim environment</p>
203 *
204 * <p>The extension function expects the following variables to be
205 * available in the calling context: $linenumbering.everyNth,
206 * $linenumbering.width, $linenumbering.separator, and
207 * $stylesheet.result.type.</p>
208 *
209 * <p>This method adds line numbers to a result tree fragment. Each
210 * newline that occurs in a text node is assumed to start a new line.
211 * The first line is always numbered, every subsequent 'everyNth' line
212 * is numbered (so if everyNth=5, lines 1, 5, 10, 15, etc. will be
213 * numbered. If there are fewer than everyNth lines in the environment,
214 * every line is numbered.</p>
215 *
216 * <p>Every line number will be right justified in a string 'width'
217 * characters long. If the line number of the last line in the
218 * environment is too long to fit in the specified width, the width
219 * is automatically increased to the smallest value that can hold the
220 * number of the last line. (In other words, if you specify the value 2
221 * and attempt to enumerate the lines of an environment that is 100 lines
222 * long, the value 3 will automatically be used for every line in the
223 * environment.)</p>
224 *
225 * <p>The 'separator' string is inserted between the line
226 * number and the original program listing. Lines that aren't numbered
227 * are preceded by a 'width' blank string and the separator.</p>
228 *
229 * <p>If inline markup extends across line breaks, markup changes are
230 * required. All the open elements are closed before the line break and
231 * "reopened" afterwards. The reopened elements will have the same
232 * attributes as the originals, except that 'name' and 'id' attributes
233 * are not duplicated if the stylesheet.result.type is "html" and
234 * 'id' attributes will not be duplicated if the result type is "fo".</p>
235 *
236 * @param context The current stylesheet context.
237 * @param rtf_ns The result tree fragment of the verbatim environment.
238 *
239 * @return The modified result tree fragment.
240 */
241 public static NodeSetValue numberLines (Context context,
242 NodeSetValue rtf_ns) {
243
244 FragmentValue rtf = (FragmentValue) rtf_ns;
245
246 setupLineNumbering(context);
247
248 try {
249 LineCountEmitter lcEmitter = new LineCountEmitter();
250 rtf.replay(lcEmitter);
251 int numLines = lcEmitter.lineCount();
252
253 int listingModulus = numLines < modulus ? 1 : modulus;
254
255 double log10numLines = Math.log(numLines) / Math.log(10);
256
257 int listingWidth = width < log10numLines+1
258 ? (int) Math.floor(log10numLines + 1)
259 : width;
260
261 Controller controller = context.getController();
262 NamePool namePool = controller.getNamePool();
263 NumberLinesEmitter nlEmitter = new NumberLinesEmitter(controller,
264 namePool,
265 startinglinenumber,
266 listingModulus,
267 listingWidth,
268 separator,
269 foStylesheet);
270 rtf.replay(nlEmitter);
271 return nlEmitter.getResultTreeFragment();
272 } catch (TransformerException e) {
273 // This "can't" happen.
274 System.out.println("Transformer Exception in numberLines");
275 return rtf;
276 }
277 }
278
279 /**
280 * <p>Setup the parameters associated with callouts</p>
281 *
282 * <p>This method queries the stylesheet for the variables
283 * associated with line numbering. It is called automatically before
284 * callouts are processed. The context is used to retrieve the values,
285 * this allows templates to redefine these variables.</p>
286 *
287 * <p>The following variables are queried. If the variables do not
288 * exist, builtin defaults will be used (but you may also get a bunch
289 * of messages from the Java interpreter).</p>
290 *
291 * <dl>
292 * <dt><code>callout.graphics</code></dt>
293 * <dd>Are we using callout graphics? A value of 0 or "" is false,
294 * any other value is true. If callout graphics are not used, the
295 * parameters related to graphis are not queried.</dd>
296 * <dt><code>callout.graphics.path</code></dt>
297 * <dd>Specifies the path to callout graphics.</dd>
298 * <dt><code>callout.graphics.extension</code></dt>
299 * <dd>Specifies the extension ot use for callout graphics.</dd>
300 * <dt><code>callout.graphics.number.limit</code></dt>
301 * <dd>Identifies the largest number that can be represented as a
302 * graphic. Larger callout numbers will be represented using text.</dd>
303 * <dt><code>callout.defaultcolumn</code></dt>
304 * <dd>Specifies the default column for callout bullets that do not
305 * specify a column.</dd>
306 * <dt><code>stylesheet.result.type</code></dt>
307 * <dd>Specifies the stylesheet result type. The value is either 'fo'
308 * (for XSL Formatting Objects) or it isn't. (builtin default: html).</dd>
309 * </dl>
310 *
311 * @param context The current stylesheet context
312 *
313 */
314 private static void setupCallouts(Context context) {
315 NamePool namePool = context.getController().getNamePool();
316
317 boolean useGraphics = false;
318 boolean useUnicode = false;
319
320 int unicodeStart = 49;
321 int unicodeMax = 0;
322
323 String unicodeFont = "";
324
325 // Hardcoded defaults
326 defaultColumn = 60;
327 graphicsPath = null;
328 graphicsExt = null;
329 graphicsMax = 0;
330 iconSize = "7pt";
331 foStylesheet = false;
332 calloutsSetup = true;
333
334 Value variable = null;
335 String varString = null;
336
337 // Get the stylesheet type
338 varString = getVariable(context, "stylesheet.result.type");
339 foStylesheet = (varString.equals("fo"));
340
341 // Get the default column
342 varString = getVariable(context, "callout.defaultcolumn");
343 try {
344 defaultColumn = Integer.parseInt(varString);
345 } catch (NumberFormatException nfe) {
346 System.out.println("$callout.defaultcolumn is not a number: "
347 + varString);
348 }
349
350 // Use graphics at all?
351 varString = getVariable(context, "callout.graphics");
352 useGraphics = !(varString.equals("0") || varString.equals(""));
353
354 // Use unicode at all?
355 varString = getVariable(context, "callout.unicode");
356 useUnicode = !(varString.equals("0") || varString.equals(""));
357
358 if (useGraphics) {
359 // Get the graphics path
360 varString = getVariable(context, "callout.graphics.path");
361 graphicsPath = varString;
362
363 // Get the graphics extension
364 varString = getVariable(context, "callout.graphics.extension");
365 graphicsExt = varString;
366
367 // Get the number limit
368 varString = getVariable(context, "callout.graphics.number.limit");
369 try {
370 graphicsMax = Integer.parseInt(varString);
371 } catch (NumberFormatException nfe) {
372 System.out.println("$callout.graphics.number.limit is not a number: "
373 + varString);
374 graphicsMax = 0;
375 }
376
377 // Get the callout icon size (used for FO)
378 if (foStylesheet) {
379 varString = getVariable(context, "callout.icon.size");
380 iconSize = varString;
381 }
382
383 fCallout = new FormatGraphicCallout(namePool,
384 graphicsPath,
385 graphicsExt,
386 graphicsMax,
387 iconSize,
388 foStylesheet);
389 } else if (useUnicode) {
390 // Get the starting character
391 varString = getVariable(context, "callout.unicode.start.character");
392 try {
393 unicodeStart = Integer.parseInt(varString);
394 } catch (NumberFormatException nfe) {
395 System.out.println("$callout.unicode.start.character is not a number: "
396 + varString);
397 unicodeStart = 48;
398 }
399
400 // Get the number limit
401 varString = getVariable(context, "callout.unicode.number.limit");
402 try {
403 unicodeMax = Integer.parseInt(varString);
404 } catch (NumberFormatException nfe) {
405 System.out.println("$callout.unicode.number.limit is not a number: "
406 + varString);
407 unicodeStart = 0;
408 }
409
410 // Get the font
411 unicodeFont = getVariable(context, "callout.unicode.font");
412 if (unicodeFont == null) {
413 unicodeFont = "";
414 }
415
416 fCallout = new FormatUnicodeCallout(namePool,
417 unicodeFont,
418 unicodeStart,
419 unicodeMax,
420 foStylesheet);
421 } else {
422 fCallout = new FormatTextCallout(namePool, foStylesheet);
423 }
424 }
425
426 /**
427 * <p>Insert text callouts into a verbatim environment.</p>
428 *
429 * <p>This method examines the <tt>areaset</tt> and <tt>area</tt> elements
430 * in the supplied <tt>areaspec</tt> and decorates the supplied
431 * result tree fragment with appropriate callout markers.</p>
432 *
433 * <p>If a <tt>label</tt> attribute is supplied on an <tt>area</tt>,
434 * its content will be used for the label, otherwise the callout
435 * number will be used, surrounded by parenthesis. Callout numbers may
436 * also be represented as graphics. Callouts are
437 * numbered in document order. All of the <tt>area</tt>s in an
438 * <tt>areaset</tt> get the same number.</p>
439 *
440 * <p>Only the <tt>linecolumn</tt> and <tt>linerange</tt> units are
441 * supported. If no unit is specifed, <tt>linecolumn</tt> is assumed.
442 * If only a line is specified, the callout decoration appears in
443 * the defaultColumn. Lines will be padded with blanks to reach the
444 * necessary column, but callouts that are located beyond the last
445 * line of the verbatim environment will be ignored.</p>
446 *
447 * <p>Callouts are inserted before the character at the line/column
448 * where they are to occur.</p>
449 *
450 * <p>If graphical callouts are used, and the callout number is less
451 * than or equal to the $callout.graphics.number.limit, the following image
452 * will be generated for HTML:
453 *
454 * <pre>
455 * &lt;img src="$callout.graphics.path/999$callout.graphics.ext"
456 * alt="conumber">
457 * </pre>
458 *
459 * If the $stylesheet.result.type is 'fo', the following image will
460 * be generated:
461 *
462 * <pre>
463 * &lt;fo:external-graphic src="$callout.graphics.path/999$callout.graphics.ext"/>
464 * </pre>
465 *
466 * <p>If the callout number exceeds $callout.graphics.number.limit,
467 * the callout will be the callout number surrounded by
468 * parenthesis.</p>
469 *
470 * @param context The stylesheet context.
471 * @param areaspecNodeList The source node set that contains the areaspec.
472 * @param rtf_ns The result tree fragment of the verbatim environment.
473 *
474 * @return The modified result tree fragment.
475 */
476 public static NodeSetValue insertCallouts (Context context,
477 NodeList areaspecNodeList,
478 NodeSetValue rtf_ns) {
479
480 FragmentValue rtf = (FragmentValue) rtf_ns;
481
482 setupCallouts(context);
483
484 try {
485 Controller controller = context.getController();
486 NamePool namePool = controller.getNamePool();
487 CalloutEmitter cEmitter = new CalloutEmitter(controller,
488 namePool,
489 defaultColumn,
490 foStylesheet,
491 fCallout);
492 cEmitter.setupCallouts(areaspecNodeList);
493 rtf.replay(cEmitter);
494 return cEmitter.getResultTreeFragment();
495 } catch (TransformerException e) {
496 // This "can't" happen.
497 System.out.println("Transformer Exception in insertCallouts");
498 return rtf;
499 }
500 }
501 }