1 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 59 public class Verbatim { 60 61 private static boolean foStylesheet = false; 62 63 private static int modulus = 0; 64 65 private static int width = 0; 66 67 private static int startinglinenumber = 1; 68 69 private static String separator = ""; 70 71 72 private static boolean calloutsSetup = false; 73 74 private static int defaultColumn = 60; 75 76 private static String graphicsPath = null; 77 78 private static String graphicsExt = null; 79 80 private static int graphicsMax = 10; 81 82 83 private static FormatCallout fCallout = null; 84 85 90 public Verbatim() { 91 } 92 93 105 protected static String getVariable(Context context, String varName) { 106 Value variable = null; 107 String varString = null; 108 109 try { 110 variable = Extensions.evaluate(context, "$" + varName); 111 varString = variable.asString(); 112 return varString; 113 } catch (TransformerException te) { 114 System.out.println("Undefined variable: " + varName); 115 return ""; 116 } catch (IllegalArgumentException iae) { 117 System.out.println("Undefined variable: " + varName); 118 return ""; 119 } 120 } 121 122 156 private static void setupLineNumbering(Context context) { 157 modulus = 5; 159 width = 3; 160 startinglinenumber = 1; 161 separator = " "; 162 foStylesheet = false; 163 164 String varString = null; 165 166 varString = getVariable(context, "linenumbering.everyNth"); 168 try { 169 modulus = Integer.parseInt(varString); 170 } catch (NumberFormatException nfe) { 171 System.out.println("$linenumbering.everyNth is not a number: " + varString); 172 } 173 174 varString = getVariable(context, "linenumbering.width"); 176 try { 177 width = Integer.parseInt(varString); 178 } catch (NumberFormatException nfe) { 179 System.out.println("$linenumbering.width is not a number: " + varString); 180 } 181 182 varString = getVariable(context, "linenumbering.startinglinenumber"); 184 try { 185 startinglinenumber = Integer.parseInt(varString); 186 } catch (NumberFormatException nfe) { 187 System.out.println("$linenumbering.startinglinenumber is not a number: " + varString); 188 } 189 190 varString = getVariable(context, "linenumbering.separator"); 192 separator = varString; 193 194 varString = getVariable(context, "stylesheet.result.type"); 196 foStylesheet = (varString.equals("fo")); 197 } 198 199 238 public static NodeSetValue numberLines (Context context, 239 NodeSetValue rtf_ns) { 240 241 FragmentValue rtf = (FragmentValue) rtf_ns; 242 243 setupLineNumbering(context); 244 245 try { 246 LineCountEmitter lcEmitter = new LineCountEmitter(); 247 rtf.replay(lcEmitter); 248 int numLines = lcEmitter.lineCount(); 249 250 int listingModulus = numLines < modulus ? 1 : modulus; 251 252 double log10numLines = Math.log(numLines) / Math.log(10); 253 254 int listingWidth = width < log10numLines+1 255 ? (int) Math.floor(log10numLines + 1) 256 : width; 257 258 Controller controller = context.getController(); 259 NamePool namePool = controller.getNamePool(); 260 NumberLinesEmitter nlEmitter = new NumberLinesEmitter(controller, 261 namePool, 262 startinglinenumber, 263 listingModulus, 264 listingWidth, 265 separator, 266 foStylesheet); 267 rtf.replay(nlEmitter); 268 return nlEmitter.getResultTreeFragment(); 269 } catch (TransformerException e) { 270 System.out.println("Transformer Exception in numberLines"); 272 return rtf; 273 } 274 } 275 276 311 private static void setupCallouts(Context context) { 312 NamePool namePool = context.getController().getNamePool(); 313 314 boolean useGraphics = false; 315 boolean useUnicode = false; 316 317 int unicodeStart = 49; 318 int unicodeMax = 0; 319 320 String unicodeFont = ""; 321 322 defaultColumn = 60; 324 graphicsPath = null; 325 graphicsExt = null; 326 graphicsMax = 0; 327 foStylesheet = false; 328 calloutsSetup = true; 329 330 Value variable = null; 331 String varString = null; 332 333 varString = getVariable(context, "stylesheet.result.type"); 335 foStylesheet = (varString.equals("fo")); 336 337 varString = getVariable(context, "callout.defaultcolumn"); 339 try { 340 defaultColumn = Integer.parseInt(varString); 341 } catch (NumberFormatException nfe) { 342 System.out.println("$callout.defaultcolumn is not a number: " 343 + varString); 344 } 345 346 varString = getVariable(context, "callout.graphics"); 348 useGraphics = !(varString.equals("0") || varString.equals("")); 349 350 varString = getVariable(context, "callout.unicode"); 352 useUnicode = !(varString.equals("0") || varString.equals("")); 353 354 if (useGraphics) { 355 varString = getVariable(context, "callout.graphics.path"); 357 graphicsPath = varString; 358 359 varString = getVariable(context, "callout.graphics.extension"); 361 graphicsExt = varString; 362 363 varString = getVariable(context, "callout.graphics.number.limit"); 365 try { 366 graphicsMax = Integer.parseInt(varString); 367 } catch (NumberFormatException nfe) { 368 System.out.println("$callout.graphics.number.limit is not a number: " 369 + varString); 370 graphicsMax = 0; 371 } 372 373 fCallout = new FormatGraphicCallout(namePool, 374 graphicsPath, 375 graphicsExt, 376 graphicsMax, 377 foStylesheet); 378 } else if (useUnicode) { 379 varString = getVariable(context, "callout.unicode.start.character"); 381 try { 382 unicodeStart = Integer.parseInt(varString); 383 } catch (NumberFormatException nfe) { 384 System.out.println("$callout.unicode.start.character is not a number: " 385 + varString); 386 unicodeStart = 48; 387 } 388 389 varString = getVariable(context, "callout.unicode.number.limit"); 391 try { 392 unicodeMax = Integer.parseInt(varString); 393 } catch (NumberFormatException nfe) { 394 System.out.println("$callout.unicode.number.limit is not a number: " 395 + varString); 396 unicodeStart = 0; 397 } 398 399 unicodeFont = getVariable(context, "callout.unicode.font"); 401 if (unicodeFont == null) { 402 unicodeFont = ""; 403 } 404 405 fCallout = new FormatUnicodeCallout(namePool, 406 unicodeFont, 407 unicodeStart, 408 unicodeMax, 409 foStylesheet); 410 } else { 411 fCallout = new FormatTextCallout(namePool, foStylesheet); 412 } 413 } 414 415 465 466 public static NodeSetValue insertCallouts (Context context, 467 NodeList areaspecNodeList, 468 NodeSetValue rtf_ns) { 469 470 FragmentValue rtf = (FragmentValue) rtf_ns; 471 472 setupCallouts(context); 473 474 try { 475 Controller controller = context.getController(); 476 NamePool namePool = controller.getNamePool(); 477 CalloutEmitter cEmitter = new CalloutEmitter(controller, 478 namePool, 479 defaultColumn, 480 foStylesheet, 481 fCallout); 482 cEmitter.setupCallouts(areaspecNodeList); 483 rtf.replay(cEmitter); 484 return cEmitter.getResultTreeFragment(); 485 } catch (TransformerException e) { 486 System.out.println("Transformer Exception in insertCallouts"); 488 return rtf; 489 } 490 } 491 } 492 | Popular Tags |