Adding more testcases to Misc xgen
[aaf/authz.git] / misc / xgen / src / main / java / org / onap / aaf / misc / xgen / html / JSGen.java
1 /**\r
2  * ============LICENSE_START====================================================\r
3  * org.onap.aaf\r
4  * ===========================================================================\r
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.\r
6  * ===========================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END====================================================\r
19  *\r
20  */\r
21 \r
22 package org.onap.aaf.misc.xgen.html;\r
23 \r
24 import java.io.BufferedReader;\r
25 import java.io.FileReader;\r
26 import java.io.IOException;\r
27 \r
28 import org.onap.aaf.misc.env.util.IndentPrintWriter;\r
29 import org.onap.aaf.misc.xgen.Back;\r
30 import org.onap.aaf.misc.xgen.Mark;\r
31 \r
32 \r
33 public class JSGen {\r
34         private HTMLGen htmlGen;\r
35         private IndentPrintWriter ipw;\r
36         private Mark mark;\r
37 \r
38         public JSGen(Mark mark, HTMLGen hg) {\r
39                 this.mark = mark==null?new Mark():mark;\r
40                 hg.incr(this.mark, "script", "language=javascript", "type=text/javascript");\r
41                 htmlGen = hg;\r
42                 ipw = hg.getWriter();\r
43         }\r
44 \r
45         public JSGen inline(String filename, int tabstop) throws IOException {\r
46                 BufferedReader br = new BufferedReader(new FileReader(filename));\r
47                 int indent = htmlGen.getIndent();\r
48                 try {\r
49                         boolean pretty = htmlGen.pretty;\r
50                         String line, el;\r
51                         int l, end;\r
52                         while((line=br.readLine())!=null) {\r
53                                 if(pretty) {\r
54                                         String[] elements = line.split("\t");\r
55                                         \r
56                                         for(int i=0; i<elements.length;++i) {\r
57                                                 el = elements[i];\r
58                                                 l = el.length();\r
59                                                 if(l==0) {// was a Tab\r
60                                                         ipw.print("  ");\r
61                                                 } else {\r
62                                                         el = el.trim();\r
63                                                         l = l-el.length();\r
64                                                         end = l/tabstop;\r
65                                                         for(int j=0;j<end;++j) {\r
66                                                                 ipw.print("  ");\r
67                                                         }\r
68                                                         end = l%tabstop;\r
69                                                         for(int j=0;j<end;++j) {\r
70                                                                 ipw.print(' ');\r
71                                                         }\r
72                                                         if(i>0) ipw.print(' ');\r
73                                                                 ipw.print(el);\r
74                                                         }\r
75                                         }\r
76                                         ipw.println();\r
77                                 } else {\r
78                                         ipw.print(line.trim());\r
79                                 }\r
80                         }\r
81                 } finally {\r
82                         htmlGen.setIndent(indent);\r
83                         try {\r
84                                 br.close();\r
85                         } catch (IOException e) {\r
86                                 e.printStackTrace();\r
87                         }\r
88                 }\r
89                 return this;\r
90         }\r
91         \r
92         public JSGen pst(String ... lines) {\r
93                 return pst(null, lines);\r
94         }\r
95         \r
96         public JSGen pst(Mark jm, String ... lines) {\r
97                 if(lines.length>0) ipw.append(lines[0]);\r
98                 ipw.append('(');\r
99                 for(int i=1;i<lines.length;++i) {\r
100                         ipw.print(lines[i]);\r
101                         ipw.print(", ");\r
102                 }\r
103                 Back back;\r
104 \r
105                 if(htmlGen.pretty) {\r
106                         back = new Back(");\n",false,false);\r
107                 } else {\r
108                         back = new Back(");",false,false);\r
109                 }\r
110                 int spot = htmlGen.pushBack(back);\r
111                 if(jm!=null)jm.spot(spot);\r
112                 return this;\r
113         }\r
114         \r
115         public JSGen li(String ... lines) {\r
116                 int current = ipw.getIndent();\r
117                 for(int i=0;i<lines.length;++i) {\r
118                         if(i==1)ipw.inc();\r
119                         if(i>0)ipw.println();\r
120                         ipw.print(lines[i]);\r
121                 }\r
122                 ipw.setIndent(current);\r
123                 ipw.append(';');\r
124                 if(htmlGen.pretty)ipw.println();\r
125                 return this;\r
126         }\r
127         \r
128         public JSGen text(String text) {\r
129                 ipw.append(text);\r
130                 if(htmlGen.pretty)ipw.println();\r
131                 return this;\r
132         }\r
133 \r
134         public JSGen function(String name, String ... params) {\r
135                 return function(null, name, params);\r
136         }\r
137         \r
138         public JSGen jqfunc(Mark mark, String name, String ... params) {\r
139                 pst(mark,"$").function(name, params);\r
140                 return this;\r
141         }\r
142         \r
143         public JSGen function(Mark jm, String name, String ... params) {\r
144                 ipw.print("function ");\r
145                 ipw.print(name);\r
146                 ipw.print('(');\r
147                 for(int i=0;i<params.length;++i) {\r
148                         if(i!=0)ipw.print(", ");\r
149                         ipw.print(params[i]);\r
150                 }\r
151                 ipw.print(") {");\r
152                 if(htmlGen.pretty) {\r
153                         ipw.println();\r
154                         ipw.inc();\r
155                 }\r
156                 int spot = htmlGen.pushBack(new Back("}",true,true));\r
157                 if(jm!=null)jm.spot(spot); \r
158                 return this;\r
159         }\r
160         \r
161         public JSGen cb(String ... lines) {\r
162                 return cb(null,lines);\r
163         }\r
164 \r
165         public JSGen cb(Mark jm, String ... lines) {\r
166                 int current = ipw.getIndent();\r
167                 for(int i=0;i<lines.length;++i) {\r
168                         if(i==1)ipw.inc();\r
169                         if(i>0)ipw.println();\r
170                         ipw.print(lines[i]);\r
171                 }\r
172                 ipw.setIndent(current);\r
173                 ipw.print('{');\r
174                 if(htmlGen.pretty) {\r
175                         ipw.println();\r
176                         ipw.inc();\r
177                 }\r
178                 int spot = htmlGen.pushBack(new Back("}",true,true));\r
179                 if(jm!=null)jm.spot(spot); \r
180                 return this;\r
181 \r
182         }\r
183 \r
184         \r
185         public JSGen comment(String ... lines) {\r
186                 if(htmlGen.pretty) {\r
187                         for(int i=0;i<lines.length;++i) {\r
188                                 ipw.print("// ");\r
189                                 ipw.println(lines[i]);\r
190                         }\r
191                 }\r
192                 return this;\r
193         }\r
194         \r
195         public JSGen end(Mark mark) {\r
196                 htmlGen.end(mark);\r
197                 return this;\r
198         }\r
199         \r
200         public HTMLGen done() {\r
201                 return htmlGen.end(mark);\r
202         }\r
203         \r
204 }\r