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