001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, 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 v2.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 */
014
015package ch.qos.logback.core.model.processor;
016
017import ch.qos.logback.core.Context;
018import ch.qos.logback.core.CoreConstants;
019import ch.qos.logback.core.model.ImportModel;
020import ch.qos.logback.core.model.Model;
021import ch.qos.logback.core.util.OptionHelper;
022
023public class ImportModelHandler extends ModelHandlerBase {
024
025    public ImportModelHandler(Context context) {
026        super(context);
027    }
028
029    static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
030        return new ImportModelHandler(context);
031    }
032
033    @Override
034    protected Class<ImportModel> getSupportedModelClass() {
035        return ImportModel.class;
036    }
037
038    @Override
039    public void handle(ModelInterpretationContext intercon, Model model) throws ModelHandlerException {
040        ImportModel importModel = (ImportModel) model;
041
042        String className = importModel.getClassName();
043        if (OptionHelper.isNullOrEmptyOrAllSpaces(className)) {
044            addWarn("Empty className not allowed");
045            return;
046        }
047
048        String stem = extractStem(className);
049        if (stem == null) {
050            addWarn("[" + className + "] could not be imported due to incorrect format");
051            return;
052        }
053
054        intercon.addImport(stem, className);
055
056    }
057
058    String extractStem(String className) {
059        if (className == null)
060            return null;
061
062        int lastDotIndex = className.lastIndexOf(CoreConstants.DOT);
063        if (lastDotIndex == -1)
064            return null;
065        if ((lastDotIndex + 1) == className.length())
066            return null;
067        return className.substring(lastDotIndex + 1);
068    }
069
070}