From: Mike S. <ms...@us...> - 2007-04-21 22:15:42
|
Update of /cvsroot/wonder/Wonder/Common/Frameworks/WOOgnl/Sources/ognl/helperfunction In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv8663/Sources/ognl/helperfunction Modified Files: WOHelperFunctionHTMLTemplateParser.java WOHelperFunctionHTMLParser.java Log Message: Guido's patch to add inline binding syntax to non-wo tags (like <div id = "$anID">..</div>) Index: WOHelperFunctionHTMLTemplateParser.java =================================================================== RCS file: /cvsroot/wonder/Wonder/Common/Frameworks/WOOgnl/Sources/ognl/helperfunction/WOHelperFunctionHTMLTemplateParser.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** WOHelperFunctionHTMLTemplateParser.java 19 Apr 2007 21:33:28 -0000 1.12 --- WOHelperFunctionHTMLTemplateParser.java 21 Apr 2007 22:15:35 -0000 1.13 *************** *** 28,32 **** private static NSMutableDictionary _tagProcessorMap = new NSMutableDictionary(); private static boolean _allowInlineBindings = false; ! public static void registerTagShortcut(String fullElementType, String shortcutElementType) { _tagShortcutMap.setObjectForKey(fullElementType, shortcutElementType); --- 28,34 ---- private static NSMutableDictionary _tagProcessorMap = new NSMutableDictionary(); private static boolean _allowInlineBindings = false; ! ! private static String WO_REPLACEMENT_MARKER = "__REPL__"; ! public static void registerTagShortcut(String fullElementType, String shortcutElementType) { _tagShortcutMap.setObjectForKey(fullElementType, shortcutElementType); *************** *** 40,44 **** _allowInlineBindings = allowInlineBindings; } ! static { WOHelperFunctionHTMLTemplateParser.log.setLevel(Level.WARN); --- 42,46 ---- _allowInlineBindings = allowInlineBindings; } ! static { WOHelperFunctionHTMLTemplateParser.log.setLevel(Level.WARN); *************** *** 173,176 **** --- 175,186 ---- elementType = shortcutType; } + else if (elementType.startsWith(WO_REPLACEMENT_MARKER)) { + // Acts only on tags, where we have "dynamified" inside the tag parser + // this takes the value found after the "wo:" part in the element and generates a WOGenericContainer with that value + // as the elementName binding + elementType = elementType.replace(WO_REPLACEMENT_MARKER, ""); + associations.setObjectForKey(WOAssociation.associationWithValue(elementType), "elementName"); + elementType = "WOGenericContainer"; + } String elementName; synchronized (this) { Index: WOHelperFunctionHTMLParser.java =================================================================== RCS file: /cvsroot/wonder/Wonder/Common/Frameworks/WOOgnl/Sources/ognl/helperfunction/WOHelperFunctionHTMLParser.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** WOHelperFunctionHTMLParser.java 10 Mar 2007 13:37:01 -0000 1.4 --- WOHelperFunctionHTMLParser.java 21 Apr 2007 22:15:36 -0000 1.5 *************** *** 2,5 **** --- 2,6 ---- import java.util.NoSuchElementException; + import java.util.Stack; import java.util.StringTokenizer; *************** *** 9,12 **** --- 10,14 ---- import com.webobjects.appserver._private.WODeclarationFormatException; import com.webobjects.appserver._private.WOHTMLFormatException; + import com.webobjects.foundation.NSMutableDictionary; import com.webobjects.foundation._NSStringUtilities; *************** *** 25,28 **** --- 27,34 ---- private static final String WEBOBJECT_END_TAG = "</webobject"; private static final String WEBOBJECT_START_TAG = "<webobject"; + private static final String WO_REPLACEMENT_MARKER = "__REPL__"; + + private static boolean _parseStandardTags = false; + private NSMutableDictionary _stackDict; static { *************** *** 35,41 **** --- 41,52 ---- _contentText = new StringBuffer(128); } + + public static void setParseStandardTags (boolean flag) { + _parseStandardTags = flag; + } public void parseHTML() throws WOHTMLFormatException, WODeclarationFormatException, ClassNotFoundException { Object obj = null; + _stackDict = new NSMutableDictionary(); StringTokenizer templateTokenizer = new StringTokenizer(_unparsedTemplate, "<"); boolean flag = true; *************** *** 63,66 **** --- 74,83 ---- token = templateTokenizer.nextToken(">"); int tagIndex; + + // parses non wo: tags for dynamic bindings + if (_parseStandardTags) { + token = checkToken(token); + } + String tagLowerCase = token.toLowerCase(); if (tagLowerCase.startsWith(WOHelperFunctionHTMLParser.WEBOBJECT_START_TAG) || tagLowerCase.startsWith(WOHelperFunctionHTMLParser.WO_START_TAG)) { *************** *** 147,152 **** --- 164,229 ---- } didParseText(); + _stackDict = null; } + /** + * Checks the current token for dynamic inline bindings + * + * @param token + * @return a rewritten token if it has an inline binding or a closing tag, if it belongs to a rewritten token + */ + private String checkToken (String token) { + if (token == null || token.toLowerCase().startsWith(WOHelperFunctionHTMLParser.WEBOBJECT_START_TAG) + || token.toLowerCase().startsWith(WOHelperFunctionHTMLParser.WO_START_TAG)) { + // we return immediately, if it is a webobject token + return token; + } + + String[] tokenParts = token.split(" "); + String tokenPart = tokenParts[0].substring(1); + + if (token.contains("\"$") && token.startsWith("<")) { + // we assume a dynamic tag + token = token.replace(tokenParts[0], "<wo:" + WO_REPLACEMENT_MARKER + tokenPart); + if (log.isDebugEnabled()) log.debug("Rewritten <" + tokenPart + " ...> tag to <wo:" + tokenPart + " ...>"); + + if (!token.endsWith("/")) { + // no need to keep information for self closing tags + Stack stack = (Stack) _stackDict.objectForKey(tokenPart); + if (stack == null) { + // create one and push a marker + stack = new Stack(); + stack.push(WO_REPLACEMENT_MARKER); + _stackDict.setObjectForKey(stack, tokenPart); + } + else { + // just push a marker + stack.push(WO_REPLACEMENT_MARKER); + _stackDict.setObjectForKey(stack, tokenPart); + } + } + } + else if (!token.startsWith("</") && this._stackDict.containsKey(tokenPart)) { + // standard opening tag + Stack stack = (Stack) this._stackDict.objectForKey(tokenPart); + if (this._stackDict != null && stack != null) { + stack.push(tokenPart); + this._stackDict.setObjectForKey(stack, tokenPart); + } + } + else { + // closing tag + Stack stack = (Stack) this._stackDict.objectForKey(tokenParts[0].substring(2)); + if (stack != null && !stack.empty()) { + String stackContent = (String) stack.pop(); + if (stackContent.equals(WO_REPLACEMENT_MARKER)) { + if (log.isDebugEnabled()) log.debug("Replaced end tag for '" + tokenParts[0].substring(2) + "' with 'wo' endtag"); + token = "</wo"; + } + } + } + return token; + } + private void startOfWebObjectTag(String token) throws WOHTMLFormatException { didParseText(); |