Re: Helper Classes questions/request

From: Peter Vandoros (pete..techgroup.com.au)
Date: Wed Jan 17 2007 - 18:49:04 EST

  • Next message: Anjo Krank: "Re: Thoughts on Replacing EOGenerator and EOReporter"

    Thanks Mike for your response.

    I realised after i posted the message that i sent it to the wrong mailing list, so i re-sent it to wonder-disc mailing list (is there a wonder-dev?).

    I have added my changes to this email since i don't use eclipse and i'm not sure how to generate a patch :(.

    So in order to use the changes i made:
    in your WOD:

    PersonName : WOString {
          value = "$currentPerson|displayName";
    }

    This is equivalent to:
    PersonName : WOString {
            value = currentPerson|displayName;
    }

    Use "!" to escape the "$" character:
    PersonName : WOString {
            value = "!$currentPerson|displayName";
    }
    This will return the String: "
    $currentPerson|displayName"

    NOTE: the escape character only escapes the first occurrence of either "$" or itself "!".

    If you want to escape the escape character:
    PersonName : WOString {
            value = "!!$currentPerson|displayName";
    }
    This will return the String: "!$currentPerson|displayName"

    or

    PersonName : WOString {
            value = "!!!$currentPerson|displayName";
    }
    This will return the String: "!!$currentPerson|displayName"

    or

    PersonName : WOString {
            value = "!!currentPerson|displayName";
    }
    This will return the String: "!currentPerson|displayName"

    These are my changes to WOHelperFunctionHTMLTemplateParser.java:
    1. Added a static constant for the symbol (you can change the symbol to any String):
    public static final String DefaultHelperClassConstantBindingFlag = "$";

    2. Added this else-if statement in the parserHelperAssociation() method:
    else if (association instanceof WOConstantValueAssociation) {
        WOConstantValueAssociation constantAssociation = (WOConstantValueAssociation) association;
        Object constantValue = constantAssociation.valueInComponent(null);

        if ( constantValue instanceof String ) {
            String stringValue = (String) constantValue;

            if ( stringValue.startsWith( DefaultHelperClassConstantBindingFlag ) ) {
                originalKeyPath = stringValue.substring( DefaultHelperClassConstantBindingFlag.length() );
            }
            // escapes the DefaultHelperClassConstantBindingFlag or the escape character
            else if ( stringValue.startsWith( "!" + DefaultHelperClassConstantBindingFlag ) || stringValue.startsWith( "!!" ) ) {
                return new WOConstantValueAssociation( stringValue.substring( 1 ) );
            }
        }
    }


    The whole method now looks like this:
    protected WOAssociation parserHelperAssociation(WOAssociation originalAssociation) {
        WOAssociation association = originalAssociation;
        String originalKeyPath = null;
        if (association instanceof WOKeyValueAssociation) {
            WOKeyValueAssociation kvAssociation = (WOKeyValueAssociation) association;
            originalKeyPath = kvAssociation.keyPath();
        }
        else if (association instanceof WOConstantValueAssociation) {
            WOConstantValueAssociation constantAssociation = (WOConstantValueAssociation) association;
            Object constantValue = constantAssociation.valueInComponent(null);

            if ( constantValue instanceof String ) {
                String stringValue = (String) constantValue;

                if ( stringValue.startsWith( DefaultHelperClassConstantBindingFlag ) ) {
                    originalKeyPath = stringValue.substring( DefaultHelperClassConstantBindingFlag.length() );
                }
                // escapes the DefaultHelperClassConstantBindingFlag or the first escape character
                else if ( stringValue.startsWith( "!" + DefaultHelperClassConstantBindingFlag ) || stringValue.startsWith( "!!" ) ) {
                    return new WOConstantValueAssociation( stringValue.substring( 1 ) );
                }
            }
        }

        if (originalKeyPath != null) {
            int pipeIndex = originalKeyPath.indexOf('|');
            if (pipeIndex != -1) {
                String targetKeyPath = originalKeyPath.substring(0, pipeIndex).trim();
                String frameworkName = WOHelperFunctionRegistry.APP_FRAMEWORK_NAME;
                String helperFunctionName = originalKeyPath.substring(pipeIndex + 1).trim();
                String otherParams = null;
                int openParenIndex = helperFunctionName.indexOf('(');
                if (openParenIndex != -1) {
                    int closeParenIndex = helperFunctionName.indexOf(')', openParenIndex + 1);
                    otherParams = helperFunctionName.substring(openParenIndex + 1, closeParenIndex);
                    helperFunctionName = helperFunctionName.substring(0, openParenIndex);
                }
                int helperFunctionDotIndex = helperFunctionName.indexOf('.');
                if (helperFunctionDotIndex != -1) {
                    frameworkName = helperFunctionName.substring(0, helperFunctionDotIndex);
                    helperFunctionName = helperFunctionName.substring(helperFunctionDotIndex + 1);
                }
                StringBuffer ognlKeyPath = new StringBuffer();
                ognlKeyPath.append("~");
                ognlKeyPath.append(.. + WOHelperFunctionRegistry.class.getName() + ..egistry()._helperInstanceForFrameworkNamed(");
                ognlKeyPath.append(targetKeyPath);
                ognlKeyPath.append(", \"");
                ognlKeyPath.append(frameworkName);
                ognlKeyPath.append("\").");
                ognlKeyPath.append(helperFunctionName);
                ognlKeyPath.append("(");
                ognlKeyPath.append(targetKeyPath);
                if (otherParams != null) {
                    ognlKeyPath.append(",");
                    ognlKeyPath.append(otherParams);
                }
                ognlKeyPath.append(")");
                if (log.isDebugEnabled()) {
                    log.debug("Converted " + originalKeyPath + " into " + ognlKeyPath);
                }
                association = new WOConstantValueAssociation(ognlKeyPath.toString());
            }
        }
        return association;
    }


    I hope this ok and covers all the corner cases. If you think of any other cases that i don't account for, please let me know.

    Thanks

    Peter

    Mike Schrag wrote:

    You actually want this one on wonder-dev instead of woproject-dev, but I suspect I didn't go forward more with it mainly because I didn't want to deal with symbol collisions in constants.  You would pretty much have to introduce some sort of leading symbol like in your example, which would subtly break anyone who was using that symbol already, and then you'd have to introduce support for escaping that symbol.  With non-constant, it's a lot easier to introduce a new symbol because there are a lot of them that can't appear in a keypath, but in a constant, it's open season.  I'm guessing I didn't want to deal with it at the time, but one of our guys has has asked for constant support also.  You can send me your patch if you'd like?

    On Jan 17, 2007, at 12:39 AM, Peter Vandoros wrote:

    I have just integrated the WOOgnl framework because Mike got me very interested about Helper Classes :)

    Anyway, i have a question/request:

    Is it possible to allow a helper class come from a constant binding instead of a key value coding binding?
    eg.
    come from: value = "*currentPersion|displayName"; (note use of '*' as the helper marker. this should be configurable)
    instead of from: value = currentPerson|displayName;

    The reason i want to be able to do this is because i am one of those developers that still uses WOBuilder for creating the component structure and it treats those as illegal bindings.

    Being open source, i looked at the source and have discovered commented-out code that used constant associations as well. Why has this been commented out?

    I can make some changes to the code in a custom version so it will optionally allow this situation but i don't want to have to reapply my changes for every new version of the framework.

    So i was wondering if i were to modify the source to optionally allow my scenario, could it possibly be included into the "official" version?

    Much appreciated

    Peter


    --
    This message has been scanned for viruses and
    dangerous content by MailScanner, and is
    believed to be clean.



    This archive was generated by hypermail 2.0.0 : Wed Jan 17 2007 - 18:51:42 EST