View Javadoc
1   package ch.qos.logback.core.model.processor;
2   
3   import ch.qos.logback.core.Context;
4   import ch.qos.logback.core.CoreConstants;
5   import ch.qos.logback.core.model.ImportModel;
6   import ch.qos.logback.core.model.Model;
7   import ch.qos.logback.core.util.OptionHelper;
8   
9   public class ImportModelHandler extends ModelHandlerBase {
10  
11      public ImportModelHandler(Context context) {
12          super(context);
13      }
14  
15      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
16          return new ImportModelHandler(context);
17      }
18  
19      @Override
20      protected Class<ImportModel> getSupportedModelClass() {
21          return ImportModel.class;
22      }
23  
24      @Override
25      public void handle(ModelInterpretationContext intercon, Model model) throws ModelHandlerException {
26          ImportModel importModel = (ImportModel) model;
27  
28          String className = importModel.getClassName();
29          if (OptionHelper.isNullOrEmptyOrAllSpaces(className)) {
30              addWarn("Empty className not allowed");
31              return;
32          }
33  
34          String stem = extractStem(className);
35          if (stem == null) {
36              addWarn("[" + className + "] could not be imported due to incorrect format");
37              return;
38          }
39  
40          intercon.addImport(stem, className);
41  
42      }
43  
44      String extractStem(String className) {
45          if (className == null)
46              return null;
47  
48          int lastDotIndex = className.lastIndexOf(CoreConstants.DOT);
49          if (lastDotIndex == -1)
50              return null;
51          if ((lastDotIndex + 1) == className.length())
52              return null;
53          return className.substring(lastDotIndex + 1);
54      }
55  
56  }