001: // UnwrapLinks.java - Saxon extension for unwrapping nested links
002:
003: package com.nwalsh.saxon;
004:
005: import java.util.Stack;
006: import java.util.StringTokenizer;
007: import org.xml.sax.*;
008: import org.w3c.dom.*;
009: import javax.xml.transform.TransformerException;
010: import com.icl.saxon.Controller;
011: import com.icl.saxon.expr.*;
012: import com.icl.saxon.om.*;
013: import com.icl.saxon.pattern.*;
014: import com.icl.saxon.Context;
015: import com.icl.saxon.tree.*;
016: import com.icl.saxon.functions.Extensions;
017: import com.nwalsh.saxon.UnwrapLinksEmitter;
018:
019: /**
020: * <p>Saxon extension for unwrapping nested links</p>
021: *
022: * <p>$Id: UnwrapLinks.java,v 1.4 2005-08-30 08:14:58 draganr Exp $</p>
023: *
024: * <p>Copyright (C) 2000, 2002 Norman Walsh.</p>
025: *
026: * <p>This class provides a
027: * <a href="http://saxon.sf.net/">Saxon 6.*</a>
028: * implementation of a link unwrapper.</p>
029: *
030: * <p><b>Change Log:</b></p>
031: * <dl>
032: * <dt>1.0</dt>
033: * <dd><p>Initial release.</p></dd>
034: * </dl>
035: *
036: * @author Norman Walsh
037: * <a href="mailto:ndw@nwalsh.com">ndw@nwalsh.com</a>
038: *
039: * @version $Id: UnwrapLinks.java,v 1.4 2005-08-30 08:14:58 draganr Exp $
040: *
041: */
042: public class UnwrapLinks {
043: /** True if the stylesheet is producing formatting objects */
044: private static boolean foStylesheet = false;
045:
046: /**
047: * <p>Constructor for UnwrapLinks</p>
048: *
049: * <p>All of the methods are static, so the constructor does nothing.</p>
050: */
051: public UnwrapLinks() {
052: }
053:
054: /**
055: * <p>Find the string value of a stylesheet variable or parameter</p>
056: *
057: * <p>Returns the string value of <code>varName</code> in the current
058: * <code>context</code>. Returns the empty string if the variable is
059: * not defined.</p>
060: *
061: * @param context The current stylesheet context
062: * @param varName The name of the variable (without the dollar sign)
063: *
064: * @return The string value of the variable
065: */
066: protected static String getVariable(Context context, String varName) {
067: Value variable = null;
068: String varString = null;
069:
070: try {
071: variable = Extensions.evaluate(context, "$" + varName);
072: varString = variable.asString();
073: return varString;
074: } catch (TransformerException te) {
075: System.out.println("Undefined variable: " + varName);
076: return "";
077: } catch (IllegalArgumentException iae) {
078: System.out.println("Undefined variable: " + varName);
079: return "";
080: }
081: }
082:
083: /**
084: * <p>Setup the parameters associated with unwrapping links</p>
085: *
086: * @param context The current stylesheet context
087: *
088: */
089: private static void setupUnwrapLinks(Context context) {
090: // Get the stylesheet type
091: String varString = getVariable(context,
092: "stylesheet.result.type");
093: foStylesheet = (varString.equals("fo"));
094: }
095:
096: /**
097: * <p>Unwrap links</p>
098: *
099: * @param rtf 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(
117: controller, namePool, 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: }
|