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 java.util.HashMap; 017import java.util.Map; 018 019import org.xml.sax.Attributes; 020 021import ch.qos.logback.core.CoreConstants; 022import ch.qos.logback.core.joran.JoranConstants; 023import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext; 024import ch.qos.logback.core.util.OptionHelper; 025 026public class ConversionRuleAction extends Action { 027 boolean inError = false; 028 029 /** 030 * Instantiates a layout of the given class and sets its name. 031 * 032 */ 033 @SuppressWarnings("unchecked") 034 public void begin(SaxEventInterpretationContext ec, String localName, Attributes attributes) { 035 // Let us forget about previous errors (in this object) 036 inError = false; 037 038 String errorMsg; 039 String conversionWord = attributes.getValue(JoranConstants.CONVERSION_WORD_ATTRIBUTE); 040 String converterClass = attributes.getValue(JoranConstants.CONVERTER_CLASS_ATTRIBUTE); 041 042 if (OptionHelper.isNullOrEmpty(conversionWord)) { 043 inError = true; 044 errorMsg = "No 'conversionWord' attribute in <conversionRule>"; 045 addError(errorMsg); 046 047 return; 048 } 049 050 if (OptionHelper.isNullOrEmpty(converterClass)) { 051 inError = true; 052 errorMsg = "No 'converterClass' attribute in <conversionRule>"; 053 ec.addError(errorMsg); 054 055 return; 056 } 057 058 try { 059 Map<String, String> ruleRegistry = (Map<String, String>) context 060 .getObject(CoreConstants.PATTERN_RULE_REGISTRY); 061 if (ruleRegistry == null) { 062 ruleRegistry = new HashMap<String, String>(); 063 context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, ruleRegistry); 064 } 065 // put the new rule into the rule registry 066 addInfo("registering conversion word " + conversionWord + " with class [" + converterClass + "]"); 067 ruleRegistry.put(conversionWord, converterClass); 068 } catch (Exception oops) { 069 inError = true; 070 errorMsg = "Could not add conversion rule to PatternLayout."; 071 addError(errorMsg); 072 } 073 } 074 075 /** 076 * Once the children elements are also parsed, now is the time to activate the 077 * appender options. 078 */ 079 public void end(SaxEventInterpretationContext ec, String n) { 080 } 081 082 public void finish(SaxEventInterpretationContext ec) { 083 } 084}