001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.joran.action;
015
016import ch.qos.logback.core.model.processor.ModelInterpretationContext;
017import ch.qos.logback.core.util.OptionHelper;
018
019public class ActionUtil {
020
021    public enum Scope {
022        LOCAL, CONTEXT, SYSTEM
023    };
024
025    /**
026     * Convert a string into a scope. Scope.LOCAL is returned by default.
027     * 
028     * @param scopeStr
029     * @return a scope corresponding to the input string; Scope.LOCAL by default.
030     */
031    static public Scope stringToScope(String scopeStr) {
032        if (Scope.SYSTEM.toString().equalsIgnoreCase(scopeStr))
033            return Scope.SYSTEM;
034        if (Scope.CONTEXT.toString().equalsIgnoreCase(scopeStr))
035            return Scope.CONTEXT;
036
037        return Scope.LOCAL;
038    }
039
040//    static public void setProperty(SaxEventInterpretationContext ic, String key, String value, Scope scope) {
041//        switch (scope) {
042//        case LOCAL:
043//            ic.addSubstitutionProperty(key, value);
044//            break;
045//        case CONTEXT:
046//            ic.getContext().putProperty(key, value);
047//            break;
048//        case SYSTEM:
049//            OptionHelper.setSystemProperty(ic, key, value);
050//        }
051//    }
052
053    static public void setProperty(ModelInterpretationContext ic, String key, String value, Scope scope) {
054        switch (scope) {
055        case LOCAL:
056            ic.addSubstitutionProperty(key, value);
057            break;
058        case CONTEXT:
059            ic.getContext().putProperty(key, value);
060            break;
061        case SYSTEM:
062            OptionHelper.setSystemProperty(ic, key, value);
063        }
064    }
065
066//    /**
067//     * Add all the properties found in the argument named 'props' to an
068//     * InterpretationContext.
069//     */
070//    static public void setProperties(SaxEventInterpretationContext ic, Properties props, Scope scope) {
071//        switch (scope) {
072//        case LOCAL:
073//            ic.addSubstitutionProperties(props);
074//            break;
075//        case CONTEXT:
076//            ContextUtil cu = new ContextUtil(ic.getContext());
077//            cu.addProperties(props);
078//            break;
079//        case SYSTEM:
080//            OptionHelper.setSystemProperties(ic, props);
081//        }
082//    }
083
084}