Source code: com/nwalsh/saxon/UnwrapLinks.java
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,v 1.1 2002/06/26 11:02:05 nwalsh Exp $</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:ndw@nwalsh.com">ndw@nwalsh.com</a>
38 *
39 * @version $Id: UnwrapLinks.java,v 1.1 2002/06/26 11:02:05 nwalsh Exp $
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 rtf The result tree fragment of the verbatim environment.
99 *
100 * @return The modified result tree fragment.
101 */
102 public static NodeSetValue unwrapLinks (Context context,
103 NodeSetValue rtf_ns) {
104
105 FragmentValue rtf = (FragmentValue) rtf_ns;
106 boolean tryAgain = true;
107
108 setupUnwrapLinks(context);
109
110 try {
111 Controller controller = context.getController();
112 NamePool namePool = controller.getNamePool();
113
114 while (tryAgain) {
115 UnwrapLinksEmitter ulEmitter = new UnwrapLinksEmitter(controller,
116 namePool,
117 foStylesheet);
118 rtf.replay(ulEmitter);
119 tryAgain = ulEmitter.tryAgain();
120 rtf = (FragmentValue) ulEmitter.getResultTreeFragment();
121 }
122
123 return rtf;
124
125 } catch (TransformerException e) {
126 // This "can't" happen.
127 System.out.println("Transformer Exception in unwrapLinks");
128 return rtf;
129 }
130 }
131 }