1 package gboat2.base.view.util;
2
3 import gboat2.base.bridge.debug.DefaultDebugHook;
4 import gboat2.base.view.model.Comp;
5 import gboat2.base.view.model.CompContainer;
6
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.nio.charset.Charset;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.apache.commons.lang3.StringUtils;
16
17 import com.fasterxml.jackson.databind.DeserializationFeature;
18 import com.fasterxml.jackson.databind.ObjectMapper;
19
20 public class DescJsonUtil {
21
22 private static String compAddress = "static/gboat2.base.view/component/";
23
24 public static String jsonFileName = "desc.json";
25
26 public static Comp covertComp(Comp comp,String address){
27 if(StringUtils.isEmpty(address)){
28 address = compAddress;
29 }
30 handleMain(comp);
31 handlePlugin(comp);
32 handleCss(comp);
33 handleI18n(comp);
34 handleDependencies(comp,address);
35 return comp;
36 }
37
38 public static Comp covertCompToMinMode(Comp comp){
39 List<String> main = comp.getMain();
40 List<String> plugin = comp.getPlugin();
41 List<String> cssFile = comp.getCssFile();
42 for(String m : main){
43 m = m.substring(0, m.length()-2);
44 m+="min.js";
45 }
46 for(String p : plugin){
47 p = p.substring(0, p.length()-2);
48 p+="min.js";
49 }
50 for(String c : cssFile){
51 c = c.substring(0, c.length()-3);
52 c+="min.css";
53 }
54 return comp;
55 }
56
57
58
59
60
61
62 public static void handleDependencies(Comp comp,String compAddress){
63 List<Comp> depens = comp.getDependencies();
64 if(depens == null || depens.size()==0){
65 return;
66 }
67 for(Comp depen : depens){
68 Comp cacheComp = CompContainer.getInstance().getComp(depen.getName());
69 if(cacheComp != null){
70 cacheComp.addDepenNums();
71 }else{
72 String resource = computeResourceUrl(depen.getName());
73 depen = loadDescJson(resource+"/"+jsonFileName);
74 depen = covertComp(depen,compAddress);
75 depen.addDepenNums();
76 CompContainer.getInstance().addComp(depen.getName(), (Comp)depen.clone());
77 }
78 }
79 }
80
81
82
83
84
85
86 private static void handlePlugin(Comp comp){
87 List<String> pluginFile = comp.getPlugin();
88 List<String> newPluginFile = new ArrayList<String>();
89 if(pluginFile == null || pluginFile.size()==0){
90 return;
91 }
92 for(String file : pluginFile){
93 file +=".js";
94 newPluginFile.add(file);
95 }
96 comp.setPlugin(newPluginFile);
97 }
98
99
100
101
102
103
104 private static Comp handleCss(Comp comp){
105
106 List<String> cssFile = comp.getCssFile();
107 List<String> newCssFile = new ArrayList<String>();
108 boolean isCssAutoAdd = false;
109 if(cssFile == null || cssFile.size()==0){
110 cssFile = new ArrayList<String>();
111 cssFile.add(comp.getName());
112 isCssAutoAdd = true;
113 }
114
115 for(String file : cssFile){
116 StringBuffer fileName = new StringBuffer(file);
117 fileName.append("-");
118 fileName.append(comp.getVersion());
119 fileName.append(".css");
120 if(!isCssAutoAdd || isFileExist(comp.getName(), fileName.toString())){
121 newCssFile.add(fileName.toString());
122 }
123 }
124 comp.setCssFile(newCssFile);
125 return comp;
126 }
127
128
129
130
131
132
133 public static boolean isNeedAddMinSign(String devMode){
134 if(!DefaultDebugHook.getInstance().devMode && !"true".equals(devMode)){
135 return true;
136 }
137 return false;
138 }
139
140
141
142
143
144
145 private static Comp handleI18n(Comp comp){
146 String i18nFile = comp.getI18nFile();
147 if(StringUtils.isEmpty(i18nFile)){
148 StringBuffer fileName = new StringBuffer(i18nFile);
149 fileName.append(".js");
150 if(isFileExist(comp.getName(), fileName.toString())){
151 i18nFile = fileName.toString();
152 comp.setI18nFile(i18nFile);
153 }
154 }
155 return comp;
156 }
157
158
159
160
161
162
163 private static void handleMain(Comp comp){
164 List<String> mainList = comp.getMain();
165 List<String> newMainList = new ArrayList<String>();
166 if(mainList == null || mainList.size()==0){
167 mainList = new ArrayList<String>();
168 mainList.add(comp.getName());
169 }
170
171 for(String file : mainList){
172 StringBuffer fileName = new StringBuffer(file);
173 fileName.append("-");
174 fileName.append(comp.getVersion());
175 fileName.append(".js");
176 newMainList.add(fileName.toString());
177 }
178 comp.setMain(newMainList);
179 }
180
181
182
183
184
185
186
187 public static String computeResourceUrl(String compName){
188 String addr = compAddress;
189 if(compName.contains("#")){
190 String[] c = compName.split("#");
191 for(String str : c){
192 addr +=str+"/";
193 }
194 return addr.substring(0, addr.length()-1);
195 }else{
196 return addr+compName;
197 }
198 }
199
200 public static boolean isFileExist(String compName,String fileName){
201 InputStream in = DescJsonUtil.class.getClassLoader().getResourceAsStream(DescJsonUtil.computeResourceUrl(compName));
202 if(in == null){
203 return false;
204 }else{
205 try {
206 in.close();
207 } catch (Exception e) {
208 }
209 return true;
210 }
211 }
212
213
214
215
216
217
218 public static Comp loadDescJson(String resource) {
219 InputStream in = null;
220 try {
221 in = DescJsonUtil.class.getClassLoader().getResourceAsStream(resource);
222 BufferedReader buf = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));
223 StringBuffer sbf = new StringBuffer();
224 String tmp = null;
225 try {
226 while ((tmp = buf.readLine()) != null) {
227 sbf.append(tmp);
228 }
229 } catch (Exception e) {
230 e.printStackTrace();
231 } finally {
232 if (buf != null) {
233 buf.close();
234 }
235 }
236 ObjectMapper mapper = new ObjectMapper();
237 mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY,true);
238 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
239 return mapper.readValue(sbf.toString(), Comp.class);
240 }catch(Exception e){
241 e.printStackTrace();
242 }finally {
243 if(in != null){
244 try {
245 in.close();
246 } catch (IOException e) {
247 e.printStackTrace();
248 }
249 }
250 }
251 return null;
252 }
253
254 public static void main(String[] args) {
255 Comp comp = DescJsonUtil.loadDescJson(DescJsonUtil.compAddress+"form/validate/desc1.json");
256 System.out.println(comp.getDependencies().get(0).getName());
257 }
258
259 }