From 1085f83d023af7bd3da5e893720c877855a02210 Mon Sep 17 00:00:00 2001 From: hns Date: Wed, 15 May 2002 17:18:32 +0000 Subject: [PATCH] Pretty much streamlined Skin parsing and rendering, trying to reduce the number of objects created. --- src/helma/framework/core/Skin.java | 109 ++++++++++++++++------------- 1 file changed, 60 insertions(+), 49 deletions(-) diff --git a/src/helma/framework/core/Skin.java b/src/helma/framework/core/Skin.java index 078aacac..a61263b4 100644 --- a/src/helma/framework/core/Skin.java +++ b/src/helma/framework/core/Skin.java @@ -1,6 +1,6 @@ // Skin.java // Copyright (c) Hannes Wallnöfer 2001 - + package helma.framework.core; import helma.framework.*; @@ -13,15 +13,17 @@ import java.io.*; import java.util.*; /** - * This represents a Helma skin, i.e. a template created from JavaScript. It uses the request path array - * from the RequestEvaluator object to resolve dynamic tokens. + * This represents a Helma skin, i.e. a template created from containing Macro tags + * that will be dynamically evaluated.. It uses the request path array + * from the RequestEvaluator object to resolve Macro handlers by type name. */ public class Skin { - Object[] parts; + Macro[] parts; Application app; - String source; + char[] source; + int sourceLength; HashSet sandbox; /** @@ -37,64 +39,74 @@ public class Skin { public Skin (String content, Application app, HashSet sandbox) { this.app = app; this.sandbox = sandbox; - parse (content); + source = content.toCharArray (); + sourceLength = source.length; + parse (); + } + + /** + * Create a skin without any restrictions on the macros from a char array. + */ + public Skin (char[] content, int length, Application app) { + this.app = app; + this.sandbox = null; + this.source = content; + this.sourceLength = length; + parse (); } /** * Parse a skin object from source text */ - public void parse (String content) { + private void parse () { - this.source = content; ArrayList partBuffer = new ArrayList (); - int l = content.length (); - char cnt[] = new char[l]; - content.getChars (0, l, cnt, 0); - int lastIdx = 0; - for (int i = 0; i < l-1; i++) { - if (cnt[i] == '<' && cnt[i+1] == '%') { + int start = 0; + for (int i = 0; i < sourceLength-1; i++) { + if (source[i] == '<' && source[i+1] == '%') { + // found macro start tag int j = i+2; - while (j < l-1 && (cnt[j] != '%' || cnt[j+1] != '>')) { + // search macr end tag + while (j < sourceLength-1 && (source[j] != '%' || source[j+1] != '>')) { j++; } if (j > i+2) { - if (i - lastIdx > 0) - partBuffer.add (new String (cnt, lastIdx, i - lastIdx)); - String macrotext = new String (cnt, i+2, (j-i)-2); - partBuffer.add (new Macro (macrotext)); - lastIdx = j+2; + partBuffer.add (new Macro (i, j+2)); + start = j+2; } i = j+1; } } - if (lastIdx < l) - partBuffer.add (new String (cnt, lastIdx, l - lastIdx)); - parts = partBuffer.toArray (); + parts = new Macro[partBuffer.size()]; + partBuffer.toArray (parts); } /** * Get the raw source text this skin was parsed from */ public String getSource () { - return source; + return new String (source, 0, sourceLength); } /** * Render this skin */ public void render (RequestEvaluator reval, Object thisObject, HashMap paramObject) throws RedirectException { - + if (parts == null) - return; - + reval.res.writeCharArray (source, 0, sourceLength); + + int written = 0; for (int i=0; i written) + reval.res.writeCharArray (source, written, parts[i].start-written); + parts[i].render (reval, thisObject, paramObject); + written = parts[i].end; } + if (written < sourceLength) + reval.res.writeCharArray (source, written, sourceLength-written); } /** @@ -128,56 +140,56 @@ public class Skin { class Macro { + int start, end; String handler; String name; String fullname; HashMap parameters; - public Macro (String str) { + public Macro (int start, int end) { + + this.start = start; + this.end = end; parameters = new HashMap (); - int l = str.length (); - char cnt[] = new char[l]; - str.getChars (0, l, cnt, 0); - int state = HANDLER; boolean escape = false; char quotechar = '\u0000'; String lastParamName = null; StringBuffer b = new StringBuffer(); - for (int i=0; i