View Javadoc
1   /*
2    *    Copyright 2006-2026 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.generator.config;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19  import static org.mybatis.generator.internal.util.messages.Messages.getString;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.jspecify.annotations.Nullable;
25  import org.mybatis.generator.exception.InvalidConfigurationException;
26  
27  public class Configuration {
28      private final List<Context> contexts;
29      private final List<String> classPathEntries;
30  
31      public Configuration() {
32          contexts = new ArrayList<>();
33          classPathEntries = new ArrayList<>();
34      }
35  
36      public void addClasspathEntry(@Nullable String entry) {
37          if (entry != null) {
38              classPathEntries.add(entry);
39          }
40      }
41  
42      public List<String> getClassPathEntries() {
43          return classPathEntries;
44      }
45  
46      /**
47       * This method does a simple validation, it makes sure that all required fields have been filled in and that all
48       * implementation classes exist and are of the proper type. It does not do any more complex operations such as
49       * validating that database tables exist or validating that named columns exist
50       *
51       * @throws InvalidConfigurationException
52       *             the invalid configuration exception
53       */
54      public void validate() throws InvalidConfigurationException {
55          List<String> errors = new ArrayList<>();
56  
57          for (String classPathEntry : classPathEntries) {
58              if (!stringHasValue(classPathEntry)) {
59                  errors.add(getString("ValidationError.19")); //$NON-NLS-1$
60                  // only need to state this error once
61                  break;
62              }
63          }
64  
65          if (contexts.isEmpty()) {
66              errors.add(getString("ValidationError.11")); //$NON-NLS-1$
67          } else {
68              for (Context context : contexts) {
69                  context.validate(errors);
70              }
71          }
72  
73          if (!errors.isEmpty()) {
74              throw new InvalidConfigurationException(getString("ValidationError.32"), errors); //$NON-NLS-1$
75          }
76      }
77  
78      public List<Context> getContexts() {
79          return contexts;
80      }
81  
82      public void addContext(Context context) {
83          contexts.add(context);
84      }
85  }