[prev in list] [next in list] [prev in thread] [next in thread] 

List:       xwt-patches
Subject:    [patches] proposed patch to HEAD: xwt.loadTemplate
From:       mlylem () xwt ! org
Date:       2003-04-08 17:24:54
[Download RAW message or body]

This patch adds the xwt.loadTemplate(Bytestream b, String templatename)
function.  This allows the user to load a new template out of a string using the \
xwt.utfEncode facility.  The bug is: http://bugs.xwt.org/show_bug.cgi?id=98
bug 98.
Note that the user MUST use a new package name (passed in the second parameter)
each time they load a template, so if one were to:
xwt.loadTemplate(b1, "a.b.c")
loading a template contained in b1 into the package a.b using template name c
One could not then load another template d into package a.b via:
xwt.loadTemplate(b2, "a.b.d")
The way to load the new template in b2 would be to choose a different package,
say a.e as in:
xwt.loadTemplate(b2, "a.x.e")
the template name can end with the xwt suffix, it is ignored.  Therefore, a
template called xwt cannot be loaded.

Index: xwt/Resources.java
===================================================================
RCS file: /cvs/xwt/src/org/xwt/Resources.java,v
retrieving revision 1.10
diff -B -u -d -r1.10 Resources.java
--- xwt/Resources.java	12 Feb 2003 06:40:22 -0000	1.10
+++ xwt/Resources.java	8 Apr 2003 17:24:22 -0000
@@ -131,6 +131,28 @@
         if (Log.verbose) Log.log(Resources.class, "done loading archive");
     }
 
+    /** Load a bytestream */
+    public static synchronized void loadTemplate(InputStream is, String name) throws \
IOException { +
+        String templateName = new String(name);
+
+        if (templateName.endsWith(".xwt")){
+            templateName = templateName.substring(0, templateName.length() - 4);
+        }
+        String packageName = JSObject.nodeNameToPackageName(templateName);
+        String shortName = templateName.substring(templateName.lastIndexOf(".")+1);
+        if (!validResourceName(shortName)) return;
+
+        Object user = usedPackages.get(packageName);
+        if (user != null){
+            if (Log.on) Log.log(Resources.class, "templates have already been loaded \
into " + packageName + "; refusing to load " + templateName); +        }else{
+            Static.createStatic(templateName, false);
+            usedPackages.put(packageName, new Object());
+            bytes.put(templateName + ".xwt", isToByteArray(is));
+        }
+    }
+
     /** holds the current theme mappings */
     static Vector mapFrom = new Vector();
 
Index: xwt/XWT.java
===================================================================
RCS file: /cvs/xwt/src/org/xwt/XWT.java,v
retrieving revision 1.18
diff -B -u -d -r1.18 XWT.java
--- xwt/XWT.java	3 Oct 2002 22:45:58 -0000	1.18
+++ xwt/XWT.java	8 Apr 2003 17:24:23 -0000
@@ -64,6 +64,7 @@
         else if (name.equals("println")) return println;
         else if (name.equals("math")) return \
org.xwt.util.JSObject.defaultObjects.get("Math", null);  else if \
(name.equals("loadArchive")) return loadArchive; +        else if \
(name.equals("loadTemplate")) return loadTemplate;  else if \
(name.equals("prefetchImage")) return prefetchImage;  else if (name.equals("prefs")) \
                return prefs;
         else if (name.equals("encodeURI")) return \
JSObject.defaultObjects.get("encodeURI", null); @@ -498,6 +499,28 @@
                     
                 } catch (IOException ioe) {
                     if (Log.on) Log.log(this, "IOException while loading archive:");
+                    if (Log.on) Log.log(this, ioe);
+                    throw new JavaScriptException(ioe.toString());
+
+                } finally {
+                    ThreadMessage.resumeThread();
+
+                }
+                return null;
+            }
+        };
+
+    private static final JSObject.JSFunction loadTemplate = new \
JSObject.JSFunction() { +            public Object call(Context cx, Scriptable \
thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException { +            \
if (!ThreadMessage.suspendThread()) return null; +
+                try {
+                    if (args == null || args.length < 2 || args[0] == null || \
args[1] == null) return null; +                    InputStream is = \
((ByteStream)(args[0])).getInputStream(); +                    String name = \
(String)args[1]; +                    Resources.loadTemplate(is, name);
+                } catch (IOException ioe) {
+                    if (Log.on) Log.log(this, "IOException while loading \
Template:");  if (Log.on) Log.log(this, ioe);
                     throw new JavaScriptException(ioe.toString());
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic