Skip to content

Commit

Permalink
ongoing work on ConversionRule support
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Aug 3, 2024
1 parent a33fdd7 commit 62d1eb1
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ protected Model buildCurrentModel(SaxEventInterpretationContext interpretationCo
@Override
protected boolean validPreconditions(SaxEventInterpretationContext seic, String name, Attributes attributes) {
PreconditionValidator validator = new PreconditionValidator(this, seic, name, attributes);
validator.generic(ENV_ENTRY_NAME_ATTR);
validator.generic(AS_ATTR);
validator.validateGivenAttribute(ENV_ENTRY_NAME_ATTR);
validator.validateGivenAttribute(AS_ATTR);

return validator.isValid();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,55 @@
*/
package ch.qos.logback.core.joran.action;

import java.util.HashMap;
import java.util.Map;

import org.xml.sax.Attributes;

import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.joran.JoranConstants;
import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
import ch.qos.logback.core.model.ConversionRuleModel;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.util.OptionHelper;
import org.xml.sax.Attributes;

import java.util.HashMap;
import java.util.Map;

import static ch.qos.logback.core.joran.JoranConstants.CONVERSION_WORD_ATTRIBUTE;

public class ConversionRuleAction extends BaseModelAction {

static public String CONVERTER_CLASS_ATTRIBUTE = "converterClass";

public class ConversionRuleAction extends Action {
boolean inError = false;

@Override
protected boolean validPreconditions(SaxEventInterpretationContext seic, String name, Attributes attributes) {
PreconditionValidator pv = new PreconditionValidator(this, seic, name, attributes);

boolean invalidConverterClassAttribute = pv.isInvalidAttribute(CONVERTER_CLASS_ATTRIBUTE);
boolean invalidClassAttribute = pv.isInvalidAttribute(CONVERTER_CLASS_ATTRIBUTE);

if(!invalidConverterClassAttribute) {
pv.addWarn("["+CONVERTER_CLASS_ATTRIBUTE +"] attribute is deprecated and replaced by ["+CLASS_ATTRIBUTE+
"]. "+pv.getLocationSuffix());
}
boolean missingClass = invalidClassAttribute && invalidConverterClassAttribute;
if(missingClass) {
pv.addMissingAttributeError(CLASS_ATTRIBUTE);
return false;
}
pv.validateGivenAttribute(CONVERSION_WORD_ATTRIBUTE);
return pv.isValid();
}



@Override
protected Model buildCurrentModel(SaxEventInterpretationContext interpretationContext, String name,
Attributes attributes) {
ConversionRuleModel conversionRuleModel = new ConversionRuleModel();
conversionRuleModel.setConversionWord(attributes.getValue(CONVERSION_WORD_ATTRIBUTE));
conversionRuleModel.setClassName(attributes.getValue(CLASS_ATTRIBUTE));

return conversionRuleModel;
}

/**
* Instantiates a layout of the given class and sets its name.
Expand All @@ -36,7 +73,7 @@ public void begin(SaxEventInterpretationContext ec, String localName, Attributes
inError = false;

String errorMsg;
String conversionWord = attributes.getValue(JoranConstants.CONVERSION_WORD_ATTRIBUTE);
String conversionWord = attributes.getValue(CONVERSION_WORD_ATTRIBUTE);
String converterClass = attributes.getValue(JoranConstants.CONVERTER_CLASS_ATTRIBUTE);

if (OptionHelper.isNullOrEmptyOrAllSpaces(conversionWord)) {
Expand Down Expand Up @@ -72,6 +109,7 @@ public void begin(SaxEventInterpretationContext ec, String localName, Attributes
}
}


/**
* Once the children elements are also parsed, now is the time to activate the
* appender options.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
public class PreconditionValidator extends ContextAwareBase {

boolean valid = true;
SaxEventInterpretationContext intercon;
SaxEventInterpretationContext seic;
Attributes attributes;
String tag;

public PreconditionValidator(ContextAware origin, SaxEventInterpretationContext intercon, String name,
public PreconditionValidator(ContextAware origin, SaxEventInterpretationContext seic, String name,
Attributes attributes) {
super(origin);
this.setContext(origin.getContext());
this.intercon = intercon;
this.seic = seic;
this.tag = name;
this.attributes = attributes;
}
Expand All @@ -43,39 +43,70 @@ public PreconditionValidator validateZeroAttributes() {

if(attributes.getLength() != 0) {
addError("Element [" + tag + "] should have no attributes, near line "
+ Action.getLineNumber(intercon));
+ Action.getLineNumber(seic));
this.valid = false;
}
return this;
}


public PreconditionValidator validateClassAttribute() {
return generic(Action.CLASS_ATTRIBUTE);
return validateGivenAttribute(Action.CLASS_ATTRIBUTE);
}

public PreconditionValidator validateNameAttribute() {
return generic(Action.NAME_ATTRIBUTE);
return validateGivenAttribute(Action.NAME_ATTRIBUTE);
}

public PreconditionValidator validateValueAttribute() {
return generic(JoranConstants.VALUE_ATTR);
return validateGivenAttribute(JoranConstants.VALUE_ATTR);
}

public PreconditionValidator validateRefAttribute() {
return generic(JoranConstants.REF_ATTRIBUTE);
return validateGivenAttribute(JoranConstants.REF_ATTRIBUTE);
}

public PreconditionValidator generic(String attributeName) {
public boolean isInvalidAttribute(String attributeName) {
String attributeValue = attributes.getValue(attributeName);
if (OptionHelper.isNullOrEmptyOrAllSpaces(attributeValue)) {
addError("Missing attribute [" + attributeName + "] in element [" + tag + "] near line "
+ Action.getLineNumber(intercon));
return OptionHelper.isNullOrEmptyOrAllSpaces(attributeValue);
}

public PreconditionValidator validateGivenAttribute(String attributeName) {
boolean invalid = isInvalidAttribute(attributeName);
if (invalid) {
addMissingAttributeError(attributeName);
this.valid = false;
}
return this;
}



/**
*
* @deprecated replaced by {@link #validateGivenAttribute(String)}
*/
@Deprecated
public PreconditionValidator generic(String attributeName) {
return validateGivenAttribute(attributeName);
}

public void addMissingAttributeError(String attributeName) {
addError("Missing attribute [" + attributeName + "]. " + getLocationSuffix());
}

public String getLocationSuffix() {
return "See element [" + tag + "] near line " + Action.getLineNumber(seic);
}

// public void addWarning(String msg) {
// super.addWarn(msg + getLocationSuffix());
// }
//
// public void addError(String msg) {
// super.addError(msg + getLocationSuffix());
// }

public boolean isValid() {
return valid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class IfAction extends BaseModelAction {
@Override
protected boolean validPreconditions(SaxEventInterpretationContext interpcont, String name, Attributes attributes) {
PreconditionValidator pv = new PreconditionValidator(this, interpcont, name, attributes);
pv.generic(CONDITION_ATTRIBUTE);
pv.validateGivenAttribute(CONDITION_ATTRIBUTE);
return pv.isValid();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.Objects;

/**
* Abstract representation of configuration elements
* Abstract representation of configuration elements which have class names and are instantiated.
*
* @author Ceki G&uuml;lc&uuml;
* @since 1.3.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2024, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/

package ch.qos.logback.core.model;

public class ConversionRuleModel extends ComponentModel {

String conversionWord;

@Override
protected ConversionRuleModel makeNewInstance() {
return new ConversionRuleModel();
}

public String getConversionWord() {
return conversionWord;
}

public void setConversionWord(String conversionWord) {
this.conversionWord = conversionWord;
}

public String getConverterClass() {
return className;
}

public void setConverterClass(String converterClass) {
setClassName(converterClass);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2024, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/

package ch.qos.logback.core.model.processor;

import ch.qos.logback.core.Context;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.model.ConversionRuleModel;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.util.OptionHelper;

import java.util.HashMap;
import java.util.Map;

import static ch.qos.logback.core.joran.JoranConstants.CONVERSION_WORD_ATTRIBUTE;

public class ConversionRuleModelHandler extends ModelHandlerBase {

private boolean inError;

public ConversionRuleModelHandler(Context context) {
super(context);
}

static public ConversionRuleModelHandler makeInstance(Context context, ModelInterpretationContext mic) {
return new ConversionRuleModelHandler(context);
}

@Override
public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {

ConversionRuleModel conversionRuleModel = (ConversionRuleModel) model;
String converterClass = conversionRuleModel.getClassName();

if (OptionHelper.isNullOrEmptyOrAllSpaces(converterClass)) {
addWarn("Missing className. This should have been caught earlier.");
inError = true;
return;
} else {
converterClass = mic.getImport(converterClass);
}

String conversionWord = conversionRuleModel.getConversionWord();


try {
Map<String, String> ruleRegistry = (Map<String, String>) context
.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
if (ruleRegistry == null) {
ruleRegistry = new HashMap<String, String>();
context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, ruleRegistry);
}
// put the new rule into the rule registry
addInfo("registering conversion word " + conversionWord + " with class [" + converterClass + "]");
ruleRegistry.put(conversionWord, converterClass);
} catch (Exception oops) {
inError = true;
String errorMsg = "Could not add conversion rule to PatternLayout.";
addError(errorMsg);
}



}
}

0 comments on commit 62d1eb1

Please sign in to comment.