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.api;
17  
18  import org.mybatis.generator.api.dom.java.CompilationUnit;
19  
20  /**
21   * The Class GeneratedJavaFile.
22   *
23   * @author Jeff Butler
24   */
25  public class GeneratedJavaFile extends GeneratedFile {
26      
27      /** The compilation unit. */
28      private CompilationUnit compilationUnit;
29      
30      /** The file encoding. */
31      private String fileEncoding;
32      
33      /** The java formatter. */
34      private JavaFormatter javaFormatter;
35  
36      /**
37       * Default constructor.
38       *
39       * @param compilationUnit
40       *            the compilation unit
41       * @param targetProject
42       *            the target project
43       * @param fileEncoding
44       *            the file encoding
45       * @param javaFormatter
46       *            the java formatter
47       */
48      public GeneratedJavaFile(CompilationUnit compilationUnit,
49              String targetProject,
50              String fileEncoding,
51              JavaFormatter javaFormatter) {
52          super(targetProject);
53          this.compilationUnit = compilationUnit;
54          this.fileEncoding = fileEncoding;
55          this.javaFormatter = javaFormatter;
56      }
57  
58      /**
59       * Instantiates a new generated java file.
60       *
61       * @param compilationUnit
62       *            the compilation unit
63       * @param targetProject
64       *            the target project
65       * @param javaFormatter
66       *            the java formatter
67       */
68      public GeneratedJavaFile(CompilationUnit compilationUnit,
69              String targetProject,
70              JavaFormatter javaFormatter) {
71          this(compilationUnit, targetProject, null, javaFormatter);
72      }
73      
74      /* (non-Javadoc)
75       * @see org.mybatis.generator.api.GeneratedFile#getFormattedContent()
76       */
77      @Override
78      public String getFormattedContent() {
79          return javaFormatter.getFormattedContent(compilationUnit);
80      }
81  
82      /* (non-Javadoc)
83       * @see org.mybatis.generator.api.GeneratedFile#getFileName()
84       */
85      @Override
86      public String getFileName() {
87          return compilationUnit.getType().getShortNameWithoutTypeArguments() + ".java"; //$NON-NLS-1$
88      }
89  
90      /* (non-Javadoc)
91       * @see org.mybatis.generator.api.GeneratedFile#getTargetPackage()
92       */
93      public String getTargetPackage() {
94          return compilationUnit.getType().getPackageName();
95      }
96  
97      /**
98       * This method is required by the Eclipse Java merger. If you are not
99       * running in Eclipse, or some other system that implements the Java merge
100      * function, you may return null from this method.
101      * 
102      * @return the CompilationUnit associated with this file, or null if the
103      *         file is not mergeable.
104      */
105     public CompilationUnit getCompilationUnit() {
106         return compilationUnit;
107     }
108 
109     /**
110      * A Java file is mergeable if the getCompilationUnit() method returns a valid compilation unit.
111      *
112      * @return true, if is mergeable
113      */
114     @Override
115     public boolean isMergeable() {
116         return true;
117     }
118 
119     /**
120      * Gets the file encoding.
121      *
122      * @return the file encoding
123      */
124     public String getFileEncoding() {
125         return fileEncoding;
126     }
127 }