001package ch.qos.logback.core.model.processor;
002
003import ch.qos.logback.core.Context;
004import ch.qos.logback.core.CoreConstants;
005import ch.qos.logback.core.model.ImportModel;
006import ch.qos.logback.core.model.Model;
007import ch.qos.logback.core.util.OptionHelper;
008
009public class ImportModelHandler extends ModelHandlerBase {
010
011    public ImportModelHandler(Context context) {
012        super(context);
013    }
014
015    static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
016        return new ImportModelHandler(context);
017    }
018
019    @Override
020    protected Class<ImportModel> getSupportedModelClass() {
021        return ImportModel.class;
022    }
023
024    @Override
025    public void handle(ModelInterpretationContext intercon, Model model) throws ModelHandlerException {
026        ImportModel importModel = (ImportModel) model;
027
028        String className = importModel.getClassName();
029        if (OptionHelper.isNullOrEmptyOrAllSpaces(className)) {
030            addWarn("Empty className not allowed");
031            return;
032        }
033
034        String stem = extractStem(className);
035        if (stem == null) {
036            addWarn("[" + className + "] could not be imported due to incorrect format");
037            return;
038        }
039
040        intercon.addImport(stem, className);
041
042    }
043
044    String extractStem(String className) {
045        if (className == null)
046            return null;
047
048        int lastDotIndex = className.lastIndexOf(CoreConstants.DOT);
049        if (lastDotIndex == -1)
050            return null;
051        if ((lastDotIndex + 1) == className.length())
052            return null;
053        return className.substring(lastDotIndex + 1);
054    }
055
056}