1 /**
2  * Logback: the reliable, generic, fast and flexible logging framework.
3  * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4  *
5  * This program and the accompanying materials are dual-licensed under
6  * either the terms of the Eclipse Public License v1.0 as published by
7  * the Eclipse Foundation
8  *
9  *   or (per the licensee's choosing)
10  *
11  * under the terms of the GNU Lesser General Public License version 2.1
12  * as published by the Free Software Foundation.
13  */

14 package ch.qos.logback.core.joran.action;
15
16 import org.xml.sax.Attributes;
17
18 import ch.qos.logback.core.joran.spi.InterpretationContext;
19 import ch.qos.logback.core.joran.util.PropertySetter;
20 import ch.qos.logback.core.joran.util.beans.BeanDescriptionCache;
21
22 public class ParamAction extends Action {
23     static String NO_NAME = "No name attribute in <param> element";
24     static String NO_VALUE = "No value attribute in <param> element";
25     boolean inError = false;
26
27     private final BeanDescriptionCache beanDescriptionCache;
28     public ParamAction(BeanDescriptionCache beanDescriptionCache) {
29         this.beanDescriptionCache=beanDescriptionCache;
30     }
31
32     public void begin(InterpretationContext ec, String localName, Attributes attributes) {
33         String name = attributes.getValue(NAME_ATTRIBUTE);
34         String value = attributes.getValue(VALUE_ATTRIBUTE);
35
36         if (name == null) {
37             inError = true;
38             addError(NO_NAME);
39             return;
40         }
41
42         if (value == null) {
43             inError = true;
44             addError(NO_VALUE);
45             return;
46         }
47
48         // remove both leading and trailing spaces
49         value = value.trim();
50
51         Object o = ec.peekObject();
52         PropertySetter propSetter = new PropertySetter(beanDescriptionCache,o);
53         propSetter.setContext(context);
54         value = ec.subst(value);
55
56         // allow for variable substitution for name as well
57         name = ec.subst(name);
58
59         // getLogger().debug(
60         // "In ParamAction setting parameter [{}] to value [{}].", name, value);
61         propSetter.setProperty(name, value);
62     }
63
64     public void end(InterpretationContext ec, String localName) {
65     }
66
67     public void finish(InterpretationContext ec) {
68     }
69 }
70