Debian Codegraph @

src/com/nwalsh/saxon/UnwrapLinks.java

raw symbols SCIP
1 // UnwrapLinks.java - Saxon extension for unwrapping nested links
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.UnwrapLinksEmitter;
18
19 /**
20 * <p>Saxon extension for unwrapping nested links</p>
21 *
22 * <p>$Id: UnwrapLinks.java 5907 2006-04-27 08:26:47Z xmldoc $</p>
23 *
24 * <p>Copyright (C) 2000, 2002 Norman Walsh.</p>
25 *
26 * <p>This class provides a
27 * <a href="http://saxon.sf.net/">Saxon 6.*</a>
28 * implementation of a link unwrapper.</p>
29 *
30 * <p><b>Change Log:</b></p>
31 * <dl>
32 * <dt>1.0</dt>
33 * <dd><p>Initial release.</p></dd>
34 * </dl>
35 *
36 * @author Norman Walsh
37 * <a href="mailto:[email protected]">[email protected]</a>
38 *
39 * @version $Id: UnwrapLinks.java 5907 2006-04-27 08:26:47Z xmldoc $
40 *
41 */
42 public class UnwrapLinks {
43 /** True if the stylesheet is producing formatting objects */
44 private static boolean foStylesheet = false;
45
46 /**
47 * <p>Constructor for UnwrapLinks</p>
48 *
49 * <p>All of the methods are static, so the constructor does nothing.</p>
50 */
51 public UnwrapLinks() {
52 }
53
54 /**
55 * <p>Find the string value of a stylesheet variable or parameter</p>
56 *
57 * <p>Returns the string value of <code>varName</code> in the current
58 * <code>context</code>. Returns the empty string if the variable is
59 * not defined.</p>
60 *
61 * @param context The current stylesheet context
62 * @param varName The name of the variable (without the dollar sign)
63 *
64 * @return The string value of the variable
65 */
66 protected static String getVariable(Context context, String varName) {
67 Value variable = null;
68 String varString = null;
69
70 try {
71 variable = Extensions.evaluate(context, "$" + varName);
72 varString = variable.asString();
73 return varString;
74 } catch (TransformerException te) {
75 System.out.println("Undefined variable: " + varName);
76 return "";
77 } catch (IllegalArgumentException iae) {
78 System.out.println("Undefined variable: " + varName);
79 return "";
80 }
81 }
82
83 /**
84 * <p>Setup the parameters associated with unwrapping links</p>
85 *
86 * @param context The current stylesheet context
87 *
88 */
89 private static void setupUnwrapLinks(Context context) {
90 // Get the stylesheet type
91 String varString = getVariable(context, "stylesheet.result.type");
92 foStylesheet = (varString.equals("fo"));
93 }
94
95 /**
96 * <p>Unwrap links</p>
97 *
98 * @param context The current stylesheet context.
99 * @param rtf_ns The result tree fragment of the verbatim environment.
100 *
101 * @return The modified result tree fragment.
102 */
103 public static NodeSetValue unwrapLinks (Context context,
104 NodeSetValue rtf_ns) {
105
106 FragmentValue rtf = (FragmentValue) rtf_ns;
107 boolean tryAgain = true;
108
109 setupUnwrapLinks(context);
110
111 try {
112 Controller controller = context.getController();
113 NamePool namePool = controller.getNamePool();
114
115 while (tryAgain) {
116 UnwrapLinksEmitter ulEmitter = new UnwrapLinksEmitter(controller,
117 namePool,
118 foStylesheet);
119 rtf.replay(ulEmitter);
120 tryAgain = ulEmitter.tryAgain();
121 rtf = (FragmentValue) ulEmitter.getResultTreeFragment();
122 }
123
124 return rtf;
125
126 } catch (TransformerException e) {
127 // This "can't" happen.
128 System.out.println("Transformer Exception in unwrapLinks");
129 return rtf;
130 }
131 }
132 }