commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
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 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.generate.core.util; |
|
26 |
|
|
27 |
import org.apache.commons.io.IOUtils; |
|
28 |
import org.apache.velocity.app.Velocity; |
|
29 |
import vip.xiaonuo.core.consts.CommonConstant; |
|
30 |
import vip.xiaonuo.core.context.constant.ConstantContext; |
|
31 |
import vip.xiaonuo.core.enums.DbIdEnum; |
|
32 |
|
|
33 |
import javax.servlet.http.HttpServletResponse; |
|
34 |
import java.io.IOException; |
|
35 |
import java.util.Properties; |
|
36 |
import java.util.regex.Matcher; |
|
37 |
import java.util.regex.Pattern; |
|
38 |
|
|
39 |
/** |
|
40 |
* 代码生成工具类 |
|
41 |
* |
|
42 |
* @author yubaoshan |
|
43 |
* @Date 2020年12月16日23:29:53 |
|
44 |
*/ |
|
45 |
public class Util { |
|
46 |
|
|
47 |
/** |
|
48 |
* 初始化vm |
|
49 |
* |
|
50 |
* @author yubaoshan |
|
51 |
* @Date 2020年12月16日23:29:53 |
|
52 |
*/ |
|
53 |
public static void initVelocity() { |
|
54 |
Properties properties = new Properties(); |
|
55 |
try { |
|
56 |
properties.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
|
57 |
properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8"); |
|
58 |
properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8"); |
|
59 |
Velocity.init(properties); |
|
60 |
} catch (Exception e) { |
|
61 |
throw new RuntimeException(e); |
|
62 |
} |
|
63 |
} |
|
64 |
|
|
65 |
/** |
|
66 |
* 生成压缩包文件 |
|
67 |
* |
|
68 |
* @author yubaoshan |
|
69 |
* @Date 2020年12月16日23:29:53 |
|
70 |
*/ |
|
71 |
public static void DownloadGen(HttpServletResponse response, byte[] bytes) throws IOException { |
|
72 |
response.reset(); |
|
73 |
response.setHeader("Content-Disposition", "attachment; filename=\"Snowy.zip\""); |
|
74 |
response.addHeader("Content-Length", "" + bytes.length); |
|
75 |
response.setContentType("application/octet-stream; charset=UTF-8"); |
|
76 |
IOUtils.write(bytes, response.getOutputStream()); |
|
77 |
} |
|
78 |
|
|
79 |
/** |
|
80 |
* 查询某字符串第i次出现的游标 |
|
81 |
* |
|
82 |
* @param string 字符串 |
|
83 |
* @param i 第i次出现 |
|
84 |
* @param str 子字符串 |
|
85 |
* @author yubaoshan |
|
86 |
* @date 2020年12月16日23:29:53 |
|
87 |
*/ |
|
88 |
private static int getIndex(String string, int i, String str) { |
|
89 |
Matcher slashMatcher = Pattern.compile(str).matcher(string); |
|
90 |
int mIdx = 0; |
|
91 |
while (slashMatcher.find()) { |
|
92 |
mIdx++; |
|
93 |
//当"/"符号第三次出现的位置 |
|
94 |
if (mIdx == i) { |
|
95 |
break; |
|
96 |
} |
|
97 |
} |
|
98 |
return slashMatcher.start(); |
|
99 |
} |
|
100 |
|
|
101 |
/** |
|
102 |
* 获取数据库用户 |
|
103 |
* |
|
104 |
* @author yubaoshan |
|
105 |
* @date 2021-03-11 18:37:00 |
|
106 |
*/ |
|
107 |
public static String getDataBasename () { |
|
108 |
String dataUrl = ConstantContext.me().getStr(CommonConstant.DATABASE_URL_NAME); |
|
109 |
String driverName = ConstantContext.me().getStr(CommonConstant.DATABASE_DRIVER_NAME); |
|
110 |
if (driverName.contains(DbIdEnum.MYSQL.getCode())) { |
|
111 |
return dataUrl.substring(getIndex(dataUrl, 3, "/") + 1, dataUrl.indexOf("?")); |
|
112 |
} else if (driverName.contains(DbIdEnum.ORACLE.getCode())) { |
|
113 |
return ConstantContext.me().getStr(CommonConstant.DATABASE_USER_NAME); |
|
114 |
} else { |
|
115 |
return ""; |
|
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
} |