Search in sources :

Example 1 with WOAssociation

use of com.webobjects.appserver.WOAssociation in project wonder-slim by undur.

the class WOHelperFunctionParser method parseInlineAssociation.

protected void parseInlineAssociation(StringBuffer keyBuffer, StringBuffer valueBuffer, NSMutableDictionary bindings) throws WOHelperFunctionHTMLFormatException {
    String key = keyBuffer.toString().trim();
    String value = valueBuffer.toString().trim();
    NSDictionary quotedStrings;
    if (value.startsWith("\"")) {
        value = value.substring(1);
        if (value.endsWith("\"")) {
            value = value.substring(0, value.length() - 1);
        } else {
            throw new WOHelperFunctionHTMLFormatException(valueBuffer + " starts with quote but does not end with one.");
        }
        if (value.startsWith("$")) {
            value = value.substring(1);
            if (value.endsWith("VALID")) {
                value = value.replaceFirst("\\s*//\\s*VALID", "");
            }
            quotedStrings = new NSDictionary();
        } else {
            value = value.replaceAll("\\\\\\$", "\\$");
            value = value.replaceAll("\\\"", "\"");
            quotedStrings = new NSDictionary(value, "_WODP_0");
            value = "_WODP_0";
        }
    } else {
        quotedStrings = new NSDictionary();
    }
    WOAssociation association = WOHelperFunctionDeclarationParser._associationWithKey(value, quotedStrings);
    bindings.setObjectForKey(association, key);
}
Also used : WOAssociation(com.webobjects.appserver.WOAssociation) NSDictionary(com.webobjects.foundation.NSDictionary) WOHTMLCommentString(com.webobjects.appserver._private.WOHTMLCommentString)

Example 2 with WOAssociation

use of com.webobjects.appserver.WOAssociation in project wonder-slim by undur.

the class WOHelperFunctionParser method processDeclaration.

protected void processDeclaration(WODeclaration declaration) {
    NSMutableDictionary associations = (NSMutableDictionary) declaration.associations();
    Enumeration bindingNameEnum = associations.keyEnumerator();
    while (bindingNameEnum.hasMoreElements()) {
        String bindingName = (String) bindingNameEnum.nextElement();
        WOAssociation association = (WOAssociation) associations.valueForKey(bindingName);
        WOAssociation helperAssociation = parserHelperAssociation(association);
        if (helperAssociation != association) {
            associations.setObjectForKey(helperAssociation, bindingName);
        }
    }
    // This will replace constant associations with ognl associations
    // when needed.
    WOOgnl.factory().convertOgnlConstantAssociations(associations);
}
Also used : WOAssociation(com.webobjects.appserver.WOAssociation) Enumeration(java.util.Enumeration) WOHTMLCommentString(com.webobjects.appserver._private.WOHTMLCommentString) NSMutableDictionary(com.webobjects.foundation.NSMutableDictionary)

Example 3 with WOAssociation

use of com.webobjects.appserver.WOAssociation in project wonder-slim by undur.

the class WOOgnl method convertOgnlConstantAssociations.

