Search in sources :

Example 46 with NSArray

use of com.webobjects.foundation.NSArray in project wonder-slim by undur.

the class ERXSimpleTemplateParser method parseTemplateWithObject.

/**
 * This method replaces the keys enclosed between the
 * delimiter with the values found in object and otherObject.
 * It first looks for a value in object, and then in otherObject
 * if the key is not found in object. Therefore, otherObject is
 * a good place to store default values while object is a
 * good place to override default values.
 * <p>
 * When the value is not found in both object and otherObject,
 * it will replace the key with the undefined key label which
 * defaults to "?". You can set the label via the constructor
 * {@link #ERXSimpleTemplateParser(String)}. Note that a <code>null</code>
 * result will also output the label, so you might want to have the empty
 * string as the undefined key label.
 *
 * @param template to use to parse
 * @param delimiter to use to check for keys
 * @param object to resolve keys off of
 * @param otherObject object used to resolve default keys
 * @return parsed template with keys replaced
 */
public String parseTemplateWithObject(String template, String delimiter, Object object, Object otherObject) {
    if (template == null)
        throw new IllegalArgumentException("Attempting to parse null template!");
    if (object == null) {
        throw new IllegalArgumentException("Attempting to parse template with null object!");
    }
    if (delimiter == null) {
        delimiter = DEFAULT_DELIMITER;
    }
    if (!isLoggingDisabled) {
        log.debug("Parsing template: {} with delimiter: {} object: {}", template, delimiter, object);
        log.debug("Template: {}", template);
        log.debug("Delim: {}", delimiter);
        log.debug("otherObject: {}", otherObject);
    }
    NSArray components = NSArray.componentsSeparatedByString(template, delimiter);
    if (!isLoggingDisabled) {
        log.debug("Components: {}", components);
    }
    // if the template starts with delim, the first component will be a zero-length string
    boolean deriveElement = false;
    StringBuilder sb = new StringBuilder();
    Object[] objects;
    if (otherObject != null) {
        objects = new Object[] { object, otherObject };
    } else {
        objects = new Object[] { object };
    }
    for (Enumeration e = components.objectEnumerator(); e.hasMoreElements(); ) {
        String element = (String) e.nextElement();
        if (!isLoggingDisabled) {
            log.debug("Processing Element: {}", element);
        }
        if (deriveElement) {
            if (!isLoggingDisabled) {
                log.debug("Deriving value ...");
            }
            if (element.length() == 0) {
                throw new IllegalArgumentException("\"\" is not a valid keypath in template: " + template);
            }
            Object result = _undefinedKeyLabel;
            for (int i = 0; i < objects.length; i++) {
                Object o = objects[i];
                if (o != null && result == _undefinedKeyLabel) {
                    try {
                        if (!isLoggingDisabled) {
                            log.debug("calling valueForKeyPath({}, {})", o, element);
                        }
                        result = doGetValue(element, o);
                        // key is not defined. (NSDictionary doesn't seem to throw the exception.)
                        if (result == null) {
                            result = _undefinedKeyLabel;
                        }
                    } catch (NSKeyValueCoding.UnknownKeyException t) {
                        result = _undefinedKeyLabel;
                    } catch (Throwable t) {
                        throw new NSForwardException(t, "An exception occured while parsing element, " + element + ", of template, \"" + template + "\": " + t.getMessage());
                    }
                }
            }
            if (result == _undefinedKeyLabel) {
                if (!isLoggingDisabled) {
                    log.debug("Could not find a value for '{}' of template, '{}' in either the object or extra data.", element, template);
                }
            }
            sb.append(result.toString());
            deriveElement = false;
        } else {
            if (element.length() > 0) {
                sb.append(element);
            }
            deriveElement = true;
        }
        if (!isLoggingDisabled) {
            log.debug("Buffer: {}", sb);
        }
    }
    return sb.toString();
}
Also used : Enumeration(java.util.Enumeration) NSArray(com.webobjects.foundation.NSArray) NSKeyValueCoding(com.webobjects.foundation.NSKeyValueCoding) NSForwardException(com.webobjects.foundation.NSForwardException)

Example 47 with NSArray

use of com.webobjects.foundation.NSArray in project wonder-slim by undur.

the class ERXProperties method pathsForUserAndBundleProperties.

