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.internal;
17  
18  import static org.mybatis.generator.internal.util.messages.Messages.getString;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.util.StringTokenizer;
25  
26  import org.mybatis.generator.api.ShellCallback;
27  import org.mybatis.generator.exception.ShellException;
28  
29  public class DefaultShellCallback implements ShellCallback {
30  
31      public DefaultShellCallback() {
32          super();
33      }
34  
35      @Override
36      public File getDirectory(String targetProject, String targetPackage) throws ShellException {
37          // targetProject is interpreted as a directory that must already exist
38          //
39          // targetPackage is interpreted as a subdirectory, but in package
40          // format (with dots instead of slashes). The subdirectory will be
41          // created if it does not already exist
42  
43          Path targetProjectDirectory = Path.of(targetProject);
44          if (!Files.isDirectory(targetProjectDirectory)) {
45              throw new ShellException(getString("Warning.9", //$NON-NLS-1$
46                      targetProject));
47          }
48  
49          StringBuilder sb = new StringBuilder();
50          StringTokenizer st = new StringTokenizer(targetPackage, "."); //$NON-NLS-1$
51          while (st.hasMoreTokens()) {
52              sb.append(st.nextToken());
53              sb.append(File.separatorChar);
54          }
55  
56          Path directory = targetProjectDirectory.resolve(sb.toString());
57          if (!Files.isDirectory(directory)) {
58              try {
59                  Files.createDirectories(directory);
60              } catch (IOException e) {
61                  throw new ShellException(getString("Warning.10", //$NON-NLS-1$
62                          directory.toFile().getAbsolutePath()));
63              }
64          }
65  
66          return directory.toFile();
67      }
68  }