public void convertOgnlConstantAssociations(NSMutableDictionary associations) {
    for (Enumeration e = associations.keyEnumerator(); e.hasMoreElements(); ) {
        String name = (String) e.nextElement();
        WOAssociation association = (WOAssociation) associations.objectForKey(name);
        boolean isConstant = false;
        String keyPath = null;
        if (association instanceof WOConstantValueAssociation) {
            WOConstantValueAssociation constantAssociation = (WOConstantValueAssociation) association;
            // AK: this sucks, but there is no API to get at the value
            Object value = constantAssociation.valueInComponent(null);
            keyPath = value != null ? value.toString() : null;
            isConstant = true;
        } else if (association instanceof WOKeyValueAssociation) {
            keyPath = association.keyPath();
        } else if (association instanceof WOBindingNameAssociation) {
            WOBindingNameAssociation b = (WOBindingNameAssociation) association;
            // AK: strictly speaking, this is not correct, as we only get the first part of
            // the path. But take a look at WOBindingNameAssociation for a bit of fun...
            keyPath = "^" + b._parentBindingName;
        }
        if (keyPath != null) {
            if (!associationMappings.isEmpty()) {
                int index = name.indexOf(':');
                if (index > 0) {
                    String prefix = name.substring(0, index);
                    if (prefix != null) {
                        Class c = associationMappings.get(prefix);
                        if (c != null) {
                            String postfix = name.substring(index + 1);
                            WOAssociation newAssociation = createAssociationForClass(c, keyPath, isConstant);
                            associations.removeObjectForKey(name);
                            associations.setObjectForKey(newAssociation, postfix);
                        }
                    }
                }
            }
            if (isConstant && keyPath.startsWith(ognlBindingFlag())) {
                String ognlExpression = keyPath.substring(ognlBindingFlag().length(), keyPath.length());
                if (ognlExpression.length() > 0) {
                    WOAssociation newAssociation = new WOOgnlAssociation(ognlExpression);
                    NSArray keys = associations.allKeysForObject(association);
                    // + (keys.count() == 1 ? keys.lastObject() : keys) + " expression: " + ognlExpression);
                    if (keys.count() == 1) {
                        associations.setObjectForKey(newAssociation, keys.lastObject());
                    } else {
                        for (Enumeration ee = keys.objectEnumerator(); ee.hasMoreElements(); ) {
                            associations.setObjectForKey(newAssociation, e.nextElement());
                        }
                    }
                }
            }
        }
    }
}
Also used : WOAssociation(com.webobjects.appserver.WOAssociation) Enumeration(java.util.Enumeration) NSArray(com.webobjects.foundation.NSArray) WOBindingNameAssociation(com.webobjects.appserver._private.WOBindingNameAssociation) WOConstantValueAssociation(com.webobjects.appserver._private.WOConstantValueAssociation) WOKeyValueAssociation(com.webobjects.appserver._private.WOKeyValueAssociation)

Example 4 with WOAssociation

use of com.webobjects.appserver.WOAssociation in project wonder-slim by undur.

the class WOHelperFunctionParser method parserHelperAssociation.

protected WOAssociation parserHelperAssociation(WOAssociation originalAssociation) {
    WOAssociation association = originalAssociation;
    String originalKeyPath = null;
    if (association instanceof WOKeyValueAssociation) {
        WOKeyValueAssociation kvAssociation = (WOKeyValueAssociation) association;
        originalKeyPath = kvAssociation.keyPath();
    }
    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);
            }
            StringBuilder ognlKeyPath = new StringBuilder();
            ognlKeyPath.append('~');
            ognlKeyPath.append("@" + WOHelperFunctionRegistry.class.getName() + "@registry()._helperInstanceForFrameworkNamed(#this, \"");
            ognlKeyPath.append(helperFunctionName);
            ognlKeyPath.append("\", \"");
            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(')');
            log.debug("Converted {} into {}", originalKeyPath, ognlKeyPath);
            association = new WOConstantValueAssociation(ognlKeyPath.toString());
        }
    }
    return association;
}
Also used : WOAssociation(com.webobjects.appserver.WOAssociation) WOConstantValueAssociation(com.webobjects.appserver._private.WOConstantValueAssociation) WOKeyValueAssociation(com.webobjects.appserver._private.WOKeyValueAssociation) WOHTMLCommentString(com.webobjects.appserver._private.WOHTMLCommentString)

Example 5 with WOAssociation

use of com.webobjects.appserver.WOAssociation in project wonder-slim by undur.

the class WOHelperFunctionDeclarationParser method _associationWithKey.

