View Javadoc
1   /**
2    *    Copyright 2006-2016 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    *       http://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.api.dom.xml.Attribute;
25  import org.mybatis.generator.api.dom.xml.Document;
26  import org.mybatis.generator.api.dom.xml.XmlElement;
27  import org.mybatis.generator.codegen.XmlConstants;
28  import org.mybatis.generator.exception.InvalidConfigurationException;
29  
30  /**
31   * The Class Configuration.
32   *
33   * @author Jeff Butler
34   */
35  public class Configuration {
36  
37      /** The contexts. */
38      private List<Context> contexts;
39      
40      /** The class path entries. */
41      private List<String> classPathEntries;
42  
43      /**
44       * Instantiates a new configuration.
45       */
46      public Configuration() {
47          super();
48          contexts = new ArrayList<Context>();
49          classPathEntries = new ArrayList<String>();
50      }
51  
52      /**
53       * Adds the classpath entry.
54       *
55       * @param entry
56       *            the entry
57       */
58      public void addClasspathEntry(String entry) {
59          classPathEntries.add(entry);
60      }
61  
62      /**
63       * Gets the class path entries.
64       *
65       * @return Returns the classPathEntries.
66       */
67      public List<String> getClassPathEntries() {
68          return classPathEntries;
69      }
70  
71      /**
72       * This method does a simple validate, it makes sure that all required fields have been filled in and that all
73       * implementation classes exist and are of the proper type. It does not do any more complex operations such as:
74       * validating that database tables exist or validating that named columns exist
75       *
76       * @throws InvalidConfigurationException
77       *             the invalid configuration exception
78       */
79      public void validate() throws InvalidConfigurationException {
80          List<String> errors = new ArrayList<String>();
81  
82          for (String classPathEntry : classPathEntries) {
83              if (!stringHasValue(classPathEntry)) {
84                  errors.add(getString("ValidationError.19")); //$NON-NLS-1$
85                  // only need to state this error once
86                  break;
87              }
88          }
89  
90          if (contexts.size() == 0) {
91              errors.add(getString("ValidationError.11")); //$NON-NLS-1$
92          } else {
93              for (Context context : contexts) {
94                  context.validate(errors);
95              }
96          }
97  
98          if (errors.size() > 0) {
99              throw new InvalidConfigurationException(errors);
100         }
101     }
102 
103     /**
104      * Gets the contexts.
105      *
106      * @return the contexts
107      */
108     public List<Context> getContexts() {
109         return contexts;
110     }
111 
112     /**
113      * Adds the context.
114      *
115      * @param context
116      *            the context
117      */
118     public void addContext(Context context) {
119         contexts.add(context);
120     }
121 
122     /**
123      * Gets the context.
124      *
125      * @param id
126      *            the id
127      * @return the context
128      */
129     public Context getContext(String id) {
130         for (Context context : contexts) {
131             if (id.equals(context.getId())) {
132                 return context;
133             }
134         }
135 
136         return null;
137     }
138 
139     /**
140      * Builds an XML representation of this configuration. This can be used to
141      * persist a programmatically generated configuration.
142      * 
143      * @return the XML representation of this configuration
144      */
145     public Document toDocument() {
146         // note that this method will not reconstruct a properties
147         // element - that element is only used in XML parsing
148 
149         Document document = new Document(
150                 XmlConstants.MYBATIS_GENERATOR_CONFIG_PUBLIC_ID,
151                 XmlConstants.MYBATIS_GENERATOR_CONFIG_SYSTEM_ID);
152         XmlElement rootElement = new XmlElement("generatorConfiguration"); //$NON-NLS-1$
153         document.setRootElement(rootElement);
154 
155         for (String classPathEntry : classPathEntries) {
156             XmlElement cpeElement = new XmlElement("classPathEntry"); //$NON-NLS-1$
157             cpeElement.addAttribute(new Attribute("location", classPathEntry)); //$NON-NLS-1$
158             rootElement.addElement(cpeElement);
159         }
160 
161         for (Context context : contexts) {
162             rootElement.addElement(context.toXmlElement());
163         }
164 
165         return document;
166     }
167 }