public static NSArray<String> pathsForUserAndBundleProperties(boolean reportLoggingEnabled) {
    NSMutableArray<String> propertiesPaths = new NSMutableArray();
    NSMutableArray<String> projectsInfo = new NSMutableArray();
    /*  Properties for frameworks */
    NSArray frameworkNames = (NSArray) NSBundle.frameworkBundles().valueForKey("name");
    Enumeration e = frameworkNames.reverseObjectEnumerator();
    while (e.hasMoreElements()) {
        String frameworkName = (String) e.nextElement();
        String propertyPath = pathForResourceNamed("Properties", frameworkName, null);
        addIfPresent(frameworkName + ".framework", propertyPath, propertiesPaths, projectsInfo);
        /**
         * Properties.dev -- per-Framework-dev properties
         * This adds support for Properties.dev in your Frameworks new load order will be
         */
        String devPropertiesPath = ERXApplication.isDevelopmentModeSafe() ? ERXProperties.variantPropertiesInBundle("dev", frameworkName) : null;
        addIfPresent(frameworkName + ".framework.dev", devPropertiesPath, propertiesPaths, projectsInfo);
        /**
         * Properties.<userName> -- per-Framework-per-User properties
         */
        String userPropertiesPath = ERXProperties.variantPropertiesInBundle(NSProperties.getProperty("user.name"), frameworkName);
        addIfPresent(frameworkName + ".framework.user", userPropertiesPath, propertiesPaths, projectsInfo);
    }
    NSBundle mainBundle = NSBundle.mainBundle();
    if (mainBundle != null) {
        String mainBundleName = mainBundle.name();
        String appPath = pathForResourceNamed("Properties", "app", null);
        addIfPresent(mainBundleName + ".app", appPath, propertiesPaths, projectsInfo);
    }
    /*  WebObjects.properties in the user home directory */
    String userHome = NSProperties.getProperty("user.home");
    if (userHome != null && userHome.length() > 0) {
        File file = new File(userHome, "WebObjects.properties");
        if (file.exists() && file.isFile() && file.canRead()) {
            try {
                String userHomePath = file.getCanonicalPath();
                addIfPresent("{$user.home}/WebObjects.properties", userHomePath, propertiesPaths, projectsInfo);
            } catch (java.io.IOException ex) {
                log.error("Failed to load the configuration file '{}'.", file, ex);
            }
        }
    }
    /*  Optional properties files */
    if (optionalConfigurationFiles() != null && optionalConfigurationFiles().count() > 0) {
        for (Enumeration configEnumerator = optionalConfigurationFiles().objectEnumerator(); configEnumerator.hasMoreElements(); ) {
            String configFile = (String) configEnumerator.nextElement();
            File file = new File(configFile);
            if (file.exists() && file.isFile() && file.canRead()) {
                try {
                    String optionalPath = file.getCanonicalPath();
                    addIfPresent("Optional Configuration", optionalPath, propertiesPaths, projectsInfo);
                } catch (java.io.IOException ex) {
                    log.error("Failed to load configuration file '{}'.", file, ex);
                }
            } else {
                log.error("The optional configuration file '{}' either does not exist or could not be read.", file);
            }
        }
    }
    optionalPropertiesLoader(NSProperties.getProperty("user.name"), propertiesPaths, projectsInfo);
    /**
     * /etc/WebObjects/AppName/Properties -- per-Application-per-Machine properties
     */
    String applicationMachinePropertiesPath = ERXProperties.applicationMachinePropertiesPath("Properties");
    addIfPresent("Application-Machine Properties", applicationMachinePropertiesPath, propertiesPaths, projectsInfo);
    /**
     * Properties.dev -- per-Application-dev properties
     */
    String applicationDeveloperPropertiesPath = ERXProperties.applicationDeveloperProperties();
    addIfPresent("Application-Developer Properties", applicationDeveloperPropertiesPath, propertiesPaths, projectsInfo);
    /**
     * Properties.<userName> -- per-Application-per-User properties
     */
    String applicationUserPropertiesPath = ERXProperties.applicationUserProperties();
    addIfPresent("Application-User Properties", applicationUserPropertiesPath, propertiesPaths, projectsInfo);
    /*  Report the result */
    if (reportLoggingEnabled && projectsInfo.count() > 0 && log.isInfoEnabled()) {
        StringBuilder message = new StringBuilder();
        message.append("\n\n").append("ERXProperties has found the following Properties files: \n");
        message.append(projectsInfo.componentsJoinedByString("\n"));
        message.append('\n');
        message.append("ERXProperties currently has the following properties:\n");
        message.append(ERXProperties.logString(NSProperties._getProperties()));
        log.info(message.toString());
    }
    return propertiesPaths.immutableClone();
}
Also used : Enumeration(java.util.Enumeration) NSBundle(com.webobjects.foundation.NSBundle) NSArray(com.webobjects.foundation.NSArray) NSMutableArray(com.webobjects.foundation.NSMutableArray) IOException(java.io.IOException) File(java.io.File)

Example 48 with NSArray

use of com.webobjects.foundation.NSArray in project wonder-slim by undur.

the class ERXProperties method optionalConfigurationFiles.

/**
 * Gets an array of optionally defined configuration files.  For each file, if it does not
 * exist as an absolute path, ERXProperties will attempt to resolve it as an application resource
 * and use that instead.
 *
 * @return array of configuration file names
 */