public static WOAssociation _associationWithKey(String associationValue, NSDictionary quotedStrings) {
    WOAssociation association = null;
    if (associationValue != null && associationValue.startsWith("~")) {
        int associationValueLength = associationValue.length();
        StringBuilder ognlValue = new StringBuilder();
        int lastIndex = 0;
        int index = 0;
        while ((index = associationValue.indexOf(WOHelperFunctionDeclarationParser.QUOTED_STRING_KEY, lastIndex)) != -1) {
            ognlValue.append(associationValue.substring(lastIndex, index));
            int wodpValueStartIndex = index + WOHelperFunctionDeclarationParser.QUOTED_STRING_KEY.length();
            int wodpValueEndIndex = wodpValueStartIndex;
            for (; wodpValueEndIndex < associationValueLength && Character.isDigit(associationValue.charAt(wodpValueEndIndex)); wodpValueEndIndex++) {
            // do nothing
            }
            String wodpKey = WOHelperFunctionDeclarationParser.QUOTED_STRING_KEY + associationValue.substring(wodpValueStartIndex, wodpValueEndIndex);
            String quotedString = (String) quotedStrings.objectForKey(wodpKey);
            if (quotedString != null) {
                quotedString = quotedString.replaceAll("\\\"", "\\\\\"");
                ognlValue.append("\"");
                ognlValue.append(quotedString);
                ognlValue.append("\"");
            }
            lastIndex = wodpValueEndIndex;
        }
        ognlValue.append(associationValue.substring(lastIndex));
        associationValue = ognlValue.toString();
        association = WOHelperFunctionAssociation.associationWithValue(associationValue);
    } else {
        String quotedString = (String) quotedStrings.objectForKey(associationValue);
        // MS: WO 5.4 converts \n to an actual newline. I don't know if WO 5.3 does, too, but let's go ahead and be compatible with them as long as nobody is yelling.
        if (quotedString != null) {
            int backslashIndex = quotedString.indexOf('\\');
            if (backslashIndex != -1) {
                StringBuilder sb = new StringBuilder(quotedString);
                int length = sb.length();
                for (int i = backslashIndex; i < length; i++) {
                    char ch = sb.charAt(i);
                    if (ch == '\\' && i < length) {
                        char nextCh = sb.charAt(i + 1);
                        if (nextCh == 'n') {
                            sb.replace(i, i + 2, "\n");
                        } else if (nextCh == 'r') {
                            sb.replace(i, i + 2, "\r");
                        } else if (nextCh == 't') {
                            sb.replace(i, i + 2, "\t");
                        } else {
                            sb.replace(i, i + 2, String.valueOf(nextCh));
                        }
                        length--;
                    }
                }
                quotedString = sb.toString();
            }
            association = WOHelperFunctionAssociation.associationWithValue(quotedString);
        } else if (_NSStringUtilities.isNumber(associationValue)) {
            Number number = null;
            if (associationValue != null && associationValue.contains(".")) {
                number = Double.valueOf(associationValue);
            } else {
                number = WOShared.unsignedIntNumber(Integer.parseInt(associationValue));
            }
            association = WOHelperFunctionAssociation.associationWithValue(number);
        } else if ("true".equalsIgnoreCase(associationValue) || "yes".equalsIgnoreCase(associationValue)) {
            association = WOConstantValueAssociation.TRUE;
        } else if ("false".equalsIgnoreCase(associationValue) || "no".equalsIgnoreCase(associationValue) || "nil".equalsIgnoreCase(associationValue) || "null".equalsIgnoreCase(associationValue)) {
            association = WOConstantValueAssociation.FALSE;
        } else {
            association = WOHelperFunctionAssociation.associationWithKeyPath(associationValue);
        }
    }
    return association;
}
Also used : WOAssociation(com.webobjects.appserver.WOAssociation)

Aggregations

WOAssociation (com.webobjects.appserver.WOAssociation)27 NSMutableDictionary (com.webobjects.foundation.NSMutableDictionary)10 Enumeration (java.util.Enumeration)10 WOHTMLCommentString (com.webobjects.appserver._private.WOHTMLCommentString)8 WOComponent (com.webobjects.appserver.WOComponent)4 WOConstantValueAssociation (com.webobjects.appserver._private.WOConstantValueAssociation)4 NSDictionary (com.webobjects.foundation.NSDictionary)4 WOKeyValueAssociation (com.webobjects.appserver._private.WOKeyValueAssociation)3 NSArray (com.webobjects.foundation.NSArray)3 WODeclaration (com.webobjects.appserver._private.WODeclaration)2 WOResponse (com.webobjects.appserver.WOResponse)1 WOBindingNameAssociation (com.webobjects.appserver._private.WOBindingNameAssociation)1 ERXMutableURL (er.extensions.foundation.ERXMutableURL)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1