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 ch.qos.logback.core.CoreConstants;
17 import ch.qos.logback.core.joran.JoranConstants;
18 import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
19 import ch.qos.logback.core.model.ConversionRuleModel;
20 import ch.qos.logback.core.model.Model;
21 import ch.qos.logback.core.util.OptionHelper;
22 import org.xml.sax.Attributes;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import static ch.qos.logback.core.joran.JoranConstants.CONVERSION_WORD_ATTRIBUTE;
28
29 public class ConversionRuleAction extends BaseModelAction {
30
31 static public String CONVERTER_CLASS_ATTRIBUTE = "converterClass";
32
33
34 @Override
35 protected boolean validPreconditions(SaxEventInterpretationContext seic, String name, Attributes attributes) {
36 PreconditionValidator pv = new PreconditionValidator(this, seic, name, attributes);
37
38 boolean invalidConverterClassAttribute = pv.isInvalidAttribute(CONVERTER_CLASS_ATTRIBUTE);
39 boolean invalidClassAttribute = pv.isInvalidAttribute(CLASS_ATTRIBUTE);
40
41 if(!invalidConverterClassAttribute) {
42 pv.addWarn("["+CONVERTER_CLASS_ATTRIBUTE +"] attribute is deprecated and replaced by ["+CLASS_ATTRIBUTE+
43 "]. "+pv.getLocationSuffix());
44 }
45 boolean missingClass = invalidClassAttribute && invalidConverterClassAttribute;
46 if(missingClass) {
47 pv.addMissingAttributeError(CLASS_ATTRIBUTE);
48 return false;
49 }
50
51 boolean multipleClassAttributes = (!invalidClassAttribute) && (!invalidConverterClassAttribute);
52 if(multipleClassAttributes) {
53 pv.addWarn("Both ["+CONVERTER_CLASS_ATTRIBUTE+"] attribute and ["+CLASS_ATTRIBUTE+"] attribute specified. ");
54 pv.addWarn( "["+CLASS_ATTRIBUTE+"] attribute will override. ");
55 }
56 pv.validateGivenAttribute(CONVERSION_WORD_ATTRIBUTE);
57 return pv.isValid();
58 }
59
60
61
62 @Override
63 protected Model buildCurrentModel(SaxEventInterpretationContext interpretationContext, String name,
64 Attributes attributes) {
65 ConversionRuleModel conversionRuleModel = new ConversionRuleModel();
66 conversionRuleModel.setConversionWord(attributes.getValue(CONVERSION_WORD_ATTRIBUTE));
67
68 String converterClassStr = attributes.getValue(CONVERTER_CLASS_ATTRIBUTE);
69 if(!OptionHelper.isNullOrEmpty(converterClassStr)) {
70 conversionRuleModel.setClassName(converterClassStr);
71 }
72 // if both converterClass and class are specified the latter overrides.
73 String classStr = attributes.getValue(CLASS_ATTRIBUTE);
74 if(!OptionHelper.isNullOrEmpty(classStr)) {
75 conversionRuleModel.setClassName(classStr);
76 }
77 return conversionRuleModel;
78 }
79
80 // /**
81 // * Instantiates a layout of the given class and sets its name.
82 // *
83 // */
84 // @SuppressWarnings("unchecked")
85 // public void begin(SaxEventInterpretationContext ec, String localName, Attributes attributes) {
86 // // Let us forget about previous errors (in this object)
87 // inError = false;
88 //
89 // String errorMsg;
90 // String conversionWord = attributes.getValue(CONVERSION_WORD_ATTRIBUTE);
91 // String converterClass = attributes.getValue(JoranConstants.CONVERTER_CLASS_ATTRIBUTE);
92 //
93 // if (OptionHelper.isNullOrEmptyOrAllSpaces(conversionWord)) {
94 // inError = true;
95 // errorMsg = "No 'conversionWord' attribute in <conversionRule>";
96 // addError(errorMsg);
97 //
98 // return;
99 // }
100 //
101 // if (OptionHelper.isNullOrEmptyOrAllSpaces(converterClass)) {
102 // inError = true;
103 // errorMsg = "No 'converterClass' attribute in <conversionRule>";
104 // ec.addError(errorMsg);
105 //
106 // return;
107 // }
108 //
109 // try {
110 // Map<String, String> ruleRegistry = (Map<String, String>) context
111 // .getObject(CoreConstants.PATTERN_RULE_REGISTRY);
112 // if (ruleRegistry == null) {
113 // ruleRegistry = new HashMap<String, String>();
114 // context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, ruleRegistry);
115 // }
116 // // put the new rule into the rule registry
117 // addInfo("registering conversion word " + conversionWord + " with class [" + converterClass + "]");
118 // ruleRegistry.put(conversionWord, converterClass);
119 // } catch (Exception oops) {
120 // inError = true;
121 // errorMsg = "Could not add conversion rule to PatternLayout.";
122 // addError(errorMsg);
123 // }
124 // }
125 //
126 //
127 // /**
128 // * Once the children elements are also parsed, now is the time to activate the
129 // * appender options.
130 // */
131 // public void end(SaxEventInterpretationContext ec, String n) {
132 // }
133 //
134 // public void finish(SaxEventInterpretationContext ec) {
135 // }
136 }