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 org.xml.sax.Attributes; 017 018import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext; 019import ch.qos.logback.core.model.Model; 020import ch.qos.logback.core.model.TimestampModel; 021import ch.qos.logback.core.util.OptionHelper; 022 023/** 024 * Given a key and a date-and-time pattern, puts a property to the context, with 025 * the specified key and value equal to the current time in the format 026 * corresponding to the specified date-and-time pattern. 027 * 028 * @author Ceki Gülcü 029 * 030 */ 031public class TimestampAction extends BaseModelAction { 032 033 public static final String DATE_PATTERN_ATTRIBUTE = "datePattern"; 034 public static final String TIME_REFERENCE_ATTRIBUTE = "timeReference"; 035 036 @Override 037 protected boolean validPreconditions(SaxEventInterpretationContext interpretationContext, String name, 038 Attributes attributes) { 039 boolean valid = true; 040 String keyStr = attributes.getValue(KEY_ATTRIBUTE); 041 if (OptionHelper.isNullOrEmpty(keyStr)) { 042 addError("Attribute named [" + KEY_ATTRIBUTE + "] cannot be empty"); 043 valid = false; 044 } 045 String datePatternStr = attributes.getValue(DATE_PATTERN_ATTRIBUTE); 046 if (OptionHelper.isNullOrEmpty(datePatternStr)) { 047 addError("Attribute named [" + DATE_PATTERN_ATTRIBUTE + "] cannot be empty"); 048 valid = false; 049 } 050 return valid; 051 } 052 053 @Override 054 protected Model buildCurrentModel(SaxEventInterpretationContext interpretationContext, String name, 055 Attributes attributes) { 056 TimestampModel timestampModel = new TimestampModel(); 057 058 timestampModel.setKey(attributes.getValue(KEY_ATTRIBUTE)); 059 timestampModel.setDatePattern(attributes.getValue(DATE_PATTERN_ATTRIBUTE)); 060 timestampModel.setTimeReference(attributes.getValue(TIME_REFERENCE_ATTRIBUTE)); 061 timestampModel.setScopeStr(attributes.getValue(SCOPE_ATTRIBUTE)); 062 063 return timestampModel; 064 065 } 066 067}