RE: setting TopLevel variables externally with XT

Subject: RE: setting TopLevel variables externally with XT
From: "Maxime Levesque" <maximel@xxxxxxxxxxxxxx>
Date: Wed, 28 Jul 1999 15:46:29 -0700

 This is not the most elegant way to do it, but it works ...


 Tell me if you succeed doing it the way you were trying
 (with ExprParer.parserVariantExpr), it looks a bit cleaner ...


You will have to put the following namespace in your stylesheet :

  xmlns:xml2Java="http://www.jclark.com/xt/statefull_java/";


public class StatefullXT {


   /**
	This will pass Object 'o' to your stylesheet

	you'll access it with :

	<xsl:variable name='pageStencil' expr='statefullXT:getRootObject()'/>

	the only reason these methods are static is to save a line of code in
	XT ... (this might be exagerated minimalism ...)
   */

   public static void parse(Object o, String xml, String xslTransformer)
throws Exception {
      StatefullExtensionContext sec = new StatefullExtensionContext(o);
      auxParse(sec, xml, xslTransformer);
   }

   /**
	This will allow your stylesheet to create an object
	and return in :

	... create $myObject ...

	<xsl:value-of select='statefullXT:setRootObject($myObject)'/>
   */

   public static Object parse(String xml, String xslTransformer) throws
Exception

      StatefullExtensionContext sec = new StatefullExtensionContext(null);
      auxParse(sec, xml, xslTransformer);
      return sec.rootObject;
   }

   private static void auxBuild(StatefullExtensionContext sec, String xml,
String xslTransformer) throws Exception {

      XSLProcessorImpl p = new XSLProcessorImpl(new SAXParser(),new
SAXParser(), sec);

      p.setStylesheet(createInputSource(xslTransformer));

      p.parse(createInputSource(xml));
   }

   private static InputSource createInputSource(String url) throws Exception
{
      return new InputSource(new URL(url).openStream());
   }


   static class StatefullExtensionContext implements ExtensionContext {

      Object rootObject;

      StatefullExtensionContext(Object o) {
         rootObject = o;
      }

      public boolean available(String name) {
         return name.equals("getRootObject");
      }

      public Object call(String name, Node currentNode, Object[] args)
throws XSLException {

         if(name.equals("getRootObject")) {
            return rootObject;
         }
         else if(name.equals("setRootObject")) {
            rootObject = args[1];
            return null;
         }
         else
            throw new RuntimeException("unknown method : " + name);
      }
   }
}


// These are the modifications one has to do in XT :

  in class XSLProcessorImpl :

   // HACK BEGIN :
  public XSLProcessorImpl(Parser sourceParser, Parser sheetParser,
com.jclark.xsl.expr.ExtensionContext extensionContext) {
    this.sourceParser = sourceParser;
    this.sheetParser = sheetParser;
    sourceLoader = new XMLProcessorImpl(sourceParser);
    if (sourceParser == sheetParser)
      sheetLoader = sourceLoader;
    else
      sheetLoader = new XMLProcessorImpl(sheetParser);

    engine = new EngineImpl(sheetLoader, new
ExtensionHandlerImpl(extensionContext));// modified.

    sheet = null;
  }

 in class ExtensionHandlerImpl :

   // HACK ADD MEMBER :
   private ExtensionContext statefullHandler_;

   //HACK BEGIN (modify constructor to set your ExtensionContext)
   public ExtensionHandlerImpl(ExtensionContext statefullHandler) {
      statefullHandler_ = statefullHandler;
      //System.out.println("Will use statefullHandler = " +
statefullHandler);
   }
   //HACK END.


  static final private String STATEFULL_JAVA_NS =
"http://www.jclark.com/xt/statefull_java/";;

  public ExtensionContext createContext(String namespace) throws
XSLException {

     //System.out.println("'" + namespace + "' asked");
     //System.out.println("'" + STATEFULL_JAVA_NS + "' is supported");

    // HACK BEGIN :
    if (namespace.startsWith(STATEFULL_JAVA_NS))
       return statefullHandler_;
    // HACK END.

    if (namespace.startsWith(JAVA_NS)) {
      System.out.println("Will use built int handler");
      try {
	return new
ContextImpl(Class.forName(namespace.substring(JAVA_NS.length())));
      }
      catch (ClassNotFoundException e) { }
    }

    return null;
  }





> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxx
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxx]On Behalf Of Jon Smirl
> Sent: Wednesday, July 28, 1999 1:54 PM
> To: XSLList
> Subject: setting TopLevel variables externally with XT
>
>
> I have modified SheetImpl.java of XT to support a function for setting top
> level variables externally:
>
>   public void setGlobal(String variable, String value) throws
> XSLException {
>     Name var = nameTable.createName(variable);
>     variableExprTable.put(var, ExprParser.parseVariantExpr(null,
> value, new
> EmptyVariableSet()));
>   }
>
> To use this, my servlet loads the stylesheet when it is first
> started. Then
> each time the servlet is executed I call
>         xsl.setGlobal("ua",  userAgent) - to the set user agent string
> and you can do things like <xsl:if test="$ua=string('mozilla')">
>
> Now I would like to modify setGlobal to allow generic objects...
>        public void setGlobal(String variable, Object value) throws
> XSLException {
>
>         xsl.setGlobal("response",  res) - to set $repsonse to the servlet
> response variable.
>
> I need this variable so that I can pass it to my static extension
> functions
> but I can't figure out what to replace ExprParer.parserVariantExpr with to
> handle the generic object case.  Can someone give me a clue?
>
> Is it legal to set a top level variable to a java object and then
> pass it to
> an extension function?
> As anyone already done this for XT?
>
> Jon Smirl
> jonsmirl@xxxxxxxxxxxx
>
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread