Adding more testcases to Misc xgen
[aaf/authz.git] / misc / xgen / src / main / java / org / onap / aaf / misc / xgen / html / HTMLGen.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.Writer;\r
25 \r
26 import org.onap.aaf.misc.xgen.Mark;\r
27 import org.onap.aaf.misc.xgen.XGen;\r
28 \r
29 public abstract class HTMLGen extends XGen<HTMLGen> {\r
30         public static final String A = "a";\r
31         public static final String P = "p";\r
32         public static final String LI = "li";\r
33         public static final String OL = "ol";\r
34         public static final String UL = "ul";\r
35         \r
36         \r
37         public static final String TABLE = "table";\r
38         public static final String THEAD = "thead";\r
39         public static final String TBODY = "tbody";\r
40         public static final String TR = "tr";\r
41         public static final String TH = "th";\r
42         public static final String TD = "td";\r
43         \r
44         public static final String TITLE = "title";\r
45         public static final String H1 = "h1";\r
46         public static final String H2 = "h2";\r
47         public static final String H3 = "h3";\r
48         public static final String H4 = "h4";\r
49         public static final String H5 = "h5";\r
50         \r
51         \r
52         \r
53         // --------------------------- HTML Version Specific -----------------------\r
54         public abstract HTMLGen html(String ... attributes);\r
55         public abstract HTMLGen charset(String charset);\r
56         public abstract Mark head();\r
57         public abstract Mark body(String ... attribs);\r
58 \r
59         \r
60         // HTML 5 has simplified sectioning\r
61         public abstract Mark header(String ... attribs);\r
62         public abstract Mark footer(String ... attribs);\r
63         public abstract Mark section(String ... attribs);\r
64         public abstract Mark article(String ... attribs);\r
65         public abstract Mark aside(String ... attribs);\r
66         public abstract Mark nav(String ... attribs);\r
67 \r
68         // --------------------------- HTML Version Specific -----------------------\r
69 \r
70         public HTMLGen imports(Imports imports) {\r
71                 //this.imports=imports;\r
72                 for(String str : imports.css) {\r
73                         forward.print("<link rel=\"stylesheet\" href=\"");\r
74                         forward.print(imports.themePath(null));\r
75                         forward.print(str);\r
76                         forward.println("\">");\r
77                 }\r
78 \r
79                 for(String str : imports.js) {\r
80                         forward.print("<script type=\"text/javascript\" src=\"");\r
81                         forward.print(imports.themePath(null));\r
82                         forward.print(str);\r
83                         forward.println("\"></script>");\r
84                 }\r
85                 return this;\r
86         }\r
87         \r
88         public HTMLGen jsVars(String ... attrs) {\r
89                 forward.println("<script type=text/javascript>");\r
90                 if(attrs!=null) {\r
91                         for(int i=0; i<attrs.length;++i) {\r
92                                 forward.append(' ');\r
93                                 String[] split = attrs[i].split("=",2);\r
94                                 switch(split.length) {\r
95                                         case 2:\r
96                                                 forward.print("  var ");\r
97                                                 forward.append(split[0]);\r
98                                                 forward.append("='");\r
99                                                 forward.append(split[1]);\r
100                                                 forward.println("';");\r
101                                                 break;\r
102                                 }\r
103                         }\r
104                 }\r
105                 forward.println("</script>");\r
106                 return this;\r
107         }\r
108 \r
109         public HTMLGen(Writer w) {\r
110                 super(w);\r
111         }\r
112 \r
113         /**\r
114          * Use "directive" to handle non-ended HTML tags like <meta ... >  and <link ...>\r
115          * @param tag\r
116          * @param attrs\r
117          * @return\r
118          */\r
119         public HTMLGen directive(String tag, String ... attrs) {\r
120                 forward.append('<');\r
121                 forward.append(tag);\r
122                 addAttrs(attrs);\r
123                 forward.append('>');\r
124                 if(pretty) {\r
125                         forward.println();\r
126                 }\r
127                 return this;\r
128         }\r
129 \r
130         public Mark divID(String ... attrs) {\r
131                 Mark div;\r
132                 if(attrs.length>0) {\r
133                         div = new Mark(attrs[0]);\r
134                         attrs[0]="id="+attrs[0];\r
135                 } else {\r
136                         div = new Mark();\r
137                 }\r
138                 incr(div, "div", attrs);\r
139                 return div;\r
140         }\r
141 \r
142         public HTMLGen img(String ... attrs) {\r
143                 return tagOnly("img", attrs);\r
144         }\r
145         \r
146         /**\r
147          * Input Cheesecake... creates a Label and Field in the form of Table Rows.\r
148          * Make sure you create a table first, ie.  incr(HTMLGen.TABLE);\r
149          * \r
150          * Setting Required to "true" will add required Attribute to both Label and Field.  In HTML5, "required" in the input will\r
151          * validate there is data in the fields before submitting.  "required" does nothing for label, but allows for\r
152          * easy CSS coding... "label[required] { ... }", so that colors can be changed\r
153          * \r
154          * @param id\r
155          * @param label\r
156          * @param required\r
157          * @param attrs\r
158          * @return\r
159          */\r
160         public HTMLGen input(String id, String label, boolean required, String ... attrs) {\r
161                 Mark mtr = new Mark(TR);\r
162                 Mark mtd = new Mark(TD);\r
163                 incr(mtr);\r
164                 incr(mtd);\r
165                 incr("label",true, "for="+id,required?"required":null).text(label).end();\r
166                 end(mtd);\r
167                 String nattrs[] = new String[attrs.length+(required?3:2)];\r
168                 nattrs[0]="id="+id;\r
169                 nattrs[1]="name="+id;\r
170                 System.arraycopy(attrs, 0, nattrs, 2, attrs.length);\r
171                 if(required) {\r
172                         nattrs[nattrs.length-1]="required";\r
173                 }\r
174                 incr(mtd);\r
175                 tagOnly("input",nattrs);\r
176                 end(mtr);\r
177                 return this;\r
178         }\r
179         \r
180         //  Common tags that do not have standard endings.  These are here to help people who don't know to pick directive  \r
181         public HTMLGen br() {\r
182                 forward.append("<br>");\r
183                 if(pretty) {\r
184                         forward.println();\r
185                 }\r
186                 return this;\r
187         }\r
188 \r
189         public HTMLGen p(String ... text) {\r
190                 forward.append("<p>");\r
191                 for(String s : text) {\r
192                         forward.append(s);\r
193                 }\r
194                 if(pretty) {\r
195                         forward.println();\r
196                 }\r
197                 return this;\r
198         }\r
199 \r
200         public HTMLGen hr() {\r
201                 forward.append("<hr>");\r
202                 if(pretty) {\r
203                         forward.println();\r
204                 }\r
205                 return this;\r
206         }\r
207 \r
208         public JSGen js(Mark mark) {\r
209                 return new JSGen(mark, this);\r
210         }\r
211 \r
212         public JSGen js() {\r
213                 return js(null);\r
214         }\r
215 //\r
216 //      protected void cssInline(String filename) {\r
217 //              File file = new File(imports.webDir,filename);\r
218 //              try {\r
219 //                      String line;\r
220 //                      BufferedReader br = new BufferedReader(new FileReader(file));\r
221 //                      try {\r
222 //                              forward.print("<style>");\r
223 //                              prettyln(forward);\r
224 //                              while((line=br.readLine())!=null) {\r
225 //                                      forward.print((pretty?line:line.trim()));\r
226 //                                      prettyln(forward);\r
227 //                              }                       \r
228 //                      }finally {\r
229 //                              forward.print("</style>");\r
230 //                              prettyln(forward);\r
231 //                              br.close();\r
232 //                      }\r
233 //              } catch (IOException e) {\r
234 //                      e.printStackTrace();\r
235 //                      // Can't read, suffice to import normally?\r
236 //                      // for now, just skip\r
237 //              }\r
238 //      }\r
239         \r
240 }\r