View Javadoc
1   /*
2    *    Copyright 2006-2023 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.mybatis.generator.exception.InvalidConfigurationException;
25  
26  public class Configuration {
27  
28      private final List<Context> contexts;
29  
30      private final List<String> classPathEntries;
31  
32      public Configuration() {
33          super();
34          contexts = new ArrayList<>();
35          classPathEntries = new ArrayList<>();
36      }
37  
38      public void addClasspathEntry(String entry) {
39          classPathEntries.add(entry);
40      }
41  
42      public List<String> getClassPathEntries() {
43          return classPathEntries;
44      }
45  
46      /**
47       * This method does a simple validate, 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(errors);
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  }