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.internal;
17
18 import static org.mybatis.generator.internal.util.messages.Messages.getString;
19
20 import java.io.File;
21 import java.util.StringTokenizer;
22
23 import org.mybatis.generator.api.ShellCallback;
24 import org.mybatis.generator.exception.ShellException;
25
26 /**
27 * The Class DefaultShellCallback.
28 *
29 * @author Jeff Butler
30 */
31 public class DefaultShellCallback implements ShellCallback {
32
33 /** The overwrite. */
34 private boolean overwrite;
35
36 /**
37 * Instantiates a new default shell callback.
38 *
39 * @param overwrite
40 * the overwrite
41 */
42 public DefaultShellCallback(boolean overwrite) {
43 super();
44 this.overwrite = overwrite;
45 }
46
47 /* (non-Javadoc)
48 * @see org.mybatis.generator.api.ShellCallback#getDirectory(java.lang.String, java.lang.String)
49 */
50 public File getDirectory(String targetProject, String targetPackage)
51 throws ShellException {
52 // targetProject is interpreted as a directory that must exist
53 //
54 // targetPackage is interpreted as a sub directory, but in package
55 // format (with dots instead of slashes). The sub directory will be
56 // created
57 // if it does not already exist
58
59 File project = new File(targetProject);
60 if (!project.isDirectory()) {
61 throw new ShellException(getString("Warning.9", //$NON-NLS-1$
62 targetProject));
63 }
64
65 StringBuilder sb = new StringBuilder();
66 StringTokenizer st = new StringTokenizer(targetPackage, "."); //$NON-NLS-1$
67 while (st.hasMoreTokens()) {
68 sb.append(st.nextToken());
69 sb.append(File.separatorChar);
70 }
71
72 File directory = new File(project, sb.toString());
73 if (!directory.isDirectory()) {
74 boolean rc = directory.mkdirs();
75 if (!rc) {
76 throw new ShellException(getString("Warning.10", //$NON-NLS-1$
77 directory.getAbsolutePath()));
78 }
79 }
80
81 return directory;
82 }
83
84 /* (non-Javadoc)
85 * @see org.mybatis.generator.api.ShellCallback#refreshProject(java.lang.String)
86 */
87 public void refreshProject(String project) {
88 // nothing to do in the default shell callback
89 }
90
91 /* (non-Javadoc)
92 * @see org.mybatis.generator.api.ShellCallback#isMergeSupported()
93 */
94 public boolean isMergeSupported() {
95 return false;
96 }
97
98 /* (non-Javadoc)
99 * @see org.mybatis.generator.api.ShellCallback#isOverwriteEnabled()
100 */
101 public boolean isOverwriteEnabled() {
102 return overwrite;
103 }
104
105 /* (non-Javadoc)
106 * @see org.mybatis.generator.api.ShellCallback#mergeJavaFile(java.lang.String, java.lang.String, java.lang.String[], java.lang.String)
107 */
108 public String mergeJavaFile(String newFileSource,
109 String existingFileFullPath, String[] javadocTags, String fileEncoding)
110 throws ShellException {
111 throw new UnsupportedOperationException();
112 }
113 }