private static NSArray optionalConfigurationFiles() {
    NSArray immutableOptionalConfigurationFiles = arrayForKey("er.extensions.ERXProperties.OptionalConfigurationFiles");
    NSMutableArray optionalConfigurationFiles = null;
    if (immutableOptionalConfigurationFiles != null) {
        optionalConfigurationFiles = immutableOptionalConfigurationFiles.mutableClone();
        for (int i = 0; i < optionalConfigurationFiles.count(); i++) {
            String optionalConfigurationFile = (String) optionalConfigurationFiles.objectAtIndex(i);
            if (!new File(optionalConfigurationFile).exists()) {
                String resourcePropertiesPath = pathForResourceNamed(optionalConfigurationFile, "app", null);
                if (resourcePropertiesPath != null) {
                    optionalConfigurationFiles.replaceObjectAtIndex(ERXProperties.getActualPath(resourcePropertiesPath), i);
                }
            }
        }
    }
    return optionalConfigurationFiles;
}
Also used : NSArray(com.webobjects.foundation.NSArray) NSMutableArray(com.webobjects.foundation.NSMutableArray) File(java.io.File)

Example 49 with NSArray

use of com.webobjects.foundation.NSArray in project wonder-slim by undur.

the class ERXWOBrowser method _fastTakeValuesFromRequest.

private void _fastTakeValuesFromRequest(WORequest worequest, WOContext wocontext) {
    WOComponent wocomponent = wocontext.component();
    if (_selections != null && !isDisabledInContext(wocontext) && wocontext.wasFormSubmitted()) {
        String s = nameInContext(wocontext, wocomponent);
        NSArray nsarray = worequest.formValuesForKey(s);
        int i = nsarray != null ? nsarray.count() : 0;
        NSMutableArray nsmutablearray = new NSMutableArray(i);
        List vector = null;
        if (i != 0) {
            NSArray nsarray1 = null;
            List vector1 = null;
            if (_list != null) {
                Object obj = _list.valueInComponent(wocomponent);
                if (obj != null) {
                    if (obj instanceof NSArray) {
                        nsarray1 = (NSArray) obj;
                    } else if (obj instanceof List) {
                        vector1 = (List) obj;
                        nsmutablearray = null;
                        vector = new ArrayList(i);
                    } else {
                        throw new IllegalArgumentException("<" + getClass().getName() + "> Evaluating 'list' binding returned a " + obj.getClass().getName() + " when it should return either a com.webobjects.foundation.NSArray, or a java.lang.Vector .");
                    }
                }
            }
            for (int j = 0; j < i; j++) {
                String s1 = (String) nsarray.objectAtIndex(j);
                int k = Integer.parseInt(s1);
                if (nsarray1 != null) {
                    Object obj2 = nsarray1.objectAtIndex(k);
                    nsmutablearray.addObject(obj2);
                } else {
                    Object obj3 = vector1.get(k);
                    vector.add(obj3);
                }
            }
        }
        Object newValue = (nsmutablearray != null ? nsmutablearray : vector);
        setSelectedValue(newValue, wocomponent);
    }
}
Also used : NSArray(com.webobjects.foundation.NSArray) WOComponent(com.webobjects.appserver.WOComponent) NSMutableArray(com.webobjects.foundation.NSMutableArray) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 50 with NSArray

use of com.webobjects.foundation.NSArray in project wonder-slim by undur.

the class ERXRadioButtonMatrix method setSelection.

public void setSelection(String anIndex) {
    if (anIndex != null) {
        // ** push the selection to the parent
        NSArray anItemList = (NSArray) valueForBinding("list");
        Object aSelectedObject = anItemList.objectAtIndex(Integer.parseInt(anIndex));
        setValueForBinding(aSelectedObject, "selection");
    // ** and force it to be pulled if there's a next time.
    }
    _selection = null;
}
Also used : NSArray(com.webobjects.foundation.NSArray)

Aggregations

NSArray (com.webobjects.foundation.NSArray)53 Enumeration (java.util.Enumeration)17 NSMutableArray (com.webobjects.foundation.NSMutableArray)13 NSBundle (com.webobjects.foundation.NSBundle)5 NSMutableDictionary (com.webobjects.foundation.NSMutableDictionary)5 File (java.io.File)4 List (java.util.List)4 WOAssociation (com.webobjects.appserver.WOAssociation)3 WOComponent (com.webobjects.appserver.WOComponent)3 NSDictionary (com.webobjects.foundation.NSDictionary)3 IOException (java.io.IOException)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 WOElement (com.webobjects.appserver.WOElement)2 WOResponse (com.webobjects.appserver.WOResponse)2 WOConstantValueAssociation (com.webobjects.appserver._private.WOConstantValueAssociation)2 EOEvent (com.webobjects.eocontrol.EOEvent)2 EOSortOrdering (com.webobjects.eocontrol.EOSortOrdering)2 NSForwardException (com.webobjects.foundation.NSForwardException)2 NSKeyValueCoding (com.webobjects.foundation.NSKeyValueCoding)2